1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2009 by Sun Microsystems, Inc.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.comp.Calc.NLPSolver.dialogs;
29 
30 import com.sun.star.comp.Calc.NLPSolver.BaseNLPSolver;
31 import com.sun.star.awt.ActionEvent;
32 import com.sun.star.awt.XActionListener;
33 import com.sun.star.comp.Calc.NLPSolver.ResourceManager;
34 import com.sun.star.lang.EventObject;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.style.VerticalAlignment;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XComponentContext;
39 import com.sun.star.comp.Calc.NLPSolver.dialogs.controls.Button;
40 import com.sun.star.comp.Calc.NLPSolver.dialogs.controls.Label;
41 import com.sun.star.comp.Calc.NLPSolver.dialogs.controls.ProgressBar;
42 
43 public class EvolutionarySolverStatusUno extends BaseDialog
44     implements IEvolutionarySolverStatusDialog,
45                XActionListener {
46 
47     private int userState;
48     private final Label lblSolutionValue;
49     private final Label lblIteration;
50     private final ProgressBar pbIteration;
51     private final Label lblIterationValue;
52     private final Label lblStagnation;
53     private final ProgressBar pbStagnation;
54     private final Label lblStagnationValue;
55     private final Label lblRuntimeValue;
56     private final Button btnStop;
57     private final Button btnOK;
58     private final Button btnContinue;
59     private final int defaultTextColor;
60     private int maxIterations;
61     private int maxStagnation;
62 
63     private final ResourceManager resourceManager;
64 
65     private static final int COLOR_RED = 0xFF0000;
66 
EvolutionarySolverStatusUno(XComponentContext xContext)67     public EvolutionarySolverStatusUno(XComponentContext xContext) {
68         super(xContext, "Solver Status", -1, -1, 170, 95); //center the dialog on the parent
69 
70         setCloseable(false);
71         userState = IEvolutionarySolverStatusDialog.OK;
72 
73         resourceManager = new ResourceManager(xContext, "com.sun.star.comp.Calc.NLPSolver", "/locale", "NLPSolverStatusDialog");
74 
75         try {
76             setProperty("Title", resourceManager.getLocalizedString("Dialog.Caption"));
77         } catch (com.sun.star.resource.MissingResourceException ex) {} //leave the title as it is
78 
79         int y = 5;
80         Label lblSolution = new Label(this, "lblSolution");
81         lblSolution.setPosition(5, y);
82         lblSolution.setSize(60, 10);
83         lblSolution.setLabel(resourceManager.getLocalizedString("Controls.lblSolution", "Current Solution:"));
84         lblSolution.setParentControl(this);
85 
86         lblSolutionValue = new Label(this, "lblSolutionValue");
87         lblSolutionValue.setPosition(65, y);
88         lblSolutionValue.setSize(100, 10);
89         lblSolutionValue.setParentControl(this);
90         defaultTextColor = lblSolutionValue.getTextColor();
91         y += 15;
92 
93         lblIteration = new Label(this, "lblIteration");
94         lblIteration.setPosition(5, y);
95         lblIteration.setSize(60, 15);
96         lblIteration.setLabel(resourceManager.getLocalizedString("Controls.lblIteration", "Iteration:"));
97         lblIteration.setVerticalAlign(VerticalAlignment.MIDDLE);
98         lblIteration.setParentControl(this);
99 
100         pbIteration = new ProgressBar(this, "pbIteration");
101         pbIteration.setPosition(65, y);
102         pbIteration.setSize(100, 15);
103         pbIteration.setParentControl(this);
104 
105         lblIterationValue = new Label(this, "lblIterationValue");
106         lblIterationValue.setPosition(65, y);
107         lblIterationValue.setSize(100, 20);
108         lblIterationValue.setVerticalAlign(VerticalAlignment.MIDDLE);
109         lblIterationValue.setMultiLine(true);
110         lblIterationValue.setParentControl(this);
111         lblIterationValue.setVisible(false);
112         y += 20;
113 
114         lblStagnation = new Label(this, "lblStagnation");
115         lblStagnation.setPosition(5, y);
116         lblStagnation.setSize(60, 15);
117         lblStagnation.setLabel(resourceManager.getLocalizedString("Controls.lblStagnation", "Stagnation:"));
118         lblStagnation.setVerticalAlign(VerticalAlignment.MIDDLE);
119         lblStagnation.setParentControl(this);
120 
121         pbStagnation = new ProgressBar(this, "pbStagnation");
122         pbStagnation.setPosition(65, y);
123         pbStagnation.setSize(100, 15);
124         pbStagnation.setParentControl(this);
125 
126         lblStagnationValue = new Label(this, "lblStagnationValue");
127         lblStagnationValue.setPosition(65, y);
128         lblStagnationValue.setSize(100, 20);
129         lblStagnationValue.setVerticalAlign(VerticalAlignment.MIDDLE);
130         lblStagnationValue.setMultiLine(true);
131         lblStagnationValue.setParentControl(this);
132         lblStagnationValue.setVisible(false);
133         y+= 20;
134 
135         Label lblRuntime = new Label(this, "lblRuntime");
136         lblRuntime.setPosition(5, y);
137         lblRuntime.setSize(60, 10);
138         lblRuntime.setLabel(resourceManager.getLocalizedString("Controls.lblRuntime", "Runtime:"));
139         lblRuntime.setParentControl(this);
140 
141         lblRuntimeValue = new Label(this, "lblRuntimeValue");
142         lblRuntimeValue.setPosition(65, y);
143         lblRuntimeValue.setSize(100, 10);
144         lblRuntimeValue.setParentControl(this);
145         y += 15;
146 
147         btnStop = new Button(this, "btnStop");
148         btnStop.setPosition(5, y);
149         btnStop.setSize(45, 15);
150         btnStop.setLabel(resourceManager.getLocalizedString("Controls.btnStop", "Stop"));
151         btnStop.setParentControl(this);
152         btnStop.addActionListener(this);
153         btnStop.setActionCommand("btnStopClick");
154 
155         btnOK = new Button(this, "btnOK");
156         btnOK.setPosition(65, y);
157         btnOK.setSize(40, 15);
158         btnOK.setLabel(resourceManager.getLocalizedString("Controls.btnOK", "OK"));
159         btnOK.setParentControl(this);
160         btnOK.addActionListener(this);
161         btnOK.setActionCommand("btnOKClick");
162         btnOK.setEnabled(false);
163 
164         btnContinue = new Button(this, "btnContinue");
165         btnContinue.setPosition(110, y);
166         btnContinue.setSize(55, 15);
167         btnContinue.setLabel(resourceManager.getLocalizedString("Controls.btnContinue", "Continue"));
168         btnContinue.setParentControl(this);
169         btnContinue.addActionListener(this);
170         btnContinue.setActionCommand("btnContinueClick");
171         btnContinue.setEnabled(false);
172         y += 15;
173     }
174 
getUserState()175     public int getUserState() {
176         return userState;
177     }
178 
setBestSolution(double solution, boolean feasible)179     public void setBestSolution(double solution, boolean feasible) {
180         lblSolutionValue.setLabel(String.format("%g", solution));
181         if (feasible)
182             lblSolutionValue.setTextColor(defaultTextColor);
183         else
184             lblSolutionValue.setTextColor(COLOR_RED); //red
185     }
186 
setMaxIterations(int maxIterations)187     public void setMaxIterations(int maxIterations) {
188         pbIteration.setRange(0, maxIterations);
189         this.maxIterations = maxIterations;
190     }
191 
setMaxStagnation(int maxStagnation)192     public void setMaxStagnation(int maxStagnation) {
193         pbStagnation.setRange(0, maxStagnation);
194         this.maxStagnation = maxStagnation;
195     }
196 
setIteration(int iteration)197     public void setIteration(int iteration) {
198         pbIteration.setValue(iteration);
199     }
200 
setStagnation(int stagnation)201     public void setStagnation(int stagnation) {
202         pbStagnation.setValue(stagnation);
203     }
204 
setRuntime(long runtime)205     public void setRuntime(long runtime) {
206         lblRuntimeValue.setLabel(BaseNLPSolver.nanoTimeToString(resourceManager, runtime));
207     }
208 
waitForUser()209     public int waitForUser() {
210         btnStop.setEnabled(false);
211         btnOK.setEnabled(true);
212         btnContinue.setEnabled(true);
213 
214         if (pbIteration.getValue() >= maxIterations) {
215             lblIteration.setTextColor(COLOR_RED);
216             if (userState != IEvolutionarySolverStatusDialog.CANCEL)
217                 lblStagnationValue.setLabel(
218                         resourceManager.getLocalizedString("Message.StopIteration",
219                         "Maximum iterations reached."));
220         }
221 
222         if (pbStagnation.getValue() >= maxStagnation) {
223             lblStagnation.setTextColor(COLOR_RED);
224             if (userState != IEvolutionarySolverStatusDialog.CANCEL)
225                 lblStagnationValue.setLabel(
226                         resourceManager.getLocalizedString("Message.StopStagnation",
227                         "Process stopped due to stagnation."));
228         }
229 
230         lblIterationValue.setLabel(String.format(
231                 resourceManager.getLocalizedString("Message.CurrentIteration",
232                 "Process stopped at iteration %d of %d."),
233                 pbIteration.getValue(), maxIterations));
234         if (userState == IEvolutionarySolverStatusDialog.CANCEL)
235             lblStagnationValue.setLabel(
236                     resourceManager.getLocalizedString("Message.StopUser",
237                     "Process stopped due to user interruption."));
238 
239         pbIteration.setVisible(false);
240         pbStagnation.setVisible(false);
241         lblIterationValue.setVisible(true);
242         lblStagnationValue.setVisible(true);
243 
244         repaint();
245 
246         userState = IEvolutionarySolverStatusDialog.WAITING;
247         xDialog.execute();
248 
249         lblIteration.setTextColor(defaultTextColor);
250         lblStagnation.setTextColor(defaultTextColor);
251 
252         lblIterationValue.setVisible(false);
253         lblStagnationValue.setVisible(false);
254         pbIteration.setVisible(true);
255         pbStagnation.setVisible(true);
256 
257         btnStop.setEnabled(true);
258         btnOK.setEnabled(false);
259         btnContinue.setEnabled(false);
260 
261         return userState;
262     }
263 
264     @Override
setVisible(boolean visible)265     public void setVisible(boolean visible) {
266         xWindow.setVisible(visible);
267     }
268 
dispose()269     public void dispose() {
270         XComponent component = UnoRuntime.queryInterface(XComponent.class, xDialog);
271         component.dispose();
272     }
273 
actionPerformed(ActionEvent actionEvent)274     public void actionPerformed(ActionEvent actionEvent) {
275         if (userState == IEvolutionarySolverStatusDialog.WAITING) {
276             xDialog.endExecute();
277             setVisible(true);
278         }
279 
280         if (actionEvent.ActionCommand.equals("btnStopClick"))
281             userState = IEvolutionarySolverStatusDialog.CANCEL;
282         else if (actionEvent.ActionCommand.equals("btnOKClick"))
283             userState = IEvolutionarySolverStatusDialog.OK;
284         else if (actionEvent.ActionCommand.equals("btnContinueClick"))
285             userState = IEvolutionarySolverStatusDialog.CONTINUE;
286     }
287 
disposing(EventObject eventObject)288     public void disposing(EventObject eventObject) {
289 
290     }
291 
292 }
293