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

History | View | Annotate | Download (8.05 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.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
import org.gvsig.fmap.geom.primitive.Envelope;
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.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

    
70
    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

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

    
89
        public void draw(Graphics2D g, AffineTransform affineTransform,
90
                        Geometry geom, Feature feature, Cancellable cancel) {
91
        
92
        if( true ) { 
93
            // Esto deberia ser para optimiza el pintado de 
94
            // geometrias grandes.
95
            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
        }
107
        
108
                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
                                LOG.warn("Error creating a polygon with an offset", e);
119
                        }
120
                }
121
                g.setColor(getColor());
122
                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
                                LOG.warn("Error drawing geometry.", e);
131
                        }
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

    
165
    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
                                                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
}