Statistics
| Revision:

root / branches / v2_0_0_prep / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / MultiLayerMarkerSymbol.java @ 21200

History | View | Annotate | Download (11.2 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: MultiLayerMarkerSymbol.java 21071 2008-06-02 10:55:35Z vcaballero $
45
* $Log$
46
* 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
* removed unnecessary imports
51
*
52
* Revision 1.13  2007/09/17 14:16:11  jaume
53
* multilayer symbols sizing bug fixed
54
*
55
* Revision 1.12  2007/09/17 11:37:55  jaume
56
* fixed multilayermarkersymbol unit symbol
57
*
58
* Revision 1.11  2007/08/09 07:57:51  jvidal
59
* javadoc
60
*
61
* Revision 1.10  2007/07/18 06:54:34  jaume
62
* continuing with cartographic support
63
*
64
* Revision 1.9  2007/07/03 10:58:29  jaume
65
* first refactor on CartographicSupport
66
*
67
* Revision 1.8  2007/06/29 13:07:01  jaume
68
* +PictureLineSymbol
69
*
70
* Revision 1.7  2007/06/07 06:50:40  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.6  2007/05/08 08:47:40  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.5  2007/04/17 07:01:53  bsanchez
77
* - 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
* *** empty log message ***
81
*
82
* Revision 1.3  2007/03/26 14:26:02  jaume
83
* implemented Print
84
*
85
* Revision 1.2  2007/03/09 11:20:57  jaume
86
* 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 org.gvsig.fmap.mapcontext.rendering.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
import org.gvsig.data.ReadException;
108
import org.gvsig.fmap.geom.Geometry;
109
import org.gvsig.fmap.mapcontext.ViewPort;
110

    
111
import com.iver.utiles.XMLEntity;
112
import com.iver.utiles.swing.threads.Cancellable;
113
/**
114
 * MultiLayerMarkerSymbol allows to group several marker symbols (xxxMarkerSymbol
115
 * implementing IMarkerSymbol)in one and treat it as an only one symbol.
116
 *
117
 * @author jaume dominguez faus - jaume.dominguez@iver.es
118
 */
119
public class MultiLayerMarkerSymbol extends AbstractMarkerSymbol implements IMarkerSymbol, IMultiLayerSymbol {
120
        private IMarkerSymbol[] layers = new IMarkerSymbol[0];
121
        private MultiLayerMarkerSymbol selectionSymbol;
122
        private Point2D offset = new Point2D.Double();
123
        private double rotation;
124
        public Color getColor() {
125
                /*
126
                 * a multilayer symbol does not define any color, the color
127
                 * of each layer is defined by the layer itself
128
                 */
129
                return null;
130
        }
131

    
132
        public Point2D getOffset() {
133
                return offset;
134
        }
135

    
136
        public double getRotation() {
137
                return rotation;
138
        }
139

    
140

    
141
        public void setColor(Color color) {
142
                /*
143
                 * will apply the color to each layer
144
                 */
145
                for (int i = 0; i < layers.length; i++) {
146
                        layers[i].setColor(color);
147
                }
148
        }
149

    
150
        public void setOffset(Point2D offset) {
151
                this.offset = offset;
152
        }
153

    
154
        public void setRotation(double rotation) {
155
                this.rotation = rotation;
156
        }
157

    
158
        public void setSize(double size) {
159
                if (size > 0 && size != getSize()) {
160

    
161
                        double scale = size / getSize();
162
                        super.setSize(size);
163
                        for (int i = 0; i < layers.length; i++) {
164
                                double lSize = layers[i].getSize();
165
                                layers[i].setSize(lSize*scale);
166
                        }
167
                }
168
        }
169

    
170
        public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Cancellable cancel) {
171
                Point2D p = (Point2D) geom;
172
                g.rotate(rotation, p.getX(), p.getY());
173
                for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < layers.length; i++) {
174
                        layers[i].draw(g, affineTransform, geom, cancel);
175
                }
176
                g.rotate(-rotation, p.getX(), p.getY());
177
        }
178

    
179
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
180
                g.rotate(rotation, r.getCenterX(), r.getCenterY());
181
                for (int i = 0; i < layers.length; i++) {
182
                        layers[i].drawInsideRectangle(g, scaleInstance, r);
183
                }
184
                g.rotate(-rotation, r.getCenterX(), r.getCenterY());
185
        }
186

    
187
        public int getOnePointRgb() {
188
                // will paint only the last layer pixel
189
                return layers[layers.length-1].getOnePointRgb();
190
        }
191

    
192
        public void getPixExtentPlus(Geometry geom, float[] distances,
193
                        ViewPort viewPort, int dpi) {
194
                float[] myDistances = new float[] {0,0};
195
                distances[0] = 0;
196
                distances[1] = 0;
197
                for (int i = 0; i < layers.length; i++) {
198
                        layers[i].getPixExtentPlus(geom, myDistances, viewPort, dpi);
199
                        distances[0] = Math.max(myDistances[0], distances[0]);
200
                        distances[1] = Math.max(myDistances[1], distances[1]);
201
                }
202
        }
203

    
204
        public ISymbol getSymbolForSelection() {
205
                if (selectionSymbol == null) {
206
                        selectionSymbol = new MultiLayerMarkerSymbol();
207
                        selectionSymbol.setDescription(getDescription());
208
                        for (int i = 0; i < layers.length; i++) {
209
                                selectionSymbol.addLayer(layers[i].getSymbolForSelection());
210
                        }
211
                }
212
                return selectionSymbol;
213

    
214
        }
215

    
216

    
217
        public XMLEntity getXMLEntity() {
218
                XMLEntity xml = new XMLEntity();
219

    
220
                xml.putProperty("className", getClass().getName());
221
                xml.putProperty("isShapeVisible", isShapeVisible());
222
                xml.putProperty("desc", getDescription());
223
                xml.putProperty("size",        getSize());
224
                xml.putProperty("unit", getUnit());
225
                xml.putProperty("referenceSystem", getReferenceSystem());
226
                for (int i = 0; i < layers.length; i++) {
227
                        xml.addChild(layers[i].getXMLEntity());
228
                }
229
                return xml;
230
        }
231

    
232
        public boolean isSuitableFor(Geometry geom) {
233
                return geom.getType() == Geometry.TYPES.POINT;
234
        }
235

    
236

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

    
241
        public void setXMLEntity(XMLEntity xml) {
242
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
243
                setDescription(xml.getStringProperty("desc"));
244
                setSize(xml.getDoubleProperty("size"));
245
                setUnit(xml.getIntProperty("unit"));
246
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
247
                for (int i = 0; i < xml.getChildrenCount(); i++) {
248
                        addLayer((IMarkerSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i));
249
                }
250
        }
251

    
252

    
253
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintRequestAttributeSet properties)
254
                        throws ReadException {
255
                for (int i = 0; i < layers.length; i++) {
256
                        layers[i].print(g, at, geom, properties);
257
                }
258

    
259
        }
260

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

    
265
        public void swapLayers(int index1, int index2) {
266
                ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
267
                layers[index2] = (IMarkerSymbol) aux1;
268
                layers[index1] = (IMarkerSymbol) aux2;
269
        }
270

    
271
        public ISymbol getLayer(int layerIndex) {
272
//                try{
273
                        return layers[layerIndex];
274
//                } catch (Exception e) {
275
//                        return null;
276
//                }
277
        }
278

    
279
        public int getLayerCount() {
280
                return layers.length;
281
        }
282

    
283
        public void addLayer(ISymbol newLayer) {
284
                addLayer(newLayer, layers.length);
285
        }
286

    
287
        public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
288
                if (newLayer == null )/*|| newLayer instanceof ILabelStyle)*/ return; // null or symbols that are styles are not allowed
289

    
290
                IMarkerSymbol newMarker = (IMarkerSymbol) newLayer;
291
                if (getLayerCount() == 0) {
292
                        // apply the new layer properties to this multilayer
293

    
294
                        setReferenceSystem(newMarker.getReferenceSystem());
295
                        setSize(newMarker.getSize());
296
                        setUnit(newMarker.getUnit());
297
                } else {
298
                        if (newMarker.getSize() > getSize()) {
299
                                setSize(newMarker.getSize());
300
                        }
301
                        newMarker.setReferenceSystem(getReferenceSystem());
302
                        newMarker.setUnit(getUnit());
303
                }
304
                selectionSymbol = null; /* forces the selection symbol to be re-created
305
                                                                  * next time it is required
306
                                                                  */
307
                if (layerIndex < 0 || layers.length < layerIndex)
308
                        throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
309
                ArrayList<ISymbol> newLayers = new ArrayList<ISymbol>();
310
                for (int i = 0; i < layers.length; i++) {
311
                        newLayers.add(layers[i]);
312
                }
313
                try {
314
                        newLayers.add(layerIndex, newLayer);
315
                        layers = (IMarkerSymbol[]) newLayers.toArray(new IMarkerSymbol[0]);
316
                } catch (ArrayStoreException asEx) {
317
                        throw new ClassCastException(newLayer.getClassName()+" is not an IMarkerSymbol");
318
                }
319
        }
320

    
321
        public boolean removeLayer(ISymbol layer) {
322

    
323
                int capacity = 0;
324
                capacity = layers.length;
325
                ArrayList lst = new ArrayList(capacity);
326
                for (int i = 0; i < capacity; i++) {
327
                        lst.add(layers[i]);
328
                }
329
                boolean contains = lst.remove(layer);
330
                layers = (IMarkerSymbol[])lst.toArray(new IMarkerSymbol[0]);
331
                return contains;
332
        }
333

    
334
        @Override
335
        public void setUnit(int unit) {
336
                super.setUnit(unit);
337
                for (int i = 0; i < layers.length; i++) {
338
                        layers[i].setUnit(unit);
339
                }
340
        }
341

    
342
        @Override
343
        public void setReferenceSystem(int system) {
344
                super.setReferenceSystem(system);
345
                for (int i = 0; i < layers.length; i++) {
346
                        layers[i].setReferenceSystem(system);
347
                }
348
        }
349

    
350
        public void setAlpha(int alpha) {
351
                // first, get the biggest alpha in the layers and the index if such layer
352
                int maxAlpha = Integer.MIN_VALUE;
353
                int maxAlphaLayerIndex = 0;
354
                for (int i = 0; i < layers.length; i++) {
355
                        if (layers[i].getColor().getAlpha() > maxAlpha) {
356
                                maxAlpha = layers[i].getColor().getAlpha();
357
                                maxAlphaLayerIndex = i;
358
                        }
359
                }
360

    
361
                // now, max alpha takes the value of the desired alpha and the rest
362
                // will take a scaled (to biggest alpha) alpha value
363
                for (int i = 0; i < layers.length; i++) {
364
                        int r = layers[i].getColor().getRed();
365
                        int g = layers[i].getColor().getGreen();
366
                        int b = layers[i].getColor().getBlue();
367

    
368
                        if (i!=maxAlphaLayerIndex) {
369
                                double scaledAlpha = (double) layers[i].getColor().getAlpha()/maxAlpha;
370
                                int myAlpha = (int) (alpha*scaledAlpha);
371
                                if (myAlpha == 0)
372
                                        myAlpha = 1;
373
                                layers[i].setColor(new Color(r, g, b, myAlpha));
374
                        } else {
375
                                int myAlpha = (int) alpha;
376
                                if (myAlpha == 0)
377
                                        myAlpha = 1;
378
                                layers[i].setColor(new Color(r, g, b, myAlpha));
379
                        }
380
                }
381

    
382
        }
383
}