Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / MultiLayerLineSymbol.java @ 38563

History | View | Annotate | Download (11.6 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 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.geom.AffineTransform;
111
import java.util.ArrayList;
112

    
113
import javax.print.attribute.PrintRequestAttributeSet;
114

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

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

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

    
151
        public double getLineWidth() {
152

    
153
                double myLineWidth = 0;
154
                for (int i = 0; i < getLayerCount(); i++) {
155
                        myLineWidth = Math.max(myLineWidth, ((ILineSymbol) getLayer(i)).getLineWidth());
156
                }
157

    
158
                if (lineWidth != myLineWidth)
159
                        lineWidth = myLineWidth;
160
                return lineWidth;
161
        }
162

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

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

    
181
        public void setLineWidth(double width) {
182

    
183
                if (width > 0 && width != getLineWidth()) {
184
                        double scaleFactor = width / getLineWidth();
185
                        this.lineWidth = width;
186
                        for (int i = 0; i < layers.length; i++) {
187
                                layers[i].setLineWidth(layers[i].getLineWidth()*scaleFactor);
188
                        }
189
                }
190
        }
191

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

    
198
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException {
199
                for (int i = 0; i < layers.length; i++) {
200
                        layers[i].drawInsideRectangle(g, scaleInstance, r, properties);
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 void getPixExtentPlus(FShape shp, float[] distances,
210
                        ViewPort viewPort, int dpi) {
211
                float[] myDistances = new float[] {0,0};
212
                distances[0] = 0;
213
                distances[1] = 0;
214
                for (int i = 0; i < layers.length; i++) {
215
                        layers[i].getPixExtentPlus(shp, myDistances, viewPort, dpi);
216
                        distances[0] = Math.max(myDistances[0], distances[0]);
217
                        distances[1] = Math.max(myDistances[1], distances[1]);
218
                }
219
        }
220

    
221
        public ISymbol getSymbolForSelection() {
222
                if (selectionSymbol == null) {
223
                        selectionSymbol = new MultiLayerLineSymbol();
224
                        selectionSymbol.setDescription(getDescription());
225
                        for (int i = 0; i < layers.length; i++) {
226
                                selectionSymbol.addLayer(layers[i].getSymbolForSelection());
227
                        }
228
                }
229
                return selectionSymbol;
230
        }
231

    
232

    
233

    
234

    
235
        public XMLEntity getXMLEntity() {
236
                XMLEntity xml = new XMLEntity();
237
                xml.putProperty("className", getClass().getName());
238
                xml.putProperty("isShapeVisible", isShapeVisible());
239
                xml.putProperty("desc", getDescription());
240
                xml.putProperty("lineWidth", getLineWidth());
241
                xml.putProperty("unit", getUnit());
242
                xml.putProperty("referenceSystem", getReferenceSystem());
243
                for (int i = 0; i < layers.length; i++) {
244
                        xml.addChild(layers[i].getXMLEntity());
245
                }
246
                return xml;
247
        }
248

    
249
        public boolean isSuitableFor(IGeometry geom) {
250
                return (geom.getGeometryType()%FShape.Z) == FShape.LINE;
251
        }
252

    
253
        public String getClassName() {
254
                return getClass().getName();
255
        }
256

    
257
        public void setXMLEntity(XMLEntity xml) {
258
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
259
                setDescription(xml.getStringProperty("desc"));
260
                setLineWidth(xml.getDoubleProperty("lineWidth"));
261
                setUnit(xml.getIntProperty("unit"));
262
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
263
                for (int i = 0; i < xml.getChildrenCount(); i++) {
264
                        addLayer((ILineSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i));
265
                }
266
        }
267

    
268
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) {
269
                for (int i = 0; i < layers.length; i++) {
270
                        layers[i].print(g, at, shape, properties);
271
                }
272

    
273
        }
274

    
275
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
276
                layers[index] = (ILineSymbol) layer;
277
        }
278

    
279
        public void swapLayers(int index1, int index2) {
280
                ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
281
                layers[index2] = (ILineSymbol) aux1;
282
                layers[index1] = (ILineSymbol) aux2;
283
        }
284

    
285
        public ISymbol getLayer(int layerIndex) {
286
                return layers[layerIndex];
287
        }
288

    
289
        public int getLayerCount() {
290
                return layers.length;
291
        }
292

    
293
        public void addLayer(ISymbol newLayer) {
294
                addLayer(newLayer, layers.length);
295
        }
296

    
297
        public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
298

    
299
                if (newLayer == null) return; // null are not allowed
300
                ILineSymbol newLine = (ILineSymbol) newLayer;
301
                if (getLayerCount() == 0) {
302
                        // apply the new layer properties to this multilayer
303

    
304
                        setReferenceSystem(newLine.getReferenceSystem());
305
                        lineWidth = newLine.getLineWidth();
306
                        setUnit(newLine.getUnit());
307
                } else {
308
                        if (newLine.getLineWidth() > getLineWidth()) {
309
                                lineWidth = newLine.getLineWidth();
310
                        }
311
                        newLine.setReferenceSystem(getReferenceSystem());
312
                        newLine.setUnit(getUnit());
313
                }
314

    
315
                selectionSymbol = null; /* forces the selection symbol to be re-created
316
                                                                  * next time it is required
317
                                                                  */
318

    
319

    
320
                if (layerIndex < 0 || layers.length < layerIndex)
321
                        throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
322
                ArrayList<ISymbol> 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<ILineSymbol> lst = new ArrayList<ILineSymbol>(capacity);
339
                for (int i = 0; i < capacity; i++) {
340
                        lst.add(layers[i]);
341
                }
342
                boolean contains = lst.remove(layer);
343
                layers = 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
                                layers[i].setAlpha(myAlpha);
378
                        } else {
379
                                int myAlpha = (int) outlineAlpha;
380
                                if (myAlpha == 0)
381
                                        myAlpha = 1;
382
                                layers[i].setAlpha(myAlpha);
383
                        }
384
                }
385

    
386
        }
387

    
388
        @Override
389
        public void setUnit(int unitIndex) {
390
                super.setUnit(unitIndex);
391
                for (int i = 0; i < layers.length; i++) {
392
                        layers[i].setUnit(unitIndex);
393
                }
394
        }
395

    
396
        @Override
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
        @Override
405
        public void setCartographicSize(double cartographicSize, FShape shp) {
406
//                super.setCartographicSize(cartographicSize, shp);
407
                setLineWidth(cartographicSize);
408
        }
409
}