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