root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / RasterFilterListManager.java @ 13916
History | View | Annotate | Download (13.9 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 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.grid.filter; |
20 |
|
21 |
import java.lang.reflect.Constructor; |
22 |
import java.lang.reflect.InvocationTargetException; |
23 |
import java.util.ArrayList; |
24 |
import java.util.Iterator; |
25 |
import java.util.Map.Entry; |
26 |
import java.util.regex.Matcher; |
27 |
import java.util.regex.Pattern; |
28 |
|
29 |
import org.gvsig.raster.util.RasterUtilities; |
30 |
import org.gvsig.raster.util.extensionPoints.ExtensionPoint; |
31 |
import org.gvsig.raster.util.extensionPoints.ExtensionPoints; |
32 |
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton; |
33 |
|
34 |
/*
|
35 |
* TODO: FUNCIONALIDAD: Anulada las estad?sticas. Hay que incluirlas de nuevo.
|
36 |
*/
|
37 |
/**
|
38 |
* Esta clase es de la parte cliente y es la encargada de la gesti?n de la pila
|
39 |
* de filtros. Cada tipo de filtro o conjunto de tipos de filtro deben tener un
|
40 |
* gestor que implementa IRasterFilterManager. Esos gestores deben registrarse
|
41 |
* en esta clase con la funci?n addClassListManager. Este registro puede hacerse
|
42 |
* desde esta misma clase o desde una extensi?n. Un cliente que desee aplicar un
|
43 |
* filtro deber? introducirlo en la lista usando para ello las funciones que su
|
44 |
* manager de filtros le ofrece. Adem?s se encarga de otras funciones como la
|
45 |
* conversi?n de un filtro a cadena de Strings y la recuperaci?n desde cadena de
|
46 |
* Strings y el control de tipos de la salida de un raster de la lista con la
|
47 |
* entrada del siguiente filtro.
|
48 |
*
|
49 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
50 |
*/
|
51 |
public class RasterFilterListManager { |
52 |
protected RasterFilterList rasterFilterList = null; |
53 |
private boolean debug = false; |
54 |
protected ArrayList filterList = null; |
55 |
private ArrayList managers = new ArrayList(); |
56 |
|
57 |
/**
|
58 |
* Constructor
|
59 |
* @param filterStack
|
60 |
*/
|
61 |
public RasterFilterListManager(RasterFilterList filterStack) {
|
62 |
this.rasterFilterList = filterStack;
|
63 |
|
64 |
// Cargamos el manager con los gestores de drivers registrados
|
65 |
|
66 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
67 |
ExtensionPoint extensionPoint = (ExtensionPoint) extensionPoints.get("RasterFilter");
|
68 |
if (extensionPoint == null) |
69 |
return;
|
70 |
Iterator iterator = extensionPoint.entrySet().iterator();
|
71 |
while (iterator.hasNext()) {
|
72 |
Entry entry = (Entry) iterator.next(); |
73 |
if (entry != null) { |
74 |
Class RasterClass = (Class) entry.getValue(); |
75 |
Object obj = RasterFilterListManager.loadClass(RasterClass, this); |
76 |
if (obj != null) |
77 |
managers.add(obj); |
78 |
} |
79 |
} |
80 |
} |
81 |
|
82 |
|
83 |
/**
|
84 |
* Sustituye el tipo de filtro de strPackage por el de newClass
|
85 |
* @param strPackage cadena con el paquete y filtro a sustituir
|
86 |
* @param newClass clase con el tipo a sustituir. Por ej: ContrastShortFilter
|
87 |
* @return devuelve strPackage con la sustituci?n hecha.
|
88 |
*/
|
89 |
/*private String getStrPackage(String strPackage, String newClass) {
|
90 |
if (newClass.endsWith("ImageFilter"))
|
91 |
strPackage = strPackage + newClass.substring(0, newClass.lastIndexOf("ImageFilter"));
|
92 |
else if (newClass.endsWith("ShortFilter"))
|
93 |
strPackage = strPackage + newClass.substring(0, newClass.lastIndexOf("ShortFilter"));
|
94 |
else if (newClass.endsWith("DoubleFilter"))
|
95 |
strPackage = strPackage + newClass.substring(0, newClass.lastIndexOf("DoubleFilter"));
|
96 |
else if (newClass.endsWith("FloatFilter"))
|
97 |
strPackage = strPackage + newClass.substring(0, newClass.lastIndexOf("FloatFilter"));
|
98 |
strPackage = strPackage + "." + newClass;
|
99 |
return strPackage;
|
100 |
}*/
|
101 |
|
102 |
/**
|
103 |
* Controla que los tipos de los filtros de la pila sean correctos, es decir,
|
104 |
* que el tipo de salida de un filtro de salida coincida con el tipo de la
|
105 |
* entrada del siguiente. En caso de no ser as? crea el filtro de tipo
|
106 |
* adecuado y lo sustituye en el no coincidente. Esto es necesario ya que en
|
107 |
* la eliminaci?n de filtros puede quedarse en inconsistencia de tipos.
|
108 |
*/
|
109 |
public void controlTypes() { |
110 |
for (int i = 0; i<rasterFilterList.lenght(); i++) { |
111 |
String classFilter = null, packageFilter = null, oldClass = null; |
112 |
try {
|
113 |
RasterFilter rf = rasterFilterList.get(i); |
114 |
if (rf == null) |
115 |
return;
|
116 |
classFilter = rf.getClass().toString(); |
117 |
packageFilter = classFilter.substring(classFilter.indexOf(" ") + 1, classFilter.lastIndexOf(".")); |
118 |
oldClass = classFilter.substring(classFilter.lastIndexOf(".") + 1, classFilter.length()); |
119 |
} catch (ArrayIndexOutOfBoundsException ex) { |
120 |
return;
|
121 |
} catch (NullPointerException ex) { |
122 |
return;
|
123 |
} |
124 |
|
125 |
// Para el primer filtro comprobamos con el tipo de dato de entrada a la pila
|
126 |
if (i == 0) { |
127 |
if (rasterFilterList.getInitDataType() != rasterFilterList.get(i).getInRasterDataType()) {
|
128 |
Pattern p = Pattern.compile(RasterUtilities.typesToString(rasterFilterList.get(i).getInRasterDataType())); |
129 |
Matcher m = p.matcher(oldClass);
|
130 |
String newClass = m.replaceAll(RasterUtilities.typesToString(rasterFilterList.getInitDataType()));
|
131 |
String strPackage = packageFilter + "." + newClass; |
132 |
|
133 |
try {
|
134 |
Class filterClass = Class.forName(strPackage); |
135 |
Constructor con = filterClass.getConstructor(null); |
136 |
RasterFilter newFilter = (RasterFilter) con.newInstance(null);
|
137 |
newFilter.params = rasterFilterList.get(i).params; |
138 |
if (newFilter.params.get("filterName") != null) |
139 |
newFilter.setName((String) newFilter.params.get("filterName")); |
140 |
else
|
141 |
newFilter.setName(rasterFilterList.get(i).getName()); |
142 |
rasterFilterList.replace(newFilter, i); |
143 |
} catch (Exception e) { |
144 |
e.printStackTrace(); |
145 |
} |
146 |
} |
147 |
|
148 |
// Desde el filtro 2 en adelante se compara la salida de uno con la
|
149 |
// entrada del siguiente
|
150 |
} else {
|
151 |
if (rasterFilterList.get(i-1).getOutRasterDataType() != rasterFilterList.get(i).getInRasterDataType()) { |
152 |
Pattern p = Pattern.compile(RasterUtilities.typesToString(rasterFilterList.get(i).getInRasterDataType())); |
153 |
Matcher m = p.matcher(oldClass);
|
154 |
String newClass = m.replaceAll(RasterUtilities.typesToString(rasterFilterList.get(i - 1).getOutRasterDataType())); |
155 |
String strPackage = packageFilter + "." + newClass; |
156 |
|
157 |
try {
|
158 |
Class filterClass = Class.forName(strPackage.trim()); |
159 |
Constructor con = filterClass.getConstructor(null); |
160 |
RasterFilter newFilter = (RasterFilter) con.newInstance(null);
|
161 |
// newFilter.setFilterName(filterStack.get(i - 1).getFilterName());
|
162 |
newFilter.params = rasterFilterList.get(i).params; |
163 |
if (newFilter.params.get("filterName") != null) |
164 |
newFilter.setName((String) newFilter.params.get("filterName")); |
165 |
else
|
166 |
newFilter.setName(rasterFilterList.get(i).getName()); |
167 |
rasterFilterList.replace(newFilter, i); |
168 |
} catch (Exception e) { |
169 |
e.printStackTrace(); |
170 |
} |
171 |
} |
172 |
} |
173 |
} |
174 |
|
175 |
if (debug)
|
176 |
rasterFilterList.show(); |
177 |
} |
178 |
|
179 |
/**
|
180 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en
|
181 |
* la pila y false si no lo est?.
|
182 |
*
|
183 |
* @param filter Tipo de filtro a comprobar
|
184 |
* @return true si est? en la pila y false si no lo est?
|
185 |
*/
|
186 |
public boolean isActive(String name) { |
187 |
return rasterFilterList.isActive(name);
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* Elimina los filtros de la pila de un determinado tipo
|
192 |
*
|
193 |
* @param type Tipo de filtro a eliminar
|
194 |
*/
|
195 |
public void removeFilter(String name) { |
196 |
rasterFilterList.remove(name); |
197 |
this.controlTypes();
|
198 |
} |
199 |
|
200 |
public ArrayList getStringsFromFilterList() { |
201 |
filterList = new ArrayList(); |
202 |
for (int i = 0; i < rasterFilterList.lenght(); i++) { |
203 |
RasterFilter rf = rasterFilterList.get(i); |
204 |
|
205 |
// Se recorren todos los managers registrados comprobando si corresponde a
|
206 |
// la clase del filtro
|
207 |
for (int j = 0; j < managers.size(); j++) |
208 |
filterList = ((IRasterFilterListManager) managers.get(j)).getStringsFromFilterList(filterList, rf); |
209 |
} |
210 |
|
211 |
return filterList;
|
212 |
} |
213 |
|
214 |
/*
|
215 |
* (non-Javadoc)
|
216 |
* @see org.gvsig.raster.grid.filter.IRasterFilterListManager#createStackFromStrings(java.util.ArrayList, java.lang.String, int)
|
217 |
*/
|
218 |
public int createStackFromStrings(ArrayList filters, String fil, int filteri) { |
219 |
return filteri;
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Crea una pila de filtros a partir de un Array de Strings. Cada elemento del array debe
|
224 |
* tener la forma elemento=valor.
|
225 |
* @param filters
|
226 |
*/
|
227 |
public void createFilterListFromStrings(ArrayList f) { |
228 |
createFilterListFromStrings(f, new Integer(0)); |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Crea una pila de filtros a partir de un Array de Strings. Cada elemento del array debe
|
233 |
* tener la forma elemento=valor.
|
234 |
* @param pos Posici?n desde la cual se empieza a analizar.
|
235 |
* @param filters
|
236 |
*/
|
237 |
private void createFilterListFromStrings(ArrayList f, Integer pos) { |
238 |
ArrayList filters = (ArrayList) f.clone(); |
239 |
rasterFilterList.clear(); |
240 |
|
241 |
int filteri = pos.intValue();
|
242 |
|
243 |
// Busca un filtro activo y despu?s todas las propiedades que necesita ese
|
244 |
// filtro para ser creado. Una vez las tiene a?ade en la pila el tipo de
|
245 |
// filtro.
|
246 |
while ((filters.size() > 0) && (filteri < filters.size())) { |
247 |
String fil = (String) filters.get(filteri); |
248 |
|
249 |
for (int j = 0; j < managers.size(); j++) { |
250 |
try {
|
251 |
filteri = ((IRasterFilterListManager) managers.get(j)).createFilterListFromStrings(filters, fil, filteri); |
252 |
} catch (NullPointerException e) { |
253 |
} |
254 |
} |
255 |
|
256 |
filteri++; |
257 |
} |
258 |
} |
259 |
|
260 |
/**
|
261 |
* Obtiene el elemento de una cadena de la forma elemento=valor
|
262 |
* @param cadena
|
263 |
* @return
|
264 |
*/
|
265 |
public static String getElem(String cadena) { |
266 |
if (cadena != null) { |
267 |
return cadena.substring(0, cadena.indexOf("=")); |
268 |
} else {
|
269 |
return null; |
270 |
} |
271 |
} |
272 |
|
273 |
/**
|
274 |
* Obtiene el valor de una cadena de la forma elemento=valor
|
275 |
* @param cadena
|
276 |
* @return
|
277 |
*/
|
278 |
public static String getValue(String cadena) { |
279 |
if (cadena != null) { |
280 |
return cadena.substring(cadena.indexOf("=") + 1, cadena.length()); |
281 |
} else {
|
282 |
return null; |
283 |
} |
284 |
} |
285 |
|
286 |
/**
|
287 |
* Convierte un rango contenido en una array doble en una cadena de strings
|
288 |
* para poder salvar a xml
|
289 |
* @param rang
|
290 |
* @return
|
291 |
*/
|
292 |
/*private String rangeToString(int[][] rang) {
|
293 |
StringBuffer rangoStr = new StringBuffer();
|
294 |
|
295 |
if (rang != null) {
|
296 |
for (int i = 0; i < rang.length; i++) {
|
297 |
rangoStr.append(String.valueOf(rang[i][0]) + ":");
|
298 |
rangoStr.append(String.valueOf(rang[i][1]) + ":");
|
299 |
}
|
300 |
|
301 |
String r = rangoStr.toString();
|
302 |
|
303 |
if (r.endsWith(":")) {
|
304 |
r = r.substring(0, r.length() - 1);
|
305 |
}
|
306 |
|
307 |
return r;
|
308 |
} else {
|
309 |
return null;
|
310 |
}
|
311 |
}*/
|
312 |
|
313 |
/**
|
314 |
* Convierte una cadena en una lista de rangos numericos para poder asignar
|
315 |
* transparencias a la imagen
|
316 |
* @param rang
|
317 |
* @return
|
318 |
*/
|
319 |
/*private int[][] stringToRange(String rang) {
|
320 |
if ((rang != null) && !rang.equals("null")) {
|
321 |
ArrayList lista = new ArrayList();
|
322 |
StringTokenizer tokenizer = new StringTokenizer(rang, ":");
|
323 |
|
324 |
while (tokenizer.hasMoreTokens())
|
325 |
lista.add(tokenizer.nextToken());
|
326 |
|
327 |
int[][] intervalos = new int[(int) (lista.size() / 2)][2];
|
328 |
|
329 |
for (int i = 0; i < lista.size(); i = i + 2) {
|
330 |
intervalos[i / 2][0] = Integer.valueOf((String) lista.get(i)).intValue();
|
331 |
intervalos[i / 2][1] = Integer.valueOf((String) lista.get(i + 1)).intValue();
|
332 |
}
|
333 |
|
334 |
return intervalos;
|
335 |
} else {
|
336 |
return null;
|
337 |
}
|
338 |
}*/
|
339 |
|
340 |
/**
|
341 |
* Obtiene la lista de filtros
|
342 |
* @return RasterFilterList
|
343 |
*/
|
344 |
public RasterFilterList getFilterList() {
|
345 |
return rasterFilterList;
|
346 |
} |
347 |
|
348 |
/**
|
349 |
* Carga una clase pasada por par?metro. Como argumento del constructor de la
|
350 |
* clase se pasar? un RasterFilterStackManager. Esto es usado para instanciar
|
351 |
* los gestores de filtros registrados
|
352 |
* @param clase Clase a instanciar
|
353 |
* @param stackManager Par?metro del constructor de la clase a instanciar
|
354 |
* @return Objeto que corresponde a la instancia de la clase pasada.
|
355 |
*/
|
356 |
public static Object loadClass(Class clase, RasterFilterListManager stackManager) { |
357 |
Object obj = null; |
358 |
Class[] args = { RasterFilterListManager.class }; |
359 |
try {
|
360 |
Constructor hazNuevo = clase.getConstructor(args);
|
361 |
Object[] args2 = { stackManager }; |
362 |
obj = hazNuevo.newInstance(args2); |
363 |
} catch (SecurityException e) { |
364 |
e.printStackTrace(); |
365 |
} catch (NoSuchMethodException e) { |
366 |
e.printStackTrace(); |
367 |
} catch (IllegalArgumentException e) { |
368 |
e.printStackTrace(); |
369 |
} catch (InstantiationException e) { |
370 |
e.printStackTrace(); |
371 |
} catch (IllegalAccessException e) { |
372 |
e.printStackTrace(); |
373 |
} catch (InvocationTargetException e) { |
374 |
e.printStackTrace(); |
375 |
} |
376 |
return obj;
|
377 |
} |
378 |
|
379 |
/**
|
380 |
* Obtiene el manager registrado a partir de la clase
|
381 |
* @return
|
382 |
*/
|
383 |
public IRasterFilterListManager getManagerByClass(Class c) { |
384 |
for (int j = 0; j < managers.size(); j++) { |
385 |
if (managers.get(j).getClass().equals(c))
|
386 |
return (IRasterFilterListManager) managers.get(j);
|
387 |
} |
388 |
return null; |
389 |
} |
390 |
|
391 |
/**
|
392 |
* Obtiene el manager registrado a partir de la clase de un filtro
|
393 |
* @return
|
394 |
*/
|
395 |
public IRasterFilterListManager getManagerByFilterClass(Class c) { |
396 |
for (int i = 0; i < managers.size(); i++) |
397 |
for (int j = 0; j < ((IRasterFilterListManager) managers.get(i)).getRasterFilterList().size(); j++) |
398 |
if (((IRasterFilterListManager) managers.get(i)).getRasterFilterList().get(j).equals(c))
|
399 |
return (IRasterFilterListManager) managers.get(i);
|
400 |
return null; |
401 |
} |
402 |
|
403 |
/*
|
404 |
* (non-Javadoc)
|
405 |
* @see org.gvsig.raster.grid.filter.IRasterFilterListManager#getRasterFilterList()
|
406 |
*/
|
407 |
public ArrayList getRasterFilterList() { |
408 |
ArrayList filters = new ArrayList(); |
409 |
for (int i = 0; i < managers.size(); i++) { |
410 |
ArrayList auxFilters = ((IRasterFilterListManager) managers.get(i)).getRasterFilterList();
|
411 |
for (int j = 0; j < auxFilters.size(); j++) { |
412 |
filters.add(auxFilters.get(j)); |
413 |
} |
414 |
} |
415 |
return filters;
|
416 |
} |
417 |
} |