Revision 18628

View differences:

trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/core/symbols/MultiLayerLineSymbol.java
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$
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 com.iver.cit.gvsig.fmap.core.symbols;
106

  
107
import java.awt.Color;
108
import java.awt.Graphics2D;
109
import java.awt.Rectangle;
110
import java.awt.Shape;
111
import java.awt.geom.AffineTransform;
112
import java.util.ArrayList;
113

  
114
import javax.print.attribute.PrintRequestAttributeSet;
115

  
116
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
117
import com.iver.cit.gvsig.fmap.MapContext;
118
import com.iver.cit.gvsig.fmap.ViewPort;
119
import com.iver.cit.gvsig.fmap.core.FShape;
120
import com.iver.cit.gvsig.fmap.core.IGeometry;
121
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
122
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
123
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
124
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
125
import com.iver.utiles.XMLEntity;
126
import com.iver.utiles.swing.threads.Cancellable;
127
/**
128
 * MultiLayerLineSymbol allows to create new symbols using a composition of several lineal
129
 * symbols (xxxLineSymbol implementing ILineSymbol)and be treated as an only one symbol.
130
 *
131
 * @author  jaume dominguez faus - jaume.dominguez@iver.es
132
 */
133
public class MultiLayerLineSymbol extends AbstractLineSymbol implements
134
		ILineSymbol, IMultiLayerSymbol {
135
	private ILineSymbol[] layers = new ILineSymbol[0];
136
	private ILineSymbol selectionSymbol;
137
	private double width;
138

  
139
	public Color getColor() {
140
		/*
141
		 * a multilayer symbol does not define any color, the color
142
		 * of each layer is defined by the layer itself
143
		 */
144
		return null;
145
	}
146

  
147
	public ILineStyle getLineStyle() {
148
		/*
149
		 * a multilayer symbol does not define any style, the style
150
		 * of each layer is defined by the layer itself
151
		 */
152
		return null;
153
	}
154

  
155
	public double getLineWidth() {
156
		return width;
157
	}
158

  
159
	public void setLineColor(Color color) {
160
		/*
161
		 * will apply the color to each layer
162
		 */
163
		for (int i = 0; i < layers.length; i++) {
164
			layers[i].setLineColor(color);
165
		}
166
	}
167

  
168
	public void setLineStyle(ILineStyle lineStyle) {
169
		/*
170
		 * will apply the same patter to each layer
171
		 */
172
		for (int i = 0; i < layers.length; i++) {
173
			layers[i].setLineStyle(lineStyle);
174
		}
175
	}
176

  
177
	public void setLineWidth(double width) {
178
		/* take the biggest width of the layer set and
179
		 * extract a factor scale that will be applied
180
		 * to each layer.
181
		 */
182
		if (width > 0) {
183
			double scaleFactor = width / getLineWidth();	
184
			this.width = 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, FShape shp, Cancellable cancel) {
192
		for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < layers.length; i++) {
193
			layers[i].draw(g, affineTransform, shp, cancel);
194
		}
195
	}
196

  
197
	public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
198
		for (int i = 0; i < layers.length; i++) {
199
			layers[i].drawInsideRectangle(g, scaleInstance, r);
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 int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
209
			Shape shp) {
210
		// TODO Implement it
211
		throw new Error("Not yet implemented!");
212

  
213
	}
214

  
215
	public ISymbol getSymbolForSelection() {
216
		if (selectionSymbol == null) {
217
			selectionSymbol = new SimpleLineSymbol();
218
			selectionSymbol.setDescription(getDescription());
219
			selectionSymbol.setLineWidth(getLineWidth());
220
			selectionSymbol.setLineColor(MapContext.getSelectionColor());
221
		}
222
		return selectionSymbol;
223
	}
224

  
225

  
226
	public XMLEntity getXMLEntity() {
227
		XMLEntity xml = new XMLEntity();
228
		xml.putProperty("className", getClass().getName());
229
		xml.putProperty("isShapeVisible", isShapeVisible());
230
		xml.putProperty("desc", getDescription());
231
		xml.putProperty("lineWidth", getLineWidth());
232
		xml.putProperty("unit", getUnit());
233
		xml.putProperty("referenceSystem", getReferenceSystem());
234
		for (int i = 0; i < layers.length; i++) {
235
			xml.addChild(layers[i].getXMLEntity());
236
		}
237
		return xml;
238
	}
239

  
240
	public boolean isSuitableFor(IGeometry geom) {
241
		return geom.getGeometryType() == FShape.LINE;
242
	}
243

  
244
	public String getClassName() {
245
		return getClass().getName();
246
	}
247

  
248
	public void setXMLEntity(XMLEntity xml) {
249
		setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
250
		setDescription(xml.getStringProperty("desc"));
251
		setLineWidth(xml.getDoubleProperty("lineWidth"));
252
		setUnit(xml.getIntProperty("unit"));
253
		setReferenceSystem(xml.getIntProperty("referenceSystem"));
254
		for (int i = 0; i < xml.getChildrenCount(); i++) {
255
			addLayer((ILineSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i));
256
		}
257
	}
258

  
259
	public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
260
			throws ReadDriverException {
261
		for (int i = 0; i < layers.length; i++) {
262
			layers[i].print(g, at, shape, properties);
263
		}
264

  
265
	}
266

  
267
	public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
268
		layers[index] = (ILineSymbol) layer;
269
	}
270

  
271
	public void swapLayers(int index1, int index2) {
272
		ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
273
		layers[index2] = (ILineSymbol) aux1;
274
		layers[index1] = (ILineSymbol) aux2;
275
	}
276

  
277
	public ISymbol getLayer(int layerIndex) {
278
		return layers[layerIndex];
279
	}
280

  
281
	public int getLayerCount() {
282
		return layers.length;
283
	}
284

  
285
	public void addLayer(ISymbol newLayer) {
286
		if (newLayer == null) return;
287
		ILineSymbol newLine = (ILineSymbol) newLayer;
288
		if (getLayerCount() == 0) {
289
			// apply the new layer properties to this multilayer
290
			
291
			setReferenceSystem(newLine.getReferenceSystem());
292
			setLineWidth(newLine.getLineWidth());
293
			setUnit(newLine.getUnit());
294
		} else {
295
			if (newLine.getLineWidth() > getLineWidth()) {
296
				setLineWidth(newLine.getLineWidth());
297
			}
298
			newLine.setReferenceSystem(getReferenceSystem());
299
			newLine.setUnit(getUnit());
300
		}
301
		
302
		selectionSymbol = null; /* forces the selection symbol to be re-created
303
								 * next time it is required
304
								 */
305
		int size = layers.length+1;
306
		ILineSymbol[] newLayers = new ILineSymbol[size];
307
		for (int i = 0; i < newLayers.length-1; i++) {
308
			newLayers[i] = layers[i];
309
		}
310
		newLayers[size-1] = (ILineSymbol) newLayer;
311
		layers = newLayers;
312
	}
313

  
314
	public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
315
		
316
		if (newLayer == null) return; // null are not allowed
317

  
318
		selectionSymbol = null; /* forces the selection symbol to be re-created
319
		 						 * next time it is required
320
		 						 */
321
		if (layerIndex < 0 || layers.length < layerIndex)
322
			throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
323
		ArrayList<ISymbol> newLayers = new ArrayList<ISymbol>();
324
		for (int i = 0; i < layers.length; i++) {
325
			newLayers.add(layers[i]);
326
		}
327
		newLayers.add(layerIndex, newLayer);
328
		layers = newLayers.toArray(new ILineSymbol[0]);
329
	}
330

  
331
	public boolean removeLayer(ISymbol layer) {
332

  
333
		int capacity = 0;
334
		capacity = layers.length;
335
		ArrayList<ILineSymbol> lst = new ArrayList<ILineSymbol>(capacity);
336
		for (int i = 0; i < capacity; i++) {
337
			lst.add(layers[i]);
338
		}
339
		boolean contains = lst.remove(layer);
340
		layers = lst.toArray(new ILineSymbol[0]);
341
		return contains;
342
	}
343

  
344
	public int getAlpha() {
345
		// will compute the acumulated opacity
346
		double myAlpha = 0;
347
		for (int i = 0; i < layers.length; i++) {
348
			double layerAlpha = layers[i].getAlpha()/255D;
349
			myAlpha += (1-myAlpha)*layerAlpha;
350
		}
351
		int result = (int) Math.round(myAlpha * 255);
352
		return (result>255) ? 255 : result;
353
	}
354

  
355
	public void setAlpha(int outlineAlpha) {
356
		// first, get the biggest alpha in the layers and the index if such layer
357
		int maxAlpha = Integer.MIN_VALUE;
358
		int maxAlphaLayerIndex = 0;
359
		for (int i = 0; i < layers.length; i++) {
360
			if (layers[i].getAlpha() > maxAlpha) {
361
				maxAlpha = layers[i].getAlpha();
362
				maxAlphaLayerIndex = i;
363
			}
364
		}
365

  
366
		// now, max alpha takes the value of the desired alpha and the rest
367
		// will take a scaled (to biggest alpha) alpha value
368
		for (int i = 0; i < layers.length; i++) {
369
			if (i!=maxAlphaLayerIndex) {
370
				double scaledAlpha = (double) layers[i].getAlpha()/maxAlpha;
371
				int myAlpha = (int) (outlineAlpha*scaledAlpha);
372
				if (myAlpha == 0)
373
					myAlpha = 1;
374
				layers[i].setAlpha(myAlpha);
375
			} else {
376
				int myAlpha = (int) outlineAlpha;
377
				if (myAlpha == 0)
378
					myAlpha = 1;
379
				layers[i].setAlpha(myAlpha);
380
			}
381
		}
382

  
383
	}
384

  
385
	@Override
386
	public void setUnit(int unitIndex) {
387
		super.setUnit(unitIndex);
388
		for (int i = 0; i < layers.length; i++) {
389
			layers[i].setUnit(unitIndex);
390
		}
391
	}
392
	
393
	@Override
394
	public void setReferenceSystem(int system) {
395
		super.setReferenceSystem(system);
396
		for (int i = 0; i < layers.length; i++) {
397
			layers[i].setReferenceSystem(system);
398
		}
399
	}
400
}

Also available in: Unified diff