svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / RasterProcess.java @ 30541
History | View | Annotate | Download (11.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 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.raster; |
20 |
|
21 |
import java.util.Hashtable; |
22 |
|
23 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
24 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
25 |
import org.gvsig.gui.beans.incrementabletask.IIncrementable; |
26 |
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
27 |
import org.gvsig.gui.beans.incrementabletask.IncrementableListener; |
28 |
import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
29 |
import org.gvsig.raster.datastruct.Extent; |
30 |
import org.gvsig.raster.process.CancelEvent; |
31 |
import org.gvsig.raster.process.RasterTask; |
32 |
import org.gvsig.raster.process.RasterTaskQueue; |
33 |
import org.gvsig.raster.util.RasterToolsUtil; |
34 |
/**
|
35 |
* Clase base de todos los procesos raster. En ella se genstionan todas las
|
36 |
* funciones comunes como incremento de la tarea, gesti?n de eventos a la tarea,
|
37 |
* par?metros de la tarea, etc ...
|
38 |
*
|
39 |
* 18/12/2007
|
40 |
* @author Nacho Brodin nachobrodin@gmail.com
|
41 |
*/
|
42 |
public abstract class RasterProcess implements IIncrementable, IncrementableListener, Runnable { |
43 |
protected IncrementableTask incrementableTask = null; |
44 |
protected volatile Thread blinker = new Thread(this); |
45 |
protected RasterTask rasterTask = null; |
46 |
protected IProcessActions externalActions = null; |
47 |
protected Hashtable taskParams = new Hashtable(); |
48 |
|
49 |
private String log = ""; |
50 |
private String lastLine = ""; |
51 |
private long time = 0; |
52 |
private boolean progressActive = true; |
53 |
private IProcessActions queueActions = null; |
54 |
private boolean cancellable = true; |
55 |
|
56 |
/**
|
57 |
* Crea la ventana de IncrementableTask
|
58 |
*/
|
59 |
private IncrementableTask getIncrementableTask() {
|
60 |
if (incrementableTask == null) { |
61 |
incrementableTask = new IncrementableTask(this); |
62 |
incrementableTask.setAskCancel(cancellable); |
63 |
incrementableTask.addIncrementableListener(this);
|
64 |
} |
65 |
return incrementableTask;
|
66 |
} |
67 |
|
68 |
/**
|
69 |
* Define si se puede cancelar el proceso. Por defecto es true.
|
70 |
* @param enabled
|
71 |
*/
|
72 |
public void setCancelable(boolean enabled) { |
73 |
cancellable = enabled; |
74 |
getIncrementableTask().getButtonsPanel().setEnabled(ButtonsPanel.BUTTON_CANCEL, enabled); |
75 |
} |
76 |
|
77 |
/**
|
78 |
* Muestra la ventana de IncrementableTask
|
79 |
*/
|
80 |
private void showIncrementableWindow() { |
81 |
if (progressActive) {
|
82 |
getIncrementableTask().showWindow(); |
83 |
getIncrementableTask().start(); |
84 |
} |
85 |
} |
86 |
|
87 |
/**
|
88 |
* Arranca el proceso de recorte de un layer
|
89 |
*/
|
90 |
public void start() { |
91 |
showIncrementableWindow(); |
92 |
blinker.start(); |
93 |
} |
94 |
|
95 |
/**
|
96 |
* Proceso de carga de par?metros. Puede no hacerse una carga de par?metros
|
97 |
* explicita y obtenerlos directamente de la tabla Hash cuando vayan a usarse.
|
98 |
* Esto queda a elecci?n del programador. En caso de hacerse una carga de par?metros
|
99 |
* explicita esta llamada deber?a hacerse justo despues del constructor de la clase
|
100 |
* que contendr? el proceso.
|
101 |
*/
|
102 |
public abstract void init(); |
103 |
|
104 |
/**
|
105 |
* Proceso
|
106 |
* @throws InterruptedException
|
107 |
*/
|
108 |
public abstract void process() throws InterruptedException; |
109 |
|
110 |
/**
|
111 |
* Obtenci?n de un objeto de resultado. Este objeto deber? ser el mismo que el que se
|
112 |
* pasa por par?metro en el "end" de IProcessActions. Este m?todo es util cuando la tarea no se
|
113 |
* lanza en un Thread (ejecutamos process directamente) y queremos recoger el resultado
|
114 |
* sin registrarnos al evento de finalizaci?n de tarea.
|
115 |
* @return objeto resultado de la tarea
|
116 |
*/
|
117 |
public Object getResult() { |
118 |
return null; |
119 |
} |
120 |
|
121 |
/**
|
122 |
* Proceso
|
123 |
*/
|
124 |
public void execute() throws InterruptedException { |
125 |
init(); |
126 |
process(); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* M?todo donde se ejecutar? el Thread. Este har? las acciones globales para
|
131 |
* cualquier tarea y llamar? al m?todo execute especifico de una tarea.
|
132 |
*/
|
133 |
public void run() { |
134 |
long t1 = new java.util.Date().getTime(); |
135 |
|
136 |
try {
|
137 |
rasterTask = new RasterTask(this); |
138 |
RasterTaskQueue.register(rasterTask); |
139 |
execute(); |
140 |
} catch (InterruptedException e) { |
141 |
if (externalActions != null) |
142 |
externalActions.interrupted(); |
143 |
Thread.currentThread().interrupt();
|
144 |
} catch (Exception e) { |
145 |
if (progressActive) {
|
146 |
if (incrementableTask != null) { |
147 |
getIncrementableTask().processFinalize(); |
148 |
incrementableTask = null;
|
149 |
} |
150 |
} |
151 |
RasterToolsUtil.messageBoxError("error_processing", this, e); |
152 |
queueActions = null;
|
153 |
} finally {
|
154 |
RasterTaskQueue.remove(rasterTask); |
155 |
if (progressActive) {
|
156 |
if (incrementableTask != null) |
157 |
getIncrementableTask().processFinalize(); |
158 |
} |
159 |
if (queueActions != null) |
160 |
queueActions.end(this);
|
161 |
time = new java.util.Date().getTime() - t1;
|
162 |
} |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Activa o desactiva el interfaz de progreso en el lanzamiento de la tarea como un thread.
|
167 |
* @param active true para activarlo o false para desactivarlo
|
168 |
*/
|
169 |
public void setProgressActive(boolean active){ |
170 |
this.progressActive = active;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Obtiene el tiempo que tard? en ejecutarse la tarea
|
175 |
* la ?ltima vez que se proces?
|
176 |
*/
|
177 |
public long getTime() { |
178 |
return time;
|
179 |
} |
180 |
|
181 |
/**
|
182 |
* Obtiene el objeto para ejecutar acciones externar.
|
183 |
* @param IProcessActions
|
184 |
*/
|
185 |
public IProcessActions getActions() {
|
186 |
return externalActions;
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* Asigna el objeto para ejecutar acciones externar.
|
191 |
* @param IProcessActions
|
192 |
*/
|
193 |
public void setActions(IProcessActions actions) { |
194 |
this.externalActions = actions;
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* Obtiene el objeto para ejecutar acciones de la cola de procesos de ejecuci?n exclusiva.
|
199 |
* @param IProcessActions
|
200 |
*/
|
201 |
public IProcessActions getUniqueProcessActions() {
|
202 |
return queueActions;
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* Asigna el objeto para ejecutar acciones externar.
|
207 |
* @param IProcessActions
|
208 |
*/
|
209 |
public void setUniqueProcessActions(IProcessActions actions) { |
210 |
this.queueActions = actions;
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* Inserta una nueva l?nea en el log del cuadro de incremento de tarea
|
215 |
* @param line
|
216 |
*/
|
217 |
protected void insertLineLog(String line) { |
218 |
lastLine = line; |
219 |
log = log + line + "\n";
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Obtiene la ?ltima l?nea introducida en el log del cuadro de incremento.
|
224 |
*/
|
225 |
public String getLabel() { |
226 |
return lastLine;
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* Obtiene el texto de log del cuadro de incremento completo.
|
231 |
*/
|
232 |
public String getLog() { |
233 |
return log;
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* Un evento de cancelado es enviado a la tarea cuando actionCanceled es activado. Para
|
238 |
* ello se crear? un objeto CancelEvent y se asignar? a la tarea en ejecuci?n. Esta lo
|
239 |
* procesar? cuando pueda e interrumpir? el proceso.
|
240 |
*/
|
241 |
public void actionCanceled(IncrementableEvent e) { |
242 |
if(cancellable)
|
243 |
rasterTask.setEvent(new CancelEvent(this)); |
244 |
} |
245 |
|
246 |
/**
|
247 |
* A?ade un par?metro a la tarea
|
248 |
* @param name Clave del par?metro
|
249 |
* @param param Objeto pasado como par?metro
|
250 |
*/
|
251 |
public void addParam(String name, Object param) { |
252 |
if (param != null) |
253 |
taskParams.put(name, param); |
254 |
else
|
255 |
taskParams.remove(name); |
256 |
} |
257 |
|
258 |
/**
|
259 |
* Elimina un par?metro de la tarea
|
260 |
* @param name Clave del par?metro a eliminar
|
261 |
*/
|
262 |
public void removeParam(String name) { |
263 |
taskParams.remove(name); |
264 |
} |
265 |
|
266 |
/**
|
267 |
* Obtiene un par?metro a partir de la clave
|
268 |
* @param name Par?metro
|
269 |
* @return Par?metro
|
270 |
*/
|
271 |
public Object getParam(String name) { |
272 |
return taskParams.get(name);
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Obtiene un par?metro String a partir de la clave
|
277 |
* @param name Par?metro
|
278 |
* @return Par?metro
|
279 |
*/
|
280 |
public String getStringParam(String name) { |
281 |
Object value = taskParams.get(name);
|
282 |
return (value != null && value instanceof String) ? (String)value : null; |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Obtiene un par?metro byte a partir de la clave
|
287 |
* @param name Par?metro
|
288 |
* @return Par?metro
|
289 |
*/
|
290 |
public byte getByteParam(String name) { |
291 |
Object value = taskParams.get(name);
|
292 |
return (value != null && value instanceof Byte) ? ((Byte)value).byteValue() : 0; |
293 |
} |
294 |
|
295 |
/**
|
296 |
* Obtiene un par?metro float a partir de la clave
|
297 |
* @param name Par?metro
|
298 |
* @return Par?metro
|
299 |
*/
|
300 |
public float getFloatParam(String name) { |
301 |
Object value = taskParams.get(name);
|
302 |
return (value != null && value instanceof Float) ? ((Float)value).floatValue() : 0F; |
303 |
} |
304 |
|
305 |
/**
|
306 |
* Obtiene un par?metro double a partir de la clave
|
307 |
* @param name Par?metro
|
308 |
* @return Par?metro
|
309 |
*/
|
310 |
public double getDoubleParam(String name) { |
311 |
Object value = taskParams.get(name);
|
312 |
return (value != null && value instanceof Double) ? ((Double)value).doubleValue() : 0D; |
313 |
} |
314 |
|
315 |
/**
|
316 |
* Obtiene un par?metro entero a partir de la clave
|
317 |
* @param name Par?metro
|
318 |
* @return Par?metro
|
319 |
*/
|
320 |
public int getIntParam(String name) { |
321 |
Object value = taskParams.get(name);
|
322 |
return (value != null && value instanceof Integer) ? ((Integer)value).intValue() : 0; |
323 |
} |
324 |
|
325 |
/**
|
326 |
* Obtiene un par?metro boolean a partir de la clave
|
327 |
* @param name Par?metro
|
328 |
* @return Par?metro
|
329 |
*/
|
330 |
public boolean getBooleanParam(String name) { |
331 |
Object value = taskParams.get(name);
|
332 |
return (value != null && value instanceof Boolean) ? ((Boolean)value).booleanValue() : false; |
333 |
} |
334 |
|
335 |
/**
|
336 |
* Obtiene un par?metro int[] a partir de la clave
|
337 |
* @param name Par?metro
|
338 |
* @return Par?metro
|
339 |
*/
|
340 |
public int[] getIntArrayParam(String name) { |
341 |
Object value = taskParams.get(name);
|
342 |
return (value != null && value instanceof int[]) ? ((int[])value) : null; |
343 |
} |
344 |
|
345 |
/**
|
346 |
* Obtiene un par?metro double[] a partir de la clave
|
347 |
* @param name Par?metro
|
348 |
* @return Par?metro
|
349 |
*/
|
350 |
public double[] getDoubleArrayParam(String name) { |
351 |
Object value = taskParams.get(name);
|
352 |
return (value != null && value instanceof double[]) ? ((double[])value) : null; |
353 |
} |
354 |
|
355 |
/**
|
356 |
* Obtiene un par?metro capa raster a partir de la clave
|
357 |
* @param name Par?metro
|
358 |
* @return Par?metro
|
359 |
*/
|
360 |
public FLyrRasterSE getLayerParam(String name) { |
361 |
Object value = taskParams.get(name);
|
362 |
return (value != null && value instanceof FLyrRasterSE) ? ((FLyrRasterSE)value) : null; |
363 |
} |
364 |
|
365 |
/**
|
366 |
* Obtiene un par?metro extent a partir de la clave
|
367 |
* @param name Par?metro
|
368 |
* @return Par?metro
|
369 |
*/
|
370 |
public Extent getExtentParam(String name) { |
371 |
Object value = taskParams.get(name);
|
372 |
return (value != null && value instanceof Extent) ? ((Extent)value) : null; |
373 |
} |
374 |
|
375 |
/*
|
376 |
* (non-Javadoc)
|
377 |
* @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionResumed(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
378 |
*/
|
379 |
public void actionResumed(IncrementableEvent e) { |
380 |
} |
381 |
|
382 |
/*
|
383 |
* (non-Javadoc)
|
384 |
* @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionSuspended(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
385 |
*/
|
386 |
public void actionSuspended(IncrementableEvent e) { |
387 |
} |
388 |
|
389 |
/*
|
390 |
* (non-Javadoc)
|
391 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#isCancelable()
|
392 |
*/
|
393 |
public boolean isCancelable() { |
394 |
return true; |
395 |
} |
396 |
|
397 |
/*
|
398 |
* (non-Javadoc)
|
399 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#isPausable()
|
400 |
*/
|
401 |
public boolean isPausable() { |
402 |
return false; |
403 |
} |
404 |
} |