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

History | View | Annotate | Download (10 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
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.primitive.Envelope;
35
import org.gvsig.fmap.geom.primitive.Point;
36
import org.gvsig.fmap.mapcontext.MapContext;
37
import org.gvsig.fmap.mapcontext.MapContextLocator;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2;
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.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.dynobject.DynStruct;
46
import org.gvsig.tools.persistence.PersistenceManager;
47
import org.gvsig.tools.persistence.PersistentState;
48
import org.gvsig.tools.persistence.exception.PersistenceException;
49
import org.gvsig.tools.swing.api.TransparencySupport;
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
 * Basic fill symbol. It will allow to paint a shape with its filling color (and
57
 * transparency) and the outline.
58
 *
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
61
 * team
62
 */
63
public class SimpleFillSymbol extends AbstractFillSymbol implements ISimpleFillSymbol, TransparencySupport {
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 SimpleFillSymbol() {
75
        super();
76
    }
77

    
78
    @Override
79
    public ISymbol getSymbolForSelection(Color selectionColor) {
80
        if (symbolForSelection == null) {
81
            SimpleFillSymbol selectionSymbol = (SimpleFillSymbol) cloneForSelection(selectionColor);
82
            if (selectionSymbol != null) {
83
                selectionSymbol.setHasFill(true);
84
                setSymbolForSelection(selectionSymbol);
85
            }
86
        } else {
87
            symbolForSelection.setColor(selectionColor);
88
        }
89
        if (symbolForSelection instanceof TransparencySupport) {
90
            symbolForSelection.setTransparency(this.getTransparency());
91
        }
92

    
93
        return symbolForSelection;
94
    }
95

    
96
    @Override
97
    public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel, Rectangle r) {
98
        if(r != null){
99
            geom = this.getSampleGeometry(r);
100
        }
101
        Color c = getFillColor();
102

    
103
        if (true) {
104
            // Esto deberia ser para optimizar el pintado de geometrias grandes.
105
            try {
106
                Geometry env = geom.getEnvelope().getGeometry();
107
                env.transform(affineTransform);
108
                Envelope env2 = env.getEnvelope();
109
                if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) {
110
                    Point upperCorner = env2.getUpperCorner();
111
                    int y = (int) upperCorner.getY();
112
                    int x = (int) upperCorner.getX();
113
                    if (c != null && hasFill()) {
114
                        g.setColor(c);
115
                        g.drawLine(x, y, x, y);
116
                    }
117
                    if (getOutline() != null && hasOutline()) {
118
                        g.setColor(getOutline().getColor());
119
                        g.drawLine(x, y, x, y);
120
                    }
121
                    return;
122
                }
123
            } catch (Exception ex) {
124
                LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
125
                // Do nothing, continue with the draw of the original geometry
126
            }
127

    
128
        }
129

    
130
        if (c != null && hasFill()) {
131
            g.setColor(c);
132
            g.fill(geom.getShape(affineTransform));
133
        }
134
        if (getOutline() != null && hasOutline()) {
135
            if (getOutline() instanceof ISymbol_v2) {
136
                getOutline().setCartographicContext(this.getCartographicContext());
137
                ((ISymbol_v2)getOutline()).draw(g, affineTransform, geom, f, cancel, null);
138
            } else {
139
                getOutline().draw(g, affineTransform, geom, f, cancel);
140
            }
141
        }
142
    }
143

    
144
    @Override
145
    public int getSymbolType() {
146
        return Geometry.TYPES.SURFACE;
147
    }
148

    
149
    public String getClassName() {
150
        return getClass().getName();
151
    }
152

    
153
    @Override
154
    public Object clone() throws CloneNotSupportedException {
155
        SimpleFillSymbol copy = (SimpleFillSymbol) super.clone();
156

    
157
        // Clone selection
158
        if (symbolForSelection != null) {
159
            copy.symbolForSelection = (SimpleFillSymbol) symbolForSelection
160
                    .clone();
161
        }
162

    
163
        return copy;
164
    }
165

    
166
    private void setSymbolForSelection(SimpleFillSymbol symbolForSelection) {
167
        this.symbolForSelection = symbolForSelection;
168
    }
169

    
170
    @Override
171
    public void loadFromState(PersistentState state)
172
            throws PersistenceException {
173
        // Set parent fill symbol properties
174
        super.loadFromState(state);
175
        setSymbolForSelection((SimpleFillSymbol) state.get(FIELD_SYMBOL_FOR_SELECTION));
176
    }
177

    
178
    @Override
179
    public void saveToState(PersistentState state) throws PersistenceException {
180
        // Save parent fill symbol properties
181
        super.saveToState(state);
182

    
183
        // Don't use the getSymbolForSelection method, as it will create it
184
        // if it does not exist, and persistence will enter an infinite loop
185
        state.set(FIELD_SYMBOL_FOR_SELECTION, symbolForSelection);
186
    }
187

    
188
    @Override
189
    public void setTransparency(double transparency) {
190
        Color c = getFillColor();
191
        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (transparency * 255));
192
        setFillColor(c);
193
        
194
        ILineSymbol theOutline = getOutline();
195
        if (theOutline != null && theOutline instanceof TransparencySupport) {
196
            ((TransparencySupport) theOutline).setTransparency(transparency);
197
        }
198
        ISymbol selectionSymbol = this.symbolForSelection;
199
        if (selectionSymbol != null && selectionSymbol instanceof TransparencySupport) {
200
            ((TransparencySupport) selectionSymbol).setTransparency(transparency);
201
        }
202
    }
203

    
204
    @Override
205
    public void setOutline(ILineSymbol outline) {
206
        super.setOutline(outline);
207
    }
208

    
209
    @Override
210
    public double getTransparency() {
211
        return ((double)getFillAlpha())/255;
212
    }
213

    
214
    public static class RegisterPersistence implements Callable {
215

    
216
        @Override
217
        public Object call() throws Exception {
218
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
219
            if (manager.getDefinition(SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
220
                DynStruct definition = manager.addDefinition(
221
                        SimpleFillSymbol.class,
222
                        SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME,
223
                        SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
224
                        null,
225
                        null
226
                );
227

    
228
                // Extend the FillSymbol base definition
229
                definition.extend(manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME));
230

    
231
                // Selection Symbol
232
                definition.addDynFieldObject(FIELD_SYMBOL_FOR_SELECTION).setClassOfValue(SimpleFillSymbol.class).setMandatory(false);
233
            }
234
            return Boolean.TRUE;
235
        }
236

    
237
    }
238

    
239
    public static class RegisterSymbol implements Callable {
240

    
241
        @Override
242
        public Object call() throws Exception {
243
            int[] shapeTypes;
244
            SymbolManager manager = MapContextLocator.getSymbolManager();
245

    
246
            shapeTypes
247
                    = new int[]{Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE,
248
                        Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE,
249
                        Geometry.TYPES.ELLIPTICARC, Geometry.TYPES.FILLEDSPLINE,
250
                        Geometry.TYPES.MULTIPOLYGON, Geometry.TYPES.POLYGON
251
                    };
252
            manager.registerSymbol(IFillSymbol.SYMBOL_NAME,
253
                    shapeTypes,
254
                    SimpleFillSymbol.class);
255

    
256
            return Boolean.TRUE;
257
        }
258

    
259
    }
260

    
261
}