Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.algorithm / src / main / java / org / gvsig / raster / algorithm / process / ProcessParamsManagement.java @ 2101

History | View | Annotate | Download (8.75 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.ArrayList;
25
import java.util.Hashtable;
26
import java.util.Iterator;
27
import java.util.List;
28

    
29
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
30
import org.gvsig.gui.beans.incrementabletask.IIncrementable;
31
import org.gvsig.gui.beans.incrementabletask.IncrementableListener;
32
import org.gvsig.tools.ToolsLocator;
33
import org.gvsig.tools.dispose.Disposable;
34
import org.gvsig.tools.extensionpoint.ExtensionPoint;
35
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
36

    
37
/**
38
 * Base class for all geoprocesses. This class contains all utilities to register
39
 * and get parameters in a geoprocess instance
40
 * 
41
 * @author Nacho Brodin nachobrodin@gmail.com
42
 */
43
public abstract class ProcessParamsManagement extends ProcessUtils implements IIncrementable, IncrementableListener, Runnable, Disposable {
44
        public static String                TIME                               = "TIME";
45
        public static String                PROCESS                            = "PROCESS";
46
        public static String                PROCESS_NAME                       = "PROCESS_NAME";
47
        
48
        protected Hashtable<String, Object> taskParams                         = new Hashtable<String, Object>();
49
        public static final String          REGISTER_INPUT_PARAMETERS_LABEL    = "RasterProcessInputParam";
50
        public static final String          REGISTER_OUTPUT_PARAMETERS_LABEL   = "RasterProcessOutputParam";
51

    
52
        /**
53
         *  Registers input parameters of a raster process
54
         */
55
        public static void registerInputParameter(String parameterLabel, Class<?> parameterClass, String processLabel) {
56
                registerParameter(processLabel, parameterLabel, parameterClass, REGISTER_INPUT_PARAMETERS_LABEL);
57
        }
58
        
59
        /**
60
         *  Registers output parameters of a raster process
61
         */
62
        public static void registerOutputParameter(String parameterLabel, Class<?> parameterClass, String processLabel) {
63
                registerParameter(processLabel, parameterLabel, parameterClass, REGISTER_OUTPUT_PARAMETERS_LABEL);
64
        }
65
        
66
        /**
67
         * Registers a parameter for a raster process
68
         */
69
        private static void registerParameter(String processLabel, String parameterLabel, Class<?> parameterClass, String type) {
70
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
71
                ExtensionPoint point = extensionPoints.add(type + "_" + processLabel);
72
                point.append(parameterLabel, "", parameterClass);
73
                registerGlobalOutputParameters(parameterLabel, parameterClass, processLabel);
74
        }
75
        
76
        public static void registerGlobalOutputParameters(String parameterLabel, Class<?> parameterClass, String processLabel) {
77
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
78
                ExtensionPoint point = extensionPoints.add(REGISTER_OUTPUT_PARAMETERS_LABEL + "_" + processLabel);
79
                if(!point.has(TIME) && !point.has(PROCESS)) {
80
                        point.append(TIME, "", Long.class);
81
                        point.append(PROCESS, "", DataProcess.class);
82
                        point.append(PROCESS_NAME, "", String.class);
83
                }
84
        }
85
        
86
        /**
87
         * Gets the key list of the input parameters
88
         * @param processLabel
89
         * @return
90
         */
91
        @SuppressWarnings("unchecked")
92
        public List<String> getRasterTaskInputParameters(String processLabel) {
93
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
94
                ExtensionPoint point = extensionPoints.add(REGISTER_INPUT_PARAMETERS_LABEL + "_" + processLabel);
95
                return point.getNames();
96
        }
97
        
98
        /**
99
         * Gets the key list of the output parameters
100
         * @param processLabel
101
         * @return
102
         */
103
        @SuppressWarnings("unchecked")
104
        public List<String> getRasterTaskOutputParameters(String processLabel) {
105
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
106
                ExtensionPoint point = extensionPoints.add(REGISTER_OUTPUT_PARAMETERS_LABEL + "_" + processLabel);
107
                return point.getNames();
108
        }
109
        
110
        /**
111
         * Gets the class of a parameter in a process.
112
         * @param processLabel
113
         * @param parameterName
114
         * @return
115
         */
116
        public Class<?> getParameterTypeByProcess(String processLabel, String parameterName) {
117
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
118
                ExtensionPoint point = extensionPoints.get(REGISTER_INPUT_PARAMETERS_LABEL + "_" + processLabel);
119
                return point.get(parameterName).getExtension();
120
        }
121
        
122
        /**
123
         * Gets a list with the class of all parameters. 
124
         * @param processLabel
125
         * @param parameterName
126
         * @return
127
         */
128
        public List<Class<?>> getParameterClassList(String processLabel, String parameterName) {
129
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
130
                ExtensionPoint point = extensionPoints.get(REGISTER_INPUT_PARAMETERS_LABEL + "_" + processLabel);
131
                List<Class<?>> classList = new ArrayList<Class<?>>();
132
                Iterator<?> it = point.iterator();
133
                while(it.hasNext()) {
134
                        Object obj = it.next();
135
                        classList.add((Class<?>)obj);
136
                }
137
                return classList;
138
        }
139
        
140
        @Override
141
        protected void finalize() throws Throwable {
142
                if(taskParams != null) {
143
                        taskParams.clear();
144
                        taskParams = null;
145
                }
146
                super.finalize();
147
        }
148
        
149
        /**
150
         * Add a parameter to this task at runtime. The 
151
         * parameter should have been registered before add
152
         * @param name key
153
         * @param param object to this task
154
         */
155
        public void addParam(String key, Object param) {
156
                if (param != null)
157
                        taskParams.put(key, param);
158
                else
159
                        taskParams.remove(key);
160
        }
161

    
162
        /**
163
         * Remove a parameter to this task at runtime.
164
         * @param name key
165
         */
166
        public void removeParam(String key) {
167
                taskParams.remove(key);
168
        }
169

    
170
        /**
171
         * Gets a parameter from its key
172
         * @param name key
173
         * @return parameter
174
         */
175
        public Object getParam(String key) {
176
                return taskParams.get(key);
177
        }
178
        
179
        /**
180
         * Gets a <code>String</code> parameter from its key
181
         * @param key
182
         * @return parameter
183
         */
184
        public String getStringParam(String key) {
185
                Object value = taskParams.get(key);
186
                return (value != null && value instanceof String) ? (String)value : null;
187
        }
188
        
189
        /**
190
         * Gets a <code>Byte</code> parameter from its key
191
         * @param key
192
         * @return parameter
193
         */
194
        public byte getByteParam(String name) {
195
                Object value = taskParams.get(name);
196
                return (value != null && value instanceof Byte) ? ((Byte)value).byteValue() : 0;
197
        }
198
        
199
        /**
200
         * Gets a <code>Float</code> parameter from its key
201
         * @param key
202
         * @return parameter
203
         */
204
        public float getFloatParam(String name) {
205
                Object value = taskParams.get(name);
206
                return (value != null && value instanceof Float) ? ((Float)value).floatValue() : 0F;
207
        }
208
        
209
        /**
210
         * Gets a <code>double</code> parameter from its key
211
         * @param key
212
         * @return parameter
213
         */
214
        public double getDoubleParam(String name) {
215
                Object value = taskParams.get(name);
216
                return (value != null && value instanceof Double) ? ((Double)value).doubleValue() : 0D;
217
        }
218
        
219
        /**
220
         * Gets a <code>int</code> parameter from its key
221
         * @param key
222
         * @return parameter
223
         */
224
        public int getIntParam(String name) {
225
                Object value = taskParams.get(name);
226
                return (value != null && value instanceof Integer) ? ((Integer)value).intValue() : 0;
227
        }
228
        
229
        /**
230
         * Gets a <code>Boolean</code> parameter from its key
231
         * @param key
232
         * @return parameter
233
         */
234
        public boolean getBooleanParam(String name) {
235
                Object value = taskParams.get(name);
236
                return (value != null && value instanceof Boolean) ? ((Boolean)value).booleanValue() : false;
237
        }
238
        
239
        /**
240
         * Gets a <code>int[]</code> parameter from its key
241
         * @param key
242
         * @return parameter
243
         */
244
        public int[] getIntArrayParam(String name) {
245
                Object value = taskParams.get(name);
246
                return (value != null && value instanceof int[]) ? ((int[])value) : null;
247
        }
248
        
249
        /**
250
         * Gets a <code>double[]</code> parameter from its key
251
         * @param key
252
         * @return parameter
253
         */
254
        public double[] getDoubleArrayParam(String name) {
255
                Object value = taskParams.get(name);
256
                return (value != null && value instanceof double[]) ? ((double[])value) : null;
257
        }
258
        
259
        /**
260
         * Gets a <code>Extent</code> parameter from its key
261
         * @param key
262
         * @return parameter
263
         */
264
        public Extent getExtentParam(String name) {
265
                Object value = taskParams.get(name);
266
                return (value != null && value instanceof Extent) ? ((Extent)value) : null;
267
        }
268
}