Statistics
| Revision:

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

History | View | Annotate | Download (8.48 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 10679 2007-03-09 11:25:01Z jaume $
45
* $Log$
46
* Revision 1.2  2007-03-09 11:20:56  jaume
47
* Advanced symbology (start committing)
48
*
49
* Revision 1.1.2.3  2007/02/21 16:09:02  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1.2.2  2007/02/21 07:34:09  jaume
53
* labeling starts working
54
*
55
* Revision 1.1.2.1  2007/02/16 10:54:12  jaume
56
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
57
*
58
*
59
*/
60
package com.iver.cit.gvsig.fmap.core.symbols;
61

    
62
import java.awt.Color;
63
import java.awt.Graphics2D;
64
import java.awt.Rectangle;
65
import java.awt.Shape;
66
import java.awt.geom.AffineTransform;
67
import java.util.ArrayList;
68

    
69
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
70
import com.iver.cit.gvsig.fmap.core.FShape;
71
import com.iver.cit.gvsig.fmap.core.IGeometry;
72
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
73
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
74
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
75
import com.iver.utiles.XMLEntity;
76

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

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

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

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

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

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

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

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

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

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

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

    
161
        }
162

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

    
173
        public int getSymbolType() {
174
                return FShape.LINE;
175
        }
176

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

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

    
192
        public String getClassName() {
193
                return getClass().getName();
194
        }
195

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

    
205
        public void print(Graphics2D g, AffineTransform at, FShape shape)
206
                        throws ReadDriverException {
207
                // TODO Implement it
208
                throw new Error("Not yet implemented!");
209

    
210
        }
211

    
212
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
213
                layers[index] = (ILineSymbol) layer;
214
        }
215

    
216
        public void swapLayers(int index1, int index2) {
217
                ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
218
                layers[index2] = (ILineSymbol) aux1;
219
                layers[index1] = (ILineSymbol) aux2;
220
        }
221

    
222
        public ISymbol getLayer(int layerIndex) {
223
                try{
224
                        return layers[layerIndex];
225
                } catch (Exception e) {
226
                        return null;
227
                }
228
        }
229

    
230
        public int getLayerCount() {
231
                return layers.length;
232
        }
233

    
234
        public void addLayer(ISymbol newLayer) {
235
                if (newLayer == null) return;
236

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

    
249
        public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
250
                if (newLayer == null) return; // null are not allowed
251

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

    
265
        public boolean removeLayer(ISymbol layer) {
266

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

    
278
        public int getAlpha() {
279
                // will compute the acumulated opacity
280
                double myAlpha = 0;
281
                for (int i = 0; i < layers.length; i++) {
282
                        double layerAlpha = layers[i].getAlpha()/255D;
283
                        myAlpha += (1-myAlpha)*layerAlpha;
284
                }
285
                int result = (int) Math.round(myAlpha * 255);
286
                return (result>255) ? 255 : result;
287
        }
288

    
289
        public void setAlpha(int outlineAlpha) {
290
                // first, get the biggest alpha in the layers and the index if such layer
291
                int maxAlpha = Integer.MIN_VALUE;
292
                int maxAlphaLayerIndex = 0;
293
                for (int i = 0; i < layers.length; i++) {
294
                        if (layers[i].getAlpha() > maxAlpha) {
295
                                maxAlpha = layers[i].getAlpha();
296
                                maxAlphaLayerIndex = i;
297
                        }
298
                }
299

    
300
                // now, max alpha takes the value of the desired alpha and the rest
301
                // will take a scaled (to biggest alpha) alpha value
302
                for (int i = 0; i < layers.length; i++) {
303
                        if (i!=maxAlphaLayerIndex) {
304
                                double scaledAlpha = (double) layers[i].getAlpha()/maxAlpha;
305
                                layers[i].setAlpha((int) (outlineAlpha*scaledAlpha));
306
                        } else {
307
                                layers[i].setAlpha(outlineAlpha);
308
                        }
309
                }
310

    
311
        }
312

    
313

    
314
}