Statistics
| Revision:

root / branches / v2_0_0_prep / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / MultiLayerFillSymbol.java @ 21200

History | View | Annotate | Download (10.4 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: MultiLayerFillSymbol.java 21071 2008-06-02 10:55:35Z vcaballero $
45
* $Log$
46
* Revision 1.12  2007-09-21 12:25:32  jaume
47
* cancellation support extended down to the IGeometry and ISymbol level
48
*
49
* Revision 1.11  2007/09/19 16:20:45  jaume
50
* removed unnecessary imports
51
*
52
* Revision 1.10  2007/08/13 11:36:50  jvidal
53
* javadoc
54
*
55
* Revision 1.9  2007/08/08 12:04:15  jvidal
56
* javadoc
57
*
58
* Revision 1.8  2007/07/23 06:52:25  jaume
59
* default selection color refactored, moved to MapContext
60
*
61
* Revision 1.7  2007/07/03 10:58:29  jaume
62
* first refactor on CartographicSupport
63
*
64
* Revision 1.6  2007/06/29 13:07:01  jaume
65
* +PictureLineSymbol
66
*
67
* Revision 1.5  2007/03/29 16:02:01  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.4  2007/03/26 14:25:29  jaume
71
* implemented Print
72
*
73
* Revision 1.3  2007/03/13 16:58:36  jaume
74
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
75
*
76
* Revision 1.2  2007/03/09 11:20:57  jaume
77
* Advanced symbology (start committing)
78
*
79
* Revision 1.1.2.3  2007/02/21 16:09:02  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.1.2.2  2007/02/21 07:34:09  jaume
83
* labeling starts working
84
*
85
* Revision 1.1.2.1  2007/02/16 10:54:12  jaume
86
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
87
*
88
*
89
*/
90
package org.gvsig.fmap.mapcontext.rendering.symbols;
91

    
92
import java.awt.Color;
93
import java.awt.Graphics2D;
94
import java.awt.Rectangle;
95
import java.awt.geom.AffineTransform;
96
import java.util.ArrayList;
97

    
98
import javax.print.attribute.PrintRequestAttributeSet;
99

    
100
import org.gvsig.data.ReadException;
101
import org.gvsig.fmap.geom.Geometry;
102
import org.gvsig.fmap.mapcontext.MapContext;
103
import org.gvsig.fmap.mapcontext.ViewPort;
104

    
105
import com.iver.utiles.XMLEntity;
106
import com.iver.utiles.swing.threads.Cancellable;
107

    
108
/**
109
 * MultiLayerFillSymbol is a symbol which allows to group several kind of fill symbols
110
 * (xxxFillSymbol implementing IFillSymbol)in one and treats it like single symbol.
111
 */
112

    
113
public class MultiLayerFillSymbol extends AbstractFillSymbol implements IFillSymbol, IMultiLayerSymbol{
114
        private static final double OPACITY_SELECTION_FACTOR = .8;
115
        private IFillSymbol[] layers = new IFillSymbol[0];
116
        private MultiLayerFillSymbol selectionSymbol;
117
        private Object symbolType;
118

    
119
        public Color getFillColor() {
120
                /*
121
                 * a multilayer symbol does not define any color, the color
122
                 * of each layer is defined by the layer itself
123
                 */
124
                return null;
125
        }
126

    
127
        public int getOnePointRgb() {
128
                // will paint only the last layer pixel
129
                return layers[layers.length-1].getOnePointRgb();
130
        }
131

    
132
        public ILineSymbol getOutline() {
133
                /*
134
                 * a multilayer symbol does not define any outline, the outline
135
                 * of each layer is defined by the layer it self
136
                 */
137
                return null;
138
        }
139

    
140
        public boolean isSuitableFor(Geometry geom) {
141
                return geom.getType() == Geometry.TYPES.SURFACE;
142
        }
143

    
144
        public void setFillColor(Color color) {
145
                /*
146
                 * Will apply the color to each layer
147
                 */
148
                for (int i = 0; i < layers.length; i++) {
149
                        layers[i].setFillColor(color);
150
                }
151
        }
152

    
153
        public void setOutline(ILineSymbol outline) {
154
                for (int i = 0; i < layers.length; i++) {
155
                        layers[i].setOutline(null);
156
                }
157
                layers[layers.length-1].setOutline(outline);
158
        }
159

    
160
        public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Cancellable cancel) {
161
                for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < layers.length; i++) {
162
                        layers[i].draw(g, affineTransform, geom, cancel);
163
                }
164
        }
165

    
166
        public void drawInsideRectangle(Graphics2D g,
167
                        AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
168
                for (int i = 0; i < layers.length; i++) {
169
                        layers[i].drawInsideRectangle(g, scaleInstance, r);
170
                }
171
        }
172

    
173
        public void getPixExtentPlus(Geometry geom, float[] distances,
174
                        ViewPort viewPort, int dpi) {
175
                float[] myDistances = new float[] {0,0};
176
                distances[0] = 0;
177
                distances[1] = 0;
178
                for (int i = 0; i < layers.length; i++) {
179
                        layers[i].getPixExtentPlus(geom, myDistances, viewPort, dpi);
180
                        distances[0] = Math.max(myDistances[0], distances[0]);
181
                        distances[1] = Math.max(myDistances[1], distances[1]);
182
                }
183
        }
184

    
185
        public ISymbol getSymbolForSelection() {
186
                if (selectionSymbol == null) {
187
                        selectionSymbol = new MultiLayerFillSymbol();
188
                        selectionSymbol.setDescription(getDescription());
189
                        selectionSymbol.symbolType = symbolType;
190
                        for (int i = 0; i < layers.length; i++) {
191
                                selectionSymbol.addLayer(layers[i].getSymbolForSelection());
192
                        }
193
                        SimpleFillSymbol selLayer = new SimpleFillSymbol();
194
                        Color c = MapContext.getSelectionColor();
195
                        c = new Color(
196
                                        c.getRed(),
197
                                        c.getGreen(),
198
                                        c.getBlue(),
199
                                        (int) (255*OPACITY_SELECTION_FACTOR));
200
                        selLayer.setFillColor(c);
201
                        selLayer.setOutline(getOutline());
202
                        selectionSymbol.addLayer(selLayer);
203
                }
204
                return selectionSymbol;
205

    
206
        }
207

    
208

    
209
        public int getSymbolType() {
210
                return Geometry.TYPES.SURFACE;
211
        }
212

    
213
        public XMLEntity getXMLEntity() {
214
                XMLEntity xml = new XMLEntity();
215
                xml.putProperty("className", getClassName());
216
                xml.putProperty("desc", getDescription());
217
                xml.putProperty("isShapeVisible", isShapeVisible());
218
                xml.putProperty("referenceSystem", getReferenceSystem());
219
                xml.putProperty("unit", getUnit());
220

    
221
                for (int i = 0; i < layers.length; i++) {
222
                        xml.addChild(layers[i].getXMLEntity());
223
                }
224
                return xml;
225
        }
226

    
227
        public void setPrintingProperties(PrintRequestAttributeSet printProperties) {
228
                // TODO Implement it
229
                throw new Error("Not yet implemented!");
230

    
231
        }
232

    
233
        public String getClassName() {
234
                return getClass().getName();
235
        }
236

    
237
        public void setXMLEntity(XMLEntity xml) {
238
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
239
                setDescription(xml.getStringProperty("desc"));
240
                for (int i = 0; i < xml.getChildrenCount(); i++) {
241
                        addLayer((IFillSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(i), "layer" + i));
242
                }
243

    
244
                if (xml.contains("unit")) { // remove this line when done
245
                // measure unit (for outline)
246
                setUnit(xml.getIntProperty("unit"));
247

    
248
                // reference system (for outline)
249
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
250
                }
251
        }
252

    
253
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintRequestAttributeSet properties)
254
                        throws ReadException {
255
                for (int i = 0; i < layers.length; i++) {
256
                        layers[i].print(g, at, geom, properties);
257
                }
258
        }
259

    
260
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
261
                layers[index] = (IFillSymbol) layer;
262
        }
263

    
264
        public void swapLayers(int index1, int index2) {
265
                ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
266
                layers[index2] = (IFillSymbol) aux1;
267
                layers[index1] = (IFillSymbol) aux2;
268
        }
269

    
270
        public ISymbol getLayer(int layerIndex) {
271
//                try{
272
                        return layers[layerIndex];
273
//                } catch (Exception e) {
274
//                        return null;
275
//                }
276
        }
277

    
278
        public int getLayerCount() {
279
                return layers.length;
280
        }
281

    
282
        public void addLayer(ISymbol newLayer) {
283
                addLayer(newLayer, layers.length);
284
        }
285

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

    
289
                selectionSymbol = null; /* forces the selection symbol to be re-created
290
                                                                  * next time it is required
291
                                                                  */
292
                if (layerIndex < 0 || layers.length < layerIndex)
293
                        throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
294
                ArrayList<ISymbol> newLayers = new ArrayList<ISymbol>();
295
                for (int i = 0; i < layers.length; i++) {
296
                        newLayers.add(layers[i]);
297
                }
298
                try {
299
                        newLayers.add(layerIndex, newLayer);
300
                        layers = (IFillSymbol[]) newLayers.toArray(new IFillSymbol[0]);
301
                } catch (ArrayStoreException asEx) {
302
                        throw new ClassCastException(newLayer.getClassName()+" is not an IFillSymbol");
303
                }
304
        }
305

    
306
        public boolean removeLayer(ISymbol layer) {
307

    
308
                int capacity = 0;
309
                capacity = layers.length;
310
                ArrayList<IFillSymbol> lst = new ArrayList<IFillSymbol>(capacity);
311
                for (int i = 0; i < capacity; i++) {
312
                        lst.add(layers[i]);
313
                }
314
                boolean contains = lst.remove(layer);
315
                layers = (IFillSymbol[])lst.toArray(new IFillSymbol[0]);
316
                return contains;
317
        }
318

    
319
        @Override
320
        public void setUnit(int unitIndex) {
321
                super.setUnit(unitIndex);
322
                for (int i = 0; i < layers.length; i++) {
323
                        layers[i].setUnit(unitIndex);
324
                }
325
        }
326

    
327
        @Override
328
        public void setReferenceSystem(int system) {
329
                super.setReferenceSystem(system);
330
                for (int i = 0; i < layers.length; i++) {
331
                        layers[i].setReferenceSystem(system);
332
                }
333
        }
334

    
335
        /**
336
         *Returns the transparency of the multi layer fill symbol created
337
         */
338
        public int getFillAlpha() {
339
                // will compute the acumulated opacity
340
                double myAlpha = 0;
341
                for (int i = 0; i < layers.length; i++) {
342
                        double layerAlpha = layers[i].getFillAlpha()/255D;
343
                        myAlpha += (1-myAlpha)*layerAlpha;
344
                }
345
                int result = (int) Math.round(myAlpha * 255);
346
                return (result>255) ? 255 : result;
347
        }
348

    
349

    
350
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
351
                double size = 0;
352
                for (int i = 0; i < layers.length; i++) {
353
                        size = Math.max(size, layers[i].toCartographicSize(viewPort, dpi, geom));
354
                }
355
                return size;
356
        }
357
}