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

History | View | Annotate | Download (12.4 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.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.GeometryLocator;
34
import org.gvsig.fmap.geom.GeometryManager;
35
import org.gvsig.fmap.geom.exception.CreateGeometryException;
36
import org.gvsig.fmap.geom.primitive.Envelope;
37
import org.gvsig.fmap.geom.primitive.Point;
38
import org.gvsig.fmap.mapcontext.MapContextLocator;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
40
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
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.marker.IMarkerSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IArrowDecoratorStyle;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle;
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

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

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

    
95
    @Override
96
    public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel, Rectangle r) {
97
        if(r != null){
98
            geom = this.getSampleGeometry(r);
99
        }
100

    
101
        Color c = getColor();
102

    
103
        if (true) {
104
            // Esto deberia ser para optimizar el pintado de 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;
124
//        try {
125
//            geomToDraw = geom.toLines();
126
//        } catch (Exception ex) {
127
            geomToDraw = geom.cloneGeometry();
128
//        }
129
        ILineStyle lineStyle = getLineStyle();
130
        
131
        if (lineStyle != null) {
132
            if(lineStyle instanceof CartographicSupport && r == null){
133
                ((CartographicSupport)lineStyle).setCartographicContext(this.getCartographicContext());
134
                g.setStroke(lineStyle.getCartographicStroke());
135
            } else {
136
                g.setStroke(lineStyle.getStroke());
137
            }
138

    
139
        }
140
        if(geomToDraw != null) {
141
            g.setColor(c);
142
            Shape shape = null;
143
            geomToDraw.transform(affineTransform);
144
            double offset = lineStyle.getOffset();
145
            if(offset != 0) {
146
                geomToDraw = applyOffset(this.toCartographicUnits(offset), r, geomToDraw);
147
            }
148
            if(geomToDraw != null){
149
                shape = geomToDraw.getShape();
150

    
151
                if (shape != null) {
152
                    g.draw(shape);
153
                    if (lineStyle != null) {
154
                        ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) lineStyle.getArrowDecorator();
155

    
156
                        if (arrowDecorator != null) {
157
                            try {
158
                                if(arrowDecorator instanceof CartographicSupport){
159
                                    if(r == null){
160
                                        arrowDecorator.setCartographicContext(this.getCartographicContext());
161
                                    } else {
162
                                        arrowDecorator.setCartographicContext((CartographicContext)null);
163
                                    }
164
                                }
165
                                //Pasamos una transformaci?n afin identidad porque la geometr?a ya viene transformada
166
                                arrowDecorator.draw(g, null, geomToDraw, f);
167
                            } catch (CreateGeometryException e) {
168
                                LOG.warn("Error drawing geometry.", e);
169
                            }
170
                        }
171
                    }
172
                }
173
            }
174
        }
175
    }
176

    
177
    private Geometry applyOffset(double offset, Rectangle r, Geometry geomToDraw) {
178
        if (offset != 0) {
179
            try {
180
                geomToDraw = geomToDraw.offset(offset);
181
//                if(offset > 0 ){
182
//                    geomToDraw.flip();
183
//                }
184
            } catch (Exception e) {
185
                LOG.warn("Error creating a polygon with an offset", e);
186
            }
187
        }
188
        return geomToDraw;
189
    }
190

    
191
//    @Override
192
//    public int getOnePointRgb() {
193
//        return getColor().getRGB();
194
//    }
195

    
196
    @Override
197
    public void setLineWidth(double width) {
198
        getLineStyle().setLineWidth((float) width);
199
    }
200

    
201
    @Override
202
    public double getLineWidth() {
203
        return getLineStyle().getLineWidth();
204
    }
205

    
206
    @Override
207
    public Object clone() throws CloneNotSupportedException {
208
        SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
209

    
210
        if (symbolForSelection != null) {
211
            copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
212
                    .clone();
213
        }
214

    
215
        return copy;
216
    }
217

    
218
    @Override
219
    public void loadFromState(PersistentState state) throws PersistenceException {
220
        // Set parent style properties
221
        super.loadFromState(state);
222

    
223
        this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
224
    }
225

    
226
    @Override
227
    public void saveToState(PersistentState state) throws PersistenceException {
228
        // Save parent fill symbol properties
229
        super.saveToState(state);
230

    
231
        // Save own properties
232
        if (this.symbolForSelection != null) {
233
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
234
        }
235
    }
236

    
237
    @Override
238
    public void setTransparency(double transparency) {
239
        Color c = getColor();
240
        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (transparency * 255));
241
        setColor(c);
242
        
243
        ISymbol selectionSymbol = this.symbolForSelection;
244
        if (selectionSymbol != null && selectionSymbol instanceof TransparencySupport) {
245
            ((TransparencySupport) selectionSymbol).setTransparency(transparency);
246
        }
247
    }
248

    
249
    @Override
250
    public double getTransparency() {
251
        return ((double)this.getAlpha())/255;
252
    }
253

    
254
    public static class RegisterPersistence implements Callable {
255

    
256
        @Override
257
        public Object call() throws Exception {
258
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
259
            if (manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
260
                DynStruct definition = manager.addDefinition(
261
                        SimpleLineSymbol.class,
262
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
263
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
264
                        null,
265
                        null
266
                );
267
                // Extend the LineSymbol base definition
268
                definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
269

    
270
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
271

    
272
            }
273
            return Boolean.TRUE;
274
        }
275

    
276
    }
277

    
278
    public static class RegisterSymbol implements Callable {
279

    
280
        @Override
281
        public Object call() throws Exception {
282
            int[] shapeTypes;
283
            SymbolManager manager = MapContextLocator.getSymbolManager();
284

    
285
            shapeTypes = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
286
                Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
287
                Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
288
                Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
289
            manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
290
                    shapeTypes,
291
                    SimpleLineSymbol.class);
292

    
293
            return Boolean.TRUE;
294
        }
295

    
296
    }
297

    
298
    @Override
299
    public void setCartographicContext(CartographicContext ctx) {
300
        super.setCartographicContext(ctx);
301
        if (getLineStyle() != null
302
                && getLineStyle().getArrowDecorator() != null) {
303
            IArrowDecoratorStyle arrowDecorator = getLineStyle().getArrowDecorator();
304
            if(arrowDecorator != null){
305
                if (arrowDecorator instanceof CartographicSupport) {
306
                    ((CartographicSupport) arrowDecorator).setCartographicContext(ctx);
307
                }
308
                if (arrowDecorator.getMarker() != null) {
309
                    IMarkerSymbol marker = arrowDecorator.getMarker();
310
                    if (marker instanceof CartographicSupport) {
311
                        ((CartographicSupport) marker).setCartographicContext(ctx);
312
                    }
313
                }
314
            }
315
        }
316
    }
317
}