Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / MultiLayerMarkerSymbol.java @ 13749

History | View | Annotate | Download (9.02 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 13749 jaume
* Revision 1.13  2007-09-17 14:16:11  jaume
47
* multilayer symbols sizing bug fixed
48
*
49
* Revision 1.12  2007/09/17 11:37:55  jaume
50 13744 jaume
* fixed multilayermarkersymbol unit symbol
51
*
52
* Revision 1.11  2007/08/09 07:57:51  jvidal
53 12992 jvidal
* javadoc
54
*
55
* Revision 1.10  2007/07/18 06:54:34  jaume
56 12657 jaume
* continuing with cartographic support
57
*
58
* Revision 1.9  2007/07/03 10:58:29  jaume
59 12467 jaume
* first refactor on CartographicSupport
60
*
61
* Revision 1.8  2007/06/29 13:07:01  jaume
62 12427 jaume
* +PictureLineSymbol
63
*
64
* Revision 1.7  2007/06/07 06:50:40  jaume
65 11486 jaume
* *** empty log message ***
66
*
67 12043 jaume
* Revision 1.6  2007/05/08 08:47:40  jaume
68
* *** empty log message ***
69
*
70 11486 jaume
* Revision 1.5  2007/04/17 07:01:53  bsanchez
71 11217 bsanchez
* - Corregido fallo de Double.MIN_VALUE por Double.NEGATIVE_INFINITY comentado por Victor Olaya.
72
*
73
* Revision 1.4  2007/03/29 16:02:01  jaume
74 10977 jaume
* *** empty log message ***
75
*
76
* Revision 1.3  2007/03/26 14:26:02  jaume
77 10902 jaume
* implemented Print
78
*
79
* Revision 1.2  2007/03/09 11:20:57  jaume
80 10679 jaume
* Advanced symbology (start committing)
81
*
82
* Revision 1.1.2.2  2007/02/21 07:34:08  jaume
83
* labeling starts working
84
*
85
* Revision 1.1.2.1  2007/02/16 10:54:12  jaume
86
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
87
*
88
*
89
*/
90
package com.iver.cit.gvsig.fmap.core.symbols;
91
92
import java.awt.Color;
93
import java.awt.Graphics2D;
94
import java.awt.Rectangle;
95
import java.awt.Shape;
96
import java.awt.geom.AffineTransform;
97
import java.awt.geom.Point2D;
98
import java.util.ArrayList;
99
100
import javax.print.attribute.PrintRequestAttributeSet;
101
102
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
103 12467 jaume
import com.iver.cit.gvsig.fmap.MapContext;
104
import com.iver.cit.gvsig.fmap.ViewPort;
105 10679 jaume
import com.iver.cit.gvsig.fmap.core.FShape;
106
import com.iver.cit.gvsig.fmap.core.IGeometry;
107
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
108
import com.iver.utiles.XMLEntity;
109 12992 jvidal
/**
110
 * MultiLayerMarkerSymbol allows to group several marker symbols (xxxMarkerSymbol
111
 * implementing IMarkerSymbol)in one and treat it as an only one symbol.
112
 *
113
 * @author jaume dominguez faus - jaume.dominguez@iver.es
114
 */
115 11486 jaume
public class MultiLayerMarkerSymbol extends AbstractMarkerSymbol implements IMarkerSymbol, IMultiLayerSymbol {
116 10679 jaume
        private IMarkerSymbol[] layers = new IMarkerSymbol[0];
117
        private MultiLayerMarkerSymbol selectionSymbol;
118
        private Point2D offset = new Point2D.Double();
119
        private double rotation;
120
        public Color getColor() {
121
                /*
122
                 * a multilayer symbol does not define any color, the color
123
                 * of each layer is defined by the layer itself
124
                 */
125
                return null;
126
        }
127
128
        public Point2D getOffset() {
129
                return offset;
130
        }
131
132
        public double getRotation() {
133
                return rotation;
134
        }
135
136
137
        public void setColor(Color color) {
138
                /*
139
                 * will apply the color to each layer
140
                 */
141
                for (int i = 0; i < layers.length; i++) {
142
                        layers[i].setColor(color);
143
                }
144
        }
145
146
        public void setOffset(Point2D offset) {
147
                this.offset = offset;
148
        }
149
150
        public void setRotation(double rotation) {
151
                this.rotation = rotation;
152
        }
153
154
        public void setSize(double size) {
155 13749 jaume
                if (size > 0) {
156
                        double scale = size / getSize();
157
                        super.setSize(size);
158
                        for (int i = 0; i < layers.length; i++) {
159
                                double lSize = layers[i].getSize();
160
                                layers[i].setSize(lSize*scale);
161
                        }
162 12043 jaume
                }
163 10679 jaume
        }
164
165
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
166
                for (int i = 0; i < layers.length; i++) {
167
                        layers[i].draw(g, affineTransform, shp);
168
                }
169
        }
170
171
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
172
                for (int i = 0; i < layers.length; i++) {
173
                        layers[i].drawInsideRectangle(g, scaleInstance, r);
174
                }
175
        }
176
177
        public int getOnePointRgb() {
178
                // will paint only the last layer pixel
179
                return layers[layers.length-1].getOnePointRgb();
180
        }
181
182
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
183
                        Shape shp) {
184
                // TODO Implement it
185
                throw new Error("Not yet implemented!");
186
187
        }
188
189
        public ISymbol getSymbolForSelection() {
190
                if (selectionSymbol == null) {
191
                        selectionSymbol = new MultiLayerMarkerSymbol();
192
                        selectionSymbol.setDescription(getDescription());
193
//                        selectionSymbol.symbolType = symbolType;
194
                        for (int i = 0; i < layers.length; i++) {
195
                                selectionSymbol.addLayer(layers[i].getSymbolForSelection());
196
                        }
197
                }
198
                return selectionSymbol;
199
200
        }
201
202
203
        public int getSymbolType() {
204
                return FShape.POINT;
205
        }
206
207
        public XMLEntity getXMLEntity() {
208
                XMLEntity xml = new XMLEntity();
209
210
                xml.putProperty("className", getClass().getName());
211
                xml.putProperty("isShapeVisible", isShapeVisible());
212
                xml.putProperty("desc", getDescription());
213 13749 jaume
                xml.putProperty("size",        getSize());
214
                xml.putProperty("unit", getUnit());
215
                xml.putProperty("referenceSystem", getReferenceSystem());
216 10679 jaume
                for (int i = 0; i < layers.length; i++) {
217
                        xml.addChild(layers[i].getXMLEntity());
218
                }
219
                return xml;
220
        }
221
222
        public boolean isSuitableFor(IGeometry geom) {
223
                return geom.getGeometryType() == FShape.POINT;
224
        }
225
226
227
        public String getClassName() {
228
                return getClass().getName();
229
        }
230
231
        public void setXMLEntity(XMLEntity xml) {
232
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
233
                setDescription(xml.getStringProperty("desc"));
234 13749 jaume
                setSize(xml.getDoubleProperty("size"));
235
                setUnit(xml.getIntProperty("unit"));
236
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
237 10679 jaume
                layers = new IMarkerSymbol[xml.getChildrenCount()];
238
                for (int i = 0; i < layers.length; i++) {
239
                        layers[i] = (IMarkerSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i);
240
                }
241
        }
242
243
244 10902 jaume
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
245 10679 jaume
                        throws ReadDriverException {
246 10902 jaume
                for (int i = 0; i < layers.length; i++) {
247
                        layers[i].print(g, at, shape, properties);
248
                }
249 10679 jaume
250
        }
251
252
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
253
                layers[index] = (IMarkerSymbol) layer;
254
        }
255
256
        public void swapLayers(int index1, int index2) {
257
                ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
258
                layers[index2] = (IMarkerSymbol) aux1;
259
                layers[index1] = (IMarkerSymbol) aux2;
260
        }
261
262
        public ISymbol getLayer(int layerIndex) {
263 12427 jaume
//                try{
264 10679 jaume
                        return layers[layerIndex];
265 12427 jaume
//                } catch (Exception e) {
266
//                        return null;
267
//                }
268 10679 jaume
        }
269
270
        public int getLayerCount() {
271
                return layers.length;
272
        }
273
274
        public void addLayer(ISymbol newLayer) {
275
                if (newLayer == null) return;
276
277
                selectionSymbol = null; /* forces the selection symbol to be re-created
278
                                                                 * next time it is required
279
                                                                 */
280
                int size = layers.length+1;
281
                IMarkerSymbol[] newLayers = new IMarkerSymbol[size];
282
                for (int i = 0; i < newLayers.length-1; i++) {
283
                        newLayers[i] = layers[i];
284
                }
285
                newLayers[size-1] = (IMarkerSymbol) newLayer;
286
                layers = newLayers;
287
        }
288
289
        public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
290 10977 jaume
                if (newLayer == null )/*|| newLayer instanceof ILabelStyle)*/ return; // null or symbols that are styles are not allowed
291 10679 jaume
292
                selectionSymbol = null; /* forces the selection symbol to be re-created
293
                                                                  * next time it is required
294
                                                                  */
295
                if (layerIndex < 0 || layers.length < layerIndex)
296
                        throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
297
                ArrayList newLayers = new ArrayList();
298
                for (int i = 0; i < layers.length; i++) {
299
                        newLayers.add(layers[i]);
300
                }
301
                newLayers.add(layerIndex, newLayer);
302
                layers = (IMarkerSymbol[]) newLayers.toArray(new IMarkerSymbol[0]);
303
        }
304
305
        public boolean removeLayer(ISymbol layer) {
306
307
                int capacity = 0;
308
                capacity = layers.length;
309
                ArrayList lst = new ArrayList(capacity);
310
                for (int i = 0; i < capacity; i++) {
311
                        lst.add(layers[i]);
312
                }
313
                boolean contains = lst.remove(layer);
314
                layers = (IMarkerSymbol[])lst.toArray(new IMarkerSymbol[0]);
315
                return contains;
316
        }
317 13744 jaume
318
        @Override
319 13749 jaume
        public void setUnit(int unit) {
320
                super.setUnit(unit);
321 13744 jaume
                for (int i = 0; i < layers.length; i++) {
322 13749 jaume
                        layers[i].setUnit(unit);
323 13744 jaume
                }
324
        }
325 10679 jaume
326 13749 jaume
        @Override
327
        public void setReferenceSystem(int system) {
328
                super.setReferenceSystem(system);
329
                for (int i = 0; i < layers.length; i++) {
330
                        layers[i].setReferenceSystem(system);
331
                }
332
        }
333 10679 jaume
}