Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / bands / ColorTableListManager.java @ 17108

History | View | Annotate | Download (10.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.bands;
20

    
21
import java.awt.Color;
22
import java.util.ArrayList;
23

    
24
import org.gvsig.raster.dataset.Params;
25
import org.gvsig.raster.datastruct.ColorItem;
26
import org.gvsig.raster.datastruct.ColorTable;
27
import org.gvsig.raster.grid.GridPalette;
28
import org.gvsig.raster.grid.filter.FilterTypeException;
29
import org.gvsig.raster.grid.filter.IRasterFilterListManager;
30
import org.gvsig.raster.grid.filter.RasterFilter;
31
import org.gvsig.raster.grid.filter.RasterFilterList;
32
import org.gvsig.raster.grid.filter.RasterFilterListManager;
33
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
34
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
35
/**
36
 * Gestor del filtro de aplicaci?n de tablas de color sobre un raster.
37
 *
38
 * @version 06/06/2007
39
 * @author Nacho Brodin (nachobrodin@gmail.com)
40
 *
41
 */
42
public class ColorTableListManager  implements IRasterFilterListManager {
43

    
44
  protected RasterFilterList        filterList = null;
45
  private RasterFilterListManager        filterListManager = null;
46

    
47
  public static void register() {
48
    ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
49
    extensionPoints.add("RasterFilter", "ColorTable", ColorTableListManager.class);
50
  }
51

    
52
  /**
53
   * Constructor.
54
   * Asigna la lista de filtros y el managener global.
55
   *
56
   * @param filterListManager
57
   */
58
  public ColorTableListManager(RasterFilterListManager filterListManager) {
59
    this.filterListManager = filterListManager;
60
    this.filterList = filterListManager.getFilterList();
61
  }
62

    
63
  /**
64
   * A?ade un filtro de tabla de color a la pila de filtros.
65
   * @param ladoVentana
66
 * @throws FilterTypeException 
67
   */
68
  public void addColorTableFilter(GridPalette palette) throws FilterTypeException {
69
    RasterFilter filter = new ColorTableByteFilter();
70

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

    
73
    if (filter != null) {
74
      filter.addParam("colorTable", palette);
75
      filter.addParam("Transparency", filterListManager.getFilterList().getEnvParam("Transparency"));
76
      filterList.add(filter);
77
    }
78
  }
79

    
80
  /*
81
   * (non-Javadoc)
82
   * @see org.gvsig.raster.grid.filter.IRasterFilterListManager#getRasterFilterList()
83
   */
84
  public ArrayList getRasterFilterList() {
85
    ArrayList filters = new ArrayList();
86
    filters.add(ColorTableFilter.class);
87
    return filters;
88
  }
89

    
90
  public void addFilter(Class classFilter, Params params) throws FilterTypeException {
91
    if (classFilter.equals(ColorTableFilter.class)) {
92
      GridPalette colorTable = null;
93
      for (int i = 0; i < params.getNumParams(); i++) {
94
        if (params.getParam(i).id.equals("colorTable"))
95
          colorTable = (GridPalette) params.getParam(i).defaultValue;
96
      }
97
      addColorTableFilter(colorTable);
98
    }
99
  }
100

    
101
  /**
102
   * Devuelve el color si lo encuentra en el arraylist y lo elimina, en caso
103
   * contrario devuelve null
104
   * @param list
105
   * @param value
106
   * @return
107
   */
108
  private static ColorItem getColorItem(ArrayList list, double value) {
109
    for (int i = 0; i < list.size(); i++) {
110
      if (((ColorItem) list.get(i)).getValue() == value) {
111
        return (ColorItem) list.remove(i);
112
      }
113
    }
114
    return null;
115
  }
116

    
117
  public static ColorTable createColorTableFromArray(ArrayList lines) {
118
    String pkgBase = "filter.colortable.";
119
    ArrayList linesCloned = (ArrayList) lines.clone();
120

    
121
    String paletteName = "";
122
    int color = 0;
123
    int alpha = 0;
124
    ArrayList rows = new ArrayList();
125

    
126
    ColorItem colorItem = new ColorItem();
127
    boolean interpolated = false;
128
    
129
    while (linesCloned.size() > 0) {
130
      String elem = (String) linesCloned.get(0);
131
      
132
      if (!elem.startsWith(pkgBase)) {
133
              linesCloned.remove(0);
134
              continue;
135
      }
136

    
137
      if (elem.startsWith(pkgBase + "name"))
138
              paletteName = RasterFilterListManager.getValue(elem);
139

    
140
      if (elem.startsWith(pkgBase + "interpolated"))
141
              interpolated = Boolean.parseBoolean(RasterFilterListManager.getValue(elem));
142

    
143
      if (elem.startsWith(pkgBase + "color" + color)) {
144
        if (elem.startsWith(pkgBase + "color" + color + ".value"))
145
          colorItem.setValue(Double.parseDouble(RasterFilterListManager.getValue(elem)));
146
        if (elem.startsWith(pkgBase + "color" + color + ".name"))
147
          colorItem.setNameClass(RasterFilterListManager.getValue(elem));
148
        if (elem.startsWith(pkgBase + "color" + color + ".rgb")) {
149
          
150
          String rgb = RasterFilterListManager.getValue(elem);
151
          int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue();
152
          int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))).intValue();
153
          int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue();
154

    
155
          colorItem.setColor(new Color(r, g, b));
156
        }
157
        if (elem.startsWith(pkgBase + "color" + color + ".interpolated"))
158
          colorItem.setInterpolated(Double.parseDouble(RasterFilterListManager.getValue(elem)));
159

    
160
        if ((linesCloned.size() <= 1) || (!((String) linesCloned.get(1)).startsWith(pkgBase + "color" + color))) {
161
                rows.add(colorItem);
162
                color++;
163
                colorItem = new ColorItem();
164
        }
165
      }
166
      
167
      if (elem.startsWith(pkgBase + "alpha" + alpha)) {
168
        if (elem.startsWith(pkgBase + "alpha" + alpha + ".value")) {
169
          ColorItem aux = getColorItem(rows, Double.parseDouble(RasterFilterListManager.getValue(elem)));
170
          if (aux != null) {
171
            colorItem = aux;
172
            colorItem.setNameClass(aux.getNameClass());
173
            colorItem.setInterpolated(aux.getInterpolated());
174
            colorItem.setColor(new Color(aux.getColor().getRed(), aux.getColor().getGreen(), aux.getColor().getBlue(), colorItem.getColor().getAlpha()));
175
          }
176
          
177
          colorItem.setValue(Double.parseDouble(RasterFilterListManager.getValue(elem)));
178
        }
179
        if (elem.startsWith(pkgBase + "alpha" + alpha + ".a")) {
180
          Color c = colorItem.getColor();
181
          colorItem.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(),Integer.parseInt(RasterFilterListManager.getValue(elem))));
182
        }
183
        if (elem.startsWith(pkgBase + "alpha" + alpha + ".interpolated"))
184
          colorItem.setInterpolated(Double.parseDouble(RasterFilterListManager.getValue(elem)));
185
        
186
        if ((linesCloned.size() <= 1) || (!((String) linesCloned.get(1)).startsWith(pkgBase + "alpha" + alpha))) {
187
                rows.add(colorItem);
188
                alpha++;
189
                colorItem = new ColorItem();
190
              }
191
      }
192

    
193
      linesCloned.remove(0);
194
    }
195
    
196
    ColorTable colorTable = new ColorTable();
197
    
198
                colorTable = new ColorTable();
199
                colorTable.setName(paletteName);
200
                colorTable.createPaletteFromColorItems(rows, false);
201
                colorTable.setInterpolated(interpolated);
202

    
203
                return colorTable;
204
  }
205
  
206
  /*
207
   * (non-Javadoc)
208
   * @see org.gvsig.raster.grid.filter.IRasterFilterListManager#createFilterListFromStrings(java.util.ArrayList, java.lang.String, int)
209
   */
210
  public int createFilterListFromStrings(ArrayList filters, String fil, int filteri) throws FilterTypeException {
211
    String pkgBase = "filter.colortable.";
212
    if (fil.startsWith(pkgBase + "active")) {
213
      boolean exec = true;
214
      if ((RasterFilterListManager.getValue(fil).equals("false")))
215
        exec = false;
216
      filters.remove(0);
217

    
218
            ColorTable colorTable = createColorTableFromArray(filters);
219
      
220
      filterList.remove(ColorTableFilter.class);
221
      addColorTableFilter(new GridPalette(colorTable));
222

    
223
      ColorTableFilter ct = (ColorTableFilter) filterList.getFilterByBaseClass(ColorTableFilter.class);
224
      ct.setExec(exec);
225
    }
226
    return filteri;
227
  }
228

    
229
  /*
230
   * (non-Javadoc)
231
   * @see org.gvsig.raster.grid.filter.IRasterFilterListManager#getStringsFromFilterList(java.util.ArrayList, org.gvsig.raster.grid.filter.RasterFilter)
232
   */
233
  public ArrayList getStringsFromFilterList(ArrayList filterList, RasterFilter rf) {
234
    if (rf instanceof ColorTableFilter) {
235
      String pkgBase = "filter.colortable.";
236
      ColorTableFilter colorTableFilter = (ColorTableFilter) rf;
237
      ColorTable colorTable = (ColorTable) colorTableFilter.getParam("colorTable");
238
      if (colorTable != null) {
239
        if (colorTableFilter.isExec())
240
          filterList.add(pkgBase + "active=true");
241
        else
242
          filterList.add(pkgBase + "active=false");
243
        
244
        filterList.add(pkgBase + "name=" + colorTable.getName());
245
        filterList.add(pkgBase + "interpolated=" + colorTable.isInterpolated());
246

    
247
        for (int i = 0; i < colorTable.getColorItems().size(); i++) {
248
          ColorItem colorItem = (ColorItem) colorTable.getColorItems().get(i);
249
          filterList.add(pkgBase + "color" + i + ".value=" + colorItem.getValue());
250
          filterList.add(pkgBase + "color" + i + ".name=" + colorItem.getNameClass());
251
          Color c = colorItem.getColor();
252
          filterList.add(pkgBase + "color" + i + ".rgb=" + c.getRed() + "," + c.getGreen() + "," + c.getBlue());
253
          filterList.add(pkgBase + "color" + i + ".interpolated=" + colorItem.getInterpolated());
254
        }
255

    
256
        for (int i = 0; i < colorTable.getColorItems().size(); i++) {
257
          ColorItem colorItem = (ColorItem) colorTable.getColorItems().get(i);
258
          filterList.add(pkgBase + "alpha" + i + ".value=" + colorItem.getValue());
259
          Color c = colorItem.getColor();
260
          filterList.add(pkgBase + "alpha" + i + ".a=" + c.getAlpha());
261
          filterList.add(pkgBase + "alpha" + i + ".interpolated=" + colorItem.getInterpolated());
262
        }
263
      }
264
    }
265
    return filterList;
266
  }
267
}