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

History | View | Annotate | Download (10 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
import org.gvsig.compat.print.PrintAttributes;
32
import org.gvsig.fmap.dal.feature.Feature;
33
import org.gvsig.fmap.geom.Geometry;
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.geom.primitive.Point;
39
import org.gvsig.fmap.mapcontext.MapContext;
40
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
43
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
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.swing.api.TransparencySupport;
53
import org.gvsig.tools.task.Cancellable;
54
import org.gvsig.tools.util.Callable;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

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

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

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

    
73
    SimpleLineSymbol symbolForSelection;
74
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
75

    
76
    public SimpleLineSymbol() {
77
        super();
78
        setLineWidth(1d);
79
   }
80

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

    
94
    @Override
95
    public void draw(Graphics2D g, AffineTransform affineTransform,
96
            Geometry geom, Feature feature, Cancellable cancel) {
97

    
98
        Color c = getColor();
99

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

    
121
        Geometry geomToDraw = geom;
122
        g.setStroke(getLineStyle().getStroke());
123

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

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

    
152
    @Override
153
    public int getOnePointRgb() {
154
        return getColor().getRGB();
155
    }
156

    
157
    @Override
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
    @Override
166
    public void setLineWidth(double width) {
167
        getLineStyle().setLineWidth((float) width);
168
    }
169

    
170
    @Override
171
    public double getLineWidth() {
172
        return getLineStyle().getLineWidth();
173
    }
174

    
175
    @Override
176
    public Object clone() throws CloneNotSupportedException {
177
        SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
178

    
179
        if (symbolForSelection != null) {
180
            copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
181
                    .clone();
182
        }
183

    
184
        return copy;
185
    }
186

    
187
    @Override
188
    public void loadFromState(PersistentState state) throws PersistenceException {
189
        // Set parent style properties
190
        super.loadFromState(state);
191

    
192
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
193
    }
194

    
195
    @Override
196
    public void saveToState(PersistentState state) throws PersistenceException {
197
        // Save parent fill symbol properties
198
        super.saveToState(state);
199

    
200
        // Save own properties
201
        if (this.symbolForSelection != null) {
202
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
203
        }
204
    }
205

    
206
    @Override
207
    public void setTransparency(double transparency) {
208
        Color c = getColor();
209
        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (transparency * 255));
210
        setColor(c);
211
        
212
        ISymbol selectionSymbol = this.symbolForSelection;
213
        if (selectionSymbol != null && selectionSymbol instanceof TransparencySupport) {
214
            ((TransparencySupport) selectionSymbol).setTransparency(transparency);
215
        }
216
    }
217

    
218
    @Override
219
    public double getTransparency() {
220
        return ((double)this.getAlpha())/255;
221
    }
222

    
223
    public static class RegisterPersistence implements Callable {
224

    
225
        @Override
226
        public Object call() throws Exception {
227
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
228
            if (manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
229
                DynStruct definition = manager.addDefinition(
230
                        SimpleLineSymbol.class,
231
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
232
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
233
                        null,
234
                        null
235
                );
236
                // Extend the LineSymbol base definition
237
                definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
238

    
239
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
240

    
241
            }
242
            return Boolean.TRUE;
243
        }
244

    
245
    }
246

    
247
    public static class RegisterSymbol implements Callable {
248

    
249
        @Override
250
        public Object call() throws Exception {
251
            int[] shapeTypes;
252
            SymbolManager manager = MapContextLocator.getSymbolManager();
253

    
254
            shapeTypes = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
255
                Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
256
                Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
257
                Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
258
            manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
259
                    shapeTypes,
260
                    SimpleLineSymbol.class);
261

    
262
            return Boolean.TRUE;
263
        }
264

    
265
    }
266

    
267
}