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

History | View | Annotate | Download (7.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.mapcontext.MapContextLocator;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
40
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
41
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.Line2DOffset;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.persistence.PersistenceManager;
48
import org.gvsig.tools.persistence.PersistentState;
49
import org.gvsig.tools.persistence.exception.PersistenceException;
50
import org.gvsig.tools.task.Cancellable;
51
import org.gvsig.tools.util.Callable;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55

    
56
/**
57
 * SimpleLineSymbol is the most basic symbol for the representation of line objects.
58
 * Allows to define the width of the line, the color and the drawn pattern.
59
 *
60
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
61
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
62
 */
63
public class SimpleLineSymbol extends AbstractLineSymbol implements ISimpleLineSymbol {
64

    
65
        private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class);
66
    public static final String SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleLineSymbol";
67
  
68
    private static final String SELECTION_SYMBOL = "symbolForSelection";
69

    
70
        SimpleLineSymbol symbolForSelection;
71
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
72

    
73
        public SimpleLineSymbol() {
74
                super();
75
                setLineWidth(1d);
76
        }
77
        
78
        public ISymbol getSymbolForSelection() {
79
                if (symbolForSelection == null) {
80
                        symbolForSelection = (SimpleLineSymbol) cloneForSelection();                        
81
                }
82
                return symbolForSelection;
83
        }
84

    
85
        public void draw(Graphics2D g, AffineTransform affineTransform,
86
                        Geometry geom, Feature feature, Cancellable cancel) {
87

    
88
                Geometry geomToDraw = geom;
89
                g.setStroke(getLineStyle().getStroke());
90

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

    
104
                ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
105

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

    
115
        public int getOnePointRgb() {
116
                return getColor().getRGB();
117
        }
118

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

    
126
        public void setLineWidth(double width) {
127
                getLineStyle().setLineWidth((float) width);
128
        }
129

    
130
        public double getLineWidth() {
131
                return getLineStyle().getLineWidth();
132
        }
133

    
134
        public Object clone() throws CloneNotSupportedException {
135
                SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
136

    
137
                if (symbolForSelection != null) {
138
                        copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
139
                                        .clone();
140
                }
141

    
142
                return copy;
143
        }
144
        
145
    public void loadFromState(PersistentState state) throws PersistenceException {
146
        // Set parent style properties
147
        super.loadFromState(state);
148

    
149
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
150
    }
151

    
152
    public void saveToState(PersistentState state) throws PersistenceException {
153
        // Save parent fill symbol properties
154
        super.saveToState(state);
155

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

    
162

    
163

    
164
        public static class RegisterPersistence implements Callable {
165

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

    
181
                        }
182
                        return Boolean.TRUE;
183
                }
184
                
185
        }
186

    
187
        public static class RegisterSymbol implements Callable {
188

    
189
                public Object call() throws Exception {
190
                        int[] shapeTypes;
191
                        SymbolManager manager = MapContextLocator.getSymbolManager();
192

    
193
                shapeTypes = new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
194
                        Geometry.TYPES.ELLIPTICARC, Geometry.TYPES.MULTICURVE };
195
                manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
196
                    shapeTypes,
197
                    SimpleLineSymbol.class);
198

    
199
                        return Boolean.TRUE;
200
                }
201
                
202
        }
203

    
204
}