Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / fill / impl / MultiLayerFillSymbol.java @ 42439

History | View | Annotate | Download (12.9 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl;
25

    
26
import java.awt.Color;
27
import java.awt.Graphics2D;
28
import java.awt.Rectangle;
29
import java.awt.geom.AffineTransform;
30
import java.util.ArrayList;
31
import java.util.List;
32

    
33
import org.gvsig.compat.print.PrintAttributes;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.mapcontext.MapContext;
37
import org.gvsig.fmap.mapcontext.MapContextLocator;
38
import org.gvsig.fmap.mapcontext.ViewPort;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
40
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IMultiLayerFillSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.dynobject.DynStruct;
48
import org.gvsig.tools.persistence.PersistenceManager;
49
import org.gvsig.tools.persistence.PersistentState;
50
import org.gvsig.tools.persistence.exception.PersistenceException;
51
import org.gvsig.tools.task.Cancellable;
52
import org.gvsig.tools.util.Callable;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

    
56

    
57
/**
58
 * MultiLayerFillSymbol is a symbol which allows to group several kind of fill symbols
59
 * (xxxFillSymbol implementing IFillSymbol)in one and treats it like single symbol.
60
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
61
 */
62
public class MultiLayerFillSymbol extends AbstractFillSymbol implements IFillSymbol, IMultiLayerSymbol, IMultiLayerFillSymbol{
63
        private static final Logger LOG = LoggerFactory.getLogger(MultiLayerFillSymbol.class);
64

    
65
    public static final String MULTILAYER_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MultiLayerFillSymbol";
66

    
67
    private static final String FIELD_LAYERS = "layers";
68

    
69
        private static final double OPACITY_SELECTION_FACTOR = .8;
70
        private IFillSymbol[] layers = new IFillSymbol[0];
71
        private MultiLayerFillSymbol selectionSymbol;
72

    
73
        public Color getFillColor() {
74
                /*
75
                 * a multilayer symbol does not define any color, the color
76
                 * of each layer is defined by the layer itself
77
                 */
78
                return null;
79
        }
80

    
81
        public int getOnePointRgb() {
82
                // will paint only the last layer pixel
83
                return layers[layers.length-1].getOnePointRgb();
84
        }
85

    
86
        public ILineSymbol getOutline() {
87
                /*
88
                 * a multilayer symbol does not define any outline, the outline
89
                 * of each layer is defined by the layer it self
90
                 */
91
                return null;
92
        }
93

    
94
        public boolean isSuitableFor(Geometry geom) {
95
                return geom.getType() == Geometry.TYPES.SURFACE;
96
        }
97

    
98
        public void setFillColor(Color color) {
99
                /*
100
                 * Will apply the color to each layer
101
                 */
102
                for (int i = 0; layers != null && i < layers.length; i++) {
103
                        layers[i].setFillColor(color);
104
                }
105
        }
106

    
107
        public void setOutline(ILineSymbol outline) {
108
                if (layers != null && layers.length > 0) {
109
                        for (int i = 0; i < layers.length - 1; i++) {
110
                                layers[i].setOutline(null);
111
                        }
112
                        layers[layers.length - 1].setOutline(outline);
113
                }
114
        }
115

    
116
        public void draw(Graphics2D g, AffineTransform affineTransform,
117
                        Geometry geom, Feature feature, Cancellable cancel) {
118
                for (int i = 0; (cancel == null || !cancel.isCanceled())
119
                                && layers != null && i < layers.length; i++) {
120
                        layers[i].draw(g, affineTransform, geom, feature, cancel);
121
                }
122
        }
123

    
124
        public void drawInsideRectangle(Graphics2D g,
125
                        AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
126
                for (int i = 0; layers != null && i < layers.length; i++) {
127
                        layers[i].drawInsideRectangle(g, scaleInstance, r, properties);
128
                }
129
        }
130

    
131
        public void getPixExtentPlus(Geometry geom, float[] distances,
132
                        ViewPort viewPort, int dpi) {
133
                float[] myDistances = new float[] {0,0};
134
                distances[0] = 0;
135
                distances[1] = 0;
136
                for (int i = 0; layers != null && i < layers.length; i++) {
137
                        layers[i].getPixExtentPlus(geom, myDistances, viewPort, dpi);
138
                        distances[0] = Math.max(myDistances[0], distances[0]);
139
                        distances[1] = Math.max(myDistances[1], distances[1]);
140
                }
141
        }
142

    
143
        public ISymbol getSymbolForSelection() {
144
            // TODO: revisar
145
        Color c = MapContext.getSelectionColor();
146
        c = new Color(
147
                c.getRed(),
148
                c.getGreen(),
149
                c.getBlue(),
150
                (int) (c.getAlpha()));//*OPACITY_SELECTION_FACTOR));
151
        if (selectionSymbol == null) {
152
                            MultiLayerFillSymbol selectionSymbol =
153
                                new MultiLayerFillSymbol();
154
                        selectionSymbol.setDescription(getDescription());
155
                        for (int i = 0; layers != null && i < layers.length; i++) {
156
                                selectionSymbol.addLayer(layers[i].getSymbolForSelection());
157
                        }
158
//                        SimpleFillSymbol selLayer = new SimpleFillSymbol();
159
//
160
//                        selLayer.setFillColor(c);
161
//                        selLayer.setOutline(getOutline());
162
//                        selectionSymbol.addLayer(selLayer);
163
                        setSymbolForSelection(selectionSymbol);
164
                } else {
165
          for (int i = 0; i < selectionSymbol.getLayerCount(); i++) {
166
                selectionSymbol.setLayer(i, selectionSymbol.getLayer(i).getSymbolForSelection());
167
//                if (i==selectionSymbol.getLayerCount()-1){
168
//                    ((SimpleFillSymbol)selectionSymbol.getLayer(i)).setFillColor(c);
169
//                }
170
            }
171
        }
172
                return selectionSymbol;
173

    
174
        }
175

    
176
        public int getSymbolType() {
177
                return Geometry.TYPES.SURFACE;
178
        }
179

    
180
//        public void setPrintingProperties(PrintAttributes printProperties) {
181
//                // TODO Implement it
182
//                throw new Error("Not yet implemented!");
183
//
184
//        }
185

    
186
//        public String getClassName() {
187
//                return getClass().getName();
188
//        }
189

    
190
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
191
                for (int i = 0; layers != null && i < layers.length; i++) {
192
                        layers[i].print(g, at, geom, properties);
193
                }
194
        }
195

    
196
        public void setLayer(int index, ISymbol layer) throws IndexOutOfBoundsException {
197
                layers[index] = (IFillSymbol) layer;
198
        }
199

    
200
        public void swapLayers(int index1, int index2) {
201
                ISymbol aux1 = getLayer(index1), aux2 = getLayer(index2);
202
                layers[index2] = (IFillSymbol) aux1;
203
                layers[index1] = (IFillSymbol) aux2;
204
        }
205

    
206
        public ISymbol getLayer(int layerIndex) {
207
//                try{
208
                        return layers[layerIndex];
209
//                } catch (Exception e) {
210
//                        return null;
211
//                }
212
        }
213

    
214
        public int getLayerCount() {
215
                return layers.length;
216
        }
217

    
218
        public void addLayer(ISymbol newLayer) {
219
                addLayer(newLayer, layers.length);
220
        }
221

    
222
        public void addLayer(ISymbol newLayer, int layerIndex) throws IndexOutOfBoundsException {
223
                if (newLayer == null ) {
224
                        /*|| newLayer instanceof ILabelStyle)*/ return; // null or symbols that are styles are not allowed
225
                }
226

    
227
                selectionSymbol = null; /* forces the selection symbol to be re-created
228
                                                                  * next time it is required
229
                                                                  */
230
                if (layerIndex < 0 || layers.length < layerIndex) {
231
                        throw new IndexOutOfBoundsException(layerIndex+" < 0 or "+layerIndex+" > "+layers.length);
232
                }
233
                List<ISymbol> newLayers = new ArrayList<ISymbol>();
234
                for (int i = 0; i < layers.length; i++) {
235
                        newLayers.add(layers[i]);
236
                }
237
                try {
238
                        newLayers.add(layerIndex, newLayer);
239
                        layers = (IFillSymbol[]) newLayers.toArray(new IFillSymbol[0]);
240
                } catch (ArrayStoreException asEx) {
241
                        throw new ClassCastException(newLayer.getClass().getName() + " is not an IFillSymbol");
242
                }
243
        }
244

    
245
        public boolean removeLayer(ISymbol layer) {
246

    
247
                int capacity = 0;
248
                capacity = layers.length;
249
                List<IFillSymbol> lst = new ArrayList<IFillSymbol>(capacity);
250
                for (int i = 0; layers != null && i < capacity; i++) {
251
                        lst.add(layers[i]);
252
                }
253
                boolean contains = lst.remove(layer);
254
                layers = (IFillSymbol[])lst.toArray(new IFillSymbol[0]);
255
                return contains;
256
        }
257

    
258
        public void setUnit(int unitIndex) {
259
                super.setUnit(unitIndex);
260
                for (int i = 0; layers != null && i < layers.length; i++) {
261
                        layers[i].setUnit(unitIndex);
262
                }
263
        }
264

    
265
        public void setReferenceSystem(int system) {
266
                super.setReferenceSystem(system);
267
                for (int i = 0; layers != null && i < layers.length; i++) {
268
                        layers[i].setReferenceSystem(system);
269
                }
270
        }
271

    
272
        /**
273
         *Returns the transparency of the multi layer fill symbol created
274
         */
275
        public int getFillAlpha() {
276
                // will compute the acumulated opacity
277
                double myAlpha = 0;
278
                for (int i = 0; layers != null && i < layers.length; i++) {
279
                        double layerAlpha = layers[i].getFillAlpha()/255D;
280
                        myAlpha += (1-myAlpha)*layerAlpha;
281
                }
282
                int result = (int) Math.round(myAlpha * 255);
283
                return (result>255) ? 255 : result;
284
        }
285

    
286

    
287
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
288
                double size = 0;
289
                for (int i = 0; layers != null && i < layers.length; i++) {
290
                        size = Math.max(size, layers[i].toCartographicSize(viewPort, dpi, geom));
291
                }
292
                return size;
293
        }
294

    
295
        public Object clone() throws CloneNotSupportedException {
296
                MultiLayerFillSymbol copy = (MultiLayerFillSymbol) super.clone();
297

    
298
                // Clone layers
299
                if (layers != null) {
300
                        IFillSymbol[] layersCopy = new IFillSymbol[layers.length];
301
                        for (int i = 0; i < layers.length; i++) {
302
                                layersCopy[i] = (IFillSymbol) layers[i].clone();
303
                        }
304
                        copy.layers = layersCopy;
305
                }
306

    
307
                // Clone selection
308
                if (selectionSymbol != null) {
309
                        copy.selectionSymbol = (MultiLayerFillSymbol) selectionSymbol
310
                                        .clone();
311
                }
312

    
313
                return copy;
314
        }
315

    
316
        private void setSymbolForSelection(MultiLayerFillSymbol symbolForSelection) {
317
                this.selectionSymbol = symbolForSelection;
318
        }
319

    
320
        @SuppressWarnings("unchecked")
321
        public void loadFromState(PersistentState state)
322
                        throws PersistenceException {
323
                // Set parent fill symbol properties
324
                super.loadFromState(state);
325
                LOG.warn("FIXME, fix implement of loadFromState");
326
                // Set own properties
327
                // setSymbolForSelection((MultiLayerFillSymbol)
328
                // state.get(FIELD_SYMBOL_FOR_SELECTION));
329
                List layers = state.getList(FIELD_LAYERS);
330
                if (layers != null) {
331
                        for (int i = 0; i < layers.size(); i++) {
332
                                addLayer((ISymbol) layers.get(i));
333
                        }
334
                }
335

    
336
        }
337

    
338
        public void saveToState(PersistentState state) throws PersistenceException {
339
                // Save parent fill symbol properties
340
                super.saveToState(state);
341
                LOG.warn("FIXME, fix implement of saveToState");
342
                // Save own properties
343

    
344
                // Don't use the getSymbolForSelection method, as it will create it
345
                // if it does not exist, and persistence will enter an infinite loop
346
                // state.set(FIELD_SYMBOL_FOR_SELECTION, selectionSymbol);
347
                state.set(FIELD_LAYERS, layers);
348
        }
349

    
350
        public static class RegisterPersistence implements Callable {
351

    
352
                public Object call() throws Exception {
353
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
354
                        if( manager.getDefinition(MULTILAYER_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
355
                                DynStruct definition = manager.addDefinition(
356
                                                MultiLayerFillSymbol.class,
357
                                                MULTILAYER_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME,
358
                                                MULTILAYER_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
359
                                                null,
360
                                                null
361
                                );
362

    
363
                                // Extend the FillSymbol base definition
364
                                definition.extend(manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME));
365

    
366
                                // Selection Symbol
367
                                // definition.addDynFieldSingle(FIELD_SYMBOL_FOR_SELECTION).setType(
368
                                // DataTypes.OBJECT);
369

    
370
                                // Layers
371
                                definition.addDynFieldList(FIELD_LAYERS).setClassOfItems(IFillSymbol.class);
372
                        }
373
                        return Boolean.TRUE;
374
                }
375

    
376
        }
377

    
378
        public static class RegisterSymbol implements Callable {
379

    
380
                public Object call() throws Exception {
381
                        int[] shapeTypes;
382
                        SymbolManager manager = MapContextLocator.getSymbolManager();
383

    
384
                        shapeTypes = new int[] { Geometry.TYPES.SURFACE,
385
                                        Geometry.TYPES.CIRCLE, Geometry.TYPES.ELLIPSE,
386
                                        Geometry.TYPES.MULTISURFACE };
387
                        manager.registerMultiLayerSymbol(IFillSymbol.SYMBOL_NAME,
388
                                        shapeTypes, MultiLayerFillSymbol.class);
389

    
390
                        return Boolean.TRUE;
391
                }
392

    
393
        }
394

    
395
}