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

History | View | Annotate | Download (8.35 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 44545 omartinez
import org.gvsig.fmap.geom.primitive.Point;
39 42439 dmartinezizquierdo
import org.gvsig.fmap.mapcontext.MapContext;
40 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
43
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.Line2DOffset;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.dynobject.DynStruct;
50
import org.gvsig.tools.persistence.PersistenceManager;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53
import org.gvsig.tools.task.Cancellable;
54
import org.gvsig.tools.util.Callable;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57
58
59
/**
60
 * SimpleLineSymbol is the most basic symbol for the representation of line objects.
61
 * Allows to define the width of the line, the color and the drawn pattern.
62
 *
63
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
64
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
65
 */
66
public class SimpleLineSymbol extends AbstractLineSymbol implements ISimpleLineSymbol {
67
68
        private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class);
69
    public static final String SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleLineSymbol";
70 42352 fdiaz
71 40435 jjdelcerro
    private static final String SELECTION_SYMBOL = "symbolForSelection";
72
73
        SimpleLineSymbol symbolForSelection;
74
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
75
76
        public SimpleLineSymbol() {
77
                super();
78
                setLineWidth(1d);
79
        }
80 42352 fdiaz
81 40435 jjdelcerro
        public ISymbol getSymbolForSelection() {
82
                if (symbolForSelection == null) {
83 42352 fdiaz
                        symbolForSelection = (SimpleLineSymbol) cloneForSelection();
84 42439 dmartinezizquierdo
                }else{
85
                    symbolForSelection.setColor(MapContext.getSelectionColor());
86 40435 jjdelcerro
                }
87
                return symbolForSelection;
88
        }
89
90
        public void draw(Graphics2D g, AffineTransform affineTransform,
91
                        Geometry geom, Feature feature, Cancellable cancel) {
92 43377 jjdelcerro
93
        if( true ) {
94
            // Esto deberia ser para optimiza el pintado de
95
            // geometrias grandes.
96 43407 jjdelcerro
            try {
97
                Geometry env = geom.getEnvelope().getGeometry();
98
                env.transform(affineTransform);
99
                Envelope env2 = env.getEnvelope();
100
                if( env2.getLength(0)<1.5 && env2.getLength(1)<1.5 ) {
101 44545 omartinez
                    g.setColor(getColor());
102
                    Point upperCorner = env2.getUpperCorner();
103
                    int x = (int) upperCorner.getX();
104
                    int y = (int) upperCorner.getY();
105
                    g.drawLine(x, y, x, y);
106
                    return;
107
                }
108 43407 jjdelcerro
            } catch(Exception ex) {
109
                                LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
110
                // Do nothing, continue with the draw of the original geometry
111
            }
112 43377 jjdelcerro
        }
113
114 40435 jjdelcerro
                Geometry geomToDraw = geom;
115
                g.setStroke(getLineStyle().getStroke());
116
117
                if (getLineStyle().getOffset() != 0) {
118
                        double offset = getLineStyle().getOffset();
119
                        try {
120
                                geomToDraw =
121
                                                geomManager.createSurface(Line2DOffset.offsetLine(
122
                                                                geomToDraw.getShape(), offset), SUBTYPES.GEOM2D);
123
                        } catch (CreateGeometryException e) {
124 43407 jjdelcerro
                                LOG.warn("Error creating a polygon with an offset", e);
125 40435 jjdelcerro
                        }
126
                }
127 42352 fdiaz
                g.setColor(getColor());
128 44545 omartinez
                g.draw(geomToDraw.getShape(affineTransform));
129 40435 jjdelcerro
130
                ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
131
132
                if (arrowDecorator != null) {
133
                        try {
134
                                arrowDecorator.draw(g, affineTransform, geomToDraw, feature);
135
                        } catch (CreateGeometryException e) {
136 43407 jjdelcerro
                                LOG.warn("Error drawing geometry.", e);
137 40435 jjdelcerro
                        }
138
                }
139
        }
140
141
        public int getOnePointRgb() {
142
                return getColor().getRGB();
143
        }
144
145
        public void drawInsideRectangle(Graphics2D g,
146
                        AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
147
                g.setColor(getColor());
148
                g.setStroke(getLineStyle().getStroke());
149
                super.drawInsideRectangle(g, scaleInstance, r, properties);
150
        }
151
152
        public void setLineWidth(double width) {
153
                getLineStyle().setLineWidth((float) width);
154
        }
155
156
        public double getLineWidth() {
157
                return getLineStyle().getLineWidth();
158
        }
159
160
        public Object clone() throws CloneNotSupportedException {
161
                SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
162
163
                if (symbolForSelection != null) {
164
                        copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
165
                                        .clone();
166
                }
167
168
                return copy;
169
        }
170 42352 fdiaz
171 40435 jjdelcerro
    public void loadFromState(PersistentState state) throws PersistenceException {
172
        // Set parent style properties
173
        super.loadFromState(state);
174
175
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
176
    }
177
178
    public void saveToState(PersistentState state) throws PersistenceException {
179
        // Save parent fill symbol properties
180
        super.saveToState(state);
181
182
        // Save own properties
183
        if (this.symbolForSelection != null){
184
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
185
        }
186
    }
187
188
189
190
        public static class RegisterPersistence implements Callable {
191
192
                public Object call() throws Exception {
193
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
194
                        if( manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
195
                                DynStruct definition = manager.addDefinition(
196
                                                SimpleLineSymbol.class,
197
                                                SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
198
                                                SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
199 42352 fdiaz
                                                null,
200 40435 jjdelcerro
                                                null
201
                                );
202
                                // Extend the LineSymbol base definition
203
                                definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
204 42352 fdiaz
205 40435 jjdelcerro
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
206
207
                        }
208
                        return Boolean.TRUE;
209
                }
210 42352 fdiaz
211 40435 jjdelcerro
        }
212
213
        public static class RegisterSymbol implements Callable {
214
215
                public Object call() throws Exception {
216
                        int[] shapeTypes;
217
                        SymbolManager manager = MapContextLocator.getSymbolManager();
218
219
                shapeTypes = new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
220 42352 fdiaz
                        Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
221
                        Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
222
                        Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
223 40435 jjdelcerro
                manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
224
                    shapeTypes,
225
                    SimpleLineSymbol.class);
226
227
                        return Boolean.TRUE;
228
                }
229 42352 fdiaz
230 40435 jjdelcerro
        }
231 44545 omartinez
232 40435 jjdelcerro
}