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