root / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / incrementabletask / IncrementableTask.java @ 34688
History | View | Annotate | Download (10.2 KB)
1 |
package org.gvsig.gui.beans.incrementabletask; |
---|---|
2 |
|
3 |
/* gvSIG. Geographic Information System of the Valencian Government
|
4 |
*
|
5 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
6 |
* of the Valencian Government (CIT)
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
21 |
* MA 02110-1301, USA.
|
22 |
*
|
23 |
*/
|
24 |
|
25 |
import java.awt.event.WindowAdapter; |
26 |
import java.awt.event.WindowEvent; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Iterator; |
29 |
|
30 |
import javax.swing.JDialog; |
31 |
import javax.swing.JOptionPane; |
32 |
|
33 |
import org.slf4j.Logger; |
34 |
import org.slf4j.LoggerFactory; |
35 |
|
36 |
import org.gvsig.gui.beans.Messages; |
37 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
38 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
39 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
40 |
import org.gvsig.gui.beans.progresspanel.ProgressPanel; |
41 |
|
42 |
/**
|
43 |
* <code>IncrementableTask</code>. Es un dialogo que contiene un ProgressPanel.
|
44 |
* Se ejecuta bajo un Thread y va consultando a un objeto de tipo IIncrementable
|
45 |
* para modificar sus valores.
|
46 |
*
|
47 |
* @version 20/08/2008
|
48 |
*
|
49 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
50 |
*/
|
51 |
public class IncrementableTask implements Runnable, ButtonsPanelListener { |
52 |
|
53 |
private static final Logger LOG = LoggerFactory |
54 |
.getLogger(IncrementableTask.class); |
55 |
|
56 |
IIncrementable iIncrementable = null;
|
57 |
private volatile ProgressPanel progressPanel = null; |
58 |
private volatile Thread blinker = null; |
59 |
private boolean threadSuspended = false; |
60 |
private boolean ended = false; |
61 |
private boolean askOnCancel = true; |
62 |
private ArrayList<IncrementableListener> actionCommandListeners = new ArrayList<IncrementableListener>(); |
63 |
private boolean bDoCallListeners = true; |
64 |
static private int eventId = Integer.MIN_VALUE; |
65 |
|
66 |
/**
|
67 |
* Constructor del IncrementableTask.
|
68 |
* @param incrementable
|
69 |
*/
|
70 |
public IncrementableTask(IIncrementable incrementable, ProgressPanel dialog) {
|
71 |
iIncrementable = incrementable; |
72 |
progressPanel = dialog; |
73 |
configureProgressPanel(); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Constructor del IncrementableTask.
|
78 |
* @param incrementable
|
79 |
*/
|
80 |
public IncrementableTask(IIncrementable incrementable) {
|
81 |
iIncrementable = incrementable; |
82 |
configureProgressPanel(); |
83 |
} |
84 |
|
85 |
/**
|
86 |
* Inicio del thread para que la ventana vaya consultando por si sola al
|
87 |
* iIncrementable
|
88 |
*/
|
89 |
public void start() { |
90 |
blinker = new Thread(this); |
91 |
blinker.start(); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Detiene el proceso de consulta de la ventana.
|
96 |
*/
|
97 |
public void stop() { |
98 |
ended = true;
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Este thread va leyendo el porcentaje hasta que se completa el histograma.
|
103 |
*/
|
104 |
public synchronized void run() { |
105 |
// while (!ended && (iIncrementable.getPercent() <= 100)) {
|
106 |
while (! ended) {
|
107 |
try {
|
108 |
getProgressPanel().setLabel(iIncrementable.getLabel()); |
109 |
getProgressPanel().setPercent(iIncrementable.getPercent()); |
110 |
getProgressPanel().setTitle(iIncrementable.getTitle()); |
111 |
getProgressPanel().setLog(iIncrementable.getLog()); |
112 |
Thread.sleep(100); |
113 |
synchronized (this) { |
114 |
while (threadSuspended && !ended)
|
115 |
wait(500);
|
116 |
} |
117 |
} catch (InterruptedException e) { |
118 |
} |
119 |
} |
120 |
|
121 |
// Forces to refresh the log with the last changes
|
122 |
getProgressPanel().setLog(iIncrementable.getLog()); |
123 |
} |
124 |
|
125 |
/**
|
126 |
* Termina el proceso de lectura de porcentajes y logs de la ventana y
|
127 |
* cierra esta.
|
128 |
*/
|
129 |
public void processFinalize() { |
130 |
stop(); |
131 |
while (isAlive());
|
132 |
hide(); |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Ocultar la ventana y parar el proceso
|
137 |
*/
|
138 |
private void hide() { |
139 |
hideWindow(); |
140 |
progressPanel = null;
|
141 |
blinker = null;
|
142 |
} |
143 |
|
144 |
/**
|
145 |
* Ocultar la ventana
|
146 |
*/
|
147 |
public void hideWindow() { |
148 |
// getProgressPanel().dispose();
|
149 |
getProgressPanel().setVisible(false);
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* Devuelve un booleano indicando si esta activa la ventana.
|
154 |
* @return boolean
|
155 |
*/
|
156 |
public boolean isAlive() { |
157 |
if (blinker == null) |
158 |
return false; |
159 |
return blinker.isAlive();
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* Muestra la ventana de incremento con el porcentaje de la construcci?n del
|
164 |
* histograma.
|
165 |
*/
|
166 |
public void showWindow() { |
167 |
getProgressPanel().setTitle(iIncrementable.getTitle()); |
168 |
getProgressPanel().showLog(false);
|
169 |
getProgressPanel().setVisible(true);
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* Devuelve el componente ProgressPanel de la ventana incrementable.
|
174 |
* @return ProgressPanel
|
175 |
*/
|
176 |
public ProgressPanel getProgressPanel() {
|
177 |
if (progressPanel == null) { |
178 |
progressPanel = new ProgressPanel(false); |
179 |
} |
180 |
return progressPanel;
|
181 |
} |
182 |
|
183 |
protected void configureProgressPanel() { |
184 |
getProgressPanel().setAlwaysOnTop(true);
|
185 |
getProgressPanel().addButtonPressedListener(this);
|
186 |
|
187 |
// Must ask if user wants to cancel the process, avoid closing the dialog
|
188 |
getProgressPanel().setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE );
|
189 |
getProgressPanel().addWindowListener(new WindowAdapter() { |
190 |
public void windowClosing(WindowEvent e) { |
191 |
// Simulates an event like the produced pressing the cancel button of the associated progress panel
|
192 |
actionButtonPressed(new ButtonsPanelEvent(getProgressPanel(), ButtonsPanel.BUTTON_CANCEL));
|
193 |
} |
194 |
}); |
195 |
} |
196 |
|
197 |
private void callActionCommandListeners(int actions) { |
198 |
if (!bDoCallListeners)
|
199 |
return;
|
200 |
Iterator<IncrementableListener> acIterator = actionCommandListeners.iterator();
|
201 |
while (acIterator.hasNext()) {
|
202 |
IncrementableListener listener = (IncrementableListener) acIterator.next(); |
203 |
switch (actions) {
|
204 |
case IncrementableEvent.RESUMED:
|
205 |
listener.actionResumed(new IncrementableEvent(this)); |
206 |
break;
|
207 |
case IncrementableEvent.SUSPENDED:
|
208 |
listener.actionSuspended(new IncrementableEvent(this)); |
209 |
break;
|
210 |
case IncrementableEvent.CANCELED:
|
211 |
listener.actionCanceled(new IncrementableEvent(this)); |
212 |
break;
|
213 |
} |
214 |
} |
215 |
eventId++; |
216 |
} |
217 |
|
218 |
/**
|
219 |
* A?adir el manejador de eventos para atender las peticiones de start,
|
220 |
* stop...
|
221 |
*
|
222 |
* @param listener
|
223 |
*/
|
224 |
public void addIncrementableListener(IncrementableListener listener) { |
225 |
if (!actionCommandListeners.contains(listener))
|
226 |
actionCommandListeners.add(listener); |
227 |
} |
228 |
|
229 |
/**
|
230 |
* Borrar un manejador de eventos.
|
231 |
* @param listener
|
232 |
*/
|
233 |
public void removeIncrementableListener(IncrementableListener listener) { |
234 |
actionCommandListeners.remove(listener); |
235 |
} |
236 |
|
237 |
/**
|
238 |
* Definir si queremos que confirme al usuario si realmente desea cancelar el
|
239 |
* proceso
|
240 |
*
|
241 |
* @param value
|
242 |
*/
|
243 |
public void setAskCancel(boolean value) { |
244 |
askOnCancel = value; |
245 |
} |
246 |
|
247 |
/**
|
248 |
* Metodo para gestionar todos los eventos del objeto.
|
249 |
*/
|
250 |
public void actionButtonPressed(ButtonsPanelEvent e) { |
251 |
switch (e.getButton()) {
|
252 |
case ButtonsPanel.BUTTON_CANCEL:
|
253 |
boolean cancelled = true; |
254 |
if (askOnCancel) {
|
255 |
if (! iIncrementable.isCancelable()) {
|
256 |
JOptionPane.showMessageDialog(null, Messages.getText("The_process_cant_be_cancelled"), Messages.getText("Information"), JOptionPane.INFORMATION_MESSAGE); |
257 |
return;
|
258 |
} |
259 |
|
260 |
/* Pauses the process */
|
261 |
if (iIncrementable.isPausable()) {
|
262 |
try {
|
263 |
callActionCommandListeners(IncrementableEvent.SUSPENDED); |
264 |
} |
265 |
catch (Exception iex) { |
266 |
LOG.error("", iex);
|
267 |
JOptionPane.showMessageDialog(null, Messages.getText("Failed_pausing_the_process"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
268 |
} |
269 |
} |
270 |
|
271 |
/* Asks user to cancel or not the process */
|
272 |
cancelled = false;
|
273 |
String string1 = Messages.getText("Yes"); |
274 |
String string2 = Messages.getText("No"); |
275 |
Object[] options = { string1, string2 }; |
276 |
int answer = JOptionPane.showOptionDialog(getProgressPanel(), Messages |
277 |
.getText("msg_cancel_incrementable"), Messages
|
278 |
.getText("title_cancel_incrementable"),
|
279 |
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, |
280 |
options, string1); |
281 |
if (answer == JOptionPane.YES_OPTION) { |
282 |
cancelled = true;
|
283 |
|
284 |
/* Continues the process */
|
285 |
try {
|
286 |
if (iIncrementable.isPausable())
|
287 |
callActionCommandListeners(IncrementableEvent.RESUMED); |
288 |
} catch (Exception e2) { |
289 |
LOG.error("", e2);
|
290 |
Messages.getText("Failed_resuming_the_process");
|
291 |
} |
292 |
} |
293 |
else {
|
294 |
/* Continues the process */
|
295 |
try {
|
296 |
if (iIncrementable.isPausable())
|
297 |
callActionCommandListeners(IncrementableEvent.RESUMED); |
298 |
} catch (Exception e2) { |
299 |
LOG.error("", e2);
|
300 |
Messages.getText("Failed_resuming_the_process");
|
301 |
} |
302 |
} |
303 |
} |
304 |
if (cancelled) {
|
305 |
// ended = true;
|
306 |
// Will wait the process to finish and notify this to stop
|
307 |
callActionCommandListeners(IncrementableEvent.CANCELED); |
308 |
} |
309 |
break;
|
310 |
case ButtonsPanel.BUTTON_PAUSE:
|
311 |
threadSuspended = true;
|
312 |
|
313 |
/* Pauses the associated process */
|
314 |
try {
|
315 |
if (! iIncrementable.isPausable()) {
|
316 |
JOptionPane.showMessageDialog(null, Messages.getText("The_process_cant_be_paused"), Messages.getText("Information"), JOptionPane.INFORMATION_MESSAGE); |
317 |
} |
318 |
else {
|
319 |
callActionCommandListeners(IncrementableEvent.SUSPENDED); |
320 |
} |
321 |
} |
322 |
catch (Exception iex) { |
323 |
LOG.error("", iex);
|
324 |
JOptionPane.showMessageDialog(null, Messages.getText("Failed_pausing_the_process"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
325 |
} |
326 |
|
327 |
break;
|
328 |
case ButtonsPanel.BUTTON_RESTART:
|
329 |
threadSuspended = false;
|
330 |
|
331 |
/* Resumes the associated process */
|
332 |
callActionCommandListeners(IncrementableEvent.RESUMED); |
333 |
break;
|
334 |
} |
335 |
} |
336 |
|
337 |
/**
|
338 |
* @see ProgressPanel#getButtonsPanel()
|
339 |
*/
|
340 |
public ButtonsPanel getButtonsPanel() {
|
341 |
return getProgressPanel().getButtonsPanel();
|
342 |
} |
343 |
} |