Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2040 / libraries / 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 @ 37287

History | View | Annotate | Download (7.02 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl;
23

    
24
import java.awt.Graphics2D;
25
import java.awt.Rectangle;
26
import java.awt.geom.AffineTransform;
27

    
28
import org.gvsig.compat.print.PrintAttributes;
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
32
import org.gvsig.fmap.geom.GeometryLocator;
33
import org.gvsig.fmap.geom.GeometryManager;
34
import org.gvsig.fmap.geom.exception.CreateGeometryException;
35
import org.gvsig.fmap.mapcontext.MapContextLocator;
36
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
39
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
40
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
41
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.Line2DOffset;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DynStruct;
45
import org.gvsig.tools.persistence.PersistenceManager;
46
import org.gvsig.tools.persistence.PersistentState;
47
import org.gvsig.tools.persistence.exception.PersistenceException;
48
import org.gvsig.tools.task.Cancellable;
49
import org.gvsig.tools.util.Callable;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53

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

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

    
68
        SimpleLineSymbol symbolForSelection;
69
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
70

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

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

    
86
                Geometry geomToDraw = geom.cloneGeometry();
87
                g.setStroke(getLineStyle().getStroke());
88

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

    
102
                ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
103

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

    
113
        public int getOnePointRgb() {
114
                return getColor().getRGB();
115
        }
116

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

    
124
        public void setLineWidth(double width) {
125
                getLineStyle().setLineWidth((float) width);
126
        }
127

    
128
        public double getLineWidth() {
129
                return getLineStyle().getLineWidth();
130
        }
131

    
132
        public Object clone() throws CloneNotSupportedException {
133
                SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
134

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

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

    
147
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
148
    }
149

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

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

    
160

    
161

    
162
        public static class RegisterPersistence implements Callable {
163

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

    
179
                        }
180
                        return Boolean.TRUE;
181
                }
182
                
183
        }
184

    
185
        public static class RegisterSymbol implements Callable {
186

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

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

    
197
                        return Boolean.TRUE;
198
                }
199
                
200
        }
201

    
202
}