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 / SimpleFillSymbol.java @ 43377

History | View | Annotate | Download (8.3 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

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

    
57

    
58
/**
59
 * Basic fill symbol. It will allow to paint a shape with its filling color (and transparency) and the outline.
60
 * @author 2005-2008  jaume dominguez faus - jaume.dominguez@iver.es
61
 * @author 2009-     <a href="cordinyana@gvsig.org">César Ordiñana</a> - gvSIG team
62
 */
63
public class SimpleFillSymbol extends AbstractFillSymbol implements ISimpleFillSymbol {
64

    
65

    
66
        private static final String SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleFillSymbol";
67

    
68
        private static final Logger LOG = LoggerFactory.getLogger(SimpleFillSymbol.class);
69
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
70

    
71
    private static final String FIELD_SYMBOL_FOR_SELECTION = "symbolForSelection";
72

    
73
        private SimpleFillSymbol symbolForSelection;
74

    
75
        public ISymbol getSymbolForSelection() {
76
                if (symbolForSelection == null) {
77
                        SimpleFillSymbol selectionSymbol = (SimpleFillSymbol) cloneForSelection();
78
                        if (selectionSymbol != null) {
79
                                selectionSymbol.setHasFill(true);
80
                                setSymbolForSelection(selectionSymbol);
81
                        }
82
                } else {
83
                    symbolForSelection.setColor(MapContext.getSelectionColor());
84
                }
85
                return symbolForSelection;
86
        }
87

    
88
        public void draw(Graphics2D g, AffineTransform affineTransform,
89
                        Geometry geom, Feature feature, Cancellable cancel) {
90
                Color c = getFillColor();
91
        
92
        if( true ) { 
93
            // Esto deberia ser para optimiza el pintado de 
94
            // geometrias grandes.
95
            Geometry env = geom.getEnvelope().getGeometry();
96
            env.transform(affineTransform);
97
            Envelope env2 = env.getEnvelope();
98
            if( env2.getLength(0)<1.5 && env2.getLength(1)<1.5 ) {
99
                geom = env2.getUpperCorner();
100
            } 
101
        }
102
        
103
                if (c!=null && hasFill()) {
104
                        g.setColor(c);
105
                        g.fill(geom.getShape(affineTransform));
106
                }
107
                if (getOutline() != null && hasOutline()) {
108
                        getOutline().draw(g, affineTransform, geom, feature, cancel);
109
                }
110
        }
111

    
112

    
113
        public int getSymbolType() {
114
                return Geometry.TYPES.SURFACE;
115
        }
116

    
117
        public void drawInsideRectangle(Graphics2D g,
118
                        AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
119
                Rectangle rect = new Rectangle(r.x, r.y, r.width, r.height);
120
                rect.setFrame(((int) rect.getMinX())+1, ((int) rect.getMinY())+1, ((int) rect.getWidth())-2, ((int) rect.getHeight())-2);
121
                Geometry geom;
122
                try {
123
                        geom = geomManager.createSurface(new GeneralPathX(rect.getPathIterator(null)), SUBTYPES.GEOM2D);
124
                } catch (CreateGeometryException e) {
125
                        LOG.error("Creating a surface", e);
126
                        throw new SymbolDrawingException(getSymbolType());
127
                }
128

    
129
                Color c = getFillColor();
130
                if (c != null && hasFill()) {
131
                        g.setColor(c);
132
                        g.fillRect(rect.x, rect.y, rect.width, rect.height);
133
                }
134

    
135
                if (getOutline() != null && hasOutline()) {
136
                        if (properties==null)
137
                                getOutline().draw(g, scaleInstance, geom, null, null);
138
                        else
139
                                print(g, new AffineTransform(), geom, properties);
140
                }
141
        }
142

    
143
        public String getClassName() {
144
                return getClass().getName();
145
        }
146

    
147

    
148
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
149
                Color c = getFillColor();
150
                if (c!=null && hasFill()) {
151
                        g.setColor(c);
152
                        g.fill(geom.getShape(at));
153
                }
154
                if (getOutline() != null && hasOutline()) {
155
                        getOutline().print(g, at, geom, properties);
156
                }
157
        }
158

    
159

    
160
        public Object clone() throws CloneNotSupportedException {
161
                SimpleFillSymbol copy = (SimpleFillSymbol) super.clone();
162

    
163
                // Clone selection
164
                if (symbolForSelection != null) {
165
                        copy.symbolForSelection = (SimpleFillSymbol) symbolForSelection
166
                                        .clone();
167
                }
168

    
169
                return copy;
170
        }
171

    
172
        private void setSymbolForSelection(SimpleFillSymbol symbolForSelection) {
173
                this.symbolForSelection = symbolForSelection;
174
        }
175

    
176
        public void loadFromState(PersistentState state)
177
                        throws PersistenceException {
178
                // Set parent fill symbol properties
179
                super.loadFromState(state);
180
                 setSymbolForSelection((SimpleFillSymbol)state.get(FIELD_SYMBOL_FOR_SELECTION));
181
        }
182

    
183
        public void saveToState(PersistentState state) throws PersistenceException {
184
                // Save parent fill symbol properties
185
                super.saveToState(state);
186

    
187
                // Don't use the getSymbolForSelection method, as it will create it
188
                // if it does not exist, and persistence will enter an infinite loop
189
                 state.set(FIELD_SYMBOL_FOR_SELECTION, symbolForSelection);
190
        }
191

    
192
        public static class RegisterPersistence implements Callable {
193

    
194
                public Object call() throws Exception {
195
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
196
                        if( manager.getDefinition(SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
197
                                DynStruct definition = manager.addDefinition(
198
                                                SimpleFillSymbol.class,
199
                                                SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME,
200
                                                SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
201
                                                null,
202
                                                null
203
                                );
204

    
205
                                // Extend the FillSymbol base definition
206
                                definition.extend(manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME));
207

    
208
                                // Selection Symbol
209
                                definition.addDynFieldObject(FIELD_SYMBOL_FOR_SELECTION).setClassOfValue(SimpleFillSymbol.class).setMandatory(false);
210
                        }
211
                        return Boolean.TRUE;
212
                }
213

    
214
        }
215

    
216
        public static class RegisterSymbol implements Callable {
217

    
218
                public Object call() throws Exception {
219
                int[] shapeTypes;
220
                SymbolManager manager = MapContextLocator.getSymbolManager();
221

    
222
                shapeTypes =
223
                    new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE,
224
                        Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE,
225
                        Geometry.TYPES.ELLIPTICARC, Geometry.TYPES.FILLEDSPLINE,
226
                    Geometry.TYPES.MULTIPOLYGON
227
                    };
228
                manager.registerSymbol(IFillSymbol.SYMBOL_NAME,
229
                    shapeTypes,
230
                    SimpleFillSymbol.class);
231

    
232
                        return Boolean.TRUE;
233
                }
234

    
235
        }
236

    
237
}