Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / 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 @ 35472

History | View | Annotate | Download (7.53 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.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DynStruct;
45
import org.gvsig.tools.persistence.PersistenceManager;
46
import org.gvsig.tools.persistence.PersistentState;
47
import org.gvsig.tools.persistence.exception.PersistenceException;
48
import org.gvsig.tools.task.Cancellable;
49
import org.gvsig.tools.util.Callable;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53

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

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

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

    
69
        private SimpleFillSymbol symbolForSelection;
70

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

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

    
94

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

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

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

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

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

    
129

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

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

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

    
158
        public void loadFromState(PersistentState state)
159
                        throws PersistenceException {
160
                // Set parent fill symbol properties
161
                super.loadFromState(state);
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

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

    
174
        public static class RegisterPersistence implements Callable {
175

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

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

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

    
198
        public static class RegisterSymbol implements Callable {
199

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

    
216
}