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 / line / impl / SimpleLineSymbol.java @ 44984

History | View | Annotate | Download (8.74 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.line.impl;
25

    
26
import java.awt.Graphics2D;
27
import java.awt.Rectangle;
28
import java.awt.geom.AffineTransform;
29

    
30
import org.gvsig.compat.print.PrintAttributes;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.geom.Geometry;
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.Envelope;
37
import org.gvsig.fmap.geom.primitive.Point;
38
import org.gvsig.fmap.mapcontext.MapContext;
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.line.ILineSymbol;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.dynobject.DynStruct;
48
import org.gvsig.tools.persistence.PersistenceManager;
49
import org.gvsig.tools.persistence.PersistentState;
50
import org.gvsig.tools.persistence.exception.PersistenceException;
51
import org.gvsig.tools.task.Cancellable;
52
import org.gvsig.tools.util.Callable;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

    
56
/**
57
 * SimpleLineSymbol is the most basic symbol for the representation of line
58
 * objects. Allows to define the width of the line, the color and the drawn
59
 * pattern.
60
 *
61
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
62
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
63
 */
64
public class SimpleLineSymbol extends AbstractLineSymbol implements ISimpleLineSymbol {
65

    
66
    private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class);
67
    public static final String SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleLineSymbol";
68

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

    
71
    SimpleLineSymbol symbolForSelection;
72
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
73

    
74
    public SimpleLineSymbol() {
75
        super();
76
        setLineWidth(1d);
77
    }
78

    
79
    public ISymbol getSymbolForSelection() {
80
        if (symbolForSelection == null) {
81
            symbolForSelection = (SimpleLineSymbol) cloneForSelection();
82
        } else {
83
            symbolForSelection.setColor(MapContext.getSelectionColor());
84
        }
85
        return symbolForSelection;
86
    }
87

    
88
    public void draw(Graphics2D g, AffineTransform affineTransform,
89
            Geometry geom, Feature feature, Cancellable cancel) {
90

    
91
        if (true) {
92
            // Esto deberia ser para optimiza el pintado de 
93
            // geometrias grandes.
94
            try {
95
                Geometry env = geom.getEnvelope().getGeometry();
96
                env.transform(affineTransform);
97
                Envelope env2 = env.getEnvelope();
98
                if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) {
99
                    g.setColor(getColor());
100
                    Point upperCorner = env2.getUpperCorner();
101
                    int x = (int) upperCorner.getX();
102
                    int y = (int) upperCorner.getY();
103
                    g.drawLine(x, y, x, y);
104
                    return;
105
                }
106
            } catch (Exception ex) {
107
                LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
108
                // Do nothing, continue with the draw of the original geometry
109
            }
110
        }
111

    
112
        Geometry geomToDraw = geom;
113
        g.setStroke(getLineStyle().getStroke());
114

    
115
        if (getLineStyle()!=null && getLineStyle().getOffset() != 0) {
116
            double offset = getLineStyle().getOffset();
117
            try {
118
                geomToDraw = geomToDraw.offset(offset);
119
            } catch (Exception e) {
120
                LOG.warn("Error creating a polygon with an offset", e);
121
            }
122
        }
123
        g.setColor(getColor());
124
        g.draw(geomToDraw.getShape(affineTransform));
125

    
126
        ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
127

    
128
        if (arrowDecorator != null) {
129
            try {
130
                arrowDecorator.draw(g, affineTransform, geomToDraw, feature);
131
            } catch (CreateGeometryException e) {
132
                LOG.warn("Error drawing geometry.", e);
133
            }
134
        }
135
    }
136

    
137
    public int getOnePointRgb() {
138
        return getColor().getRGB();
139
    }
140

    
141
    public void drawInsideRectangle(Graphics2D g,
142
            AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
143
        g.setColor(getColor());
144
        g.setStroke(getLineStyle().getStroke());
145
        super.drawInsideRectangle(g, scaleInstance, r, properties);
146
    }
147

    
148
    public void setLineWidth(double width) {
149
        getLineStyle().setLineWidth((float) width);
150
    }
151

    
152
    public double getLineWidth() {
153
        return getLineStyle().getLineWidth();
154
    }
155

    
156
    public Object clone() throws CloneNotSupportedException {
157
        SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
158

    
159
        if (symbolForSelection != null) {
160
            copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
161
                    .clone();
162
        }
163

    
164
        return copy;
165
    }
166

    
167
    public void loadFromState(PersistentState state) throws PersistenceException {
168
        // Set parent style properties
169
        super.loadFromState(state);
170

    
171
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
172
    }
173

    
174
    public void saveToState(PersistentState state) throws PersistenceException {
175
        // Save parent fill symbol properties
176
        super.saveToState(state);
177

    
178
        // Save own properties
179
        if (this.symbolForSelection != null) {
180
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
181
        }
182
    }
183

    
184
    public static class RegisterPersistence implements Callable {
185

    
186
        public Object call() throws Exception {
187
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
188
            if (manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
189
                DynStruct definition = manager.addDefinition(
190
                        SimpleLineSymbol.class,
191
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
192
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
193
                        null,
194
                        null
195
                );
196
                // Extend the LineSymbol base definition
197
                definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
198

    
199
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
200

    
201
            }
202
            return Boolean.TRUE;
203
        }
204

    
205
    }
206

    
207
    public static class RegisterSymbol implements Callable {
208

    
209
        public Object call() throws Exception {
210
            int[] shapeTypes;
211
            SymbolManager manager = MapContextLocator.getSymbolManager();
212

    
213
            shapeTypes = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
214
                Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
215
                Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
216
                Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
217
            manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
218
                    shapeTypes,
219
                    SimpleLineSymbol.class);
220

    
221
            return Boolean.TRUE;
222
        }
223

    
224
    }
225

    
226
}