Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / filter / DefaultRasterFilterList.java @ 1736

History | View | Annotate | Download (22.8 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.impl.grid.filter;
23

    
24
import java.lang.reflect.Constructor;
25
import java.lang.reflect.InvocationTargetException;
26
import java.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.List;
29
import java.util.Stack;
30
import java.util.TreeMap;
31

    
32
import org.gvsig.fmap.dal.coverage.RasterLocator;
33
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
34
import org.gvsig.fmap.dal.coverage.datastruct.Params;
35
import org.gvsig.fmap.dal.coverage.exception.FilterManagerException;
36
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
37
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
38
import org.gvsig.fmap.dal.coverage.grid.FilterListChangeEvent;
39
import org.gvsig.fmap.dal.coverage.grid.FilterListChangeListener;
40
import org.gvsig.fmap.dal.coverage.grid.RasterFilter;
41
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
42
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager;
43
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter;
44
import org.gvsig.raster.impl.store.ParamsImpl;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.extensionpoint.ExtensionPoint;
48
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
49
import org.gvsig.tools.persistence.PersistenceManager;
50
import org.gvsig.tools.persistence.PersistentState;
51
import org.gvsig.tools.persistence.exception.PersistenceException;
52
/**
53
 * Esta clase representa la lista de filtros que debe ser manejada desde el
54
 * RasterFilterListManager.
55
 *
56
 * @author Nacho Brodin (nachobrodin@gmail.com)
57
 */
58
public class DefaultRasterFilterList implements RasterFilterList {
59
        public static final String      PERSISTENT_NAME        = "RasterFilterList_Persistent";
60
    public static final String      PERSISTENT_DESCRIPTION = "RasterFilterList Persistent";
61
        private Buffer                  rasterBuf              = null;
62
        private Buffer                  alphaBand              = null;
63
        private int                     typeFilter             = -1;
64
        private TreeMap<String, Object>
65
                                        environment            = new TreeMap<String, Object>();
66

    
67
        // Pila de objetos Filter (Contiene un RasterFilter)
68
        private List<RasterFilter>        
69
                                        list                   = new ArrayList<RasterFilter>();
70
        private Stack<List<RasterFilter>>            
71
                                        status                 = new Stack<List<RasterFilter>>();
72
        /**
73
         * Array de listeners que ser?n informados cuando cambia la lista de filtros
74
         */
75
        private List<FilterListChangeListener>        
76
                                        filterListListener     = new ArrayList<FilterListChangeListener>();
77
        private RasterFilterListManagerImpl
78
                                    manager                = null;
79
        
80
        /**
81
         * Asigna un listener a la lista que ser? informado cuando cambie un
82
         * filtro o posici?n en la lista.
83
         * @param listener FilterListListener
84
         */
85
        public void addFilterListListener(FilterListChangeListener listener) {
86
                filterListListener.add(listener);
87
        }
88

    
89
        /**
90
         * M?todo llamado cuando hay un cambio en una propiedad de visualizaci?n
91
         */
92
        private void callFilterListChanged(Object obj) {
93
                if(filterListListener != null) {
94
                        for (int i = 0; i < filterListListener.size(); i++) {
95
                                FilterListChangeEvent ev = new FilterListChangeEvent(obj);
96
                                ((FilterListChangeListener)filterListListener.get(i)).filterListChanged(ev);
97
                        }
98
                }
99
        }
100

    
101
        /**
102
         * A?ade un par?metro a la lista de filtros. Estos par?metros luego pueden ser
103
         * utilizados por los managers que se registren
104
         * @param key Nombre del par?metro que coincide con el nombre de la clase.
105
         * @param value Objeto
106
         */
107
        public void addEnvParam(String key, Object value) {
108
                environment.put(key, value);
109
        }
110

    
111
        /**
112
         * Obtiene un par?metro de la lista de filtros.
113
         * @param key Identificador del par?metro. Coincide con el nombre de la clase del par?metro.
114
         */
115
        public Object getEnvParam(String key) {
116
                return environment.get(key);
117
        }
118

    
119
        /**
120
         * Controla que los tipos de entrada y salida de los filtros sean los
121
         * correctos
122
         * @throws FilterTypeException
123
         */
124
        public void controlTypes() throws FilterTypeException {
125
                RasterFilterListManagerImpl stackManager = new RasterFilterListManagerImpl(this);
126
                stackManager.controlTypes();
127
        }
128

    
129
        /**
130
         * A?ade un filtro al final de la lista
131
         * @param filter        filtro a?adido
132
         * @throws FilterTypeException
133
         */
134
        public void add(RasterFilter filter) throws FilterTypeException {
135
                if (isActive(filter.getName()))
136
                        replace(filter, filter.getName());
137
                else {
138
                        list.add(filter);
139
                        controlTypes();
140
                }
141
                filter.setEnv(environment);
142
                callFilterListChanged(this);
143
        }
144

    
145
        /**
146
         * Sustituye un filtro de una posici?n de la pila por otro
147
         * @param filter
148
         * @param i
149
         * @throws FilterTypeException
150
         */
151
        public void replace(RasterFilter filter, String name) throws FilterTypeException {
152
                boolean changed = false;
153
                filter.setEnv(environment);
154
                for (int i = list.size() - 1; i >= 0; i--)
155
                        if (((BaseRasterFilter) list.get(i)).getName().equals(name)) {
156
                                list.remove(i);
157
                                list.add(i, filter);
158
                                changed = true;
159
                        }
160

    
161
                if (changed)
162
                        controlTypes();
163
                callFilterListChanged(this);
164
        }
165

    
166
        /**
167
         * A?ade un filtro en la lista en la posici?n indicada.
168
         * @param filter        filtro a?adido
169
         * @param pos        posici?n
170
         * @throws FilterTypeException
171
         */
172
        public void add(RasterFilter filter, int pos) throws FilterTypeException {
173
                try {
174
                        list.add(pos, filter);
175
                        controlTypes();
176
                } catch (IndexOutOfBoundsException e) {
177
                        add(filter);
178
                }
179
                filter.setEnv(environment);
180
                callFilterListChanged(this);
181
        }
182

    
183
        /**
184
         * Elimina un filtro a partir de su nombre
185
         * @param name Nombre del filtro a eliminar
186
         * @throws FilterTypeException
187
         */
188
        public void remove(String name) throws FilterTypeException {
189
                boolean changed = false;
190
                for (int i = list.size() - 1; i >= 0; i--)
191
                        if (((BaseRasterFilter) list.get(i)).getName().equals(name)) {
192
                                list.remove(i);
193
                                changed = true;
194
                        }
195
                if (changed)
196
                        controlTypes();
197
                callFilterListChanged(this);
198
        }
199

    
200
        /**
201
         * Elimina un filtro por clase.
202
         *
203
         * @param baseFilterClass
204
         * @throws FilterTypeException
205
         */
206
        @SuppressWarnings("unchecked")
207
        public void remove(Class baseFilterClass) throws FilterTypeException {
208
                boolean changed = false;
209
                for (int i = 0; i < lenght(); i++)
210
                        if (baseFilterClass.isInstance(list.get(i))) {
211
                                list.remove(i);
212
                                i--;
213
                                changed = true;
214
                        }
215
                if (changed)
216
                        controlTypes();
217
                callFilterListChanged(this);
218
        }
219

    
220
        /**
221
         * Devuelve el tipo de dato de retorno al aplicar la pila de filtros
222
         * @return
223
         */
224
        public int getOutDataType() {
225
                if (list.size() > 0)
226
                        return ((BaseRasterFilter) list.get(list.size() - 1)).getOutRasterDataType();
227
                else
228
                        return rasterBuf.getDataType();
229
        }
230

    
231
        /**
232
         * Devuelve el raster resultado de la aplicacion de la pila de filtros
233
         * @return
234
         */
235
        public Buffer getResult() {
236
                return rasterBuf;
237
        }
238

    
239
        /**
240
         * Devuelve la banda alpha para los filtros que generan una
241
         * @return
242
         */
243
        public Buffer getAlphaBand() {
244
                return alphaBand;
245
        }
246

    
247
        /**
248
         * Obtiene la cantidad de filtros en la lista
249
         * @return N?mero de filtros apilados
250
         */
251
        public int lenght() {
252
                return list.size();
253
        }
254

    
255
        /**
256
         * Obtiene el filtro apilado de la posici?n i o null si el indice es incorrecto
257
         * @param i        Posici?n a acceder en la pila
258
         * @return        Filtro
259
         */
260
        public RasterFilter get(int i) {
261
                if (i >= list.size() || i < 0)
262
                        return null;
263
                return (BaseRasterFilter) list.get(i);
264
        }
265

    
266
        /**
267
         * Obtiene el filtro apilado de nombre name o null si no existe
268
         * @param i       Nombre del filtro buscado
269
         * @return        Filtro
270
         */
271
        public RasterFilter get(String name) {
272
                for (int i = list.size() - 1; i >= 0; i--)
273
                        if (((BaseRasterFilter) list.get(i)).getName().equals(name))
274
                                return (BaseRasterFilter) list.get(i);
275
                return null;
276
        }
277

    
278
        /**
279
         * Obtiene el filtro apilado que corresponde con el nombre
280
         * @param name        Nombre de filtro
281
         * @return      Filtro en caso de que exista un filtro apilado de ese tipo
282
         * o null si no hay ninguno.
283
         */
284
        public RasterFilter getByName(String name) {
285
                for (int i = 0; i < lenght(); i++)
286
                        if (((BaseRasterFilter) list.get(i)).getName().equals(name))
287
                                return (BaseRasterFilter) list.get(i);
288
                return null;
289
        }
290

    
291
        /**
292
         * Obtiene el primer filtro de la lista que es instancia de la clase pasada por
293
         * par?metro
294
         * @param baseFilterClass Filtro base
295
         * @return RasterFilter
296
         */
297
        @SuppressWarnings("unchecked")
298
        public RasterFilter getFilterByBaseClass(Class baseFilterClass) {
299
                for (int i = 0; i < lenght(); i++)
300
                        if (baseFilterClass.isInstance(list.get(i)))
301
                                return (BaseRasterFilter) list.get(i);
302
                return null;
303
        }
304

    
305
        /**
306
         * Obtiene el tipo del filtro de la pila de la posici?n i
307
         * @param i Posici?n a acceder en la pila
308
         * @return tipo de filtro
309
         */
310
        public String getName(int i) {
311
                return ((BaseRasterFilter) list.get(i)).getName();
312
        }
313

    
314
        /**
315
         * Removes all filter of this stack
316
         */
317
        public void clear() {
318
                list.clear();
319
                callFilterListChanged(this);
320
        }
321

    
322
        /**
323
         * Replaces a filter in the i position by another
324
         * @param filter
325
         * @param i
326
         */
327
        public void replace(RasterFilter filter, int i) {
328
                filter.setEnv(environment);
329
                list.remove(i);
330
                list.add(i, filter);
331
                callFilterListChanged(this);
332
        }
333

    
334
        /**
335
         * Moves a specific filter to anothe position
336
         * @param filter 
337
         *                         Filter to move
338
         * @param position 
339
         *                         New position
340
         * @return If the filter existed returns true else it will return false
341
         */
342
        @SuppressWarnings("unchecked")
343
        public boolean move(Class filter, int position) {
344
                BaseRasterFilter f = null;
345
                for (int i = 0; i < list.size(); i++)
346
                        if(filter.isInstance(list.get(i))) {
347
                                f = (BaseRasterFilter) list.get(i);
348
                                list.remove(i);
349
                                break;
350
                        }
351
                if(f != null) {
352
                        list.add(position, f);
353
                        return true;
354
                }
355
                return false;
356
        }
357

    
358
        /**
359
         * Asigna el raster de entrada inicial
360
         * @param raster
361
         */
362
        public void setInitRasterBuf(Buffer raster) {
363
                rasterBuf = raster;
364
                if(rasterBuf != null)
365
                        typeFilter = rasterBuf.getDataType();
366
        }
367

    
368
        /**
369
         * Devuelve el tipo de datos inicial de la lista
370
         * @return Tipo de dato del raster inicial
371
         */
372
        public int getInitDataType() {
373
                return typeFilter;
374
        }
375

    
376
        /**
377
         * Asigna el tipo de dato inicial
378
         * @param dt
379
         */
380
        public void setInitDataType(int dt) {
381
                this.typeFilter = dt;
382
        }
383

    
384
        /**
385
         * M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
386
         * pila y false si no lo est?.
387
         * @param type        Tipo de par?metro a comprobar
388
         * @return true si est? en la pila y false si no lo est?
389
         */
390
        public boolean isActive(String name) {
391
                for (int i = list.size() - 1; i >= 0; i--)
392
                        if (((RasterFilter) list.get(i)).getName().equals(name))
393
                                return true;
394
                return false;
395
        }
396

    
397
        /**
398
         * M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en
399
         * la pila y false si no lo est?.
400
         *
401
         * @param filter Tipo de filtro a comprobar
402
         * @return true si est? en la pila y false si no lo est?
403
         */
404
        public boolean isActive(RasterFilter filter) {
405
                for (int i = list.size() - 1; i >= 0; i--)
406
                        if (((RasterFilter) list.get(i)).equals(filter))
407
                                return true;
408
                return false;
409
        }
410

    
411
        /**
412
         * Devuelve la posici?n en la lista de una clase de filtro concreta
413
         *
414
         * @param c Clase a buscar en la lista
415
         * @return posici?n en la lista
416
         */
417
        @SuppressWarnings("unchecked")
418
        public int getPosition(Class c) {
419
                for (int i = 0; i < list.size(); i++)
420
                        if (c.isInstance(list.get(i)))
421
                                return i;
422
                return -1;
423
        }
424

    
425
        /**
426
         * Aplica los filtros de la pila sobre el buffer correspondiente
427
         * @param dataType
428
         * @throws ProcessInterruptedException
429
         */
430
        private void executeFilterByDataType(int dataType) throws ProcessInterruptedException {
431
                environment.put("FirstUseTransparency", Boolean.TRUE);
432
                environment.put("HasNoDataFilter", Boolean.valueOf(isActive("nodata")));
433

    
434
                environment.put("FirstRaster", rasterBuf);
435
                alphaBand = null;
436
                for (int i = 0; i < list.size(); i++) {
437
                        BaseRasterFilter filter = (BaseRasterFilter) list.get(i);
438

    
439
                        // TODO: Arquitectura. Quitar el ControlTypes y en este momento
440
                        // cerciorarse de si el tipo del filtro es totalmente el correcto o hay
441
                        // que recrearlo. Ejemplo:
442
                        // Si el filtro que tenemos antes de preprocesar es de tipo Byte y la
443
                        // entrada de datos es de tipo float, reconstruir solo este filtro para
444
                        // que sea de tipo float
445

    
446
                        filter.addParam("raster", rasterBuf);
447
                        filter.execute();
448

    
449
                        if (filter.getResult("raster") != null)
450
                                this.rasterBuf = (Buffer) filter.getResult("raster");
451

    
452
                        //Si el filtro genera una banda alpha se mezcla con la que han generado otros
453
                        if (filter.getResult("alphaBand") != null)
454
                                if(alphaBand != null)
455
                                        alphaBand = RasterLocator.getManager().getColorConversion().mergeTransparencyBuffers(alphaBand, (Buffer)filter.getResult("alphaBand"));
456
                                else
457
                                        alphaBand = (Buffer)filter.getResult("alphaBand");
458
                }
459
                environment.remove("FirstRaster");
460
        }
461

    
462
        /**
463
         * Aplica los filtros sobre un RasterBuf
464
         * @return Buffer de salida
465
         * @throws ProcessInterruptedException
466
         */
467
        public Buffer execute() throws ProcessInterruptedException {
468
                if (rasterBuf == null)
469
                        return null;
470
                executeFilterByDataType(rasterBuf.getDataType());
471
                return rasterBuf;
472
        }
473

    
474
        /**
475
         * Muestra el contenido de la pila de filtros para depuraci?n
476
         */
477
        public void show() {
478
                System.out.println("--------------------------------------------");
479

    
480
                for (int i = 0; i < list.size() ; i++)
481
                        System.out.println("FILTRO:" + i + " NAME:" + ((BaseRasterFilter) list.get(i)).getName() + " FIL:" + ((BaseRasterFilter) list.get(i)).toString());
482
        }
483

    
484
        public void resetPercent() {
485
                for (int i = 0; i < list.size(); i++)
486
                        ((BaseRasterFilter) list.get(i)).resetPercent();
487
        }
488

    
489
        public int getPercent() {
490
                int percent = 0;
491
                if (list.size() == 0)
492
                        return 0;
493
                for (int i = 0; i < list.size(); i++)
494
                        percent += ((BaseRasterFilter) list.get(i)).getPercent();
495

    
496
                percent = percent / list.size();
497
                return percent;
498
        }
499

    
500
        /**
501
         * Guarda el estado de la lista de filtros en una pila, que se podr?
502
         * ir recuperando con popStatus()
503
         */
504
        public void pushStatus() {
505
                status.push(getStatusCloned());
506
        }
507

    
508
        /**
509
         * Obtiene el estado actual de los filtros, el ArrayList devuelto es una
510
         * clonaci?n del original, asi no compartiran datos.
511
         * @return
512
         */
513
        public List<RasterFilter> getStatusCloned() {
514
                List<RasterFilter> newArray = new ArrayList<RasterFilter>();
515
                for (int i = 0; i < list.size(); i++)
516
                        try {
517
                                newArray.add((RasterFilter)(((BaseRasterFilter) list.get(i)).clone()));
518
                        } catch (CloneNotSupportedException e) {
519
                                System.out.println("No se ha podido clonar");
520
                        }
521
                return newArray;
522
        }
523

    
524
        /**
525
         * Saves the current status of the filters
526
         * @param newArray
527
         */
528
        public void setStatus(List<RasterFilter> newArray) {
529
                if(list != null)
530
                        list.clear();
531
                if(newArray == null)
532
                        return;
533
                for (int i = 0; i < newArray.size(); i++)
534
                        if(list != null)
535
                                list.add(newArray.get(i));
536
                callFilterListChanged(this);
537
        }
538

    
539
        /**
540
         * Gets a status saved before through the call <code>pushStatus()</code>
541
         */
542
        public void popStatus() {
543
                if (status.size() <= 0)
544
                        return;
545

    
546
                setStatus((List<RasterFilter>) status.pop());
547
        }
548

    
549
        /**
550
         * Gets a TreeMap with the environment parameters 
551
         * @return TreeMap
552
         */
553
        public TreeMap<String, Object> getEnv() {
554
                return environment;
555
        }
556

    
557
        /**
558
         * Sets a TreeMap with the environment parameters 
559
         * @param env
560
         */
561
        public void setEnv(TreeMap<String, Object> env) {
562
                this.environment = env;
563
        }
564
        
565
        /*
566
         * (non-Javadoc)
567
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterList#createFilterListFromStrings(java.util.ArrayList)
568
         */
569
        public void createFilterListFromStrings(List<String> f) throws FilterTypeException {
570
                RasterFilterListManagerImpl.createFilterListFromStrings(this, f);
571
        }
572
        
573
        /*
574
         * (non-Javadoc)
575
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterList#getManagerByID(java.lang.String)
576
         */
577
        public RasterFilterListManager getManagerByID(String id) throws FilterManagerException {
578
                return new RasterFilterListManagerImpl(this).getManagerByID(id);
579
        }
580
        
581
        /*
582
         * (non-Javadoc)
583
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterList#getManagerByFilterClass(java.lang.Class)
584
         */
585
        @SuppressWarnings("unchecked")
586
        public RasterFilterListManager getManagerByFilterClass(Class c) {
587
                if(manager == null)
588
                        manager = new RasterFilterListManagerImpl(this);
589
                return manager.getManagerByFilterClass(c);
590
        }
591
        
592
        /*
593
         * (non-Javadoc)
594
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterList#getManagerByClass(java.lang.Class)
595
         */
596
        @SuppressWarnings("unchecked")
597
        public RasterFilterListManager getManagerByClass(Class clase) {
598
                Object obj = null;
599
                Class[] args = { RasterFilterList.class };
600
                try {
601
                        Constructor hazNuevo = clase.getConstructor(args);
602
                        Object[] args2 = { this };
603
                        obj = hazNuevo.newInstance(args2);
604
                } catch (SecurityException e) {
605
                        e.printStackTrace();
606
                } catch (NoSuchMethodException e) {
607
                        e.printStackTrace();
608
                } catch (IllegalArgumentException e) {
609
                        e.printStackTrace();
610
                } catch (InstantiationException e) {
611
                        e.printStackTrace();
612
                } catch (IllegalAccessException e) {
613
                        e.printStackTrace();
614
                } catch (InvocationTargetException e) {
615
                        e.printStackTrace();
616
                }
617
                return (RasterFilterListManager)obj;
618
        }
619
        
620
        public Class<?> getFilterClassByID(String id) {
621
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
622
                ExtensionPoint point = extensionPoints.get("RasterFilter");
623
                Iterator<?> it = point.iterator();
624
                while(it.hasNext()) {
625
                        ExtensionPoint.Extension entry = (ExtensionPoint.Extension) it.next();
626
                        if (entry != null) {
627
                                Class<?> managerClass = entry.getExtension();
628
                                RasterFilterListManager manager = getManagerByClass(managerClass);
629
                                if(manager != null) {
630
                                        Class<?> c = manager.getFilterClassByID(id);
631
                                        if(c != null)
632
                                                return c;
633
                                }
634
                        }
635
                }
636
                return null;
637
        }
638
        
639
        /*
640
         * (non-Javadoc)
641
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterList#getEmptyFilterParams()
642
         */
643
        public Params createEmptyFilterParams() {
644
                return new ParamsImpl();
645
        }
646
        
647
        public List<Class<?>> getRegisteredFilterList() {
648
                if(manager == null)
649
                        manager = new RasterFilterListManagerImpl(this);
650
                return manager.getRasterFilterList();
651
        }
652
        
653
        public List<Class<?>> getRegisteredFilterListByDataType(int dataType) {
654
                if(manager == null)
655
                        manager = new RasterFilterListManagerImpl(this);
656
                return manager.getRasterFilterListByDataType(dataType);
657
        }
658
        
659
        public RasterFilter createEmptyFilter(String strPackage) throws FilterTypeException {
660
                Class<?> filterClass = null;
661
                try {
662
                        filterClass = Class.forName(strPackage.trim());
663
                } catch (ClassNotFoundException e) {
664
                        throw new FilterTypeException("No puedo instanciar " + strPackage.trim());
665
                }
666

    
667
                Constructor<?> con = null;
668
                try {
669
                        con = filterClass.getConstructor();
670
                } catch (SecurityException e) {
671
                        throw new FilterTypeException("");
672
                } catch (NoSuchMethodException e) {
673
                        throw new FilterTypeException("");
674
                }
675

    
676
                BaseRasterFilter newFilter = null;
677
                try {
678
                        newFilter = (BaseRasterFilter) con.newInstance();
679
                } catch (IllegalArgumentException e) {
680
                        throw new FilterTypeException("");
681
                } catch (InstantiationException e) {
682
                        throw new FilterTypeException("");
683
                } catch (IllegalAccessException e) {
684
                        throw new FilterTypeException("");
685
                } catch (InvocationTargetException e) {
686
                        throw new FilterTypeException("");
687
                }
688
                return newFilter;
689
        }
690

    
691
        /*
692
         * (non-Javadoc)
693
         * @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
694
         */
695
        public void loadFromState(PersistentState state)
696
                        throws PersistenceException {
697
                this.typeFilter = state.getInt("typeFilter");
698
                
699
                /*List<PersistencyFilterParam> listFilterUsed = state.getList("paramlist");
700
                
701
                ArrayList<Exception> exc = new ArrayList<Exception>();
702
                for (int i = 0; i < listFilterUsed.size(); i++) {
703
                        try {
704
                                PersistencyFilterParam pfp = (PersistencyFilterParam) listFilterUsed.get(i);
705
                                if(pfp != null && pfp.getFilterClass() != null && pfp.getFilterParam() != null) {
706
                                        RasterFilterListManager filterManager = getManagerByFilterClass(pfp.getFilterClass());
707
                                        filterManager.setFilterList(this);
708
                                        if(filterManager != null)
709
                                                filterManager.addFilter(pfp.getFilterClass(), pfp.getFilterParam());
710
                                }
711
                        } catch (FilterTypeException e) {
712
                                exc.add(e);
713
                        }
714
                }*/
715
                
716
        }
717

    
718
        /*
719
         * (non-Javadoc)
720
         * @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
721
         */
722
        public void saveToState(PersistentState state) throws PersistenceException {
723
                state.set("typeFilter", typeFilter);
724
                
725
                /*ArrayList<PersistencyFilterParam> filters = new ArrayList<PersistencyFilterParam>();
726
                for (int i = 0; i < list.size(); i++) {
727
                        RasterFilter f = list.get(i);
728
                        Params uipar = f.getUIParams(f.getName());
729
                        PersistencyFilterParam param = new PersistencyFilterParam();
730
                        param.setFilterParam(uipar);
731
                        param.setFilterClass(f.getClass());
732
                        param.setFilterName(f.getName());
733
                        filters.add(param);
734
                }
735
                state.set("paramlist", filters);*/
736
        }        
737
        
738
        public static void registerPersistence() {
739
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
740
                DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
741
                if( definition == null ) {
742
                        definition = manager.addDefinition(
743
                                        DefaultRasterFilterList.class,
744
                                        PERSISTENT_NAME,
745
                                        PERSISTENT_DESCRIPTION,
746
                                        null, 
747
                                        null
748
                        );
749
                        
750
                        definition.addDynFieldInt("typeFilter").setMandatory(false);
751
                        //definition.addDynFieldList("paramlist").setClassOfItems(PersistencyFilterParam.class).setMandatory(false);
752
                }
753
        }
754
        
755
        /**
756
         * Releases buffer resources
757
         */
758
        public void dispose() {
759
                if (rasterBuf != null)
760
                        rasterBuf.dispose();
761
                if (alphaBand != null)
762
                        alphaBand.dispose();
763
                /*if(list != null) {
764
                        for (int i = 0; i < lenght(); i++)
765
                                ((BaseRasterFilter) list.get(i)).dispose();
766
                }*/
767
                try {
768
                        finalize();
769
                } catch (Throwable e) {
770
                }
771
        }
772
        
773
        /*
774
         * (non-Javadoc)
775
         * @see java.lang.Object#finalize()
776
         */
777
        protected void finalize() throws Throwable {
778
                rasterBuf           = null;
779
                alphaBand           = null;
780
                if(filterListListener != null) {
781
                        filterListListener.clear();
782
                        filterListListener = null;
783
                }
784
                if(status != null) {
785
                        status.clear();
786
                        status = null;
787
                }
788
                if(list != null) {
789
                        list.clear();
790
                        list = null;
791
                }
792
                super.finalize();
793
        }
794
}