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

History | View | Annotate | Download (7.58 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.MapContextLocator;
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.ISimpleFillSymbol;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.persistence.PersistenceManager;
48
import org.gvsig.tools.persistence.PersistentState;
49
import org.gvsig.tools.persistence.exception.PersistenceException;
50
import org.gvsig.tools.task.Cancellable;
51
import org.gvsig.tools.util.Callable;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55

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

    
64
        private static final String SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleFillSymbol";
65
        
66
        private static final Logger LOG = LoggerFactory.getLogger(SimpleFillSymbol.class);
67
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
68

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

    
71
        private SimpleFillSymbol symbolForSelection;
72

    
73
        public ISymbol getSymbolForSelection() {
74
                if (symbolForSelection == null) {
75
                        SimpleFillSymbol selectionSymbol = (SimpleFillSymbol) cloneForSelection();
76
                        if (selectionSymbol != null) {
77
                                selectionSymbol.setHasFill(true);
78
                                setSymbolForSelection(selectionSymbol);
79
                        }
80
                }
81
                return symbolForSelection;
82
        }
83

    
84
        public void draw(Graphics2D g, AffineTransform affineTransform,
85
                        Geometry geom, Feature feature, Cancellable cancel) {
86
                Color c = getFillColor();
87

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

    
97

    
98
        public int getSymbolType() {
99
                return Geometry.TYPES.SURFACE;
100
        }
101

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

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

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

    
128
        public String getClassName() {
129
                return getClass().getName();
130
        }
131

    
132

    
133
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
134
                Color c = getFillColor();
135
                if (c!=null && hasFill()) {
136
                        g.setColor(c);
137
                        g.fill(geom.getShape());
138
                }
139
                if (getOutline() != null && hasOutline()) {
140
                        getOutline().print(g, at, geom, properties);
141
                }
142
        }
143
        
144
        
145
        public Object clone() throws CloneNotSupportedException {
146
                SimpleFillSymbol copy = (SimpleFillSymbol) super.clone();
147

    
148
                // Clone selection
149
                if (symbolForSelection != null) {
150
                        copy.symbolForSelection = (SimpleFillSymbol) symbolForSelection
151
                                        .clone();
152
                }
153

    
154
                return copy;
155
        }
156
        
157
        private void setSymbolForSelection(SimpleFillSymbol symbolForSelection) {
158
                this.symbolForSelection = symbolForSelection;
159
        }
160

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

    
168
        public void saveToState(PersistentState state) throws PersistenceException {
169
                // Save parent fill symbol properties
170
                super.saveToState(state);
171

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

    
177
        public static class RegisterPersistence implements Callable {
178

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

    
190
                                // Extend the FillSymbol base definition
191
                                definition.extend(manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME));
192

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

    
201
        public static class RegisterSymbol implements Callable {
202

    
203
                public Object call() throws Exception {
204
                int[] shapeTypes;
205
                SymbolManager manager = MapContextLocator.getSymbolManager();
206
                
207
                shapeTypes =
208
                    new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE,
209
                        Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE };
210
                manager.registerSymbol(IFillSymbol.SYMBOL_NAME,
211
                    shapeTypes,
212
                    SimpleFillSymbol.class);
213
                
214
                        return Boolean.TRUE;
215
                }
216
                
217
        }
218

    
219
}