Statistics
| Revision:

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

History | View | Annotate | Download (8.52 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 12728 2007-07-23 06:58:42Z jaume $
45
* $Log$
46
* Revision 1.8  2007-07-23 06:52:25  jaume
47
* default selection color refactored, moved to MapContext
48
*
49
* Revision 1.7  2007/07/03 10:58:29  jaume
50
* first refactor on CartographicSupport
51
*
52
* Revision 1.6  2007/06/29 13:07:01  jaume
53
* +PictureLineSymbol
54
*
55
* Revision 1.5  2007/03/29 16:02:01  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.4  2007/03/26 14:25:29  jaume
59
* implemented Print
60
*
61
* Revision 1.3  2007/03/13 16:58:36  jaume
62
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
63
*
64
* Revision 1.2  2007/03/09 11:20:57  jaume
65
* Advanced symbology (start committing)
66
*
67
* Revision 1.1.2.3  2007/02/21 16:09:02  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.1.2.2  2007/02/21 07:34:09  jaume
71
* labeling starts working
72
*
73
* Revision 1.1.2.1  2007/02/16 10:54:12  jaume
74
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
75
*
76
*
77
*/
78
package com.iver.cit.gvsig.fmap.core.symbols;
79

    
80
import java.awt.Color;
81
import java.awt.Graphics2D;
82
import java.awt.Rectangle;
83
import java.awt.Shape;
84
import java.awt.geom.AffineTransform;
85
import java.util.ArrayList;
86

    
87
import javax.print.attribute.PrintRequestAttributeSet;
88

    
89
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
90
import com.iver.cit.gvsig.fmap.MapContext;
91
import com.iver.cit.gvsig.fmap.core.FShape;
92
import com.iver.cit.gvsig.fmap.core.IGeometry;
93
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
94
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
95
import com.iver.utiles.XMLEntity;
96

    
97
public class MultiLayerFillSymbol extends AbstractFillSymbol implements IFillSymbol, IMultiLayerSymbol{
98
        private static final double OPACITY_SELECTION_FACTOR = .8;
99
        private IFillSymbol[] layers = new IFillSymbol[0];
100
        private MultiLayerFillSymbol selectionSymbol;
101
        private Object symbolType;
102

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

    
111
        public int getOnePointRgb() {
112
                // will paint only the last layer pixel
113
                return layers[layers.length-1].getOnePointRgb();
114
        }
115

    
116
        public ILineSymbol getOutline() {
117
                /*
118
                 * a multilayer symbol does not define any outline, the outline
119
                 * of each layer is defined by the layer it self
120
                 */
121
                return null;
122
        }
123

    
124
        public boolean isSuitableFor(IGeometry geom) {
125
                return geom.getGeometryType() == FShape.POLYGON;
126
        }
127

    
128
        public void setFillColor(Color color) {
129
                /*
130
                 * Will apply the color to each layer
131
                 */
132
                for (int i = 0; i < layers.length; i++) {
133
                        layers[i].setFillColor(color);
134
                }
135
        }
136

    
137
        public void setOutline(ILineSymbol outline) {
138
                for (int i = 0; i < layers.length; i++) {
139
                        layers[i].setOutline(null);
140
                }
141
                layers[layers.length-1].setOutline(outline);
142
        }
143

    
144
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
145
                for (int i = 0; i < layers.length; i++) {
146
                        layers[i].draw(g, affineTransform, shp);
147
                }
148
        }
149

    
150
        public void drawInsideRectangle(Graphics2D g,
151
                        AffineTransform scaleInstance, Rectangle r) {
152
                for (int i = 0; i < layers.length; i++) {
153
                        layers[i].drawInsideRectangle(g, scaleInstance, r);
154
                }
155
        }
156

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

    
162
        }
163

    
164
        public ISymbol getSymbolForSelection() {
165
                if (selectionSymbol == null) {
166
                        selectionSymbol = new MultiLayerFillSymbol();
167
                        selectionSymbol.setDescription(getDescription());
168
                        selectionSymbol.symbolType = symbolType;
169
                        for (int i = 0; i < layers.length; i++) {
170
                                selectionSymbol.addLayer(layers[i].getSymbolForSelection());
171
                        }
172
                        SimpleFillSymbol selLayer = new SimpleFillSymbol();
173
                        Color c = MapContext.getSelectionColor();
174
                        c = new Color(
175
                                        c.getRed(),
176
                                        c.getGreen(),
177
                                        c.getBlue(),
178
                                        (int) (255*OPACITY_SELECTION_FACTOR));
179
                        selLayer.setFillColor(c);
180
                        selLayer.setOutline(getOutline());
181
                        selectionSymbol.addLayer(selLayer);
182
                }
183
                return selectionSymbol;
184

    
185
        }
186

    
187

    
188
        public int getSymbolType() {
189
                return FShape.POLYGON;
190
        }
191

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

    
203
        public void setPrintingProperties(PrintRequestAttributeSet printProperties) {
204
                // TODO Implement it
205
                throw new Error("Not yet implemented!");
206

    
207
        }
208

    
209
        public String getClassName() {
210
                return getClass().getName();
211
        }
212

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

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

    
229
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
230
                layers[index] = (IFillSymbol) layer;
231
        }
232

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

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

    
247
        public int getLayerCount() {
248
                return layers.length;
249
        }
250

    
251
        public void addLayer(ISymbol newLayer) {
252
                if (newLayer == null) return;
253

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

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

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

    
282
        public boolean removeLayer(ISymbol layer) {
283

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

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

    
306
}