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 @ 2443

History | View | Annotate | Download (22 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.dataset.Buffer;
33
import org.gvsig.fmap.dal.coverage.datastruct.Params;
34
import org.gvsig.fmap.dal.coverage.exception.FilterAddException;
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.fmap.dal.coverage.store.props.Transparency;
45
import org.gvsig.raster.impl.store.ParamsImpl;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.dynobject.DynStruct;
48
import org.gvsig.tools.extensionpoint.ExtensionPoint;
49
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
50
import org.gvsig.tools.persistence.PersistenceManager;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53
/**
54
 * Esta clase representa la lista de filtros que debe ser manejada desde el
55
 * RasterFilterListManager.
56
 *
57
 * @author Nacho Brodin (nachobrodin@gmail.com)
58
 */
59
public class DefaultRasterFilterList implements RasterFilterList {
60
        public static final String      PERSISTENT_NAME        = "RasterFilterList_Persistent";
61
    public static final String      PERSISTENT_DESCRIPTION = "RasterFilterList Persistent";
62
        private Buffer                  initialRasterBuffer    = null;
63
        private Buffer                  outputRasterBuffer     = null;
64
        private int                     typeFilter             = -1;
65
        private TreeMap<String, Object>
66
                                        environment            = new TreeMap<String, Object>();
67

    
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
        public void removeAll() {
201
                list.clear();
202
                callFilterListChanged(this);
203
        }
204

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

    
224
        /**
225
         * Devuelve el tipo de dato de retorno al aplicar la pila de filtros
226
         * @return
227
         */
228
        public int getOutDataType() {
229
                if (list.size() > 0)
230
                        return ((BaseRasterFilter) list.get(list.size() - 1)).getOutRasterDataType();
231
                else
232
                        if(outputRasterBuffer != null)
233
                                return outputRasterBuffer.getDataType();
234
                        else
235
                                return initialRasterBuffer.getDataType();
236
        }
237

    
238
        /**
239
         * Devuelve el raster resultado de la aplicacion de la pila de filtros
240
         * @return
241
         */
242
        public Buffer getResult() {
243
                return outputRasterBuffer;
244
        }
245

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
421
        /**
422
         * Aplica los filtros de la pila sobre el buffer correspondiente
423
         * @param dataType
424
         * @throws ProcessInterruptedException
425
         */
426
        private void executeFilterByDataType(int dataType) throws ProcessInterruptedException, FilterAddException {
427
                if(outputRasterBuffer != null && initialRasterBuffer != outputRasterBuffer) {
428
                        outputRasterBuffer.dispose();
429
                        outputRasterBuffer = null;
430
                }
431

    
432
                Buffer bufferToProcess = initialRasterBuffer;
433
                for (int i = 0; i < list.size(); i++) {
434
                        BaseRasterFilter filter = (BaseRasterFilter) list.get(i);
435

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

    
443
                        filter.addParam("raster", bufferToProcess);
444
                        filter.execute();
445

    
446
                        if (filter.getResult(RasterFilter.RESULT_BUFFER) != null) {
447
                                //Si no es el ?ltimo filtro se asigna el buffer de la pr?xima iteraci?n y se libera el anterior
448
                                if(i < (list.size() - 1)) {
449
                                        if(bufferToProcess != null && initialRasterBuffer != bufferToProcess)
450
                                                bufferToProcess.dispose();
451
                                        bufferToProcess = (Buffer) filter.getResult(RasterFilter.RESULT_BUFFER);
452
                                } else {
453
                                        //Si es el ?ltimo se carga el de salida
454
                                        outputRasterBuffer = (Buffer) filter.getResult(RasterFilter.RESULT_BUFFER);
455
                                }
456
                        }
457
                        
458
                        if (i == list.size() - 1  && filter.getResult(RasterFilter.RESULT_TRANSPARENCY) != null) {
459
                                addEnvParam("Transparency", filter.getResult(RasterFilter.RESULT_TRANSPARENCY));
460
                        }
461
                }
462
        }
463

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

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

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

    
487
        public void resetPercent() {
488
                for (int i = 0; i < list.size(); i++)
489
                        ((BaseRasterFilter) list.get(i)).resetPercent();
490
        }
491

    
492
        public int getPercent() {
493
                int percent = 0;
494
                if (list == null || list.size() == 0)
495
                        return 0;
496
                for (int i = 0; i < list.size(); i++)
497
                        percent += ((BaseRasterFilter) list.get(i)).getPercent();
498

    
499
                percent = percent / list.size();
500
                return percent;
501
        }
502

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

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

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

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

    
549
                setStatus((List<RasterFilter>) status.pop());
550
        }
551

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

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

    
648
                Constructor<?> con = null;
649
                try {
650
                        con = filterClass.getConstructor();
651
                } catch (SecurityException e) {
652
                        throw new FilterTypeException("");
653
                } catch (NoSuchMethodException e) {
654
                        throw new FilterTypeException("");
655
                }
656

    
657
                BaseRasterFilter newFilter = null;
658
                try {
659
                        newFilter = (BaseRasterFilter) con.newInstance();
660
                } catch (IllegalArgumentException e) {
661
                        throw new FilterTypeException("");
662
                } catch (InstantiationException e) {
663
                        throw new FilterTypeException("");
664
                } catch (IllegalAccessException e) {
665
                        throw new FilterTypeException("");
666
                } catch (InvocationTargetException e) {
667
                        throw new FilterTypeException("");
668
                }
669
                return newFilter;
670
        }
671

    
672
        public void loadFromState(PersistentState state)
673
                        throws PersistenceException {
674
                this.typeFilter = state.getInt("typeFilter");
675
                
676
                /*List<PersistencyFilterParam> listFilterUsed = state.getList("paramlist");
677
                
678
                ArrayList<Exception> exc = new ArrayList<Exception>();
679
                for (int i = 0; i < listFilterUsed.size(); i++) {
680
                        try {
681
                                PersistencyFilterParam pfp = (PersistencyFilterParam) listFilterUsed.get(i);
682
                                if(pfp != null && pfp.getFilterClass() != null && pfp.getFilterParam() != null) {
683
                                        RasterFilterListManager filterManager = getManagerByFilterClass(pfp.getFilterClass());
684
                                        filterManager.setFilterList(this);
685
                                        if(filterManager != null)
686
                                                filterManager.addFilter(pfp.getFilterClass(), pfp.getFilterParam());
687
                                }
688
                        } catch (FilterTypeException e) {
689
                                exc.add(e);
690
                        }
691
                }*/
692
                
693
        }
694

    
695
        public void saveToState(PersistentState state) throws PersistenceException {
696
                state.set("typeFilter", typeFilter);
697
                
698
                /*ArrayList<PersistencyFilterParam> filters = new ArrayList<PersistencyFilterParam>();
699
                for (int i = 0; i < list.size(); i++) {
700
                        RasterFilter f = list.get(i);
701
                        Params uipar = f.getUIParams(f.getName());
702
                        PersistencyFilterParam param = new PersistencyFilterParam();
703
                        param.setFilterParam(uipar);
704
                        param.setFilterClass(f.getClass());
705
                        param.setFilterName(f.getName());
706
                        filters.add(param);
707
                }
708
                state.set("paramlist", filters);*/
709
        }        
710
        
711
        public static void registerPersistence() {
712
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
713
                DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
714
                if( definition == null ) {
715
                        definition = manager.addDefinition(
716
                                        DefaultRasterFilterList.class,
717
                                        PERSISTENT_NAME,
718
                                        PERSISTENT_DESCRIPTION,
719
                                        null, 
720
                                        null
721
                        );
722
                        
723
                        definition.addDynFieldInt("typeFilter").setMandatory(false);
724
                        //definition.addDynFieldList("paramlist").setClassOfItems(PersistencyFilterParam.class).setMandatory(false);
725
                }
726
        }
727
        
728
        /**
729
         * Releases buffer resources
730
         */
731
        public void dispose() {
732
                if (initialRasterBuffer != null)
733
                        initialRasterBuffer.dispose();
734
                try {
735
                        finalize();
736
                } catch (Throwable e) {
737
                }
738
        }
739
        
740
        public Transparency getTransparency() {
741
                return (Transparency)getEnvParam("Transparency");
742
        }
743
        
744
        protected void finalize() throws Throwable {
745
                initialRasterBuffer = null;
746
                if(filterListListener != null) {
747
                        filterListListener.clear();
748
                        filterListListener = null;
749
                }
750
                if(status != null) {
751
                        status.clear();
752
                        status = null;
753
                }
754
                if(list != null) {
755
                        list.clear();
756
                        list = null;
757
                }
758
                super.finalize();
759
        }
760
}