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 @ 42439

History | View | Annotate | Download (7.82 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.GeneralPathX;
39
import org.gvsig.fmap.mapcontext.MapContext;
40
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
43
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol;
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
 * Basic fill symbol. It will allow to paint a shape with its filling color (and transparency) and the outline.
59
 * @author 2005-2008  jaume dominguez faus - jaume.dominguez@iver.es
60
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
61
 */
62
public class SimpleFillSymbol extends AbstractFillSymbol implements ISimpleFillSymbol {
63

    
64

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

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

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

    
72
        private SimpleFillSymbol symbolForSelection;
73

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

    
87
        public void draw(Graphics2D g, AffineTransform affineTransform,
88
                        Geometry geom, Feature feature, Cancellable cancel) {
89
                Color c = getFillColor();
90

    
91
                if (c!=null && hasFill()) {
92
                        g.setColor(c);
93
                        g.fill(geom.getShape(affineTransform));
94
                }
95
                if (getOutline() != null && hasOutline()) {
96
                        getOutline().draw(g, affineTransform, geom, feature, cancel);
97
                }
98
        }
99

    
100

    
101
        public int getSymbolType() {
102
                return Geometry.TYPES.SURFACE;
103
        }
104

    
105
        public void drawInsideRectangle(Graphics2D g,
106
                        AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
107
                Rectangle rect = new Rectangle(r.x, r.y, r.width, r.height);
108
                rect.setFrame(((int) rect.getMinX())+1, ((int) rect.getMinY())+1, ((int) rect.getWidth())-2, ((int) rect.getHeight())-2);
109
                Geometry geom;
110
                try {
111
                        geom = geomManager.createSurface(new GeneralPathX(rect.getPathIterator(null)), SUBTYPES.GEOM2D);
112
                } catch (CreateGeometryException e) {
113
                        LOG.error("Creating a surface", e);
114
                        throw new SymbolDrawingException(getSymbolType());
115
                }
116

    
117
                Color c = getFillColor();
118
                if (c != null && hasFill()) {
119
                        g.setColor(c);
120
                        g.fillRect(rect.x, rect.y, rect.width, rect.height);
121
                }
122

    
123
                if (getOutline() != null && hasOutline()) {
124
                        if (properties==null)
125
                                getOutline().draw(g, scaleInstance, geom, null, null);
126
                        else
127
                                print(g, new AffineTransform(), geom, properties);
128
                }
129
        }
130

    
131
        public String getClassName() {
132
                return getClass().getName();
133
        }
134

    
135

    
136
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
137
                Color c = getFillColor();
138
                if (c!=null && hasFill()) {
139
                        g.setColor(c);
140
                        g.fill(geom.getShape(at));
141
                }
142
                if (getOutline() != null && hasOutline()) {
143
                        getOutline().print(g, at, geom, properties);
144
                }
145
        }
146

    
147

    
148
        public Object clone() throws CloneNotSupportedException {
149
                SimpleFillSymbol copy = (SimpleFillSymbol) super.clone();
150

    
151
                // Clone selection
152
                if (symbolForSelection != null) {
153
                        copy.symbolForSelection = (SimpleFillSymbol) symbolForSelection
154
                                        .clone();
155
                }
156

    
157
                return copy;
158
        }
159

    
160
        private void setSymbolForSelection(SimpleFillSymbol symbolForSelection) {
161
                this.symbolForSelection = symbolForSelection;
162
        }
163

    
164
        public void loadFromState(PersistentState state)
165
                        throws PersistenceException {
166
                // Set parent fill symbol properties
167
                super.loadFromState(state);
168
                 setSymbolForSelection((SimpleFillSymbol)state.get(FIELD_SYMBOL_FOR_SELECTION));
169
        }
170

    
171
        public void saveToState(PersistentState state) throws PersistenceException {
172
                // Save parent fill symbol properties
173
                super.saveToState(state);
174

    
175
                // Don't use the getSymbolForSelection method, as it will create it
176
                // if it does not exist, and persistence will enter an infinite loop
177
                 state.set(FIELD_SYMBOL_FOR_SELECTION, symbolForSelection);
178
        }
179

    
180
        public static class RegisterPersistence implements Callable {
181

    
182
                public Object call() throws Exception {
183
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
184
                        if( manager.getDefinition(SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
185
                                DynStruct definition = manager.addDefinition(
186
                                                SimpleFillSymbol.class,
187
                                                SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME,
188
                                                SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
189
                                                null,
190
                                                null
191
                                );
192

    
193
                                // Extend the FillSymbol base definition
194
                                definition.extend(manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME));
195

    
196
                                // Selection Symbol
197
                                definition.addDynFieldObject(FIELD_SYMBOL_FOR_SELECTION).setClassOfValue(SimpleFillSymbol.class).setMandatory(false);
198
                        }
199
                        return Boolean.TRUE;
200
                }
201

    
202
        }
203

    
204
        public static class RegisterSymbol implements Callable {
205

    
206
                public Object call() throws Exception {
207
                int[] shapeTypes;
208
                SymbolManager manager = MapContextLocator.getSymbolManager();
209

    
210
                shapeTypes =
211
                    new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE,
212
                        Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE,
213
                        Geometry.TYPES.ELLIPTICARC, Geometry.TYPES.FILLEDSPLINE,
214
                    Geometry.TYPES.MULTIPOLYGON
215
                    };
216
                manager.registerSymbol(IFillSymbol.SYMBOL_NAME,
217
                    shapeTypes,
218
                    SimpleFillSymbol.class);
219

    
220
                        return Boolean.TRUE;
221
                }
222

    
223
        }
224

    
225
}