root / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / legend / ColorTableLegend.java @ 31631
History | View | Annotate | Download (6.72 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.fmap.raster.legend; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.util.Arrays; |
23 |
import java.util.List; |
24 |
|
25 |
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedLegend; |
26 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
27 |
import org.gvsig.fmap.mapcontext.rendering.legend.IRasterLegend; |
28 |
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendContentsChangedListener; |
29 |
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent; |
30 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
31 |
import org.gvsig.raster.datastruct.ColorItem; |
32 |
import org.gvsig.raster.datastruct.ColorTable; |
33 |
import org.gvsig.raster.util.MathUtils; |
34 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.SimpleFillSymbol; |
35 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl.SimpleLineSymbol; |
36 |
import org.gvsig.tools.ToolsLocator; |
37 |
import org.gvsig.tools.dynobject.DynClass; |
38 |
import org.gvsig.tools.persistence.PersistenceException; |
39 |
import org.gvsig.tools.persistence.PersistentState; |
40 |
|
41 |
/**
|
42 |
* Leyenda para tablas de color aplicadas a un raster.
|
43 |
*
|
44 |
* @version 27/06/2007
|
45 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
46 |
*/
|
47 |
public class ColorTableLegend implements IClassifiedLegend, IRasterLegend { |
48 |
public static final String COLOR_TABLE_LEGEND_DYNCLASS_NAME = |
49 |
"ColorTableLegend";
|
50 |
|
51 |
private static final String FIELD_SYMBOLS = "symbols"; |
52 |
private static final String FIELD_DESCRIPTIONS = "descriptions"; |
53 |
|
54 |
ISymbol[] symbols = null; |
55 |
String[] desc = null; |
56 |
|
57 |
/**
|
58 |
* Crea una leyenda de tipo ColorTableLegend a partir de un objeto ColorTable
|
59 |
* @param colorTable
|
60 |
* @return ColorTableLegend
|
61 |
*/
|
62 |
public static ColorTableLegend createLegend(ColorTable colorTable) { |
63 |
if ((colorTable == null) || (colorTable.getColorItems() == null)) |
64 |
return null; |
65 |
|
66 |
SimpleLineSymbol line = new SimpleLineSymbol();
|
67 |
line.setLineColor(Color.BLACK);
|
68 |
ISymbol[] symbol = new ISymbol[colorTable.getColorItems().size()]; |
69 |
String[] desc = new String[colorTable.getColorItems().size()]; |
70 |
|
71 |
for (int i = 0; i < colorTable.getColorItems().size(); i++) { |
72 |
SimpleFillSymbol s = new SimpleFillSymbol();
|
73 |
s.setOutline(line); |
74 |
s.setFillColor(((ColorItem) colorTable.getColorItems().get(i)).getColor()); |
75 |
if (i < (colorTable.getColorItems().size() - 1)) |
76 |
desc[i] = "[" + MathUtils.format(((ColorItem) colorTable.getColorItems().get(i)).getValue(), 2) + " , " + MathUtils.format(((ColorItem) colorTable.getColorItems().get(i + 1)).getValue(), 2) + "[ " + ((ColorItem) colorTable.getColorItems().get(i)).getNameClass(); |
77 |
else
|
78 |
desc[i] = "[" + MathUtils.format(((ColorItem) colorTable.getColorItems().get(i)).getValue(), 2) + "] " + ((ColorItem) colorTable.getColorItems().get(i)).getNameClass(); |
79 |
symbol[i] = s; |
80 |
} |
81 |
|
82 |
return new ColorTableLegend(symbol, desc); |
83 |
} |
84 |
|
85 |
/**
|
86 |
* Leyenda para tablas de color raster.
|
87 |
* @param s Lista de simbolos
|
88 |
* @param d Lista de descripciones de simbolos
|
89 |
*/
|
90 |
public ColorTableLegend(ISymbol[] s, String[] d) { |
91 |
symbols = s; |
92 |
desc = d; |
93 |
} |
94 |
|
95 |
/**
|
96 |
* Empty constructor used only for persistence purposes.
|
97 |
*/
|
98 |
public ColorTableLegend() {
|
99 |
} |
100 |
|
101 |
/*
|
102 |
* (non-Javadoc)
|
103 |
* @see com.iver.cit.gvsig.fmap.rendering.IClassifiedLegend#getDescriptions()
|
104 |
*/
|
105 |
public String[] getDescriptions() { |
106 |
return desc;
|
107 |
} |
108 |
|
109 |
/*
|
110 |
* (non-Javadoc)
|
111 |
* @see com.iver.cit.gvsig.fmap.rendering.IClassifiedLegend#getSymbols()
|
112 |
*/
|
113 |
public ISymbol[] getSymbols() { |
114 |
return symbols;
|
115 |
} |
116 |
|
117 |
/*
|
118 |
* (non-Javadoc)
|
119 |
* @see com.iver.cit.gvsig.fmap.rendering.IClassifiedLegend#getValues()
|
120 |
*/
|
121 |
public Object[] getValues() { |
122 |
return desc;
|
123 |
} |
124 |
|
125 |
/*
|
126 |
* (non-Javadoc)
|
127 |
* @see com.iver.cit.gvsig.fmap.rendering.ILegend#cloneLegend()
|
128 |
*/
|
129 |
public ILegend cloneLegend() {
|
130 |
return null; |
131 |
} |
132 |
|
133 |
/*
|
134 |
* (non-Javadoc)
|
135 |
* @see com.iver.cit.gvsig.fmap.rendering.ILegend#getDefaultSymbol()
|
136 |
*/
|
137 |
public ISymbol getDefaultSymbol() {
|
138 |
return null; |
139 |
} |
140 |
|
141 |
/*
|
142 |
* (non-Javadoc)
|
143 |
* @see com.iver.cit.gvsig.fmap.rendering.ILegend#getSLDString(java.lang.String)
|
144 |
*/
|
145 |
public String getSLDString(String layerName) { |
146 |
return null; |
147 |
} |
148 |
|
149 |
/*
|
150 |
* (non-Javadoc)
|
151 |
* @see com.iver.utiles.IPersistance#getClassName()
|
152 |
*/
|
153 |
public String getClassName() { |
154 |
return null; |
155 |
} |
156 |
|
157 |
public void addLegendListener(LegendContentsChangedListener listener) { |
158 |
// TODO Auto-generated method stub
|
159 |
} |
160 |
|
161 |
public void fireDefaultSymbolChangedEvent(SymbolLegendEvent event) { |
162 |
// TODO Auto-generated method stub
|
163 |
|
164 |
} |
165 |
|
166 |
public LegendContentsChangedListener[] getListeners() { |
167 |
// TODO Auto-generated method stub
|
168 |
return null; |
169 |
} |
170 |
|
171 |
|
172 |
public void removeLegendListener(LegendContentsChangedListener listener) { |
173 |
// TODO Auto-generated method stub
|
174 |
} |
175 |
|
176 |
@Override
|
177 |
public Object clone() throws CloneNotSupportedException { |
178 |
// TODO Auto-generated method stub
|
179 |
return super.clone(); |
180 |
} |
181 |
|
182 |
private void setDescriptions(String[] descriptions) { |
183 |
this.desc = descriptions;
|
184 |
} |
185 |
|
186 |
private void setSymbols(ISymbol[] symbols) { |
187 |
this.symbols = symbols;
|
188 |
} |
189 |
|
190 |
@SuppressWarnings("unchecked") |
191 |
public void loadFromState(PersistentState state) |
192 |
throws PersistenceException {
|
193 |
List<ISymbol> symbols = (List<ISymbol>) state.get(FIELD_SYMBOLS); |
194 |
setSymbols(symbols.toArray(new ISymbol[symbols.size()]));
|
195 |
|
196 |
List<String> descriptions = |
197 |
(List<String>) state.get(FIELD_DESCRIPTIONS); |
198 |
setDescriptions(descriptions.toArray(new String[descriptions.size()])); |
199 |
} |
200 |
|
201 |
public void saveToState(PersistentState state) throws PersistenceException { |
202 |
state.set(FIELD_SYMBOLS, Arrays.asList(getSymbols()));
|
203 |
state.set(FIELD_DESCRIPTIONS, Arrays.asList(getDescriptions()));
|
204 |
} |
205 |
|
206 |
public static void registerPersistence() { |
207 |
// Add the DynClass definition.
|
208 |
DynClass dynClass = |
209 |
ToolsLocator.getDynObjectManager().add( |
210 |
COLOR_TABLE_LEGEND_DYNCLASS_NAME); |
211 |
|
212 |
// Symbols
|
213 |
dynClass.addDynFieldList(FIELD_SYMBOLS).setMandatory(true);
|
214 |
// Descriptions
|
215 |
dynClass.addDynFieldList(FIELD_DESCRIPTIONS).setMandatory(true);
|
216 |
|
217 |
// Register in persistence
|
218 |
ToolsLocator.getPersistenceManager().registerClass( |
219 |
ColorTableLegend.class, dynClass); |
220 |
} |
221 |
|
222 |
} |