Revision 31952 branches/v2_0_0_prep/libraries/libUIComponent/src/org/gvsig/gui/beans/incrementabletask/IncrementableTask.java
IncrementableTask.java | ||
---|---|---|
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
1 |
package org.gvsig.gui.beans.incrementabletask; |
|
2 |
|
|
3 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 | 4 |
* |
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana. |
|
4 |
* |
|
5 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
6 |
* of the Valencian Government (CIT) |
|
7 |
* |
|
5 | 8 |
* This program is free software; you can redistribute it and/or |
6 | 9 |
* modify it under the terms of the GNU General Public License |
7 | 10 |
* as published by the Free Software Foundation; either version 2 |
8 | 11 |
* of the License, or (at your option) any later version. |
9 |
* |
|
12 |
*
|
|
10 | 13 |
* This program is distributed in the hope that it will be useful, |
11 | 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | 16 |
* GNU General Public License for more details. |
14 |
* |
|
17 |
*
|
|
15 | 18 |
* You should have received a copy of the GNU General Public License |
16 | 19 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
21 |
* MA 02110-1301, USA. |
|
22 |
* |
|
18 | 23 |
*/ |
19 |
package org.gvsig.gui.beans.incrementabletask; |
|
20 | 24 |
|
25 |
import java.awt.event.WindowAdapter; |
|
26 |
import java.awt.event.WindowEvent; |
|
21 | 27 |
import java.util.ArrayList; |
22 | 28 |
import java.util.Iterator; |
23 | 29 |
|
30 |
import javax.swing.JDialog; |
|
24 | 31 |
import javax.swing.JOptionPane; |
25 |
import javax.swing.WindowConstants; |
|
26 | 32 |
|
33 |
import org.apache.log4j.Logger; |
|
27 | 34 |
import org.gvsig.gui.beans.Messages; |
28 | 35 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
29 | 36 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
30 | 37 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
31 | 38 |
import org.gvsig.gui.beans.progresspanel.ProgressPanel; |
39 |
|
|
32 | 40 |
/** |
33 | 41 |
* <code>IncrementableTask</code>. Es un dialogo que contiene un ProgressPanel. |
34 | 42 |
* Se ejecuta bajo un Thread y va consultando a un objeto de tipo IIncrementable |
35 | 43 |
* para modificar sus valores. |
36 | 44 |
* |
37 |
* @version 23/04/2008 |
|
45 |
* @version 20/08/2008 |
|
46 |
* |
|
38 | 47 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es) |
39 | 48 |
*/ |
40 | 49 |
public class IncrementableTask implements Runnable, ButtonsPanelListener { |
41 |
private ArrayList<IncrementableListener> actionCommandListeners = new ArrayList<IncrementableListener>();
|
|
42 |
private IIncrementable iIncrementable = null;
|
|
43 |
private volatile ProgressPanel progressPanel = null;
|
|
44 |
private volatile Thread blinker = null;
|
|
45 |
private boolean threadSuspended = false;
|
|
46 |
private boolean ended = false;
|
|
47 |
private boolean askOnCancel = true;
|
|
48 |
private boolean bDoCallListeners = true;
|
|
49 |
static private int eventId = Integer.MIN_VALUE;
|
|
50 |
|
|
50 |
IIncrementable iIncrementable = null;
|
|
51 |
private volatile ProgressPanel progressPanel = null;
|
|
52 |
private volatile Thread blinker = null;
|
|
53 |
private boolean threadSuspended = false;
|
|
54 |
private boolean ended = false;
|
|
55 |
private boolean askOnCancel = true;
|
|
56 |
private ArrayList<IncrementableListener> actionCommandListeners = new ArrayList<IncrementableListener>();
|
|
57 |
private boolean bDoCallListeners = true;
|
|
58 |
static private int eventId = Integer.MIN_VALUE;
|
|
59 |
|
|
51 | 60 |
/** |
52 | 61 |
* Constructor del IncrementableTask. |
53 | 62 |
* @param incrementable |
54 | 63 |
*/ |
64 |
public IncrementableTask(IIncrementable incrementable, ProgressPanel dialog) { |
|
65 |
iIncrementable = incrementable; |
|
66 |
progressPanel = dialog; |
|
67 |
configureProgressPanel(); |
|
68 |
} |
|
69 |
|
|
70 |
/** |
|
71 |
* Constructor del IncrementableTask. |
|
72 |
* @param incrementable |
|
73 |
*/ |
|
55 | 74 |
public IncrementableTask(IIncrementable incrementable) { |
56 | 75 |
iIncrementable = incrementable; |
76 |
configureProgressPanel(); |
|
57 | 77 |
} |
58 | 78 |
|
59 | 79 |
/** |
... | ... | |
71 | 91 |
public void stop() { |
72 | 92 |
ended = true; |
73 | 93 |
} |
74 |
|
|
94 |
|
|
75 | 95 |
/** |
76 | 96 |
* Este thread va leyendo el porcentaje hasta que se completa el histograma. |
77 | 97 |
*/ |
78 | 98 |
public synchronized void run() { |
79 |
while (!ended && (iIncrementable.getPercent() <= 100)) { |
|
99 |
// while (!ended && (iIncrementable.getPercent() <= 100)) { |
|
100 |
while (! ended) { |
|
80 | 101 |
try { |
81 | 102 |
getProgressPanel().setLabel(iIncrementable.getLabel()); |
82 | 103 |
getProgressPanel().setPercent(iIncrementable.getPercent()); |
... | ... | |
90 | 111 |
} catch (InterruptedException e) { |
91 | 112 |
} |
92 | 113 |
} |
114 |
|
|
115 |
// Forces to refresh the log with the last changes |
|
116 |
getProgressPanel().setLog(iIncrementable.getLog()); |
|
93 | 117 |
} |
94 | 118 |
|
95 | 119 |
/** |
... | ... | |
115 | 139 |
* Ocultar la ventana |
116 | 140 |
*/ |
117 | 141 |
public void hideWindow() { |
142 |
// getProgressPanel().dispose(); |
|
118 | 143 |
getProgressPanel().setVisible(false); |
119 | 144 |
} |
120 | 145 |
|
... | ... | |
145 | 170 |
public ProgressPanel getProgressPanel() { |
146 | 171 |
if (progressPanel == null) { |
147 | 172 |
progressPanel = new ProgressPanel(false); |
148 |
progressPanel.setAlwaysOnTop(true); |
|
149 |
progressPanel.addButtonPressedListener(this); |
|
150 |
progressPanel.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
|
151 | 173 |
} |
152 | 174 |
return progressPanel; |
153 | 175 |
} |
176 |
|
|
177 |
protected void configureProgressPanel() { |
|
178 |
getProgressPanel().setAlwaysOnTop(true); |
|
179 |
getProgressPanel().addButtonPressedListener(this); |
|
180 |
|
|
181 |
// Must ask if user wants to cancel the process, avoid closing the dialog |
|
182 |
getProgressPanel().setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE ); |
|
183 |
getProgressPanel().addWindowListener(new WindowAdapter() { |
|
184 |
public void windowClosing(WindowEvent e) { |
|
185 |
// Simulates an event like the produced pressing the cancel button of the associated progress panel |
|
186 |
actionButtonPressed(new ButtonsPanelEvent(getProgressPanel(), ButtonsPanel.BUTTON_CANCEL)); |
|
187 |
} |
|
188 |
}); |
|
189 |
} |
|
154 | 190 |
|
155 | 191 |
private void callActionCommandListeners(int actions) { |
156 | 192 |
if (!bDoCallListeners) |
157 | 193 |
return; |
158 | 194 |
Iterator<IncrementableListener> acIterator = actionCommandListeners.iterator(); |
159 | 195 |
while (acIterator.hasNext()) { |
160 |
IncrementableListener listener = acIterator.next(); |
|
196 |
IncrementableListener listener = (IncrementableListener) acIterator.next();
|
|
161 | 197 |
switch (actions) { |
162 | 198 |
case IncrementableEvent.RESUMED: |
163 | 199 |
listener.actionResumed(new IncrementableEvent(this)); |
... | ... | |
210 | 246 |
case ButtonsPanel.BUTTON_CANCEL: |
211 | 247 |
boolean cancelled = true; |
212 | 248 |
if (askOnCancel) { |
249 |
if (! iIncrementable.isCancelable()) { |
|
250 |
JOptionPane.showMessageDialog(null, Messages.getText("The_process_cant_be_cancelled"), Messages.getText("Information"), JOptionPane.INFORMATION_MESSAGE); |
|
251 |
return; |
|
252 |
} |
|
253 |
|
|
254 |
/* Pauses the process */ |
|
255 |
if (iIncrementable.isPausable()) { |
|
256 |
try { |
|
257 |
callActionCommandListeners(IncrementableEvent.SUSPENDED); |
|
258 |
} |
|
259 |
catch (Exception iex) { |
|
260 |
Logger.getLogger(IncrementableTask.class).error(iex); |
|
261 |
JOptionPane.showMessageDialog(null, Messages.getText("Failed_pausing_the_process"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
|
262 |
} |
|
263 |
} |
|
264 |
|
|
265 |
/* Asks user to cancel or not the process */ |
|
213 | 266 |
cancelled = false; |
214 |
String string1 = Messages.getText("si");
|
|
215 |
String string2 = Messages.getText("no");
|
|
267 |
String string1 = Messages.getText("Yes");
|
|
268 |
String string2 = Messages.getText("No");
|
|
216 | 269 |
Object[] options = { string1, string2 }; |
217 | 270 |
int answer = JOptionPane.showOptionDialog(getProgressPanel(), Messages |
218 | 271 |
.getText("msg_cancel_incrementable"), Messages |
219 | 272 |
.getText("title_cancel_incrementable"), |
220 | 273 |
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, |
221 | 274 |
options, string1); |
222 |
if (answer == JOptionPane.YES_OPTION) |
|
275 |
if (answer == JOptionPane.YES_OPTION) {
|
|
223 | 276 |
cancelled = true; |
277 |
|
|
278 |
/* Continues the process */ |
|
279 |
try { |
|
280 |
if (iIncrementable.isPausable()) |
|
281 |
callActionCommandListeners(IncrementableEvent.RESUMED); |
|
282 |
} catch (Exception e2) { |
|
283 |
Logger.getLogger(IncrementableTask.class).error(e2); |
|
284 |
Messages.getText("Failed_resuming_the_process"); |
|
285 |
} |
|
286 |
} |
|
287 |
else { |
|
288 |
/* Continues the process */ |
|
289 |
try { |
|
290 |
if (iIncrementable.isPausable()) |
|
291 |
callActionCommandListeners(IncrementableEvent.RESUMED); |
|
292 |
} catch (Exception e2) { |
|
293 |
Logger.getLogger(IncrementableTask.class).error(e2); |
|
294 |
Messages.getText("Failed_resuming_the_process"); |
|
295 |
} |
|
296 |
} |
|
224 | 297 |
} |
225 | 298 |
if (cancelled) { |
226 |
ended = true; |
|
299 |
// ended = true; |
|
300 |
// Will wait the process to finish and notify this to stop |
|
227 | 301 |
callActionCommandListeners(IncrementableEvent.CANCELED); |
228 | 302 |
} |
229 | 303 |
break; |
230 | 304 |
case ButtonsPanel.BUTTON_PAUSE: |
231 | 305 |
threadSuspended = true; |
232 |
callActionCommandListeners(IncrementableEvent.SUSPENDED); |
|
306 |
|
|
307 |
/* Pauses the associated process */ |
|
308 |
try { |
|
309 |
if (! iIncrementable.isPausable()) { |
|
310 |
JOptionPane.showMessageDialog(null, Messages.getText("The_process_cant_be_paused"), Messages.getText("Information"), JOptionPane.INFORMATION_MESSAGE); |
|
311 |
} |
|
312 |
else { |
|
313 |
callActionCommandListeners(IncrementableEvent.SUSPENDED); |
|
314 |
} |
|
315 |
} |
|
316 |
catch (Exception iex) { |
|
317 |
Logger.getLogger(IncrementableTask.class).error(iex); |
|
318 |
JOptionPane.showMessageDialog(null, Messages.getText("Failed_pausing_the_process"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
|
319 |
} |
|
320 |
|
|
233 | 321 |
break; |
234 | 322 |
case ButtonsPanel.BUTTON_RESTART: |
235 | 323 |
threadSuspended = false; |
324 |
|
|
325 |
/* Resumes the associated process */ |
|
236 | 326 |
callActionCommandListeners(IncrementableEvent.RESUMED); |
237 | 327 |
break; |
238 | 328 |
} |
Also available in: Unified diff