Statistics
| Revision:

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

History | View | Annotate | Download (9.17 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 11486 2007-05-08 08:47:40Z jaume $
45
* $Log$
46
* Revision 1.6  2007-05-08 08:47:40  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.5  2007/04/17 07:01:53  bsanchez
50
* - Corregido fallo de Double.MIN_VALUE por Double.NEGATIVE_INFINITY comentado por Victor Olaya.
51
*
52
* Revision 1.4  2007/03/26 14:26:02  jaume
53
* implemented Print
54
*
55
* Revision 1.3  2007/03/20 16:01:21  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.2  2007/03/09 11:20:56  jaume
59
* Advanced symbology (start committing)
60
*
61
* Revision 1.1.2.3  2007/02/21 16:09:02  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1.2.2  2007/02/21 07:34:09  jaume
65
* labeling starts working
66
*
67
* Revision 1.1.2.1  2007/02/16 10:54:12  jaume
68
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
69
*
70
*
71
*/
72
package com.iver.cit.gvsig.fmap.core.symbols;
73

    
74
import java.awt.Color;
75
import java.awt.Graphics2D;
76
import java.awt.Rectangle;
77
import java.awt.Shape;
78
import java.awt.geom.AffineTransform;
79
import java.util.ArrayList;
80

    
81
import javax.print.attribute.PrintRequestAttributeSet;
82

    
83
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
84
import com.iver.cit.gvsig.fmap.core.FShape;
85
import com.iver.cit.gvsig.fmap.core.IGeometry;
86
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
87
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
88
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
89
import com.iver.utiles.XMLEntity;
90

    
91
public class MultiLayerLineSymbol extends AbstractSymbol implements
92
                ILineSymbol, IMultiLayerSymbol {
93
        private ILineSymbol[] layers = new ILineSymbol[0];
94
        private ILineSymbol selectionSymbol;
95

    
96
        public Color getColor() {
97
                /*
98
                 * a multilayer symbol does not define any color, the color
99
                 * of each layer is defined by the layer itself
100
                 */
101
                return null;
102
        }
103

    
104
        public ILineStyle getLineStyle() {
105
                /*
106
                 * a multilayer symbol does not define any style, the style
107
                 * of each layer is defined by the layer itself
108
                 */
109
                return null;
110
        }
111

    
112
        public double getLineWidth() {
113
                /*
114
                 * will return the widest symbol's width
115
                 */
116
                double width = Double.NEGATIVE_INFINITY;
117
                for (int i = 0; i < layers.length; i++) {
118
                        width = Math.max(width, layers[i].getLineWidth());
119
                }
120
                return width;
121
        }
122

    
123
        public void setLineColor(Color color) {
124
                /*
125
                 * will apply the color to each layer
126
                 */
127
                for (int i = 0; i < layers.length; i++) {
128
                        layers[i].setLineColor(color);
129
                }
130
        }
131

    
132
        public void setLineStyle(ILineStyle lineStyle) {
133
                /*
134
                 * will apply the same patter to each layer
135
                 */
136
                for (int i = 0; i < layers.length; i++) {
137
                        layers[i].setLineStyle(lineStyle);
138
                }
139
        }
140

    
141
        public void setLineWidth(double width) {
142
                /* take the biggest width of the layer set and
143
                 * extract a factor scale that will be applied
144
                 * to each layer.
145
                 */
146
                double myWidth = getLineWidth();
147
                double scaleFactor = width / myWidth;
148
                for (int i = 0; i < layers.length; i++) {
149
                        layers[i].setLineWidth(layers[i].getLineWidth()*scaleFactor);
150
                }
151
        }
152

    
153
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
154
                for (int i = 0; i < layers.length; i++) {
155
                        layers[i].draw(g, affineTransform, shp);
156
                }
157
        }
158

    
159
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
160
                for (int i = 0; i < layers.length; i++) {
161
                        layers[i].drawInsideRectangle(g, scaleInstance, r);
162
                }
163
        }
164

    
165
        public int getOnePointRgb() {
166
                // will paint only the last layer pixel
167
                return layers[layers.length-1].getOnePointRgb();
168
        }
169

    
170
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
171
                        Shape shp) {
172
                // TODO Implement it
173
                throw new Error("Not yet implemented!");
174

    
175
        }
176

    
177
        public ISymbol getSymbolForSelection() {
178
                if (selectionSymbol == null) {
179
                        selectionSymbol = new SimpleLineSymbol();
180
                        selectionSymbol.setDescription(getDescription());
181
                        selectionSymbol.setLineWidth(getLineWidth());
182
                        selectionSymbol.setLineColor(FSymbol.getSelectionColor());
183
                }
184
                return selectionSymbol;
185
        }
186

    
187
        public int getSymbolType() {
188
                return FShape.LINE;
189
        }
190

    
191
        public XMLEntity getXMLEntity() {
192
                XMLEntity xml = new XMLEntity();
193
                xml.putProperty("className", getClass().getName());
194
                xml.putProperty("isShapeVisible", isShapeVisible());
195
                xml.putProperty("desc", getDescription());
196
                for (int i = 0; i < layers.length; i++) {
197
                        xml.addChild(layers[i].getXMLEntity());
198
                }
199
                return xml;
200
        }
201

    
202
        public boolean isSuitableFor(IGeometry geom) {
203
                return geom.getGeometryType() == FShape.LINE;
204
        }
205

    
206
        public String getClassName() {
207
                return getClass().getName();
208
        }
209

    
210
        public void setXMLEntity(XMLEntity xml) {
211
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
212
                setDescription(xml.getStringProperty("desc"));
213
                layers = new ILineSymbol[xml.getChildrenCount()];
214
                for (int i = 0; i < layers.length; i++) {
215
                        layers[i] = (ILineSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i);
216
                }
217
        }
218

    
219
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
220
                        throws ReadDriverException {
221
                for (int i = 0; i < layers.length; i++) {
222
                        layers[i].print(g, at, shape, properties);
223
                }
224

    
225
        }
226

    
227
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
228
                layers[index] = (ILineSymbol) layer;
229
        }
230

    
231
        public void swapLayers(int index1, int index2) {
232
                ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
233
                layers[index2] = (ILineSymbol) aux1;
234
                layers[index1] = (ILineSymbol) aux2;
235
        }
236

    
237
        public ISymbol getLayer(int layerIndex) {
238
                try{
239
                        return layers[layerIndex];
240
                } catch (Exception e) {
241
                        return null;
242
                }
243
        }
244

    
245
        public int getLayerCount() {
246
                return layers.length;
247
        }
248

    
249
        public void addLayer(ISymbol newLayer) {
250
                if (newLayer == null) return;
251

    
252
                selectionSymbol = null; /* forces the selection symbol to be re-created
253
                                                                 * next time it is required
254
                                                                 */
255
                int size = layers.length+1;
256
                ILineSymbol[] newLayers = new ILineSymbol[size];
257
                for (int i = 0; i < newLayers.length-1; i++) {
258
                        newLayers[i] = layers[i];
259
                }
260
                newLayers[size-1] = (ILineSymbol) newLayer;
261
                layers = newLayers;
262
        }
263

    
264
        public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
265
                if (newLayer == null) return; // null are not allowed
266

    
267
                selectionSymbol = null; /* forces the selection symbol to be re-created
268
                                                                  * next time it is required
269
                                                                  */
270
                if (layerIndex < 0 || layers.length < layerIndex)
271
                        throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
272
                ArrayList newLayers = new ArrayList();
273
                for (int i = 0; i < layers.length; i++) {
274
                        newLayers.add(layers[i]);
275
                }
276
                newLayers.add(layerIndex, newLayer);
277
                layers = (ILineSymbol[]) newLayers.toArray(new ILineSymbol[0]);
278
        }
279

    
280
        public boolean removeLayer(ISymbol layer) {
281

    
282
                int capacity = 0;
283
                capacity = layers.length;
284
                ArrayList lst = new ArrayList(capacity);
285
                for (int i = 0; i < capacity; i++) {
286
                        lst.add(layers[i]);
287
                }
288
                boolean contains = lst.remove(layer);
289
                layers = (ILineSymbol[])lst.toArray(new ILineSymbol[0]);
290
                return contains;
291
        }
292

    
293
        public int getAlpha() {
294
                // will compute the acumulated opacity
295
                double myAlpha = 0;
296
                for (int i = 0; i < layers.length; i++) {
297
                        double layerAlpha = layers[i].getAlpha()/255D;
298
                        myAlpha += (1-myAlpha)*layerAlpha;
299
                }
300
                int result = (int) Math.round(myAlpha * 255);
301
                return (result>255) ? 255 : result;
302
        }
303

    
304
        public void setAlpha(int outlineAlpha) {
305
                // first, get the biggest alpha in the layers and the index if such layer
306
                int maxAlpha = Integer.MIN_VALUE;
307
                int maxAlphaLayerIndex = 0;
308
                for (int i = 0; i < layers.length; i++) {
309
                        if (layers[i].getAlpha() > maxAlpha) {
310
                                maxAlpha = layers[i].getAlpha();
311
                                maxAlphaLayerIndex = i;
312
                        }
313
                }
314

    
315
                // now, max alpha takes the value of the desired alpha and the rest
316
                // will take a scaled (to biggest alpha) alpha value
317
                for (int i = 0; i < layers.length; i++) {
318
                        if (i!=maxAlphaLayerIndex) {
319
                                double scaledAlpha = (double) layers[i].getAlpha()/maxAlpha;
320
                                layers[i].setAlpha((int) (outlineAlpha*scaledAlpha));
321
                        } else {
322
                                layers[i].setAlpha(outlineAlpha);
323
                        }
324
                }
325

    
326
        }
327

    
328
        public int getUnit() {
329
                return layers[0].getUnit();
330
        }
331

    
332
        public void setUnit(int unitIndex) {
333
                for (int i = 0; i < layers.length; i++) {
334
                        layers[i].setUnit(unitIndex);
335
                }
336
        }
337
}