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

History | View | Annotate | Download (7.29 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.mapcontext.MapContext;
38
import org.gvsig.fmap.mapcontext.MapContextLocator;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
40
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.Line2DOffset;
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
/**
58
 * SimpleLineSymbol is the most basic symbol for the representation of line objects.
59
 * Allows to define the width of the line, the color and the drawn 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
                Geometry geomToDraw = geom;
92
                g.setStroke(getLineStyle().getStroke());
93

    
94
                if (getLineStyle().getOffset() != 0) {
95
                        double offset = getLineStyle().getOffset();
96
                        try {
97
                                geomToDraw =
98
                                                geomManager.createSurface(Line2DOffset.offsetLine(
99
                                                                geomToDraw.getShape(), offset), SUBTYPES.GEOM2D);
100
                        } catch (CreateGeometryException e) {
101
                                LOG.error("Creating a Surface", e);
102
                        }
103
                }
104
                g.setColor(getColor());
105
                g.draw(geomToDraw.getShape(affineTransform));
106

    
107
                ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
108

    
109
                if (arrowDecorator != null) {
110
                        try {
111
                                arrowDecorator.draw(g, affineTransform, geomToDraw, feature);
112
                        } catch (CreateGeometryException e) {
113
                                LOG.error("Error creating a geometry");
114
                        }
115
                }
116
        }
117

    
118
        public int getOnePointRgb() {
119
                return getColor().getRGB();
120
        }
121

    
122
        public void drawInsideRectangle(Graphics2D g,
123
                        AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
124
                g.setColor(getColor());
125
                g.setStroke(getLineStyle().getStroke());
126
                super.drawInsideRectangle(g, scaleInstance, r, properties);
127
        }
128

    
129
        public void setLineWidth(double width) {
130
                getLineStyle().setLineWidth((float) width);
131
        }
132

    
133
        public double getLineWidth() {
134
                return getLineStyle().getLineWidth();
135
        }
136

    
137
        public Object clone() throws CloneNotSupportedException {
138
                SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
139

    
140
                if (symbolForSelection != null) {
141
                        copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
142
                                        .clone();
143
                }
144

    
145
                return copy;
146
        }
147

    
148
    public void loadFromState(PersistentState state) throws PersistenceException {
149
        // Set parent style properties
150
        super.loadFromState(state);
151

    
152
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
153
    }
154

    
155
    public void saveToState(PersistentState state) throws PersistenceException {
156
        // Save parent fill symbol properties
157
        super.saveToState(state);
158

    
159
        // Save own properties
160
        if (this.symbolForSelection != null){
161
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
162
        }
163
    }
164

    
165

    
166

    
167
        public static class RegisterPersistence implements Callable {
168

    
169
                public Object call() throws Exception {
170
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
171
                        if( manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
172
                                DynStruct definition = manager.addDefinition(
173
                                                SimpleLineSymbol.class,
174
                                                SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
175
                                                SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
176
                                                null,
177
                                                null
178
                                );
179
                                // Extend the LineSymbol base definition
180
                                definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
181

    
182
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
183

    
184
                        }
185
                        return Boolean.TRUE;
186
                }
187

    
188
        }
189

    
190
        public static class RegisterSymbol implements Callable {
191

    
192
                public Object call() throws Exception {
193
                        int[] shapeTypes;
194
                        SymbolManager manager = MapContextLocator.getSymbolManager();
195

    
196
                shapeTypes = new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
197
                        Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
198
                        Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
199
                        Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
200
                manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
201
                    shapeTypes,
202
                    SimpleLineSymbol.class);
203

    
204
                        return Boolean.TRUE;
205
                }
206

    
207
        }
208

    
209
}