Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / filter / band / ColorTableListManager.java @ 2328

History | View | Annotate | Download (10.4 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.band;
23

    
24
import java.awt.Color;
25
import java.util.ArrayList;
26
import java.util.List;
27

    
28
import org.gvsig.fmap.dal.coverage.datastruct.ColorItem;
29
import org.gvsig.fmap.dal.coverage.datastruct.Params;
30
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
31
import org.gvsig.fmap.dal.coverage.grid.AbstractRasterFilterManager;
32
import org.gvsig.fmap.dal.coverage.grid.RasterFilter;
33
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
34
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter;
35
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
36
import org.gvsig.raster.impl.datastruct.ColorItemImpl;
37
import org.gvsig.raster.impl.grid.filter.RasterFilterListManagerImpl;
38
import org.gvsig.raster.impl.store.ParamImpl;
39
import org.gvsig.raster.impl.store.properties.DataStoreColorTable;
40
/**
41
 * Gestor del filtro de aplicaci?n de tablas de color sobre un raster.
42
 *
43
 * @version 06/06/2007
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 *
46
 */
47
public class ColorTableListManager  extends AbstractRasterFilterManager {
48
        private static String   ID = "ColorTable";
49
        
50
        /**
51
         * Default constructor. Sets the filter list.
52
         * @param filterList
53
         */
54
        public ColorTableListManager(RasterFilterList filterList) {
55
                super(filterList);
56
        }
57
        
58
        public String getManagerID() {
59
                return ID;
60
        }
61

    
62
        public static void register() {
63
                AbstractRasterFilterManager.register(ID, ColorTableListManager.class);
64
        }
65
        
66
        public boolean isDataTypeSupported(int dataType) {
67
                return true;
68
        }
69
        
70
        public Class<?> getFilterClassByID(String id) {
71
                if( id.compareTo("colortable") == 0)
72
                        return ColorTableFilter.class;
73
                return null;
74
        }
75
        
76

    
77
        /**
78
         * Constructor.
79
         * Asigna la lista de filtros y el managener global.
80
         *
81
         * @param filterListManager
82
         */
83
        public ColorTableListManager(RasterFilterListManagerImpl filterListManager) {
84
                super(filterListManager.getFilterList());
85
        }
86

    
87
        /**
88
         * A?ade un filtro de tabla de color a la pila de filtros.
89
         * @param ladoVentana
90
 * @throws FilterTypeException
91
         */
92
        public void addColorTableFilter(ColorTable palette) throws FilterTypeException {
93
                BaseRasterFilter filter = new ColorTableByteFilter();
94

    
95
                //Cuando el filtro esta creado, tomamos los valores y lo a?adimos a la pila
96

    
97
                if (filter != null) {
98
                        filter.addParam("colorTable", palette);
99
                        getFilterList().add(filter);
100
                }
101
        }
102

    
103
        public List<Class<?>> getRasterFilterList() {
104
                List<Class<?>> filters = new ArrayList<Class<?>>();
105
                filters.add(ColorTableFilter.class);
106
                return filters;
107
        }
108

    
109
        public void addFilter(Class<?> classFilter, Params params) throws FilterTypeException {
110
                if (ColorTableFilter.class.isAssignableFrom(classFilter)) {
111
                        ColorTable colorTable = null;
112
                        for (int i = 0; i < params.getNumParams(); i++) {
113
                                if (((ParamImpl)params.getParam(i)).getId().equals("colorTable"))
114
                                        colorTable = (ColorTable) ((ParamImpl)params.getParam(i)).getDefaultValue();
115
                        }
116
                        addColorTableFilter(colorTable);
117
                }
118
        }
119

    
120
        /**
121
         * Devuelve el color si lo encuentra en el arraylist y lo elimina, en caso
122
         * contrario devuelve null
123
         * @param list
124
         * @param value
125
         * @return
126
         */
127
        private static ColorItem getColorItem(List<ColorItem> list, double value) {
128
                for (int i = 0; i < list.size(); i++) {
129
                        if (((ColorItem) list.get(i)).getValue() == value) {
130
                                return (ColorItem) list.remove(i);
131
                        }
132
                }
133
                return null;
134
        }
135

    
136
        @SuppressWarnings("unchecked")
137
        public static ColorTable createColorTableFromArray(List<String> lines) {
138
                String pkgBase = "filter.colortable.";
139
                List<String> linesCloned = null;
140
                
141
                if(lines instanceof ArrayList) {
142
                        linesCloned = (List<String>)((ArrayList<?>) lines).clone();
143
                } else {
144
                        linesCloned = new ArrayList<String>();
145
                        for (int i = 0; i < lines.size(); i++) {
146
                                linesCloned.add(lines.get(i));
147
                        }
148
                }
149

    
150
                String paletteName = "";
151
                int color = 0;
152
                int alpha = 0;
153
                List<ColorItem> rows = new ArrayList<ColorItem>();
154

    
155
                ColorItem colorItem = new ColorItemImpl();
156
                boolean interpolated = false;
157

    
158
                while (linesCloned.size() > 0) {
159
                        String elem = (String) linesCloned.get(0);
160

    
161
                        if (!elem.startsWith(pkgBase)) {
162
                                linesCloned.remove(0);
163
                                continue;
164
                        }
165

    
166
                        if (elem.startsWith(pkgBase + "name"))
167
                                paletteName = RasterFilterListManagerImpl.getValue(elem);
168

    
169
                        if (elem.startsWith(pkgBase + "interpolated"))
170
                                interpolated = Boolean.parseBoolean(RasterFilterListManagerImpl.getValue(elem));
171

    
172
                        if (elem.startsWith(pkgBase + "color" + color)) {
173
                                if (elem.startsWith(pkgBase + "color" + color + ".value"))
174
                                        colorItem.setValue(Double.parseDouble(RasterFilterListManagerImpl.getValue(elem)));
175
                                if (elem.startsWith(pkgBase + "color" + color + ".name"))
176
                                        colorItem.setNameClass(RasterFilterListManagerImpl.getValue(elem));
177
                                if (elem.startsWith(pkgBase + "color" + color + ".rgb")) {
178

    
179
                                        String rgb = RasterFilterListManagerImpl.getValue(elem);
180
                                        int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue();
181
                                        int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))).intValue();
182
                                        int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue();
183

    
184
                                        colorItem.setColor(new Color(r, g, b));
185
                                }
186
                                if (elem.startsWith(pkgBase + "color" + color + ".interpolated"))
187
                                        colorItem.setInterpolated(Double.parseDouble(RasterFilterListManagerImpl.getValue(elem)));
188

    
189
                                if ((linesCloned.size() <= 1) || (!((String) linesCloned.get(1)).startsWith(pkgBase + "color" + color))) {
190
                                        rows.add(colorItem);
191
                                        color++;
192
                                        colorItem = new ColorItemImpl();
193
                                }
194
                        }
195

    
196
                        if (elem.startsWith(pkgBase + "alpha" + alpha)) {
197
                                if (elem.startsWith(pkgBase + "alpha" + alpha + ".value")) {
198
                                        ColorItem aux = getColorItem(rows, Double.parseDouble(RasterFilterListManagerImpl.getValue(elem)));
199
                                        if (aux != null) {
200
                                                colorItem = aux;
201
                                                colorItem.setNameClass(aux.getNameClass());
202
                                                colorItem.setInterpolated(aux.getInterpolated());
203
                                                colorItem.setColor(new Color(aux.getColor().getRed(), aux.getColor().getGreen(), aux.getColor().getBlue(), colorItem.getColor().getAlpha()));
204
                                        }
205

    
206
                                        colorItem.setValue(Double.parseDouble(RasterFilterListManagerImpl.getValue(elem)));
207
                                }
208
                                if (elem.startsWith(pkgBase + "alpha" + alpha + ".a")) {
209
                                        Color c = colorItem.getColor();
210
                                        colorItem.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(),Integer.parseInt(RasterFilterListManagerImpl.getValue(elem))));
211
                                }
212
                                if (elem.startsWith(pkgBase + "alpha" + alpha + ".interpolated"))
213
                                        colorItem.setInterpolated(Double.parseDouble(RasterFilterListManagerImpl.getValue(elem)));
214

    
215
                                if ((linesCloned.size() <= 1) || (!((String) linesCloned.get(1)).startsWith(pkgBase + "alpha" + alpha))) {
216
                                        rows.add(colorItem);
217
                                        alpha++;
218
                                        colorItem = new ColorItemImpl();
219
                                }
220
                        }
221

    
222
                        linesCloned.remove(0);
223
                }
224

    
225
                ColorTable colorTable = new DataStoreColorTable();
226

    
227
                colorTable = new DataStoreColorTable();
228
                colorTable.setName(paletteName);
229
                colorTable.createPaletteFromColorItems(rows, false);
230
                colorTable.setInterpolated(interpolated);
231

    
232
                return colorTable;
233
        }
234

    
235
        public int createFilterListFromStrings(List<String> filters, String fil, int filteri) throws FilterTypeException {
236
                String pkgBase = "filter.colortable.";
237
                if (fil.startsWith(pkgBase + "active")) {
238
                        boolean exec = true;
239
                        if ((RasterFilterListManagerImpl.getValue(fil).equals("false")))
240
                                exec = false;
241
                        filters.remove(0);
242

    
243
                        ColorTable colorTable = createColorTableFromArray(filters);
244

    
245
                        getFilterList().remove(ColorTableFilter.class);
246
                        addColorTableFilter(new DataStoreColorTable(colorTable));
247

    
248
                        ColorTableFilter ct = (ColorTableFilter) getFilterList().getFilterByBaseClass(ColorTableFilter.class);
249
                        ct.setExec(exec);
250
                }
251
                return filteri;
252
        }
253

    
254
        public List<String> getStringsFromFilterList(List<String> filterList, RasterFilter rf) {
255
                if (rf instanceof ColorTableFilter) {
256
                        String pkgBase = "filter.colortable.";
257
                        ColorTableFilter colorTableFilter = (ColorTableFilter) rf;
258
                        ColorTable colorTable = (ColorTable) colorTableFilter.getParam("colorTable");
259
                        if (colorTable != null) {
260
                                if (colorTableFilter.isExec())
261
                                        filterList.add(pkgBase + "active=true");
262
                                else
263
                                        filterList.add(pkgBase + "active=false");
264

    
265
                                filterList.add(pkgBase + "name=" + colorTable.getName());
266
                                filterList.add(pkgBase + "interpolated=" + colorTable.isInterpolated());
267

    
268
                                for (int i = 0; i < colorTable.getColorItems().size(); i++) {
269
                                        ColorItem colorItem = (ColorItem) colorTable.getColorItems().get(i);
270
                                        filterList.add(pkgBase + "color" + i + ".value=" + colorItem.getValue());
271
                                        filterList.add(pkgBase + "color" + i + ".name=" + colorItem.getNameClass());
272
                                        Color c = colorItem.getColor();
273
                                        filterList.add(pkgBase + "color" + i + ".rgb=" + c.getRed() + "," + c.getGreen() + "," + c.getBlue());
274
                                        filterList.add(pkgBase + "color" + i + ".interpolated=" + colorItem.getInterpolated());
275
                                }
276

    
277
                                for (int i = 0; i < colorTable.getColorItems().size(); i++) {
278
                                        ColorItem colorItem = (ColorItem) colorTable.getColorItems().get(i);
279
                                        filterList.add(pkgBase + "alpha" + i + ".value=" + colorItem.getValue());
280
                                        Color c = colorItem.getColor();
281
                                        filterList.add(pkgBase + "alpha" + i + ".a=" + c.getAlpha());
282
                                        filterList.add(pkgBase + "alpha" + i + ".interpolated=" + colorItem.getInterpolated());
283
                                }
284
                        }
285
                }
286
                return filterList;
287
        }
288
        
289
        public void addFilter(Params params) throws FilterTypeException {
290
                addFilter(ColorTableFilter.class, params);
291
        }
292
        
293
        public RasterFilter createFilter(Params params) {
294
                ColorTable colorTable = ((ColorTable) params.getParamById("colorTable").getDefaultValue());
295
                
296
                RasterFilter filter = new ColorTableByteFilter();
297
                filter.addParam("colorTable", colorTable);
298
                return filter;
299
        }
300
}