root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / incrementabletask / IncrementableTask.java @ 13136
History | View | Annotate | Download (6.7 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.gui.beans.incrementabletask; |
20 |
|
21 |
import java.awt.event.WindowAdapter; |
22 |
import java.awt.event.WindowEvent; |
23 |
import java.util.ArrayList; |
24 |
import java.util.Iterator; |
25 |
|
26 |
import javax.swing.JOptionPane; |
27 |
|
28 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
29 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
30 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
31 |
import org.gvsig.gui.beans.messages.Messages; |
32 |
import org.gvsig.gui.beans.progresspanel.ProgressPanel; |
33 |
/**
|
34 |
* <code>IncrementableTask</code>. Es un dialogo que contiene un ProgressPanel.
|
35 |
* Se ejecuta bajo un Thread y va consultando a un objeto de tipo IIncrementable
|
36 |
* para modificar sus valores.
|
37 |
*
|
38 |
* @version 29/03/2007
|
39 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
40 |
*/
|
41 |
public class IncrementableTask implements Runnable, ButtonsPanelListener { |
42 |
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 |
|
49 |
private ArrayList actionCommandListeners = new ArrayList(); |
50 |
private boolean bDoCallListeners = true; |
51 |
static private int eventId = Integer.MIN_VALUE; |
52 |
|
53 |
/**
|
54 |
* Constructor del IncrementableTask.
|
55 |
* @param incrementable
|
56 |
*/
|
57 |
public IncrementableTask(IIncrementable incrementable) {
|
58 |
iIncrementable = incrementable; |
59 |
} |
60 |
|
61 |
/**
|
62 |
* Inicio del thread para que la ventana vaya consultando por si sola al
|
63 |
* iIncrementable
|
64 |
*/
|
65 |
public void start() { |
66 |
blinker = new Thread(this); |
67 |
blinker.start(); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* Detiene el proceso de consulta de la ventana.
|
72 |
*/
|
73 |
public void stop() { |
74 |
ended = true;
|
75 |
} |
76 |
|
77 |
/**
|
78 |
* Este thread va leyendo el porcentaje hasta que se completa el histograma.
|
79 |
*/
|
80 |
public synchronized void run() { |
81 |
while (!ended && (iIncrementable.getPercent() <= 100)) { |
82 |
try {
|
83 |
getProgressPanel().setLabel(iIncrementable.getLabel()); |
84 |
getProgressPanel().setPercent(iIncrementable.getPercent()); |
85 |
getProgressPanel().setTitle(iIncrementable.getTitle()); |
86 |
getProgressPanel().setLog(iIncrementable.getLog()); |
87 |
Thread.sleep(100); |
88 |
synchronized (this) { |
89 |
while (threadSuspended && !ended)
|
90 |
wait(500);
|
91 |
} |
92 |
} catch (InterruptedException e) { |
93 |
} |
94 |
} |
95 |
} |
96 |
|
97 |
/**
|
98 |
* Termina el proceso de lectura de porcentajes y logs de la ventana y
|
99 |
* cierra esta.
|
100 |
*/
|
101 |
public void processFinalize() { |
102 |
stop(); |
103 |
while (isAlive());
|
104 |
hide(); |
105 |
} |
106 |
|
107 |
/**
|
108 |
* Ocultar la ventana y parar el proceso
|
109 |
*/
|
110 |
private void hide() { |
111 |
getProgressPanel().setVisible(false);
|
112 |
progressPanel = null;
|
113 |
blinker = null;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Devuelve un booleano indicando si esta activa la ventana.
|
118 |
* @return boolean
|
119 |
*/
|
120 |
public boolean isAlive() { |
121 |
return blinker.isAlive();
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* Muestra la ventana de incremento con el porcentaje de la construcci?n del
|
126 |
* histograma.
|
127 |
*/
|
128 |
public void showWindow() { |
129 |
getProgressPanel().setTitle(iIncrementable.getTitle()); |
130 |
getProgressPanel().showLog(false);
|
131 |
getProgressPanel().setVisible(true);
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* Devuelve el componente ProgressPanel de la ventana incrementable.
|
136 |
* @return ProgressPanel
|
137 |
*/
|
138 |
private ProgressPanel getProgressPanel() {
|
139 |
if (progressPanel == null) { |
140 |
progressPanel = new ProgressPanel(false); |
141 |
progressPanel.addButtonPressedListener(this);
|
142 |
progressPanel.addWindowListener(new WindowAdapter() { |
143 |
public void windowClosing(WindowEvent e) { |
144 |
ended = true;
|
145 |
callActionCommandListeners(IncrementableEvent.CANCELED); |
146 |
} |
147 |
}); |
148 |
} |
149 |
return progressPanel;
|
150 |
} |
151 |
|
152 |
private void callActionCommandListeners(int actions) { |
153 |
if (!bDoCallListeners)
|
154 |
return;
|
155 |
Iterator acIterator = actionCommandListeners.iterator();
|
156 |
while (acIterator.hasNext()) {
|
157 |
IncrementableListener listener = (IncrementableListener) acIterator.next(); |
158 |
switch (actions) {
|
159 |
case IncrementableEvent.RESUMED:
|
160 |
listener.actionResumed(new IncrementableEvent(this)); |
161 |
break;
|
162 |
case IncrementableEvent.SUSPENDED:
|
163 |
listener.actionSuspended(new IncrementableEvent(this)); |
164 |
break;
|
165 |
case IncrementableEvent.CANCELED:
|
166 |
listener.actionCanceled(new IncrementableEvent(this)); |
167 |
break;
|
168 |
} |
169 |
} |
170 |
eventId++; |
171 |
} |
172 |
|
173 |
/**
|
174 |
* A?adir el manejador de eventos para atender las peticiones de start,
|
175 |
* stop...
|
176 |
*
|
177 |
* @param listener
|
178 |
*/
|
179 |
public void addIncrementableListener(IncrementableListener listener) { |
180 |
if (!actionCommandListeners.contains(listener))
|
181 |
actionCommandListeners.add(listener); |
182 |
} |
183 |
|
184 |
/**
|
185 |
* Borrar un manejador de eventos.
|
186 |
* @param listener
|
187 |
*/
|
188 |
public void removeIncrementableListener(IncrementableListener listener) { |
189 |
actionCommandListeners.remove(listener); |
190 |
} |
191 |
|
192 |
/**
|
193 |
* Definir si queremos que confirme al usuario si realmente desea cancelar el
|
194 |
* proceso
|
195 |
*
|
196 |
* @param value
|
197 |
*/
|
198 |
public void setAskCancel(boolean value) { |
199 |
askOnCancel = value; |
200 |
} |
201 |
|
202 |
/**
|
203 |
* Metodo para gestionar todos los eventos del objeto.
|
204 |
*/
|
205 |
public void actionButtonPressed(ButtonsPanelEvent e) { |
206 |
switch (e.getButton()) {
|
207 |
case ButtonsPanel.BUTTON_CANCEL:
|
208 |
boolean cancelled = true; |
209 |
if (askOnCancel) {
|
210 |
cancelled = false;
|
211 |
String string1 = Messages.getText("si"); |
212 |
String string2 = Messages.getText("no"); |
213 |
Object[] options = { string1, string2 }; |
214 |
int answer = JOptionPane.showOptionDialog(getProgressPanel(), Messages |
215 |
.getText("msg_cancel_incrementable"), Messages
|
216 |
.getText("title_cancel_incrementable"),
|
217 |
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, |
218 |
options, string1); |
219 |
if (answer == JOptionPane.YES_OPTION) |
220 |
cancelled = true;
|
221 |
} |
222 |
if (cancelled) {
|
223 |
ended = true;
|
224 |
callActionCommandListeners(IncrementableEvent.CANCELED); |
225 |
} |
226 |
break;
|
227 |
case ButtonsPanel.BUTTON_PAUSE:
|
228 |
threadSuspended = true;
|
229 |
callActionCommandListeners(IncrementableEvent.SUSPENDED); |
230 |
break;
|
231 |
case ButtonsPanel.BUTTON_RESTART:
|
232 |
threadSuspended = false;
|
233 |
callActionCommandListeners(IncrementableEvent.RESUMED); |
234 |
break;
|
235 |
} |
236 |
} |
237 |
} |