Statistics
| Revision:

root / branches / simbologia / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / MultiLayerLineSymbol.java @ 10450

History | View | Annotate | Download (8.51 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 10450 2007-02-21 16:09:35Z jaume $
45
* $Log$
46
* Revision 1.1.2.3  2007-02-21 16:09:02  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.2  2007/02/21 07:34:09  jaume
50
* labeling starts working
51
*
52
* Revision 1.1.2.1  2007/02/16 10:54:12  jaume
53
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
54
*
55
*
56
*/
57
package com.iver.cit.gvsig.fmap.core.symbols;
58

    
59
import java.awt.Color;
60
import java.awt.Graphics2D;
61
import java.awt.Rectangle;
62
import java.awt.Shape;
63
import java.awt.geom.AffineTransform;
64
import java.util.ArrayList;
65

    
66
import com.iver.cit.gvsig.fmap.DriverException;
67
import com.iver.cit.gvsig.fmap.core.FShape;
68
import com.iver.cit.gvsig.fmap.core.IGeometry;
69
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
70
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
71
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
72
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
73
import com.iver.utiles.XMLEntity;
74

    
75
public class MultiLayerLineSymbol extends AbstractSymbol implements
76
                ILineSymbol, IMultiLayerSymbol {
77
        private ILineSymbol[] layers = new ILineSymbol[0];
78
        private ILineSymbol selectionSymbol;        
79

    
80
        public Color getColor() {
81
                /*
82
                 * a multilayer symbol does not define any color, the color
83
                 * of each layer is defined by the layer itself
84
                 */
85
                return null;
86
        }
87

    
88
        public ILineStyle getLineStyle() {
89
                /*
90
                 * a multilayer symbol does not define any style, the style
91
                 * of each layer is defined by the layer itself
92
                 */
93
                return null;
94
        }
95

    
96
        public double getWidth() {
97
                /*
98
                 * will return the widest symbol's width 
99
                 */
100
                double width = Double.MIN_VALUE;
101
                for (int i = 0; i < layers.length; i++) {
102
                        width = Math.max(width, layers[i].getWidth());
103
                }
104
                return width;
105
        }
106

    
107
        public void setColor(Color color) {
108
                /*
109
                 * will apply the color to each layer
110
                 */
111
                for (int i = 0; i < layers.length; i++) {
112
                        layers[i].setColor(color);
113
                }
114
        }
115

    
116
        public void setLineStyle(ILineStyle lineStyle) {
117
                /*
118
                 * will apply the same patter to each layer
119
                 */
120
                for (int i = 0; i < layers.length; i++) {
121
                        layers[i].setLineStyle(lineStyle);
122
                }
123
        }
124

    
125
        public void setWidth(double width) {
126
                /* take the biggest width of the layer set and
127
                 * extract a factor scale that will be applied
128
                 * to each layer.
129
                 */
130
                double myWidth = getWidth();
131
                double scaleFactor = width / myWidth;
132
                for (int i = 0; i < layers.length; i++) {
133
                        layers[i].setWidth(layers[i].getWidth()*scaleFactor);
134
                }
135
        }
136

    
137
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
138
                for (int i = 0; i < layers.length; i++) {
139
                        layers[i].draw(g, affineTransform, shp);
140
                }
141
        }
142

    
143
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
144
                for (int i = 0; i < layers.length; i++) {
145
                        layers[i].drawInsideRectangle(g, scaleInstance, r);
146
                }
147
        }
148

    
149
        public int getOnePointRgb() {
150
                // will paint only the last layer pixel
151
                return layers[layers.length-1].getOnePointRgb();
152
        }
153

    
154
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
155
                        Shape shp) {
156
                // TODO Implement it
157
                throw new Error("Not yet implemented!");
158
                
159
        }
160

    
161
        public ISymbol getSymbolForSelection() {
162
                if (selectionSymbol == null) {
163
                        selectionSymbol = new SimpleLineSymbol();
164
                        selectionSymbol.setDescription(getDescription());
165
                        selectionSymbol.setWidth(getWidth());
166
                        selectionSymbol.setColor(FSymbol.getSelectionColor());
167
                }
168
                return selectionSymbol;
169
        }
170

    
171
        public int getSymbolType() {
172
                return FShape.LINE;
173
        }
174

    
175
        public XMLEntity getXMLEntity() {
176
                XMLEntity xml = new XMLEntity();
177
                xml.putProperty("className", getClass().getName());
178
                xml.putProperty("isShapeVisible", isShapeVisible());
179
                xml.putProperty("desc", getDescription());
180
                for (int i = 0; i < layers.length; i++) {
181
                        xml.addChild(layers[i].getXMLEntity());
182
                }
183
                return xml;
184
        }
185

    
186
        public boolean isSuitableFor(IGeometry geom) {
187
                return geom.getGeometryType() == FShape.LINE;
188
        }
189

    
190
        public String getClassName() {
191
                return getClass().getName();
192
        }
193

    
194
        public void setXMLEntity(XMLEntity xml) {
195
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
196
                setDescription(xml.getStringProperty("desc"));
197
                layers = new ILineSymbol[xml.getChildrenCount()];
198
                for (int i = 0; i < layers.length; i++) {
199
                        layers[i] = (ILineSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i);
200
                }
201
        }
202

    
203
        public void print(Graphics2D g, AffineTransform at, FShape shape)
204
                        throws DriverException {
205
                // TODO Implement it
206
                throw new Error("Not yet implemented!");
207
                
208
        }
209
        
210
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
211
                layers[index] = (ILineSymbol) layer;
212
        }
213
        
214
        public void swapLayers(int index1, int index2) {
215
                ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
216
                layers[index2] = (ILineSymbol) aux1;
217
                layers[index1] = (ILineSymbol) aux2;
218
        }
219
        
220
        public ISymbol getLayer(int layerIndex) {
221
                try{
222
                        return layers[layerIndex];
223
                } catch (Exception e) {
224
                        return null;
225
                }
226
        }
227

    
228
        public int getLayerCount() {
229
                return layers.length;
230
        }
231

    
232
        public void addLayer(ISymbol newLayer) {
233
                if (newLayer == null) return;
234

    
235
                selectionSymbol = null; /* forces the selection symbol to be re-created
236
                                                                 * next time it is required
237
                                                                 */
238
                int size = layers.length+1;
239
                ILineSymbol[] newLayers = new ILineSymbol[size];
240
                for (int i = 0; i < newLayers.length-1; i++) {
241
                        newLayers[i] = layers[i];
242
                }
243
                newLayers[size-1] = (ILineSymbol) newLayer;
244
                layers = newLayers;
245
        }
246

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

    
250
                selectionSymbol = null; /* forces the selection symbol to be re-created
251
                                                                  * next time it is required
252
                                                                  */
253
                if (layerIndex < 0 || layers.length < layerIndex)
254
                        throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
255
                ArrayList newLayers = new ArrayList();
256
                for (int i = 0; i < layers.length; i++) {
257
                        newLayers.add(layers[i]);
258
                }
259
                newLayers.add(layerIndex, newLayer);
260
                layers = (ILineSymbol[]) newLayers.toArray(new ILineSymbol[0]);
261
        }
262

    
263
        public boolean removeLayer(ISymbol layer) {
264

    
265
                int capacity = 0;
266
                capacity = layers.length;
267
                ArrayList lst = new ArrayList(capacity);
268
                for (int i = 0; i < capacity; i++) {
269
                        lst.add(layers[i]);
270
                }
271
                boolean contains = lst.remove(layer);
272
                layers = (ILineSymbol[])lst.toArray(new ILineSymbol[0]);
273
                return contains;
274
        }
275

    
276
        public int getAlpha() {
277
                // will compute the acumulated opacity
278
                double myAlpha = 0;
279
                for (int i = 0; i < layers.length; i++) {
280
                        double layerAlpha = layers[i].getAlpha()/255D;
281
                        myAlpha += (1-myAlpha)*layerAlpha;
282
                }
283
                int result = (int) Math.round(myAlpha * 255); 
284
                return (result>255) ? 255 : result;
285
        }
286
        
287
        public void setAlpha(int outlineAlpha) {
288
                // first, get the biggest alpha in the layers and the index if such layer
289
                int maxAlpha = Integer.MIN_VALUE;
290
                int maxAlphaLayerIndex = 0;
291
                for (int i = 0; i < layers.length; i++) {
292
                        if (layers[i].getAlpha() > maxAlpha) {
293
                                maxAlpha = layers[i].getAlpha();
294
                                maxAlphaLayerIndex = i;
295
                        }
296
                }
297
                
298
                // now, max alpha takes the value of the desired alpha and the rest
299
                // will take a scaled (to biggest alpha) alpha value
300
                for (int i = 0; i < layers.length; i++) {
301
                        if (i!=maxAlphaLayerIndex) {
302
                                double scaledAlpha = (double) layers[i].getAlpha()/maxAlpha;
303
                                layers[i].setAlpha((int) (outlineAlpha*scaledAlpha));
304
                        } else {
305
                                layers[i].setAlpha(outlineAlpha);
306
                        }
307
                }
308
                
309
        }
310
        
311
        
312
}