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

History | View | Annotate | Download (8.05 KB)

1 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40560 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40560 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40560 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40560 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40560 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40560 jjdelcerro
 *
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 40435 jjdelcerro
 */
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.Geometry.SUBTYPES;
34
import org.gvsig.fmap.geom.GeometryLocator;
35
import org.gvsig.fmap.geom.GeometryManager;
36
import org.gvsig.fmap.geom.exception.CreateGeometryException;
37 43377 jjdelcerro
import org.gvsig.fmap.geom.primitive.Envelope;
38 42439 dmartinezizquierdo
import org.gvsig.fmap.mapcontext.MapContext;
39 40435 jjdelcerro
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.symbology.fmap.mapcontext.rendering.symbol.style.Line2DOffset;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.dynobject.DynStruct;
49
import org.gvsig.tools.persistence.PersistenceManager;
50
import org.gvsig.tools.persistence.PersistentState;
51
import org.gvsig.tools.persistence.exception.PersistenceException;
52
import org.gvsig.tools.task.Cancellable;
53
import org.gvsig.tools.util.Callable;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56
57
58
/**
59
 * SimpleLineSymbol is the most basic symbol for the representation of line objects.
60
 * Allows to define the width of the line, the color and the drawn pattern.
61
 *
62
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
63
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
64
 */
65
public class SimpleLineSymbol extends AbstractLineSymbol implements ISimpleLineSymbol {
66
67
        private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class);
68
    public static final String SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleLineSymbol";
69 42352 fdiaz
70 40435 jjdelcerro
    private static final String SELECTION_SYMBOL = "symbolForSelection";
71
72
        SimpleLineSymbol symbolForSelection;
73
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
74
75
        public SimpleLineSymbol() {
76
                super();
77
                setLineWidth(1d);
78
        }
79 42352 fdiaz
80 40435 jjdelcerro
        public ISymbol getSymbolForSelection() {
81
                if (symbolForSelection == null) {
82 42352 fdiaz
                        symbolForSelection = (SimpleLineSymbol) cloneForSelection();
83 42439 dmartinezizquierdo
                }else{
84
                    symbolForSelection.setColor(MapContext.getSelectionColor());
85 40435 jjdelcerro
                }
86
                return symbolForSelection;
87
        }
88
89
        public void draw(Graphics2D g, AffineTransform affineTransform,
90
                        Geometry geom, Feature feature, Cancellable cancel) {
91 43377 jjdelcerro
92
        if( true ) {
93
            // Esto deberia ser para optimiza el pintado de
94
            // geometrias grandes.
95 43407 jjdelcerro
            try {
96
                Geometry env = geom.getEnvelope().getGeometry();
97
                env.transform(affineTransform);
98
                Envelope env2 = env.getEnvelope();
99
                if( env2.getLength(0)<1.5 && env2.getLength(1)<1.5 ) {
100
                    geom = env2.getUpperCorner();
101
                }
102
            } catch(Exception ex) {
103
                                LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
104
                // Do nothing, continue with the draw of the original geometry
105
            }
106 43377 jjdelcerro
        }
107
108 40435 jjdelcerro
                Geometry geomToDraw = geom;
109
                g.setStroke(getLineStyle().getStroke());
110
111
                if (getLineStyle().getOffset() != 0) {
112
                        double offset = getLineStyle().getOffset();
113
                        try {
114
                                geomToDraw =
115
                                                geomManager.createSurface(Line2DOffset.offsetLine(
116
                                                                geomToDraw.getShape(), offset), SUBTYPES.GEOM2D);
117
                        } catch (CreateGeometryException e) {
118 43407 jjdelcerro
                                LOG.warn("Error creating a polygon with an offset", e);
119 40435 jjdelcerro
                        }
120
                }
121 42352 fdiaz
                g.setColor(getColor());
122 40435 jjdelcerro
                g.draw(geomToDraw.getShape(affineTransform));
123
124
                ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
125
126
                if (arrowDecorator != null) {
127
                        try {
128
                                arrowDecorator.draw(g, affineTransform, geomToDraw, feature);
129
                        } catch (CreateGeometryException e) {
130 43407 jjdelcerro
                                LOG.warn("Error drawing geometry.", e);
131 40435 jjdelcerro
                        }
132
                }
133
        }
134
135
        public int getOnePointRgb() {
136
                return getColor().getRGB();
137
        }
138
139
        public void drawInsideRectangle(Graphics2D g,
140
                        AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
141
                g.setColor(getColor());
142
                g.setStroke(getLineStyle().getStroke());
143
                super.drawInsideRectangle(g, scaleInstance, r, properties);
144
        }
145
146
        public void setLineWidth(double width) {
147
                getLineStyle().setLineWidth((float) width);
148
        }
149
150
        public double getLineWidth() {
151
                return getLineStyle().getLineWidth();
152
        }
153
154
        public Object clone() throws CloneNotSupportedException {
155
                SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
156
157
                if (symbolForSelection != null) {
158
                        copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
159
                                        .clone();
160
                }
161
162
                return copy;
163
        }
164 42352 fdiaz
165 40435 jjdelcerro
    public void loadFromState(PersistentState state) throws PersistenceException {
166
        // Set parent style properties
167
        super.loadFromState(state);
168
169
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
170
    }
171
172
    public void saveToState(PersistentState state) throws PersistenceException {
173
        // Save parent fill symbol properties
174
        super.saveToState(state);
175
176
        // Save own properties
177
        if (this.symbolForSelection != null){
178
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
179
        }
180
    }
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 42352 fdiaz
                                                null,
194 40435 jjdelcerro
                                                null
195
                                );
196
                                // Extend the LineSymbol base definition
197
                                definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
198 42352 fdiaz
199 40435 jjdelcerro
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
200
201
                        }
202
                        return Boolean.TRUE;
203
                }
204 42352 fdiaz
205 40435 jjdelcerro
        }
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 42352 fdiaz
                        Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
215
                        Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
216
                        Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
217 40435 jjdelcerro
                manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
218
                    shapeTypes,
219
                    SimpleLineSymbol.class);
220
221
                        return Boolean.TRUE;
222
                }
223 42352 fdiaz
224 40435 jjdelcerro
        }
225
226
}