svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / fill / impl / MultiLayerFillSymbol.java @ 40560
History | View | Annotate | Download (12.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Graphics2D; |
28 |
import java.awt.Rectangle; |
29 |
import java.awt.geom.AffineTransform; |
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
|
33 |
import org.gvsig.compat.print.PrintAttributes; |
34 |
import org.gvsig.fmap.dal.feature.Feature; |
35 |
import org.gvsig.fmap.geom.Geometry; |
36 |
import org.gvsig.fmap.mapcontext.MapContext; |
37 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
38 |
import org.gvsig.fmap.mapcontext.ViewPort; |
39 |
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol; |
40 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
41 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
42 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
43 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol; |
44 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IMultiLayerFillSymbol; |
45 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
46 |
import org.gvsig.tools.ToolsLocator; |
47 |
import org.gvsig.tools.dynobject.DynStruct; |
48 |
import org.gvsig.tools.persistence.PersistenceManager; |
49 |
import org.gvsig.tools.persistence.PersistentState; |
50 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
51 |
import org.gvsig.tools.task.Cancellable; |
52 |
import org.gvsig.tools.util.Callable; |
53 |
import org.slf4j.Logger; |
54 |
import org.slf4j.LoggerFactory; |
55 |
|
56 |
|
57 |
/**
|
58 |
* MultiLayerFillSymbol is a symbol which allows to group several kind of fill symbols
|
59 |
* (xxxFillSymbol implementing IFillSymbol)in one and treats it like single symbol.
|
60 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
61 |
*/
|
62 |
public class MultiLayerFillSymbol extends AbstractFillSymbol implements IFillSymbol, IMultiLayerSymbol, IMultiLayerFillSymbol{ |
63 |
private static final Logger LOG = LoggerFactory.getLogger(MultiLayerFillSymbol.class); |
64 |
|
65 |
public static final String MULTILAYER_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MultiLayerFillSymbol"; |
66 |
|
67 |
private static final String FIELD_LAYERS = "layers"; |
68 |
|
69 |
private static final double OPACITY_SELECTION_FACTOR = .8; |
70 |
private IFillSymbol[] layers = new IFillSymbol[0]; |
71 |
private MultiLayerFillSymbol selectionSymbol;
|
72 |
|
73 |
public Color getFillColor() { |
74 |
/*
|
75 |
* a multilayer symbol does not define any color, the color
|
76 |
* of each layer is defined by the layer itself
|
77 |
*/
|
78 |
return null; |
79 |
} |
80 |
|
81 |
public int getOnePointRgb() { |
82 |
// will paint only the last layer pixel
|
83 |
return layers[layers.length-1].getOnePointRgb(); |
84 |
} |
85 |
|
86 |
public ILineSymbol getOutline() {
|
87 |
/*
|
88 |
* a multilayer symbol does not define any outline, the outline
|
89 |
* of each layer is defined by the layer it self
|
90 |
*/
|
91 |
return null; |
92 |
} |
93 |
|
94 |
public boolean isSuitableFor(Geometry geom) { |
95 |
return geom.getType() == Geometry.TYPES.SURFACE;
|
96 |
} |
97 |
|
98 |
public void setFillColor(Color color) { |
99 |
/*
|
100 |
* Will apply the color to each layer
|
101 |
*/
|
102 |
for (int i = 0; layers != null && i < layers.length; i++) { |
103 |
layers[i].setFillColor(color); |
104 |
} |
105 |
} |
106 |
|
107 |
public void setOutline(ILineSymbol outline) { |
108 |
if (layers != null && layers.length > 0) { |
109 |
for (int i = 0; i < layers.length - 1; i++) { |
110 |
layers[i].setOutline(null);
|
111 |
} |
112 |
layers[layers.length - 1].setOutline(outline);
|
113 |
} |
114 |
} |
115 |
|
116 |
public void draw(Graphics2D g, AffineTransform affineTransform, |
117 |
Geometry geom, Feature feature, Cancellable cancel) { |
118 |
for (int i = 0; (cancel == null || !cancel.isCanceled()) |
119 |
&& layers != null && i < layers.length; i++) {
|
120 |
layers[i].draw(g, affineTransform, geom, feature, cancel); |
121 |
} |
122 |
} |
123 |
|
124 |
public void drawInsideRectangle(Graphics2D g, |
125 |
AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
126 |
for (int i = 0; layers != null && i < layers.length; i++) { |
127 |
layers[i].drawInsideRectangle(g, scaleInstance, r, properties); |
128 |
} |
129 |
} |
130 |
|
131 |
public void getPixExtentPlus(Geometry geom, float[] distances, |
132 |
ViewPort viewPort, int dpi) {
|
133 |
float[] myDistances = new float[] {0,0}; |
134 |
distances[0] = 0; |
135 |
distances[1] = 0; |
136 |
for (int i = 0; layers != null && i < layers.length; i++) { |
137 |
layers[i].getPixExtentPlus(geom, myDistances, viewPort, dpi); |
138 |
distances[0] = Math.max(myDistances[0], distances[0]); |
139 |
distances[1] = Math.max(myDistances[1], distances[1]); |
140 |
} |
141 |
} |
142 |
|
143 |
public ISymbol getSymbolForSelection() {
|
144 |
// TODO: revisar
|
145 |
|
146 |
if (selectionSymbol == null) { |
147 |
MultiLayerFillSymbol selectionSymbol = |
148 |
new MultiLayerFillSymbol();
|
149 |
selectionSymbol.setDescription(getDescription()); |
150 |
for (int i = 0; layers != null && i < layers.length; i++) { |
151 |
selectionSymbol.addLayer(layers[i].getSymbolForSelection()); |
152 |
} |
153 |
SimpleFillSymbol selLayer = new SimpleFillSymbol();
|
154 |
Color c = MapContext.getSelectionColor();
|
155 |
c = new Color( |
156 |
c.getRed(), |
157 |
c.getGreen(), |
158 |
c.getBlue(), |
159 |
(int) (255*OPACITY_SELECTION_FACTOR)); |
160 |
selLayer.setFillColor(c); |
161 |
selLayer.setOutline(getOutline()); |
162 |
selectionSymbol.addLayer(selLayer); |
163 |
setSymbolForSelection(selectionSymbol); |
164 |
} |
165 |
return selectionSymbol;
|
166 |
|
167 |
} |
168 |
|
169 |
public int getSymbolType() { |
170 |
return Geometry.TYPES.SURFACE;
|
171 |
} |
172 |
|
173 |
// public void setPrintingProperties(PrintAttributes printProperties) {
|
174 |
// // TODO Implement it
|
175 |
// throw new Error("Not yet implemented!");
|
176 |
//
|
177 |
// }
|
178 |
|
179 |
// public String getClassName() {
|
180 |
// return getClass().getName();
|
181 |
// }
|
182 |
|
183 |
public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) { |
184 |
for (int i = 0; layers != null && i < layers.length; i++) { |
185 |
layers[i].print(g, at, geom, properties); |
186 |
} |
187 |
} |
188 |
|
189 |
public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException { |
190 |
layers[index] = (IFillSymbol) layer; |
191 |
} |
192 |
|
193 |
public void swapLayers(int index1, int index2) { |
194 |
ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2); |
195 |
layers[index2] = (IFillSymbol) aux1; |
196 |
layers[index1] = (IFillSymbol) aux2; |
197 |
} |
198 |
|
199 |
public ISymbol getLayer(int layerIndex) { |
200 |
// try{
|
201 |
return layers[layerIndex];
|
202 |
// } catch (Exception e) {
|
203 |
// return null;
|
204 |
// }
|
205 |
} |
206 |
|
207 |
public int getLayerCount() { |
208 |
return layers.length;
|
209 |
} |
210 |
|
211 |
public void addLayer(ISymbol newLayer) { |
212 |
addLayer(newLayer, layers.length); |
213 |
} |
214 |
|
215 |
public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException { |
216 |
if (newLayer == null ) { |
217 |
/*|| newLayer instanceof ILabelStyle)*/ return; // null or symbols that are styles are not allowed |
218 |
} |
219 |
|
220 |
selectionSymbol = null; /* forces the selection symbol to be re-created |
221 |
* next time it is required
|
222 |
*/
|
223 |
if (layerIndex < 0 || layers.length < layerIndex) { |
224 |
throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length); |
225 |
} |
226 |
List<ISymbol> newLayers = new ArrayList<ISymbol>(); |
227 |
for (int i = 0; i < layers.length; i++) { |
228 |
newLayers.add(layers[i]); |
229 |
} |
230 |
try {
|
231 |
newLayers.add(layerIndex, newLayer); |
232 |
layers = (IFillSymbol[]) newLayers.toArray(new IFillSymbol[0]); |
233 |
} catch (ArrayStoreException asEx) { |
234 |
throw new ClassCastException(newLayer.getClass().getName() + " is not an IFillSymbol"); |
235 |
} |
236 |
} |
237 |
|
238 |
public boolean removeLayer(ISymbol layer) { |
239 |
|
240 |
int capacity = 0; |
241 |
capacity = layers.length; |
242 |
List<IFillSymbol> lst = new ArrayList<IFillSymbol>(capacity); |
243 |
for (int i = 0; layers != null && i < capacity; i++) { |
244 |
lst.add(layers[i]); |
245 |
} |
246 |
boolean contains = lst.remove(layer);
|
247 |
layers = (IFillSymbol[])lst.toArray(new IFillSymbol[0]); |
248 |
return contains;
|
249 |
} |
250 |
|
251 |
public void setUnit(int unitIndex) { |
252 |
super.setUnit(unitIndex);
|
253 |
for (int i = 0; layers != null && i < layers.length; i++) { |
254 |
layers[i].setUnit(unitIndex); |
255 |
} |
256 |
} |
257 |
|
258 |
public void setReferenceSystem(int system) { |
259 |
super.setReferenceSystem(system);
|
260 |
for (int i = 0; layers != null && i < layers.length; i++) { |
261 |
layers[i].setReferenceSystem(system); |
262 |
} |
263 |
} |
264 |
|
265 |
/**
|
266 |
*Returns the transparency of the multi layer fill symbol created
|
267 |
*/
|
268 |
public int getFillAlpha() { |
269 |
// will compute the acumulated opacity
|
270 |
double myAlpha = 0; |
271 |
for (int i = 0; layers != null && i < layers.length; i++) { |
272 |
double layerAlpha = layers[i].getFillAlpha()/255D; |
273 |
myAlpha += (1-myAlpha)*layerAlpha;
|
274 |
} |
275 |
int result = (int) Math.round(myAlpha * 255); |
276 |
return (result>255) ? 255 : result; |
277 |
} |
278 |
|
279 |
|
280 |
public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
281 |
double size = 0; |
282 |
for (int i = 0; layers != null && i < layers.length; i++) { |
283 |
size = Math.max(size, layers[i].toCartographicSize(viewPort, dpi, geom));
|
284 |
} |
285 |
return size;
|
286 |
} |
287 |
|
288 |
public Object clone() throws CloneNotSupportedException { |
289 |
MultiLayerFillSymbol copy = (MultiLayerFillSymbol) super.clone();
|
290 |
|
291 |
// Clone layers
|
292 |
if (layers != null) { |
293 |
IFillSymbol[] layersCopy = new IFillSymbol[layers.length]; |
294 |
for (int i = 0; i < layers.length; i++) { |
295 |
layersCopy[i] = (IFillSymbol) layers[i].clone(); |
296 |
} |
297 |
copy.layers = layersCopy; |
298 |
} |
299 |
|
300 |
// Clone selection
|
301 |
if (selectionSymbol != null) { |
302 |
copy.selectionSymbol = (MultiLayerFillSymbol) selectionSymbol |
303 |
.clone(); |
304 |
} |
305 |
|
306 |
return copy;
|
307 |
} |
308 |
|
309 |
private void setSymbolForSelection(MultiLayerFillSymbol symbolForSelection) { |
310 |
this.selectionSymbol = symbolForSelection;
|
311 |
} |
312 |
|
313 |
@SuppressWarnings("unchecked") |
314 |
public void loadFromState(PersistentState state) |
315 |
throws PersistenceException {
|
316 |
// Set parent fill symbol properties
|
317 |
super.loadFromState(state);
|
318 |
LOG.warn("FIXME, fix implement of loadFromState");
|
319 |
// Set own properties
|
320 |
// setSymbolForSelection((MultiLayerFillSymbol)
|
321 |
// state.get(FIELD_SYMBOL_FOR_SELECTION));
|
322 |
List layers = state.getList(FIELD_LAYERS);
|
323 |
if (layers != null) { |
324 |
for (int i = 0; i < layers.size(); i++) { |
325 |
addLayer((ISymbol) layers.get(i)); |
326 |
} |
327 |
} |
328 |
|
329 |
} |
330 |
|
331 |
public void saveToState(PersistentState state) throws PersistenceException { |
332 |
// Save parent fill symbol properties
|
333 |
super.saveToState(state);
|
334 |
LOG.warn("FIXME, fix implement of saveToState");
|
335 |
// Save own properties
|
336 |
|
337 |
// Don't use the getSymbolForSelection method, as it will create it
|
338 |
// if it does not exist, and persistence will enter an infinite loop
|
339 |
// state.set(FIELD_SYMBOL_FOR_SELECTION, selectionSymbol);
|
340 |
state.set(FIELD_LAYERS, layers); |
341 |
} |
342 |
|
343 |
public static class RegisterPersistence implements Callable { |
344 |
|
345 |
public Object call() throws Exception { |
346 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
347 |
if( manager.getDefinition(MULTILAYER_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
348 |
DynStruct definition = manager.addDefinition( |
349 |
MultiLayerFillSymbol.class, |
350 |
MULTILAYER_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
351 |
MULTILAYER_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
352 |
null,
|
353 |
null
|
354 |
); |
355 |
|
356 |
// Extend the FillSymbol base definition
|
357 |
definition.extend(manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
358 |
|
359 |
// Selection Symbol
|
360 |
// definition.addDynFieldSingle(FIELD_SYMBOL_FOR_SELECTION).setType(
|
361 |
// DataTypes.OBJECT);
|
362 |
|
363 |
// Layers
|
364 |
definition.addDynFieldList(FIELD_LAYERS).setClassOfItems(IFillSymbol.class); |
365 |
} |
366 |
return Boolean.TRUE; |
367 |
} |
368 |
|
369 |
} |
370 |
|
371 |
public static class RegisterSymbol implements Callable { |
372 |
|
373 |
public Object call() throws Exception { |
374 |
int[] shapeTypes; |
375 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
376 |
|
377 |
shapeTypes = new int[] { Geometry.TYPES.SURFACE, |
378 |
Geometry.TYPES.CIRCLE, Geometry.TYPES.ELLIPSE, |
379 |
Geometry.TYPES.MULTISURFACE }; |
380 |
manager.registerMultiLayerSymbol(IFillSymbol.SYMBOL_NAME, |
381 |
shapeTypes, MultiLayerFillSymbol.class); |
382 |
|
383 |
return Boolean.TRUE; |
384 |
} |
385 |
|
386 |
} |
387 |
|
388 |
} |