Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / fill / impl / SimpleFillSymbol.java @ 32880

History | View | Annotate | Download (8.82 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.GeometryLocator;
33
import org.gvsig.fmap.geom.GeometryManager;
34
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
35
import org.gvsig.fmap.geom.exception.CreateGeometryException;
36
import org.gvsig.fmap.geom.primitive.GeneralPathX;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dynobject.DynStruct;
41
import org.gvsig.tools.persistence.PersistenceManager;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.gvsig.tools.persistence.exception.PersistenceException;
44
import org.gvsig.tools.task.Cancellable;
45
import org.gvsig.utils.XMLEntity;
46
import org.gvsig.utils.XMLException;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50

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

    
60
        public static final String SIMPLE_FILL_SYMBOL_DYNCLASS_NAME = "SimpleFillSymbol";
61
        
62
        private SimpleFillSymbol symbolForSelection;
63
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
64
        private static final Logger logger = 
65
            LoggerFactory.getLogger(SimpleFillSymbol.class);
66

    
67
        public ISymbol getSymbolForSelection() {
68
                if (symbolForSelection == null) {
69
                        SimpleFillSymbol selectionSymbol = (SimpleFillSymbol) cloneForSelection();
70
                        if (selectionSymbol != null) {
71
                                selectionSymbol.setHasFill(true);
72
                                setSymbolForSelection(selectionSymbol);
73
                        }
74
//                        
75
//                        XMLEntity xml=null;
76
//                        try {
77
//                                xml = getXMLEntity();
78
//                        } catch (XMLException e) {
79
//                                // TODO Auto-generated catch block
80
//                                e.printStackTrace();
81
//                        }
82
//                        xml.putProperty("hasFill", true);
83
//                        xml.putProperty("color", StringUtilities.color2String(MapContext.getSelectionColor()));
84
//                        symbolForSelection = (SimpleFillSymbol) SymbologyFactory.
85
//                                        createSymbolFromXML(xml, getDescription()+" version for selection");
86
                }
87
                return symbolForSelection;
88
        }
89

    
90
        public void draw(Graphics2D g, AffineTransform affineTransform,
91
                        Geometry geom, Feature feature, Cancellable cancel) {
92
                Color c = getFillColor();
93
                if (c!=null && hasFill()) {
94
                        g.setColor(c);
95
                        g.fill(geom);
96
                }
97
                if (getOutline() != null && hasOutline()) {
98
                        getOutline().draw(g, affineTransform, geom, feature, cancel);
99
                }
100
        }
101

    
102
        public XMLEntity getXMLEntity() throws XMLException {
103
                XMLEntity xml = new XMLEntity();
104

    
105
//                // the class name
106
//                xml.putProperty("className", getClassName());
107
//
108
//                // color
109
//                Color c = getFillColor();
110
//                if (c!=null) {
111
//                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
112
//                }
113
//
114
//                //has Fill
115
//                xml.putProperty("hasFill", hasFill());
116
//                // description
117
//                xml.putProperty("desc", getDescription());
118
//
119
//                // is shape visible
120
//                xml.putProperty("isShapeVisible", isShapeVisible());
121
//
122
//                xml.putProperty("referenceSystem", getReferenceSystem());
123
//                xml.putProperty("unit", getUnit());
124
//
125
//                // outline
126
//                if (getOutline() != null) {
127
//                        xml.addChild(getOutline().getXMLEntity());
128
//                }
129
//
130
//                //has Outline
131
//                xml.putProperty("hasOutline", hasOutline());
132

    
133
                return xml;
134
        }
135

    
136
        public int getSymbolType() {
137
                return Geometry.TYPES.SURFACE;
138
        }
139

    
140
        public void drawInsideRectangle(Graphics2D g,
141
                        AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
142
                Rectangle rect = new Rectangle(r.x, r.y, r.width, r.height);
143
                rect.setFrame(((int) rect.getMinX())+1, ((int) rect.getMinY())+1, ((int) rect.getWidth())-2, ((int) rect.getHeight())-2);
144
                Geometry geom;
145
                try {
146
                        geom = geomManager.createSurface(new GeneralPathX(rect.getPathIterator(null)), SUBTYPES.GEOM2D);
147
                } catch (CreateGeometryException e) {
148
                        logger.error("Creating a surface", e);
149
                        throw new SymbolDrawingException(getSymbolType());
150
                }
151

    
152
                Color c = getFillColor();
153
                if (c != null && hasFill()) {
154
                        g.setColor(c);
155
                        g.fillRect(rect.x, rect.y, rect.width, rect.height);
156
                }
157

    
158
                if (getOutline() != null && hasOutline()) {
159
                        if (properties==null)
160
                                getOutline().draw(g, scaleInstance, geom, null, null);
161
                        else
162
                                print(g, new AffineTransform(), geom, properties);
163
                }
164
        }
165

    
166
        public String getClassName() {
167
                return getClass().getName();
168
        }
169

    
170
        /**
171
         * TODO: replace with the new Persistence API
172
         */
173
        public void setXMLEntity(XMLEntity xml) {
174
//                // color
175
//                if (xml.contains("color")) {
176
//                        setFillColor(StringUtilities.string2Color(xml.getStringProperty("color")));
177
//                }
178
//
179
//                //has Fill
180
//                if(xml.contains("hasFill")) {
181
//                        setHasFill(xml.getBooleanProperty("hasFill"));
182
//                }
183
//
184
//                // description
185
//                setDescription(xml.getStringProperty("desc"));
186
//
187
//                // is shape visible
188
//                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
189
//
190
//
191
//                // outline
192
//                if (xml.getChildrenCount() > 0) {
193
//                        setOutline((ILineSymbol) SymbologyFactory.createSymbolFromXML(xml.getChild(0), null));
194
//                }
195
//
196
//                if (xml.contains("unit")) { // remove this line when done
197
//                // measure unit (for outline)
198
//                setUnit(xml.getIntProperty("unit"));
199
//
200
//                // reference system (for outline)
201
//                setReferenceSystem(xml.getIntProperty("referenceSystem"));
202
//                }
203
//
204
//                //has Outline
205
//                if(xml.contains("hasOutline")) {
206
//                        setHasOutline(xml.getBooleanProperty("hasOutline"));
207
//                }
208
        }
209

    
210
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
211
                Color c = getFillColor();
212
                if (c!=null && hasFill()) {
213
                        g.setColor(c);
214
                        g.fill(geom);
215
                }
216
                if (getOutline() != null && hasOutline()) {
217
                        getOutline().print(g, at, geom, properties);
218
                }
219
        }
220
        
221
        
222
        public Object clone() throws CloneNotSupportedException {
223
                SimpleFillSymbol copy = (SimpleFillSymbol) super.clone();
224

    
225
                // Clone selection
226
                if (symbolForSelection != null) {
227
                        copy.symbolForSelection = (SimpleFillSymbol) symbolForSelection
228
                                        .clone();
229
                }
230

    
231
                return copy;
232
        }
233
        
234
        private void setSymbolForSelection(SimpleFillSymbol symbolForSelection) {
235
                this.symbolForSelection = symbolForSelection;
236
        }
237

    
238
        public void loadFromState(PersistentState state)
239
                        throws PersistenceException {
240
                // Set parent fill symbol properties
241
                super.loadFromState(state);
242
                // Set own properties
243
                // setSymbolForSelection((SimpleFillSymbol)
244
                // state.get(FIELD_SYMBOL_FOR_SELECTION));
245
        }
246

    
247
        public void saveToState(PersistentState state) throws PersistenceException {
248
                // Save parent fill symbol properties
249
                super.saveToState(state);
250
                // Save own properties
251

    
252
                // Don't use the getSymbolForSelection method, as it will create it
253
                // if it does not exist, and persistence will enter an infinite loop
254
                // state.set(FIELD_SYMBOL_FOR_SELECTION, symbolForSelection);
255
        }
256

    
257
        public static void registerPersistence() {
258
                // Add the SimpleFillSymbol DynClass definition.
259
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
260
                DynStruct definition = manager.addDefinition(
261
                                SimpleFillSymbol.class,
262
                                SIMPLE_FILL_SYMBOL_DYNCLASS_NAME,
263
                                SIMPLE_FILL_SYMBOL_DYNCLASS_NAME+" Persistence definition",
264
                                null, 
265
                                null
266
                );
267

    
268
                // Extend the FillSymbol base definition
269
                definition.extend(FILL_SYMBOL_DYNCLASS_NAME);
270

    
271
                // Selection Symbol
272
                // definition.addDynFieldSingle(FIELD_SYMBOL_FOR_SELECTION).setType(
273
                // DataTypes.OBJECT);
274
        }
275

    
276
}