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

History | View | Annotate | Download (7.76 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
            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
                geom = env2.getUpperCorner();
100
            } 
101
        }
102
        
103
                Geometry geomToDraw = geom;
104
                g.setStroke(getLineStyle().getStroke());
105

    
106
                if (getLineStyle().getOffset() != 0) {
107
                        double offset = getLineStyle().getOffset();
108
                        try {
109
                                geomToDraw =
110
                                                geomManager.createSurface(Line2DOffset.offsetLine(
111
                                                                geomToDraw.getShape(), offset), SUBTYPES.GEOM2D);
112
                        } catch (CreateGeometryException e) {
113
                                LOG.error("Creating a Surface", e);
114
                        }
115
                }
116
                g.setColor(getColor());
117
                g.draw(geomToDraw.getShape(affineTransform));
118

    
119
                ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
120

    
121
                if (arrowDecorator != null) {
122
                        try {
123
                                arrowDecorator.draw(g, affineTransform, geomToDraw, feature);
124
                        } catch (CreateGeometryException e) {
125
                                LOG.error("Error creating a geometry");
126
                        }
127
                }
128
        }
129

    
130
        public int getOnePointRgb() {
131
                return getColor().getRGB();
132
        }
133

    
134
        public void drawInsideRectangle(Graphics2D g,
135
                        AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
136
                g.setColor(getColor());
137
                g.setStroke(getLineStyle().getStroke());
138
                super.drawInsideRectangle(g, scaleInstance, r, properties);
139
        }
140

    
141
        public void setLineWidth(double width) {
142
                getLineStyle().setLineWidth((float) width);
143
        }
144

    
145
        public double getLineWidth() {
146
                return getLineStyle().getLineWidth();
147
        }
148

    
149
        public Object clone() throws CloneNotSupportedException {
150
                SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
151

    
152
                if (symbolForSelection != null) {
153
                        copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
154
                                        .clone();
155
                }
156

    
157
                return copy;
158
        }
159

    
160
    public void loadFromState(PersistentState state) throws PersistenceException {
161
        // Set parent style properties
162
        super.loadFromState(state);
163

    
164
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
165
    }
166

    
167
    public void saveToState(PersistentState state) throws PersistenceException {
168
        // Save parent fill symbol properties
169
        super.saveToState(state);
170

    
171
        // Save own properties
172
        if (this.symbolForSelection != null){
173
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
174
        }
175
    }
176

    
177

    
178

    
179
        public static class RegisterPersistence implements Callable {
180

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

    
194
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
195

    
196
                        }
197
                        return Boolean.TRUE;
198
                }
199

    
200
        }
201

    
202
        public static class RegisterSymbol implements Callable {
203

    
204
                public Object call() throws Exception {
205
                        int[] shapeTypes;
206
                        SymbolManager manager = MapContextLocator.getSymbolManager();
207

    
208
                shapeTypes = new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
209
                        Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
210
                        Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
211
                        Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
212
                manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
213
                    shapeTypes,
214
                    SimpleLineSymbol.class);
215

    
216
                        return Boolean.TRUE;
217
                }
218

    
219
        }
220

    
221
}