svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / MultiLayerLineSymbol.java @ 29312
History | View | Annotate | Download (11.5 KB)
1 |
/* 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: MultiLayerLineSymbol.java 15675 2007-10-30 16:41:53Z jdominguez $
|
45 |
* $Log$
|
46 |
* Revision 1.18 2007-09-21 12:25:32 jaume
|
47 |
* cancellation support extended down to the IGeometry and ISymbol level
|
48 |
*
|
49 |
* Revision 1.17 2007/09/18 07:30:15 cesar
|
50 |
* Revert last change
|
51 |
*
|
52 |
* Revision 1.15 2007/09/17 15:26:24 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
* Revision 1.14 2007/09/17 14:16:11 jaume
|
56 |
* multilayer symbols sizing bug fixed
|
57 |
*
|
58 |
* Revision 1.13 2007/09/17 09:33:47 jaume
|
59 |
* some multishapedsymbol bugs fixed
|
60 |
*
|
61 |
* Revision 1.12 2007/08/09 08:04:48 jvidal
|
62 |
* javadoc
|
63 |
*
|
64 |
* Revision 1.11 2007/07/23 06:52:55 jaume
|
65 |
* Added support for arrow line decorator (start commiting)
|
66 |
*
|
67 |
* Revision 1.10 2007/07/18 06:54:34 jaume
|
68 |
* continuing with cartographic support
|
69 |
*
|
70 |
* Revision 1.9 2007/07/03 10:58:29 jaume
|
71 |
* first refactor on CartographicSupport
|
72 |
*
|
73 |
* Revision 1.8 2007/06/29 13:07:01 jaume
|
74 |
* +PictureLineSymbol
|
75 |
*
|
76 |
* Revision 1.7 2007/05/17 09:32:06 jaume
|
77 |
* *** empty log message ***
|
78 |
*
|
79 |
* Revision 1.6 2007/05/08 08:47:40 jaume
|
80 |
* *** empty log message ***
|
81 |
*
|
82 |
* Revision 1.5 2007/04/17 07:01:53 bsanchez
|
83 |
* - Corregido fallo de Double.MIN_VALUE por Double.NEGATIVE_INFINITY comentado por Victor Olaya.
|
84 |
*
|
85 |
* Revision 1.4 2007/03/26 14:26:02 jaume
|
86 |
* implemented Print
|
87 |
*
|
88 |
* Revision 1.3 2007/03/20 16:01:21 jaume
|
89 |
* *** empty log message ***
|
90 |
*
|
91 |
* Revision 1.2 2007/03/09 11:20:56 jaume
|
92 |
* Advanced symbology (start committing)
|
93 |
*
|
94 |
* Revision 1.1.2.3 2007/02/21 16:09:02 jaume
|
95 |
* *** empty log message ***
|
96 |
*
|
97 |
* Revision 1.1.2.2 2007/02/21 07:34:09 jaume
|
98 |
* labeling starts working
|
99 |
*
|
100 |
* Revision 1.1.2.1 2007/02/16 10:54:12 jaume
|
101 |
* multilayer splitted to multilayerline, multilayermarker,and multilayerfill
|
102 |
*
|
103 |
*
|
104 |
*/
|
105 |
package org.gvsig.fmap.mapcontext.rendering.symbols; |
106 |
|
107 |
import java.awt.Color; |
108 |
import java.awt.Graphics2D; |
109 |
import java.awt.Rectangle; |
110 |
import java.awt.geom.AffineTransform; |
111 |
import java.util.ArrayList; |
112 |
|
113 |
import org.gvsig.compat.print.PrintAttributes; |
114 |
import org.gvsig.fmap.geom.Geometry; |
115 |
import org.gvsig.fmap.mapcontext.ViewPort; |
116 |
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILineStyle; |
117 |
import org.gvsig.tools.task.Cancellable; |
118 |
|
119 |
import com.iver.utiles.XMLEntity; |
120 |
import com.iver.utiles.XMLException; |
121 |
/**
|
122 |
* MultiLayerLineSymbol allows to create new symbols using a composition of several lineal
|
123 |
* symbols (xxxLineSymbol implementing ILineSymbol)and be treated as an only one symbol.
|
124 |
*
|
125 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
126 |
*/
|
127 |
public class MultiLayerLineSymbol extends AbstractLineSymbol implements |
128 |
ILineSymbol, IMultiLayerSymbol { |
129 |
private ILineSymbol[] layers = new ILineSymbol[0]; |
130 |
private MultiLayerLineSymbol selectionSymbol;
|
131 |
private double lineWidth; |
132 |
|
133 |
public Color getColor() { |
134 |
/*
|
135 |
* a multilayer symbol does not define any color, the color
|
136 |
* of each layer is defined by the layer itself
|
137 |
*/
|
138 |
return null; |
139 |
} |
140 |
|
141 |
public ILineStyle getLineStyle() {
|
142 |
/*
|
143 |
* a multilayer symbol does not define any style, the style
|
144 |
* of each layer is defined by the layer itself
|
145 |
*/
|
146 |
return null; |
147 |
} |
148 |
|
149 |
public double getLineWidth() { |
150 |
|
151 |
double myLineWidth = 0; |
152 |
for (int i = 0; i < getLayerCount(); i++) { |
153 |
myLineWidth = Math.max(myLineWidth, ((ILineSymbol) getLayer(i)).getLineWidth());
|
154 |
} |
155 |
|
156 |
if (lineWidth != myLineWidth) {
|
157 |
lineWidth = myLineWidth; |
158 |
} |
159 |
return lineWidth;
|
160 |
} |
161 |
|
162 |
public void setLineColor(Color color) { |
163 |
/*
|
164 |
* will apply the color to each layer
|
165 |
*/
|
166 |
for (int i = 0; i < layers.length; i++) { |
167 |
layers[i].setLineColor(color); |
168 |
} |
169 |
} |
170 |
|
171 |
public void setLineStyle(ILineStyle lineStyle) { |
172 |
/*
|
173 |
* will apply the same patter to each layer
|
174 |
*/
|
175 |
for (int i = 0; i < layers.length; i++) { |
176 |
layers[i].setLineStyle(lineStyle); |
177 |
} |
178 |
} |
179 |
|
180 |
public void setLineWidth(double width) { |
181 |
|
182 |
if (width > 0 && width != getLineWidth()) { |
183 |
double scaleFactor = width / getLineWidth();
|
184 |
this.lineWidth = width;
|
185 |
for (int i = 0; i < layers.length; i++) { |
186 |
layers[i].setLineWidth(layers[i].getLineWidth()*scaleFactor); |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Cancellable cancel) { |
192 |
for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < layers.length; i++) { |
193 |
layers[i].draw(g, affineTransform, geom, cancel); |
194 |
} |
195 |
} |
196 |
|
197 |
public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
198 |
for (int i = 0; i < layers.length; i++) { |
199 |
layers[i].drawInsideRectangle(g, scaleInstance, r, properties); |
200 |
} |
201 |
} |
202 |
|
203 |
public int getOnePointRgb() { |
204 |
// will paint only the last layer pixel
|
205 |
return layers[layers.length-1].getOnePointRgb(); |
206 |
} |
207 |
|
208 |
public void getPixExtentPlus(Geometry geom, float[] distances, |
209 |
ViewPort viewPort, int dpi) {
|
210 |
float[] myDistances = new float[] {0,0}; |
211 |
distances[0] = 0; |
212 |
distances[1] = 0; |
213 |
for (int i = 0; i < layers.length; i++) { |
214 |
layers[i].getPixExtentPlus(geom, myDistances, viewPort, dpi); |
215 |
distances[0] = Math.max(myDistances[0], distances[0]); |
216 |
distances[1] = Math.max(myDistances[1], distances[1]); |
217 |
} |
218 |
} |
219 |
|
220 |
public ISymbol getSymbolForSelection() {
|
221 |
if (selectionSymbol == null) { |
222 |
selectionSymbol = new MultiLayerLineSymbol();
|
223 |
selectionSymbol.setDescription(getDescription()); |
224 |
for (int i = 0; i < layers.length; i++) { |
225 |
selectionSymbol.addLayer(layers[i].getSymbolForSelection()); |
226 |
} |
227 |
} |
228 |
return selectionSymbol;
|
229 |
} |
230 |
|
231 |
|
232 |
public XMLEntity getXMLEntity() throws XMLException { |
233 |
XMLEntity xml = new XMLEntity();
|
234 |
xml.putProperty("className", getClass().getName());
|
235 |
xml.putProperty("isShapeVisible", isShapeVisible());
|
236 |
xml.putProperty("desc", getDescription());
|
237 |
xml.putProperty("lineWidth", getLineWidth());
|
238 |
xml.putProperty("unit", getUnit());
|
239 |
xml.putProperty("referenceSystem", getReferenceSystem());
|
240 |
for (int i = 0; i < layers.length; i++) { |
241 |
xml.addChild(layers[i].getXMLEntity()); |
242 |
} |
243 |
return xml;
|
244 |
} |
245 |
|
246 |
public boolean isSuitableFor(Geometry geom) { |
247 |
return geom.getType() == Geometry.TYPES.CURVE;
|
248 |
} |
249 |
|
250 |
public String getClassName() { |
251 |
return getClass().getName();
|
252 |
} |
253 |
|
254 |
public void setXMLEntity(XMLEntity xml) { |
255 |
setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
256 |
setDescription(xml.getStringProperty("desc"));
|
257 |
setLineWidth(xml.getDoubleProperty("lineWidth"));
|
258 |
setUnit(xml.getIntProperty("unit"));
|
259 |
setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
260 |
for (int i = 0; i < xml.getChildrenCount(); i++) { |
261 |
addLayer(SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i));
|
262 |
} |
263 |
} |
264 |
|
265 |
public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) { |
266 |
for (int i = 0; i < layers.length; i++) { |
267 |
layers[i].print(g, at, geom, properties); |
268 |
} |
269 |
|
270 |
} |
271 |
|
272 |
public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException { |
273 |
layers[index] = (ILineSymbol) layer; |
274 |
} |
275 |
|
276 |
public void swapLayers(int index1, int index2) { |
277 |
ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2); |
278 |
layers[index2] = (ILineSymbol) aux1; |
279 |
layers[index1] = (ILineSymbol) aux2; |
280 |
} |
281 |
|
282 |
public ISymbol getLayer(int layerIndex) { |
283 |
return layers[layerIndex];
|
284 |
} |
285 |
|
286 |
public int getLayerCount() { |
287 |
return layers.length;
|
288 |
} |
289 |
|
290 |
public void addLayer(ISymbol newLayer) { |
291 |
addLayer(newLayer, layers.length); |
292 |
} |
293 |
|
294 |
public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException { |
295 |
|
296 |
if (newLayer == null) { |
297 |
return; // null are not allowed |
298 |
} |
299 |
ILineSymbol newLine = (ILineSymbol) newLayer; |
300 |
if (getLayerCount() == 0) { |
301 |
// apply the new layer properties to this multilayer
|
302 |
|
303 |
setReferenceSystem(newLine.getReferenceSystem()); |
304 |
lineWidth = newLine.getLineWidth(); |
305 |
setUnit(newLine.getUnit()); |
306 |
} else {
|
307 |
if (newLine.getLineWidth() > getLineWidth()) {
|
308 |
lineWidth = newLine.getLineWidth(); |
309 |
} |
310 |
newLine.setReferenceSystem(getReferenceSystem()); |
311 |
newLine.setUnit(getUnit()); |
312 |
} |
313 |
|
314 |
selectionSymbol = null; /* forces the selection symbol to be re-created |
315 |
* next time it is required
|
316 |
*/
|
317 |
|
318 |
|
319 |
if (layerIndex < 0 || layers.length < layerIndex) { |
320 |
throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length); |
321 |
} |
322 |
ArrayList newLayers = new ArrayList(); //<ISymbol> |
323 |
for (int i = 0; i < layers.length; i++) { |
324 |
newLayers.add(layers[i]); |
325 |
} |
326 |
try {
|
327 |
newLayers.add(layerIndex, newLayer); |
328 |
layers = (ILineSymbol[])newLayers.toArray(new ILineSymbol[0]); |
329 |
} catch (ArrayStoreException asEx) { |
330 |
throw new ClassCastException(newLayer.getClassName()+" is not an ILineSymbol"); |
331 |
} |
332 |
} |
333 |
|
334 |
public boolean removeLayer(ISymbol layer) { |
335 |
|
336 |
int capacity = 0; |
337 |
capacity = layers.length; |
338 |
ArrayList lst = new ArrayList(capacity); //<ILineSymbol> |
339 |
for (int i = 0; i < capacity; i++) { |
340 |
lst.add(layers[i]); |
341 |
} |
342 |
boolean contains = lst.remove(layer);
|
343 |
layers = (ILineSymbol[])lst.toArray(new ILineSymbol[0]); |
344 |
return contains;
|
345 |
} |
346 |
|
347 |
public int getAlpha() { |
348 |
// will compute the acumulated opacity
|
349 |
double myAlpha = 0; |
350 |
for (int i = 0; i < layers.length; i++) { |
351 |
double layerAlpha = layers[i].getAlpha()/255D; |
352 |
myAlpha += (1-myAlpha)*layerAlpha;
|
353 |
} |
354 |
int result = (int) Math.round(myAlpha * 255); |
355 |
return (result>255) ? 255 : result; |
356 |
} |
357 |
|
358 |
public void setAlpha(int outlineAlpha) { |
359 |
// first, get the biggest alpha in the layers and the index if such layer
|
360 |
int maxAlpha = Integer.MIN_VALUE; |
361 |
int maxAlphaLayerIndex = 0; |
362 |
for (int i = 0; i < layers.length; i++) { |
363 |
if (layers[i].getAlpha() > maxAlpha) {
|
364 |
maxAlpha = layers[i].getAlpha(); |
365 |
maxAlphaLayerIndex = i; |
366 |
} |
367 |
} |
368 |
|
369 |
// now, max alpha takes the value of the desired alpha and the rest
|
370 |
// will take a scaled (to biggest alpha) alpha value
|
371 |
for (int i = 0; i < layers.length; i++) { |
372 |
if (i!=maxAlphaLayerIndex) {
|
373 |
double scaledAlpha = (double) layers[i].getAlpha()/maxAlpha; |
374 |
int myAlpha = (int) (outlineAlpha*scaledAlpha); |
375 |
if (myAlpha == 0) { |
376 |
myAlpha = 1;
|
377 |
} |
378 |
layers[i].setAlpha(myAlpha); |
379 |
} else {
|
380 |
int myAlpha = outlineAlpha;
|
381 |
if (myAlpha == 0) { |
382 |
myAlpha = 1;
|
383 |
} |
384 |
layers[i].setAlpha(myAlpha); |
385 |
} |
386 |
} |
387 |
|
388 |
} |
389 |
|
390 |
public void setUnit(int unitIndex) { |
391 |
super.setUnit(unitIndex);
|
392 |
for (int i = 0; i < layers.length; i++) { |
393 |
layers[i].setUnit(unitIndex); |
394 |
} |
395 |
} |
396 |
|
397 |
public void setReferenceSystem(int system) { |
398 |
super.setReferenceSystem(system);
|
399 |
for (int i = 0; i < layers.length; i++) { |
400 |
layers[i].setReferenceSystem(system); |
401 |
} |
402 |
} |
403 |
|
404 |
public void setCartographicSize(double cartographicSize, Geometry geom) { |
405 |
// super.setCartographicSize(cartographicSize, shp);
|
406 |
setLineWidth(cartographicSize); |
407 |
} |
408 |
} |