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

History | View | Annotate | Download (9.94 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.Color;
27
import java.awt.Graphics2D;
28
import java.awt.Rectangle;
29
import java.awt.Shape;
30
import java.awt.geom.AffineTransform;
31

    
32
import org.gvsig.compat.print.PrintAttributes;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
37
import org.gvsig.fmap.geom.exception.CreateGeometryException;
38
import org.gvsig.fmap.geom.primitive.Envelope;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.fmap.mapcontext.MapContext;
41
import org.gvsig.fmap.mapcontext.MapContextLocator;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
43
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
44
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.dynobject.DynStruct;
50
import org.gvsig.tools.persistence.PersistenceManager;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53
import org.gvsig.tools.swing.api.TransparencySupport;
54
import org.gvsig.tools.task.Cancellable;
55
import org.gvsig.tools.util.Callable;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

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

    
69
    private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class);
70
    public static final String SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleLineSymbol";
71

    
72
    private static final String SELECTION_SYMBOL = "symbolForSelection";
73

    
74
    SimpleLineSymbol symbolForSelection;
75
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
76
    private double transparency;
77

    
78
    public SimpleLineSymbol() {
79
        super();
80
        setLineWidth(1d);
81
        this.transparency = 1.0;
82
    }
83

    
84
    @Override
85
    public ISymbol getSymbolForSelection() {
86
        if (symbolForSelection == null) {
87
            symbolForSelection = (SimpleLineSymbol) cloneForSelection();
88
        } else {
89
            symbolForSelection.setColor(MapContext.getSelectionColor());
90
        }
91
        if (symbolForSelection instanceof TransparencySupport) {
92
            symbolForSelection.setTransparency(this.transparency);
93
        }
94
        return symbolForSelection;
95
    }
96

    
97
    public void draw(Graphics2D g, AffineTransform affineTransform,
98
            Geometry geom, Feature feature, Cancellable cancel) {
99

    
100
        Color c = getColor();
101
        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (this.transparency * c.getAlpha()));
102
        if (true) {
103
            // Esto deberia ser para optimiza el pintado de 
104
            // geometrias grandes.
105
            try {
106
                Geometry env = geom.getEnvelope().getGeometry();
107
                env.transform(affineTransform);
108
                Envelope env2 = env.getEnvelope();
109
                if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) {
110
                    g.setColor(c);
111
                    Point upperCorner = env2.getUpperCorner();
112
                    int x = (int) upperCorner.getX();
113
                    int y = (int) upperCorner.getY();
114
                    g.drawLine(x, y, x, y);
115
                    return;
116
                }
117
            } catch (Exception ex) {
118
                LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
119
                // Do nothing, continue with the draw of the original geometry
120
            }
121
        }
122

    
123
        Geometry geomToDraw = geom;
124
        g.setStroke(getLineStyle().getStroke());
125

    
126
        if (getLineStyle() != null && getLineStyle().getOffset() != 0) {
127
            double offset = getLineStyle().getOffset();
128
            try {
129
                geomToDraw = geomToDraw.offset(offset);
130
            } catch (Exception e) {
131
                LOG.warn("Error creating a polygon with an offset", e);
132
            }
133
        }
134
        if(geomToDraw != null) {
135
            g.setColor(c);
136
            Shape s = geomToDraw.getShape(affineTransform);
137
            if (s != null) {
138
                g.draw(s);
139
                if (getLineStyle() != null) {
140
                    ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
141

    
142
                    if (arrowDecorator != null) {
143
                        try {
144
                            arrowDecorator.draw(g, affineTransform, geomToDraw, feature);
145
                        } catch (CreateGeometryException e) {
146
                            LOG.warn("Error drawing geometry.", e);
147
                        }
148
                    }
149
                }
150
            }
151
        }
152
    }
153

    
154
    public int getOnePointRgb() {
155
        return getColor().getRGB();
156
    }
157

    
158
    public void drawInsideRectangle(Graphics2D g,
159
            AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
160
        g.setColor(getColor());
161
        g.setStroke(getLineStyle().getStroke());
162
        super.drawInsideRectangle(g, scaleInstance, r, properties);
163
    }
164

    
165
    public void setLineWidth(double width) {
166
        getLineStyle().setLineWidth((float) width);
167
    }
168

    
169
    public double getLineWidth() {
170
        return getLineStyle().getLineWidth();
171
    }
172

    
173
    public Object clone() throws CloneNotSupportedException {
174
        SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
175

    
176
        if (symbolForSelection != null) {
177
            copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
178
                    .clone();
179
        }
180

    
181
        return copy;
182
    }
183

    
184
    public void loadFromState(PersistentState state) throws PersistenceException {
185
        // Set parent style properties
186
        super.loadFromState(state);
187

    
188
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
189
    }
190

    
191
    public void saveToState(PersistentState state) throws PersistenceException {
192
        // Save parent fill symbol properties
193
        super.saveToState(state);
194

    
195
        // Save own properties
196
        if (this.symbolForSelection != null) {
197
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
198
        }
199
    }
200

    
201
    @Override
202
    public void setTransparency(double transparency) {
203
        this.transparency = transparency;
204
        ISymbol selectionSymbol = this.symbolForSelection;
205
        if (selectionSymbol != null && selectionSymbol instanceof TransparencySupport) {
206
            ((TransparencySupport) selectionSymbol).setTransparency(transparency);
207
        }
208
    }
209

    
210
    @Override
211
    public double getTransparency() {
212
        return this.transparency;
213
    }
214

    
215
    public static class RegisterPersistence implements Callable {
216

    
217
        public Object call() throws Exception {
218
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
219
            if (manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
220
                DynStruct definition = manager.addDefinition(
221
                        SimpleLineSymbol.class,
222
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
223
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
224
                        null,
225
                        null
226
                );
227
                // Extend the LineSymbol base definition
228
                definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
229

    
230
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
231

    
232
            }
233
            return Boolean.TRUE;
234
        }
235

    
236
    }
237

    
238
    public static class RegisterSymbol implements Callable {
239

    
240
        public Object call() throws Exception {
241
            int[] shapeTypes;
242
            SymbolManager manager = MapContextLocator.getSymbolManager();
243

    
244
            shapeTypes = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
245
                Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
246
                Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
247
                Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
248
            manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
249
                    shapeTypes,
250
                    SimpleLineSymbol.class);
251

    
252
            return Boolean.TRUE;
253
        }
254

    
255
    }
256

    
257
}