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