Statistics
| Revision:

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

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

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

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

    
152
        public double getLineWidth() {
153

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

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

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

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

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

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

    
199
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
200
                for (int i = 0; i < layers.length; i++) {
201
                        layers[i].drawInsideRectangle(g, scaleInstance, r);
202
                }
203
        }
204

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

    
210
        public void getPixExtentPlus(FShape shp, float[] distances,
211
                        ViewPort viewPort, int dpi) {
212
                float[] myDistances = new float[] {0,0};
213
                distances[0] = 0;
214
                distances[1] = 0;
215
                for (int i = 0; i < layers.length; i++) {
216
                        layers[i].getPixExtentPlus(shp, myDistances, viewPort, dpi);
217
                        distances[0] = Math.max(myDistances[0], distances[0]);
218
                        distances[1] = Math.max(myDistances[1], distances[1]);
219
                }
220
        }
221

    
222
        public ISymbol getSymbolForSelection() {
223
                if (selectionSymbol == null) {
224
                        selectionSymbol = new SimpleLineSymbol();
225
                        selectionSymbol.setDescription(getDescription());
226
                        selectionSymbol.setLineWidth(getLineWidth());
227
                        selectionSymbol.setLineColor(MapContext.getSelectionColor());
228
                }
229
                return selectionSymbol;
230
        }
231

    
232

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

    
247
        public boolean isSuitableFor(IGeometry geom) {
248
                return geom.getGeometryType() == FShape.LINE;
249
        }
250

    
251
        public String getClassName() {
252
                return getClass().getName();
253
        }
254

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

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

    
272
        }
273

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

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

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

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

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

    
296
        public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
297
                
298
                if (newLayer == null) return; // null are not allowed
299
                ILineSymbol newLine = (ILineSymbol) newLayer;
300
                if (getLayerCount() == 0) {
301
                        // apply the new layer properties to this multilayer
302
                        
303
                        setReferenceSystem(newLine.getReferenceSystem());
304
                        lineWidth = newLine.getLineWidth();
305
                        setUnit(newLine.getUnit());
306
                } else {
307
                        if (newLine.getLineWidth() > getLineWidth()) {
308
                                lineWidth = newLine.getLineWidth();
309
                        }
310
                        newLine.setReferenceSystem(getReferenceSystem());
311
                        newLine.setUnit(getUnit());
312
                }                
313
                
314
                selectionSymbol = null; /* forces the selection symbol to be re-created
315
                                                                  * next time it is required
316
                                                                  */
317
                
318
                
319
                if (layerIndex < 0 || layers.length < layerIndex)
320
                        throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
321
                ArrayList<ISymbol> newLayers = new ArrayList<ISymbol>();
322
                for (int i = 0; i < layers.length; i++) {
323
                        newLayers.add(layers[i]);
324
                }
325
                try {
326
                        newLayers.add(layerIndex, newLayer);
327
                        layers = newLayers.toArray(new ILineSymbol[0]);
328
                } catch (ArrayStoreException asEx) {
329
                        throw new ClassCastException(newLayer.getClassName()+" is not an ILineSymbol");
330
                }
331
        }
332

    
333
        public boolean removeLayer(ISymbol layer) {
334

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

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

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

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

    
385
        }
386

    
387
        @Override
388
        public void setUnit(int unitIndex) {
389
                super.setUnit(unitIndex);
390
                for (int i = 0; i < layers.length; i++) {
391
                        layers[i].setUnit(unitIndex);
392
                }
393
        }
394
        
395
        @Override
396
        public void setReferenceSystem(int system) {
397
                super.setReferenceSystem(system);
398
                for (int i = 0; i < layers.length; i++) {
399
                        layers[i].setReferenceSystem(system);
400
                }
401
        }
402
        
403
        @Override
404
        public void setCartographicSize(double cartographicSize, FShape shp) {
405
//                super.setCartographicSize(cartographicSize, shp);
406
                setLineWidth(cartographicSize);
407
        }
408
}