Revision 10677

View differences:

trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/core/symbols/MultiLayerSymbol.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.8  2007-03-06 17:08:54  caballero
47
* Exceptions
48
*
49
* Revision 1.7  2007/01/25 16:25:23  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.6  2007/01/24 17:58:22  jaume
53
* new features and architecture error fixes
54
*
55
* Revision 1.5  2007/01/16 11:50:44  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.4  2007/01/12 10:08:26  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.3  2007/01/11 12:17:34  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.2  2007/01/10 16:39:41  jaume
65
* ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
66
*
67
* Revision 1.1  2007/01/10 16:31:36  jaume
68
* *** empty log message ***
69
*
70
*
71
*/
72
package com.iver.cit.gvsig.fmap.core.symbols;
73

  
74
import java.awt.Graphics2D;
75
import java.awt.Rectangle;
76
import java.awt.Shape;
77
import java.awt.geom.AffineTransform;
78
import java.util.ArrayList;
79

  
80
import javax.print.attribute.PrintRequestAttributeSet;
81

  
82
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
83
import com.iver.cit.gvsig.fmap.core.FShape;
84
import com.iver.cit.gvsig.fmap.core.IGeometry;
85
import com.iver.cit.gvsig.fmap.core.SymbolFactory;
86
import com.iver.utiles.XMLEntity;
87

  
88
/**
89
 * <p>Symbol that allows to combine many symbols into one and treat it
90
 * as a single one. Within MultiLayerSymbol, each symbol is defined as a
91
 * layer inside the MultiLayerSymbol.<br></p>
92
 *
93
 * <p>This symbol does not perform anything by itself. Rather than that it
94
 * keeps a list of symbols and iterates over each element performing the
95
 * operation over each one.<br></p>
96
 *
97
 * <p>The elements of the list can be any symbol, even other MultiLayerSymbol
98
 * so, it is also possible to define symbols as a tree of symbols</p>
99
 *
100
 * @author jaume dominguez faus - jaume.dominguez@iver.es
101
 *
102
 */
103
public class MultiLayerSymbol extends AbstractSymbol {
104
	private ISymbol[] layers = new ISymbol[0];
105
	private MultiLayerSymbol selectionSymbol;
106
	private int symbolType;
107

  
108

  
109
	/**
110
	 * Gets the symbol in the layerIndex position of the symbol list.
111
	 * @param int layerIndex
112
	 * @return
113
	 */
114
	public ISymbol getLayer(int layerIndex) {
115
		try{
116
			return layers[layerIndex];
117
		} catch (Exception e) {
118
			return null;
119
		}
120
	}
121

  
122
	public int getLayerCount() {
123
		return layers.length;
124
	}
125

  
126
	public ISymbol getSymbolForSelection() {
127
		if (selectionSymbol == null) {
128
			selectionSymbol = new MultiLayerSymbol();
129
			selectionSymbol.setDescription(getDescription());
130
			selectionSymbol.symbolType = symbolType;
131
			for (int i = 0; i < layers.length; i++) {
132
				selectionSymbol.addLayer(layers[i].getSymbolForSelection());
133
			}
134
		}
135
		return selectionSymbol;
136
	}
137

  
138
	/**
139
	 * Stacks a new symbol to the symbol list. The symbol is appended to the
140
	 * list and, in terms of use, it will be the last symbol to be processed.
141
	 * @param ISymbol newLayer
142
	 */
143
	public void addLayer(ISymbol newLayer) {
144
		if (newLayer == null) return;
145

  
146
		selectionSymbol = null; /* forces the selection symbol to be re-created
147
								 * next time it is required
148
								 */
149
		int size = layers.length+1;
150
		ISymbol[] newLayers = new ISymbol[size];
151
		for (int i = 0; i < newLayers.length-1; i++) {
152
			newLayers[i] = layers[i];
153
		}
154
		newLayers[size-1] = newLayer;
155
		layers = newLayers;
156
	}
157

  
158
	public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
159
		if (newLayer == null) return;
160

  
161
		selectionSymbol = null; /* forces the selection symbol to be re-created
162
		 						 * next time it is required
163
		 						 */
164
		if (layerIndex < 0 || layers.length < layerIndex)
165
			throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
166
		int size = layers.length+1;
167
		ArrayList newLayers = new ArrayList();
168
		for (int i = 0; i < layers.length; i++) {
169
			newLayers.add(layers[i]);
170
		}
171
		newLayers.add(layerIndex, newLayer);
172
		layers = (ISymbol[]) newLayers.toArray(new ISymbol[0]);
173
	}
174
	/**
175
	 * TODO maybe push it up to ISymbol
176
	 * @param layer
177
	 * @return true if this symbol contains the removed one
178
	 */
179
	public boolean removeLayer(ISymbol layer) {
180

  
181
		int capacity = 0;
182
		capacity = layers.length;
183
		ArrayList lst = new ArrayList(capacity);
184
		for (int i = 0; i < capacity; i++) {
185
			lst.add(layers[i]);
186
		}
187
		boolean contains = lst.remove(layer);
188
		layers = (ISymbol[])lst.toArray(new ISymbol[0]);
189
		return contains;
190
	}
191

  
192
	public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
193
		for (int i = 0; i < layers.length; i++) {
194
			layers[i].draw(g, affineTransform, shp);
195
		}
196
	}
197

  
198
	public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, Shape shp) {
199
		// TODO Auto-generated method stub
200
		throw new Error("Not yet implemented!");
201

  
202
	}
203

  
204
	public int getOnePointRgb() {
205
		// will paint only the last layer pixel
206
		return layers[layers.length-1].getOnePointRgb();
207
	}
208

  
209
	public XMLEntity getXMLEntity() {
210
		XMLEntity xml = new XMLEntity();
211

  
212
		xml.putProperty("className", getClass().getName());
213
		xml.putProperty("isShapeVisible", isShapeVisible());
214
		xml.putProperty("symbolType", symbolType);
215
		xml.putProperty("desc", getDescription());
216
		for (int i = 0; i < layers.length; i++) {
217
			xml.addChild(layers[i].getXMLEntity());
218
		}
219
		return xml;
220
	}
221

  
222
	public int getSymbolType() {
223
		return symbolType;
224
	}
225

  
226
	public boolean isSuitableFor(IGeometry geom) {
227
		return (geom.getGeometryType() & symbolType) > 0;
228
	}
229

  
230
	public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
231
		for (int i = 0; i < layers.length; i++) {
232
			layers[i].drawInsideRectangle(g, scaleInstance, r);
233
		}
234
	}
235

  
236
	public String getClassName() {
237
		return getClass().getName();
238
	}
239

  
240
	public void setXMLEntity(XMLEntity xml) {
241
		setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
242
		setDescription(xml.getStringProperty("desc"));
243
		symbolType = xml.getIntProperty("symbolType");
244
		layers = new ISymbol[xml.getChildrenCount()];
245
		for (int i = 0; i < layers.length; i++) {
246
			layers[i] = SymbolFactory.createFromXML(xml.getChild(i), "layer" + i);
247
		}
248
	}
249

  
250
	public void setPrintingProperties(PrintRequestAttributeSet properties) {
251
		for (int i = 0; i < layers.length; i++) {
252
			layers[i].setPrintingProperties(properties);
253
		}
254
	}
255

  
256
	public void print(Graphics2D g, AffineTransform at, FShape shape) throws ReadDriverException {
257
		// TODO Implement it
258
		throw new Error("Not yet implemented!");
259

  
260
	}
261

  
262
	public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
263
		layers[index] = layer;
264
	}
265

  
266
	public void setMultiLayerSymbolType(int type) {
267
		symbolType = type;
268
	}
269

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

Also available in: Unified diff