Statistics
| Revision:

root / tags / v2_0_0_Build_2021 / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / fill / impl / SimpleFillSymbol.java @ 34111

History | View | Annotate | Download (7.59 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl;
23

    
24
import java.awt.Color;
25
import java.awt.Graphics2D;
26
import java.awt.Rectangle;
27
import java.awt.geom.AffineTransform;
28

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

    
52

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

    
61
        private static final String SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleFillSymbol";
62
        
63
        private static final Logger LOG = LoggerFactory.getLogger(SimpleFillSymbol.class);
64
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
65

    
66
    private static final String FIELD_SYMBOL_FOR_SELECTION = "symbolForSelection";
67

    
68
        private SimpleFillSymbol symbolForSelection;
69

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

    
81
        public void draw(Graphics2D g, AffineTransform affineTransform,
82
                        Geometry geom, Feature feature, Cancellable cancel) {
83
                Color c = getFillColor();
84
                if (c!=null && hasFill()) {
85
                        g.setColor(c);
86
                        g.fill(geom);
87
                }
88
                if (getOutline() != null && hasOutline()) {
89
                        getOutline().draw(g, affineTransform, geom, feature, cancel);
90
                }
91
        }
92

    
93

    
94
        public int getSymbolType() {
95
                return Geometry.TYPES.SURFACE;
96
        }
97

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

    
110
                Color c = getFillColor();
111
                if (c != null && hasFill()) {
112
                        g.setColor(c);
113
                        g.fillRect(rect.x, rect.y, rect.width, rect.height);
114
                }
115

    
116
                if (getOutline() != null && hasOutline()) {
117
                        if (properties==null)
118
                                getOutline().draw(g, scaleInstance, geom, null, null);
119
                        else
120
                                print(g, new AffineTransform(), geom, properties);
121
                }
122
        }
123

    
124
        public String getClassName() {
125
                return getClass().getName();
126
        }
127

    
128

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

    
144
                // Clone selection
145
                if (symbolForSelection != null) {
146
                        copy.symbolForSelection = (SimpleFillSymbol) symbolForSelection
147
                                        .clone();
148
                }
149

    
150
                return copy;
151
        }
152
        
153
        private void setSymbolForSelection(SimpleFillSymbol symbolForSelection) {
154
                this.symbolForSelection = symbolForSelection;
155
        }
156

    
157
        public void loadFromState(PersistentState state)
158
                        throws PersistenceException {
159
                // Set parent fill symbol properties
160
                super.loadFromState(state);
161
//                LOG.warn("FIXME, need to implement loadFromState");
162
                 setSymbolForSelection((SimpleFillSymbol)state.get(FIELD_SYMBOL_FOR_SELECTION));
163
        }
164

    
165
        public void saveToState(PersistentState state) throws PersistenceException {
166
                // Save parent fill symbol properties
167
                super.saveToState(state);
168
//                LOG.warn("FIXME, need to implement saveToState");
169

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

    
175
        public static class RegisterPersistence implements Callable {
176

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

    
188
                                // Extend the FillSymbol base definition
189
                                definition.extend(manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME));
190

    
191
                                // Selection Symbol
192
                                // definition.addDynFieldSingle(FIELD_SYMBOL_FOR_SELECTION).setType(
193
                                // DataTypes.OBJECT);
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
}