Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / RasterProcess.java @ 20832

History | View | Annotate | Download (9.7 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.process.CancelEvent;
30
import org.gvsig.raster.process.RasterTask;
31
import org.gvsig.raster.process.RasterTaskQueue;
32
/**
33
 * Clase base de todos los procesos raster. En ella se genstionan todas las
34
 * funciones comunes como incremento de la tarea, gesti?n de eventos a la tarea, 
35
 * par?metros de la tarea, etc ...
36
 * 
37
 * 18/12/2007
38
 * @author Nacho Brodin nachobrodin@gmail.com
39
 */
40
public abstract class RasterProcess implements IIncrementable, IncrementableListener, Runnable {
41
        protected IncrementableTask incrementableTask = null;
42
        protected volatile Thread   blinker           = new Thread(this);
43
        protected RasterTask        rasterTask        = null;
44
        protected IProcessActions   externalActions   = null;
45
        protected Hashtable         taskParams        = new Hashtable();
46

    
47
        private String              log               = "";
48
        private String              lastLine          = "";
49
        private long                time              = 0;
50
        private boolean             progressActive    = true;
51
        private IProcessActions     queueActions      = null;
52

    
53
        /**
54
         * Crea la ventana de IncrementableTask
55
         */
56
        private IncrementableTask getIncrementableTask() {
57
                if (incrementableTask == null) {
58
                        incrementableTask = new IncrementableTask(this);
59
                        incrementableTask.addIncrementableListener(this);
60
                }
61
                return incrementableTask;
62
        }
63
        
64
        /**
65
         * Define si se puede cancelar el proceso. Por defecto es true.
66
         * @param enabled
67
         */
68
        public void setCancelable(boolean enabled) {
69
                getIncrementableTask().getButtonsPanel().setEnabled(ButtonsPanel.BUTTON_CANCEL, enabled);
70
        }
71
        
72
        /**
73
         * Muestra la ventana de IncrementableTask
74
         */
75
        private void showIncrementableWindow() {
76
                if (progressActive) {
77
                        getIncrementableTask().showWindow();
78
                        getIncrementableTask().start();
79
                }
80
        }
81

    
82
        /**
83
         * Arranca el proceso de recorte de un layer
84
         */
85
        public void start() {
86
                showIncrementableWindow();
87
                blinker.start();
88
        }
89
                
90
        /**
91
         * Proceso de carga de par?metros. Puede no hacerse una carga de par?metros 
92
         * explicita y obtenerlos directamente de la tabla Hash cuando vayan a usarse. 
93
         * Esto queda a elecci?n del programador. En caso de hacerse una carga de par?metros
94
         * explicita esta llamada deber?a hacerse justo despues del constructor de la clase
95
         * que contendr? el proceso.
96
         */
97
        public abstract void init();
98
        
99
        /**
100
         * Proceso
101
         * @throws InterruptedException
102
         */
103
        public abstract void process() throws InterruptedException;
104
        
105
        /**
106
         * Obtenci?n de un objeto de resultado. Este objeto deber? ser el mismo que el que se
107
         * pasa por par?metro en el "end" de IProcessActions. Este m?todo es util cuando la tarea no se
108
         * lanza en un Thread (ejecutamos process directamente) y queremos recoger el resultado
109
         * sin registrarnos al evento de finalizaci?n de tarea.
110
         * @return objeto resultado de la tarea
111
         */
112
        public Object getResult() {
113
                return null;
114
        }
115
        
116
        /**
117
         * Proceso
118
         */
119
        public void execute() throws InterruptedException {
120
                init();
121
                process();
122
        }
123
        
124
        /**
125
         * M?todo donde se ejecutar? el Thread. Este har? las acciones globales para 
126
         * cualquier tarea y llamar? al m?todo execute especifico de una tarea.
127
         */
128
        public void run() {
129
                long t1 = new java.util.Date().getTime();
130

    
131
                try {
132
                        rasterTask = new RasterTask(this);
133
                        RasterTaskQueue.register(rasterTask);
134
                        execute();
135
                } catch (InterruptedException e) {
136
                        if (externalActions != null)
137
                                externalActions.interrupted();
138
                        Thread.currentThread().interrupt();
139
                } finally {
140

    
141
                        RasterTaskQueue.remove(rasterTask);
142
                        if (progressActive) {
143
                                if (incrementableTask != null)
144
                                        getIncrementableTask().processFinalize();
145
                        }
146
                        if (queueActions != null)
147
                                queueActions.end(this);
148
                        time = new java.util.Date().getTime() - t1;
149
                }
150
        }
151
        
152
        /**
153
         * Activa o desactiva el interfaz de progreso en el lanzamiento de la tarea como un thread.
154
         * @param active true para activarlo o false para desactivarlo
155
         */
156
        public void setProgressActive(boolean active){
157
                this.progressActive = active;
158
        }
159
        
160
        /**
161
         * Obtiene el tiempo que tard? en ejecutarse la tarea 
162
         * la ?ltima vez que se proces?
163
         */
164
        public long getTime() {
165
                return time;
166
        }
167
        
168
        /**
169
         * Obtiene el objeto para ejecutar acciones externar.
170
         * @param IProcessActions
171
         */
172
        public IProcessActions getActions() {
173
                return externalActions;
174
        }
175

    
176
        /**
177
         * Asigna el objeto para ejecutar acciones externar.
178
         * @param IProcessActions
179
         */
180
        public void setActions(IProcessActions actions) {
181
                this.externalActions = actions;
182
        }
183
        
184
        /**
185
         * Obtiene el objeto para ejecutar acciones de la cola de procesos de ejecuci?n exclusiva.
186
         * @param IProcessActions
187
         */
188
        public IProcessActions getUniqueProcessActions() {
189
                return queueActions;
190
        }
191

    
192
        /**
193
         * Asigna el objeto para ejecutar acciones externar.
194
         * @param IProcessActions
195
         */
196
        public void setUniqueProcessActions(IProcessActions actions) {
197
                this.queueActions = actions;
198
        }
199
        
200
        /**
201
         * Inserta una nueva l?nea en el log del cuadro de incremento de tarea
202
         * @param line
203
         */
204
        protected void insertLineLog(String line) {
205
                lastLine = line;
206
                log = log + line + "\n";
207
        }
208
        
209
        /**
210
         * Obtiene la ?ltima l?nea introducida en el log del cuadro de incremento.
211
         */
212
        public String getLabel() {
213
                return lastLine;
214
        }
215
        
216
        /**
217
         * Obtiene el texto de log del cuadro de incremento completo.
218
         */
219
        public String getLog() {
220
                return log;
221
        }
222
        
223
        /**
224
         * Un evento de cancelado es enviado a la tarea cuando actionCanceled es activado. Para
225
         * ello se crear? un objeto CancelEvent y se asignar? a la tarea en ejecuci?n. Esta lo
226
         * procesar? cuando pueda e interrumpir? el proceso.
227
         */
228
        public void actionCanceled(IncrementableEvent e) {
229
                rasterTask.setEvent(new CancelEvent(this));
230
        }
231
        
232
        /**
233
         * A?ade un par?metro a la tarea
234
         * @param name Clave del par?metro
235
         * @param param Objeto pasado como par?metro
236
         */
237
        public void addParam(String name, Object param) {
238
                if (param != null)
239
                        taskParams.put(name, param);
240
                else
241
                        taskParams.remove(name);
242
        }
243

    
244
        /**
245
         * Elimina un par?metro de la tarea
246
         * @param name Clave del par?metro a eliminar
247
         */
248
        public void removeParam(String name) {
249
                taskParams.remove(name);
250
        }
251

    
252
        /**
253
         * Obtiene un par?metro a partir de la clave
254
         * @param name Par?metro
255
         * @return Par?metro
256
         */
257
        public Object getParam(String name) {
258
                return taskParams.get(name);
259
        }
260
        
261
        /**
262
         * Obtiene un par?metro String a partir de la clave
263
         * @param name Par?metro
264
         * @return Par?metro
265
         */
266
        public String getStringParam(String name) {
267
                Object value = taskParams.get(name);
268
                return (value != null && value instanceof String) ? (String)value : null;
269
        }
270
        
271
        /**
272
         * Obtiene un par?metro byte a partir de la clave
273
         * @param name Par?metro
274
         * @return Par?metro
275
         */
276
        public byte getByteParam(String name) {
277
                Object value = taskParams.get(name);
278
                return (value != null && value instanceof Byte) ? ((Byte)value).byteValue() : 0;
279
        }
280
        
281
        /**
282
         * Obtiene un par?metro float a partir de la clave
283
         * @param name Par?metro
284
         * @return Par?metro
285
         */
286
        public float getFloatParam(String name) {
287
                Object value = taskParams.get(name);
288
                return (value != null && value instanceof Float) ? ((Float)value).floatValue() : 0F;
289
        }
290
        
291
        /**
292
         * Obtiene un par?metro double a partir de la clave
293
         * @param name Par?metro
294
         * @return Par?metro
295
         */
296
        public double getDoubleParam(String name) {
297
                Object value = taskParams.get(name);
298
                return (value != null && value instanceof Double) ? ((Double)value).doubleValue() : 0D;
299
        }
300
        
301
        /**
302
         * Obtiene un par?metro entero a partir de la clave
303
         * @param name Par?metro
304
         * @return Par?metro
305
         */
306
        public int getIntParam(String name) {
307
                Object value = taskParams.get(name);
308
                return (value != null && value instanceof Integer) ? ((Integer)value).intValue() : 0;
309
        }
310
        
311
        /**
312
         * Obtiene un par?metro boolean a partir de la clave
313
         * @param name Par?metro
314
         * @return Par?metro
315
         */
316
        public boolean getBooleanParam(String name) {
317
                Object value = taskParams.get(name);
318
                return (value != null && value instanceof Boolean) ? ((Boolean)value).booleanValue() : false;
319
        }
320
        
321
        /**
322
         * Obtiene un par?metro int[] a partir de la clave
323
         * @param name Par?metro
324
         * @return Par?metro
325
         */
326
        public int[] getIntArrayParam(String name) {
327
                Object value = taskParams.get(name);
328
                return (value != null && value instanceof int[]) ? ((int[])value) : null;
329
        }
330
        
331
        /**
332
         * Obtiene un par?metro capa raster a partir de la clave
333
         * @param name Par?metro
334
         * @return Par?metro
335
         */
336
        public FLyrRasterSE getLayerParam(String name) {
337
                Object value = taskParams.get(name);
338
                return (value != null && value instanceof FLyrRasterSE) ? ((FLyrRasterSE)value) : null;
339
        }
340
        
341
        public void actionResumed(IncrementableEvent e) {}
342
        public void actionSuspended(IncrementableEvent e) {}
343
}