root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / MultiLayerMarkerSymbol.java @ 38563
History | View | Annotate | Download (11.5 KB)
1 | 10679 | jaume | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2005 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 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | |||
42 | /* CVS MESSAGES:
|
||
43 | *
|
||
44 | * $Id$
|
||
45 | * $Log$
|
||
46 | 13953 | jaume | * Revision 1.17 2007-09-21 12:25:32 jaume
|
47 | * cancellation support extended down to the IGeometry and ISymbol level
|
||
48 | *
|
||
49 | * Revision 1.16 2007/09/19 16:21:32 jaume
|
||
50 | 13881 | jaume | * removed unnecessary imports
|
51 | 13762 | cesar | *
|
52 | * Revision 1.13 2007/09/17 14:16:11 jaume
|
||
53 | 13749 | jaume | * multilayer symbols sizing bug fixed
|
54 | *
|
||
55 | * Revision 1.12 2007/09/17 11:37:55 jaume
|
||
56 | 13744 | jaume | * fixed multilayermarkersymbol unit symbol
|
57 | *
|
||
58 | * Revision 1.11 2007/08/09 07:57:51 jvidal
|
||
59 | 12992 | jvidal | * javadoc
|
60 | *
|
||
61 | * Revision 1.10 2007/07/18 06:54:34 jaume
|
||
62 | 12657 | jaume | * continuing with cartographic support
|
63 | *
|
||
64 | * Revision 1.9 2007/07/03 10:58:29 jaume
|
||
65 | 12467 | jaume | * first refactor on CartographicSupport
|
66 | *
|
||
67 | * Revision 1.8 2007/06/29 13:07:01 jaume
|
||
68 | 12427 | jaume | * +PictureLineSymbol
|
69 | *
|
||
70 | * Revision 1.7 2007/06/07 06:50:40 jaume
|
||
71 | 11486 | jaume | * *** empty log message ***
|
72 | *
|
||
73 | 12043 | jaume | * Revision 1.6 2007/05/08 08:47:40 jaume
|
74 | * *** empty log message ***
|
||
75 | *
|
||
76 | 11486 | jaume | * Revision 1.5 2007/04/17 07:01:53 bsanchez
|
77 | 11217 | bsanchez | * - Corregido fallo de Double.MIN_VALUE por Double.NEGATIVE_INFINITY comentado por Victor Olaya.
|
78 | *
|
||
79 | * Revision 1.4 2007/03/29 16:02:01 jaume
|
||
80 | 10977 | jaume | * *** empty log message ***
|
81 | *
|
||
82 | * Revision 1.3 2007/03/26 14:26:02 jaume
|
||
83 | 10902 | jaume | * implemented Print
|
84 | *
|
||
85 | * Revision 1.2 2007/03/09 11:20:57 jaume
|
||
86 | 10679 | jaume | * Advanced symbology (start committing)
|
87 | *
|
||
88 | * Revision 1.1.2.2 2007/02/21 07:34:08 jaume
|
||
89 | * labeling starts working
|
||
90 | *
|
||
91 | * Revision 1.1.2.1 2007/02/16 10:54:12 jaume
|
||
92 | * multilayer splitted to multilayerline, multilayermarker,and multilayerfill
|
||
93 | *
|
||
94 | *
|
||
95 | */
|
||
96 | package com.iver.cit.gvsig.fmap.core.symbols; |
||
97 | |||
98 | import java.awt.Color; |
||
99 | import java.awt.Graphics2D; |
||
100 | import java.awt.Rectangle; |
||
101 | import java.awt.geom.AffineTransform; |
||
102 | import java.awt.geom.Point2D; |
||
103 | import java.util.ArrayList; |
||
104 | |||
105 | import javax.print.attribute.PrintRequestAttributeSet; |
||
106 | |||
107 | 18621 | jdominguez | import com.iver.cit.gvsig.fmap.ViewPort; |
108 | 20024 | vcaballero | import com.iver.cit.gvsig.fmap.core.FPoint2D; |
109 | 10679 | jaume | import com.iver.cit.gvsig.fmap.core.FShape; |
110 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
111 | import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
||
112 | import com.iver.utiles.XMLEntity; |
||
113 | 13953 | jaume | import com.iver.utiles.swing.threads.Cancellable; |
114 | 12992 | jvidal | /**
|
115 | * MultiLayerMarkerSymbol allows to group several marker symbols (xxxMarkerSymbol
|
||
116 | * implementing IMarkerSymbol)in one and treat it as an only one symbol.
|
||
117 | *
|
||
118 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
119 | */
|
||
120 | 11486 | jaume | public class MultiLayerMarkerSymbol extends AbstractMarkerSymbol implements IMarkerSymbol, IMultiLayerSymbol { |
121 | 10679 | jaume | private IMarkerSymbol[] layers = new IMarkerSymbol[0]; |
122 | private MultiLayerMarkerSymbol selectionSymbol;
|
||
123 | private Point2D offset = new Point2D.Double(); |
||
124 | private double rotation; |
||
125 | 22712 | vcaballero | private double markerSize; |
126 | 10679 | jaume | public Color getColor() { |
127 | /*
|
||
128 | * a multilayer symbol does not define any color, the color
|
||
129 | * of each layer is defined by the layer itself
|
||
130 | */
|
||
131 | return null; |
||
132 | } |
||
133 | |||
134 | public Point2D getOffset() { |
||
135 | return offset;
|
||
136 | } |
||
137 | |||
138 | public double getRotation() { |
||
139 | return rotation;
|
||
140 | } |
||
141 | |||
142 | |||
143 | public void setColor(Color color) { |
||
144 | /*
|
||
145 | * will apply the color to each layer
|
||
146 | */
|
||
147 | for (int i = 0; i < layers.length; i++) { |
||
148 | layers[i].setColor(color); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | public void setOffset(Point2D offset) { |
||
153 | this.offset = offset;
|
||
154 | } |
||
155 | |||
156 | public void setRotation(double rotation) { |
||
157 | this.rotation = rotation;
|
||
158 | } |
||
159 | |||
160 | 22730 | vcaballero | |
161 | 22712 | vcaballero | public double getSize() { |
162 | double myMarkerSize = 0; |
||
163 | 22730 | vcaballero | |
164 | 22712 | vcaballero | for (int i = 0; i < getLayerCount(); i++) { |
165 | myMarkerSize = Math.max(myMarkerSize, ((IMarkerSymbol) getLayer(i)).getSize());
|
||
166 | } |
||
167 | |||
168 | if (markerSize != myMarkerSize)
|
||
169 | markerSize = myMarkerSize; |
||
170 | return markerSize;
|
||
171 | } |
||
172 | 22730 | vcaballero | |
173 | 10679 | jaume | public void setSize(double size) { |
174 | 18621 | jdominguez | if (size > 0 && size != getSize()) { |
175 | 20024 | vcaballero | |
176 | 13749 | jaume | double scale = size / getSize();
|
177 | 22712 | vcaballero | this.markerSize = size;
|
178 | 13749 | jaume | for (int i = 0; i < layers.length; i++) { |
179 | double lSize = layers[i].getSize();
|
||
180 | layers[i].setSize(lSize*scale); |
||
181 | 31297 | fpenarrubia | AffineTransform auxT = AffineTransform.getScaleInstance(scale, scale); |
182 | Point2D auxP = new Point2D.Double(); |
||
183 | auxT.transform(layers[i].getOffset(), auxP); |
||
184 | layers[i].setOffset(auxP); |
||
185 | 13749 | jaume | } |
186 | 12043 | jaume | } |
187 | 10679 | jaume | } |
188 | |||
189 | 13953 | jaume | public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { |
190 | 20024 | vcaballero | FPoint2D p = (FPoint2D) shp; |
191 | g.rotate(rotation, p.getX(), p.getY()); |
||
192 | 13953 | jaume | for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < layers.length; i++) { |
193 | layers[i].draw(g, affineTransform, shp, cancel); |
||
194 | 10679 | jaume | } |
195 | 20024 | vcaballero | g.rotate(-rotation, p.getX(), p.getY()); |
196 | 10679 | jaume | } |
197 | |||
198 | 28367 | vcaballero | public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException { |
199 | 20024 | vcaballero | g.rotate(rotation, r.getCenterX(), r.getCenterY()); |
200 | 10679 | jaume | for (int i = 0; i < layers.length; i++) { |
201 | 34263 | vcaballero | if (layers[i]!=null) |
202 | layers[i].drawInsideRectangle(g, scaleInstance, r, properties); |
||
203 | 10679 | jaume | } |
204 | 20024 | vcaballero | g.rotate(-rotation, r.getCenterX(), r.getCenterY()); |
205 | 10679 | jaume | } |
206 | |||
207 | public int getOnePointRgb() { |
||
208 | // will paint only the last layer pixel
|
||
209 | return layers[layers.length-1].getOnePointRgb(); |
||
210 | } |
||
211 | |||
212 | 18621 | jdominguez | public void getPixExtentPlus(FShape shp, float[] distances, |
213 | ViewPort viewPort, int dpi) {
|
||
214 | float[] myDistances = new float[] {0,0}; |
||
215 | distances[0] = 0; |
||
216 | distances[1] = 0; |
||
217 | for (int i = 0; i < layers.length; i++) { |
||
218 | layers[i].getPixExtentPlus(shp, myDistances, viewPort, dpi); |
||
219 | distances[0] = Math.max(myDistances[0], distances[0]); |
||
220 | distances[1] = Math.max(myDistances[1], distances[1]); |
||
221 | } |
||
222 | 10679 | jaume | } |
223 | |||
224 | public ISymbol getSymbolForSelection() {
|
||
225 | if (selectionSymbol == null) { |
||
226 | selectionSymbol = new MultiLayerMarkerSymbol();
|
||
227 | selectionSymbol.setDescription(getDescription()); |
||
228 | for (int i = 0; i < layers.length; i++) { |
||
229 | selectionSymbol.addLayer(layers[i].getSymbolForSelection()); |
||
230 | } |
||
231 | } |
||
232 | return selectionSymbol;
|
||
233 | |||
234 | } |
||
235 | |||
236 | |||
237 | public XMLEntity getXMLEntity() {
|
||
238 | XMLEntity xml = new XMLEntity();
|
||
239 | |||
240 | xml.putProperty("className", getClass().getName());
|
||
241 | xml.putProperty("isShapeVisible", isShapeVisible());
|
||
242 | xml.putProperty("desc", getDescription());
|
||
243 | 13749 | jaume | xml.putProperty("size", getSize());
|
244 | xml.putProperty("unit", getUnit());
|
||
245 | xml.putProperty("referenceSystem", getReferenceSystem());
|
||
246 | 10679 | jaume | for (int i = 0; i < layers.length; i++) { |
247 | xml.addChild(layers[i].getXMLEntity()); |
||
248 | } |
||
249 | return xml;
|
||
250 | } |
||
251 | |||
252 | public boolean isSuitableFor(IGeometry geom) { |
||
253 | 29132 | vcaballero | return (geom.getGeometryType()%FShape.Z) == FShape.POINT;
|
254 | 10679 | jaume | } |
255 | |||
256 | |||
257 | public String getClassName() { |
||
258 | return getClass().getName();
|
||
259 | } |
||
260 | |||
261 | public void setXMLEntity(XMLEntity xml) { |
||
262 | setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
||
263 | setDescription(xml.getStringProperty("desc"));
|
||
264 | 22730 | vcaballero | setSize(xml.getDoubleProperty("size"));
|
265 | 13763 | cesar | setUnit(xml.getIntProperty("unit"));
|
266 | setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
||
267 | 14461 | jdominguez | for (int i = 0; i < xml.getChildrenCount(); i++) { |
268 | addLayer((IMarkerSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i));
|
||
269 | 10679 | jaume | } |
270 | } |
||
271 | |||
272 | |||
273 | 28367 | vcaballero | public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) { |
274 | 10902 | jaume | for (int i = 0; i < layers.length; i++) { |
275 | layers[i].print(g, at, shape, properties); |
||
276 | } |
||
277 | 10679 | jaume | |
278 | } |
||
279 | |||
280 | public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException { |
||
281 | layers[index] = (IMarkerSymbol) layer; |
||
282 | } |
||
283 | |||
284 | public void swapLayers(int index1, int index2) { |
||
285 | ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2); |
||
286 | layers[index2] = (IMarkerSymbol) aux1; |
||
287 | layers[index1] = (IMarkerSymbol) aux2; |
||
288 | } |
||
289 | |||
290 | public ISymbol getLayer(int layerIndex) { |
||
291 | 12427 | jaume | // try{
|
292 | 10679 | jaume | return layers[layerIndex];
|
293 | 12427 | jaume | // } catch (Exception e) {
|
294 | // return null;
|
||
295 | // }
|
||
296 | 10679 | jaume | } |
297 | |||
298 | public int getLayerCount() { |
||
299 | return layers.length;
|
||
300 | } |
||
301 | |||
302 | public void addLayer(ISymbol newLayer) { |
||
303 | 18621 | jdominguez | addLayer(newLayer, layers.length); |
304 | } |
||
305 | |||
306 | public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException { |
||
307 | if (newLayer == null )/*|| newLayer instanceof ILabelStyle)*/ return; // null or symbols that are styles are not allowed |
||
308 | |||
309 | 14461 | jdominguez | IMarkerSymbol newMarker = (IMarkerSymbol) newLayer; |
310 | if (getLayerCount() == 0) { |
||
311 | // apply the new layer properties to this multilayer
|
||
312 | 20024 | vcaballero | |
313 | 14461 | jdominguez | setReferenceSystem(newMarker.getReferenceSystem()); |
314 | 22712 | vcaballero | markerSize = newMarker.getSize(); |
315 | //setSize(newMarker.getSize());
|
||
316 | 14461 | jdominguez | setUnit(newMarker.getUnit()); |
317 | } else {
|
||
318 | if (newMarker.getSize() > getSize()) {
|
||
319 | 22712 | vcaballero | //setSize(newMarker.getSize());
|
320 | markerSize = newMarker.getSize(); |
||
321 | 14461 | jdominguez | } |
322 | newMarker.setReferenceSystem(getReferenceSystem()); |
||
323 | newMarker.setUnit(getUnit()); |
||
324 | 20024 | vcaballero | } |
325 | 10679 | jaume | selectionSymbol = null; /* forces the selection symbol to be re-created |
326 | * next time it is required
|
||
327 | */
|
||
328 | if (layerIndex < 0 || layers.length < layerIndex) |
||
329 | throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length); |
||
330 | 18621 | jdominguez | ArrayList<ISymbol> newLayers = new ArrayList<ISymbol>(); |
331 | 10679 | jaume | for (int i = 0; i < layers.length; i++) { |
332 | newLayers.add(layers[i]); |
||
333 | } |
||
334 | 20778 | vcaballero | try {
|
335 | newLayers.add(layerIndex, newLayer); |
||
336 | layers = (IMarkerSymbol[]) newLayers.toArray(new IMarkerSymbol[0]); |
||
337 | } catch (ArrayStoreException asEx) { |
||
338 | throw new ClassCastException(newLayer.getClassName()+" is not an IMarkerSymbol"); |
||
339 | } |
||
340 | 10679 | jaume | } |
341 | |||
342 | public boolean removeLayer(ISymbol layer) { |
||
343 | |||
344 | int capacity = 0; |
||
345 | capacity = layers.length; |
||
346 | ArrayList lst = new ArrayList(capacity); |
||
347 | for (int i = 0; i < capacity; i++) { |
||
348 | lst.add(layers[i]); |
||
349 | } |
||
350 | boolean contains = lst.remove(layer);
|
||
351 | layers = (IMarkerSymbol[])lst.toArray(new IMarkerSymbol[0]); |
||
352 | return contains;
|
||
353 | } |
||
354 | 20024 | vcaballero | |
355 | 13744 | jaume | @Override
|
356 | 13749 | jaume | public void setUnit(int unit) { |
357 | super.setUnit(unit);
|
||
358 | 13744 | jaume | for (int i = 0; i < layers.length; i++) { |
359 | 13749 | jaume | layers[i].setUnit(unit); |
360 | 13744 | jaume | } |
361 | } |
||
362 | 10679 | jaume | |
363 | 13749 | jaume | @Override
|
364 | public void setReferenceSystem(int system) { |
||
365 | super.setReferenceSystem(system);
|
||
366 | for (int i = 0; i < layers.length; i++) { |
||
367 | layers[i].setReferenceSystem(system); |
||
368 | } |
||
369 | } |
||
370 | 14767 | jdominguez | |
371 | public void setAlpha(int alpha) { |
||
372 | // first, get the biggest alpha in the layers and the index if such layer
|
||
373 | int maxAlpha = Integer.MIN_VALUE; |
||
374 | int maxAlphaLayerIndex = 0; |
||
375 | for (int i = 0; i < layers.length; i++) { |
||
376 | if (layers[i].getColor().getAlpha() > maxAlpha) {
|
||
377 | maxAlpha = layers[i].getColor().getAlpha(); |
||
378 | maxAlphaLayerIndex = i; |
||
379 | } |
||
380 | } |
||
381 | |||
382 | // now, max alpha takes the value of the desired alpha and the rest
|
||
383 | // will take a scaled (to biggest alpha) alpha value
|
||
384 | for (int i = 0; i < layers.length; i++) { |
||
385 | int r = layers[i].getColor().getRed();
|
||
386 | int g = layers[i].getColor().getGreen();
|
||
387 | int b = layers[i].getColor().getBlue();
|
||
388 | 20024 | vcaballero | |
389 | 14767 | jdominguez | if (i!=maxAlphaLayerIndex) {
|
390 | double scaledAlpha = (double) layers[i].getColor().getAlpha()/maxAlpha; |
||
391 | int myAlpha = (int) (alpha*scaledAlpha); |
||
392 | if (myAlpha == 0) |
||
393 | myAlpha = 1;
|
||
394 | layers[i].setColor(new Color(r, g, b, myAlpha)); |
||
395 | } else {
|
||
396 | int myAlpha = (int) alpha; |
||
397 | if (myAlpha == 0) |
||
398 | myAlpha = 1;
|
||
399 | layers[i].setColor(new Color(r, g, b, myAlpha)); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | } |
||
404 | 10679 | jaume | } |