gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.algorithm / src / main / java / org / gvsig / raster / algorithm / process / DataProcess.java @ 2127
History | View | Annotate | Download (10.2 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
package org.gvsig.raster.algorithm.process; |
23 |
|
24 |
import java.util.HashMap; |
25 |
import java.util.Hashtable; |
26 |
|
27 |
import javax.swing.SwingUtilities; |
28 |
|
29 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
30 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
31 |
import org.gvsig.fmap.dal.coverage.process.CancelEvent; |
32 |
import org.gvsig.fmap.dal.coverage.process.TaskEventManager; |
33 |
import org.gvsig.raster.algorithm.gui.IIncrementable; |
34 |
import org.gvsig.raster.algorithm.gui.IncrementableEvent; |
35 |
import org.gvsig.raster.algorithm.gui.IncrementableListener; |
36 |
import org.gvsig.raster.algorithm.gui.IncrementableTask; |
37 |
import org.gvsig.raster.swing.basepanel.IButtonsPanel; |
38 |
import org.gvsig.tools.dispose.Disposable; |
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
/**
|
42 |
* Clase base de todos los procesos raster. En ella se genstionan todas las
|
43 |
* funciones comunes como incremento de la tarea, gesti�n de eventos a la tarea,
|
44 |
* par�metros de la tarea, etc ...
|
45 |
*
|
46 |
* 18/12/2007
|
47 |
* @author Nacho Brodin nachobrodin@gmail.com
|
48 |
*/
|
49 |
public abstract class DataProcess extends ProcessParamsManagement implements IIncrementable, IncrementableListener, Runnable, Disposable { |
50 |
protected IncrementableTask incrementableTask = null; |
51 |
protected volatile Thread blinker = null; |
52 |
protected TaskEventManager taskEventManager = null; |
53 |
protected IProcessActions externalActions = null; |
54 |
protected Hashtable<String, Object> |
55 |
taskParams = new Hashtable<String, Object>(); |
56 |
HashMap<String, Object> output = new HashMap<String, Object>(); |
57 |
|
58 |
private String log = ""; |
59 |
private String lastLine = ""; |
60 |
private long time = 0; |
61 |
private boolean progressActive = true; |
62 |
private IProcessActions queueActions = null; |
63 |
protected Logger logger = LoggerFactory.getLogger(DataProcess.class.toString()); |
64 |
private int percent = 0; |
65 |
private String processName = null; |
66 |
|
67 |
//***************************************************
|
68 |
//Task control
|
69 |
//***************************************************
|
70 |
|
71 |
/**
|
72 |
* Crea la ventana de IncrementableTask
|
73 |
*/
|
74 |
public IncrementableTask getIncrementableTask() {
|
75 |
if (incrementableTask == null) { |
76 |
incrementableTask = new IncrementableTask(this); |
77 |
incrementableTask.addIncrementableListener(this);
|
78 |
} |
79 |
return incrementableTask;
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* Define si se puede cancelar el proceso. Por defecto es true.
|
84 |
* @param enabled
|
85 |
*/
|
86 |
public void setCancelable(boolean enabled) { |
87 |
getIncrementableTask().getButtonsPanel().setEnabled(IButtonsPanel.BUTTON_CANCEL, enabled); |
88 |
} |
89 |
|
90 |
/**
|
91 |
* Muestra la ventana de IncrementableTask
|
92 |
*/
|
93 |
public void showIncrementableWindow() { |
94 |
if (progressActive) {
|
95 |
getIncrementableTask().showWindow(); |
96 |
getIncrementableTask().start(); |
97 |
} |
98 |
} |
99 |
|
100 |
/**
|
101 |
* Arranca el proceso de recorte de un layer
|
102 |
*/
|
103 |
public void start() { |
104 |
showIncrementableWindow(); |
105 |
if(blinker == null) |
106 |
blinker = new Thread(this); |
107 |
blinker.start(); |
108 |
} |
109 |
|
110 |
/**
|
111 |
* Proceso de carga de par�metros. Puede no hacerse una carga de par�metros
|
112 |
* explicita y obtenerlos directamente de la tabla Hash cuando vayan a usarse.
|
113 |
* Esto queda a elecci�n del programador. En caso de hacerse una carga de par�metros
|
114 |
* explicita esta llamada deber�a hacerse justo despues del constructor de la clase
|
115 |
* que contendr� el proceso.
|
116 |
*/
|
117 |
public abstract void init(); |
118 |
|
119 |
/**
|
120 |
* Proceso
|
121 |
* @throws InterruptedException
|
122 |
*/
|
123 |
public abstract void process() throws ProcessInterruptedException, ProcessException; |
124 |
|
125 |
/**
|
126 |
* Gets the result of the process. All processes will have a parameter PROCESS that it is
|
127 |
* the own process. It is useful to get the process name or other data.
|
128 |
*
|
129 |
* This result of the process is sent to the method "end" in <code>IProcessActions</code> when the task
|
130 |
* is thrown in a thread or is recovered by the user when the task is thrown sequentially.
|
131 |
*
|
132 |
* @return Result of the task
|
133 |
*/
|
134 |
public HashMap<String, Object> getResult() { |
135 |
output.put("PROCESS", this); |
136 |
output.put(TIME, getTime()); |
137 |
output.put(PROCESS_NAME, getName()); |
138 |
return output;
|
139 |
} |
140 |
|
141 |
/**
|
142 |
* Adds a output from the process in runtime
|
143 |
* @param key
|
144 |
* @param value
|
145 |
*/
|
146 |
protected void addOutputValue(String key, Object value) { |
147 |
output.put(key, value); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Proceso
|
152 |
* @throws ProcessException
|
153 |
*/
|
154 |
public void execute() throws ProcessInterruptedException, ProcessException { |
155 |
init(); |
156 |
process(); |
157 |
|
158 |
//Sets in the output the result of the process
|
159 |
SwingUtilities.invokeLater(new Runnable() { |
160 |
public void run() { |
161 |
if (externalActions != null) { |
162 |
externalActions.end(getResult()); |
163 |
} |
164 |
} |
165 |
}); |
166 |
} |
167 |
|
168 |
/**
|
169 |
* M�todo donde se ejecutar� el Thread. Este har� las acciones globales para
|
170 |
* cualquier tarea y llamar� al m�todo execute especifico de una tarea.
|
171 |
*/
|
172 |
public void run() { |
173 |
long t1 = new java.util.Date().getTime(); |
174 |
|
175 |
try {
|
176 |
taskEventManager = RasterLocator.getManager().createRasterTask(this);
|
177 |
execute(); |
178 |
} catch (ProcessInterruptedException e) {
|
179 |
if (externalActions != null) |
180 |
externalActions.interrupted(); |
181 |
Thread.currentThread().interrupt();
|
182 |
} catch (ProcessException e) {
|
183 |
if (progressActive) {
|
184 |
if (incrementableTask != null) { |
185 |
getIncrementableTask().processFinalize(); |
186 |
incrementableTask = null;
|
187 |
} |
188 |
} |
189 |
logger.warn(RasterLocator.getManager().getRasterUtils().getTrace(e)); |
190 |
if(e.getMessage() != null && e.getMessage().compareTo("") != 0) |
191 |
messageBoxError(e.getMessage(), this);
|
192 |
else
|
193 |
messageBoxError("error_processing", this); |
194 |
queueActions = null;
|
195 |
|
196 |
} catch (Exception e) { |
197 |
if (progressActive) {
|
198 |
if (incrementableTask != null) { |
199 |
getIncrementableTask().processFinalize(); |
200 |
incrementableTask = null;
|
201 |
} |
202 |
} |
203 |
logger.warn(RasterLocator.getManager().getRasterUtils().getTrace(e)); |
204 |
if(e.getMessage() != null && e.getMessage().compareTo("") != 0) |
205 |
messageBoxError(e.getMessage(), null);
|
206 |
else
|
207 |
messageBoxError("error_processing", null); |
208 |
queueActions = null;
|
209 |
} finally {
|
210 |
taskEventManager.removeTask(); |
211 |
if (progressActive) {
|
212 |
if (incrementableTask != null) |
213 |
getIncrementableTask().processFinalize(); |
214 |
} |
215 |
if (queueActions != null) |
216 |
queueActions.end(this);
|
217 |
time = new java.util.Date().getTime() - t1;
|
218 |
blinker = null;
|
219 |
} |
220 |
} |
221 |
|
222 |
/**
|
223 |
* Activa o desactiva el interfaz de progreso en el lanzamiento de la tarea como un thread.
|
224 |
* @param active true para activarlo o false para desactivarlo
|
225 |
*/
|
226 |
public void setProgressActive(boolean active) { |
227 |
this.progressActive = active;
|
228 |
} |
229 |
|
230 |
/**
|
231 |
* Obtiene el tiempo que tard� en ejecutarse la tarea
|
232 |
* la �ltima vez que se proces�
|
233 |
*/
|
234 |
public long getTime() { |
235 |
return time;
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* Obtiene el objeto para ejecutar acciones externar.
|
240 |
* @param IProcessActions
|
241 |
*/
|
242 |
public IProcessActions getActions() {
|
243 |
return externalActions;
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* Asigna el objeto para ejecutar acciones externar.
|
248 |
* @param IProcessActions
|
249 |
*/
|
250 |
public void setActions(IProcessActions actions) { |
251 |
this.externalActions = actions;
|
252 |
} |
253 |
|
254 |
/**
|
255 |
* Obtiene el objeto para ejecutar acciones de la cola de procesos de ejecuci�n exclusiva.
|
256 |
* @param IProcessActions
|
257 |
*/
|
258 |
public IProcessActions getUniqueProcessActions() {
|
259 |
return queueActions;
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* Asigna el objeto para ejecutar acciones externar.
|
264 |
* @param IProcessActions
|
265 |
*/
|
266 |
public void setUniqueProcessActions(IProcessActions actions) { |
267 |
this.queueActions = actions;
|
268 |
} |
269 |
|
270 |
/**
|
271 |
* Inserta una nueva l�nea en el log del cuadro de incremento de tarea
|
272 |
* @param line
|
273 |
*/
|
274 |
protected void insertLineLog(String line) { |
275 |
lastLine = line; |
276 |
log = log + line + "\n";
|
277 |
} |
278 |
|
279 |
/**
|
280 |
* Obtiene la �ltima l�nea introducida en el log del cuadro de incremento.
|
281 |
*/
|
282 |
public String getLabel() { |
283 |
return lastLine;
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* Obtiene el texto de log del cuadro de incremento completo.
|
288 |
*/
|
289 |
public String getLog() { |
290 |
return log;
|
291 |
} |
292 |
|
293 |
/**
|
294 |
* Un evento de cancelado es enviado a la tarea cuando actionCanceled es activado. Para
|
295 |
* ello se crear� un objeto CancelEvent y se asignar� a la tarea en ejecuci�n. Esta lo
|
296 |
* procesar� cuando pueda e interrumpir� el proceso.
|
297 |
*/
|
298 |
public void actionCanceled(IncrementableEvent e) { |
299 |
taskEventManager.setEvent(new CancelEvent(this)); |
300 |
} |
301 |
|
302 |
/**
|
303 |
* Updates the percentaje of progress and manages the cancelation
|
304 |
* @param parcial
|
305 |
* @param total
|
306 |
* @throws ProcessInterruptedException
|
307 |
*/
|
308 |
public void updatePercent(int parcial, int total) throws ProcessInterruptedException { |
309 |
if (taskEventManager != null && taskEventManager.getEvent() != null) |
310 |
taskEventManager.manageEvent(taskEventManager.getEvent()); |
311 |
if(externalActions != null) |
312 |
externalActions.updateProgress(parcial, total); |
313 |
percent = (int)((parcial * 100) / total); |
314 |
} |
315 |
|
316 |
public int getPercent() { |
317 |
return percent;
|
318 |
} |
319 |
|
320 |
public void actionResumed(IncrementableEvent e) { |
321 |
} |
322 |
|
323 |
public void actionSuspended(IncrementableEvent e) { |
324 |
} |
325 |
|
326 |
public boolean isCancelable() { |
327 |
return true; |
328 |
} |
329 |
|
330 |
public boolean isPausable() { |
331 |
return false; |
332 |
} |
333 |
|
334 |
public String getName() { |
335 |
return processName;
|
336 |
} |
337 |
|
338 |
public void setName(String name) { |
339 |
this.processName = name;
|
340 |
} |
341 |
|
342 |
public void dispose() { |
343 |
try {
|
344 |
finalize(); |
345 |
} catch (Throwable e) { |
346 |
} |
347 |
} |
348 |
|
349 |
@Override
|
350 |
protected void finalize() throws Throwable { |
351 |
incrementableTask = null;
|
352 |
blinker = null;
|
353 |
externalActions = null;
|
354 |
super.finalize();
|
355 |
} |
356 |
|
357 |
} |