Statistics
| Revision:

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

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

    
71
import java.awt.Color;
72
import java.awt.Graphics2D;
73
import java.awt.Rectangle;
74
import java.awt.Shape;
75
import java.awt.geom.AffineTransform;
76
import java.util.ArrayList;
77

    
78
import javax.print.attribute.PrintRequestAttributeSet;
79

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

    
88
public class MultiLayerLineSymbol extends AbstractSymbol implements
89
                ILineSymbol, IMultiLayerSymbol {
90
        private ILineSymbol[] layers = new ILineSymbol[0];
91
        private ILineSymbol selectionSymbol;
92

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

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

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

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

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

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

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

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

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

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

    
172
        }
173

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

    
184
        public int getSymbolType() {
185
                return FShape.LINE;
186
        }
187

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

    
199
        public boolean isSuitableFor(IGeometry geom) {
200
                return geom.getGeometryType() == FShape.LINE;
201
        }
202

    
203
        public String getClassName() {
204
                return getClass().getName();
205
        }
206

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

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

    
222
        }
223

    
224
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
225
                layers[index] = (ILineSymbol) layer;
226
        }
227

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

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

    
242
        public int getLayerCount() {
243
                return layers.length;
244
        }
245

    
246
        public void addLayer(ISymbol newLayer) {
247
                if (newLayer == null) return;
248

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

    
261
        public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
262
                if (newLayer == null) return; // null are not allowed
263

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

    
277
        public boolean removeLayer(ISymbol layer) {
278

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

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

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

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

    
323
        }
324

    
325

    
326
}