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 / style / SimpleLineStyle.java @ 47476

History | View | Annotate | Download (11.9 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.style;
25

    
26
import java.awt.BasicStroke;
27
import java.awt.Color;
28
import java.awt.Graphics2D;
29
import java.awt.Rectangle;
30
import java.awt.Stroke;
31
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
32
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
33
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.dynobject.DynStruct;
36
import org.gvsig.tools.persistence.PersistenceManager;
37
import org.gvsig.tools.persistence.PersistentState;
38
import org.gvsig.tools.persistence.exception.PersistenceException;
39
import org.gvsig.tools.util.Callable;
40

    
41
/**
42
 * @see http://www.oreilly.com/catalog/java2d/chapter/ch04.html
43
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
44
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
45
 */
46
public class SimpleLineStyle extends AbstractStyle implements CartographicSupport, ISimpleLineStyle {
47
        public static final String SIMPLE_LINE_STYLE_PERSISTENCE_DEFINITION_NAME = "SimpleLineStyle";
48

    
49
        private static final String FIELD_DASH_ARRAY = "dashArray";
50
        private static final String FIELD_TEMP_DASH_ARRAY = "tempDashArray";
51
        private static final String FIELD_LINE_WIDTH = "lineWidth";
52
        private static final String FIELD_DASH_PHASE = "dashPhase";
53
        private static final String FIELD_END_CAP = "endCap";
54
        private static final String FIELD_LINE_JOIN = "lineJoin";
55
        private static final String FIELD_MITER_LIMIT = "miterLimit";
56
        private static final String FIELD_OFFSET = "offset";
57
        private static final String FIELD_UNIT = "unit";
58
        private static final String FIELD_ARROW_DECORATOR = "arrowDecorator";
59

    
60
        private final static Color PREVIEW_COLOR_1= new Color(150, 255, 200); //light blue
61
        private final static Color PREVIEW_COLOR_2 = new Color(255, 200, 100); //orange
62
        private final static int COLOR1_STROKE = 1;
63
        private final static int COLOR2_STROKE = 3;
64
        private float[] dashArray, tempDashArray;
65
        private float dashPhase;
66
        private int endCap = BasicStroke.CAP_BUTT;
67
        private int lineJoin;
68
        private float miterlimit;
69
        private float lineWidth = 1f;
70
        private double offset = 0;
71
        private IArrowDecoratorStyle arrowDecorator;
72
        /**
73
         * Constructor method
74
         *
75
         */
76
        public SimpleLineStyle() {
77
                super();
78
                BasicStroke dummy = new BasicStroke();
79
                dashArray = dummy.getDashArray();
80
                tempDashArray = dummy.getDashArray();
81
                dashPhase = dummy.getDashPhase();
82
                endCap = BasicStroke.CAP_BUTT;
83
                lineJoin = BasicStroke.JOIN_BEVEL;
84
                miterlimit = dummy.getMiterLimit();
85
        }
86
        /**
87
         * Constructor method
88
         *
89
         * @param width
90
         * @param cap
91
         * @param join
92
         * @param miterlimit
93
         * @param dash
94
         * @param dash_phase
95
         */
96
        public SimpleLineStyle(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) {
97
                super();
98
                this.lineWidth = width;
99
                this.endCap = cap;
100
                this.lineJoin = join;
101
                this.miterlimit = miterlimit;
102
                this.dashArray = dash;
103
                this.tempDashArray = dash;
104
                this.dashPhase = dash_phase;
105
        }
106

    
107
        @Override
108
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
109
                Stroke oldStroke = g.getStroke();
110
                int h = (int) (r.getHeight() / 2);
111
                int margins = 2;
112

    
113
                BasicStroke stroke;
114
                stroke = new BasicStroke(COLOR1_STROKE, endCap, lineJoin, miterlimit, tempDashArray, dashPhase);
115
                g.setStroke(stroke);
116
                g.setColor(PREVIEW_COLOR_1);
117
                g.drawLine(margins, h, r.width-margins-margins, h);
118

    
119
                stroke = new BasicStroke(COLOR2_STROKE, endCap, lineJoin, miterlimit, tempDashArray, dashPhase);
120
                g.setStroke(stroke);
121
                g.setColor(PREVIEW_COLOR_2);
122
                g.drawLine(margins, h, r.width-margins-margins, h);
123
                g.setStroke(oldStroke);
124
        }
125

    
126
        public String getClassName() {
127
                return getClass().getName();
128
        }
129

    
130
        @Override
131
        public Stroke getStroke() {
132
                return new BasicStroke((float) lineWidth, endCap, lineJoin, miterlimit, tempDashArray, dashPhase);
133
        }
134

    
135
        @Override
136
    public Stroke getCartographicStroke() {
137
        if (dashArray != null) {
138
            for (int i = 0; i < dashArray.length; i++) {
139
                tempDashArray[i] = (float) (toCartographicUnits(dashArray[i]));
140
            }
141
        }
142
        return new BasicStroke((float) (toCartographicUnits(lineWidth)), endCap, lineJoin, miterlimit, tempDashArray, dashPhase);
143
    }
144

    
145
        @Override
146
        public float getLineWidth() {
147
                return lineWidth;
148
        }
149

    
150

    
151
        @Override
152
        public void setLineWidth(float width) {
153
                this.lineWidth = width;
154

    
155
        }
156

    
157
        @Override
158
        public boolean isSuitableFor(ISymbol symbol) {
159
                return symbol instanceof ILineSymbol;
160
        }
161

    
162
        @Override
163
        public void setStroke(Stroke stroke) {
164
                if (stroke != null) {
165
                        BasicStroke dummy = (BasicStroke) stroke;
166
                        dashArray = dummy.getDashArray();
167
                        if (dashArray != null) {
168
                                tempDashArray = new float[dashArray.length];
169
                            System.arraycopy(dashArray, 0, tempDashArray, 0, dashArray.length);
170
                        } else
171
                                tempDashArray = null;
172

    
173

    
174
                        dashPhase = dummy.getDashPhase();
175
                        endCap = dummy.getEndCap();
176
                        lineJoin = dummy.getLineJoin();
177
                        miterlimit = dummy.getMiterLimit();
178
                        lineWidth = dummy.getLineWidth();
179
                        offset = getOffset();
180
                }
181
        }
182

    
183
        @Override
184
        public void drawOutline(Graphics2D g, Rectangle r) {
185
                drawInsideRectangle(g, r);
186
        }
187

    
188
        @Override
189
        public double getOffset() {
190
                return offset;
191
        }
192

    
193
        @Override
194
        public double getCartographicOffset() {
195
                return toCartographicUnits(offset);
196
        }
197

    
198
        @Override
199
        public void setOffset(double offset) {
200
                this.offset = offset;
201
        }
202

    
203
        @Override
204
        public IArrowDecoratorStyle getArrowDecorator() {
205
                return arrowDecorator;
206

    
207
        }
208

    
209
        @Override
210
        public void setArrowDecorator(IArrowDecoratorStyle arrowDecoratorStyle) {
211
                this.arrowDecorator = arrowDecoratorStyle;
212
        }
213

    
214
        @Override
215
        public Object clone() throws CloneNotSupportedException {
216
                SimpleLineStyle copy = (SimpleLineStyle) super.clone();
217
                if (arrowDecorator != null) {
218
                        copy.arrowDecorator = (ArrowDecoratorStyle) arrowDecorator.clone();
219
                }
220

    
221
                if (dashArray != null) {
222
                        copy.dashArray = new float[dashArray.length];
223
                        System.arraycopy(dashArray, 0, copy.dashArray, 0, dashArray.length);
224
                }
225
                if (tempDashArray != null) {
226
                        copy.tempDashArray = new float[tempDashArray.length];
227
                        System.arraycopy(tempDashArray, 0, copy.tempDashArray, 0,
228
                                        tempDashArray.length);
229
                }
230

    
231
                return copy;
232
        }
233

    
234
        /**
235
         * @return the dashArray
236
         */
237
        private float[] getDashArray() {
238
                return dashArray;
239
        }
240

    
241
        /**
242
         * @param dashArray
243
         *            the dashArray to set
244
         */
245
        private void setDashArray(float[] dashArray) {
246
                this.dashArray = dashArray;
247
        }
248

    
249
        /**
250
         * @return the dashPhase
251
         */
252
        private float getDashPhase() {
253
                return dashPhase;
254
        }
255

    
256
        /**
257
         * @param dashPhase
258
         *            the dashPhase to set
259
         */
260
        private void setDashPhase(float dashPhase) {
261
                this.dashPhase = dashPhase;
262
        }
263

    
264
        /**
265
         * @return the endCap
266
         */
267
        private int getEndCap() {
268
                return endCap;
269
        }
270

    
271
        /**
272
         * @param endCap
273
         *            the endCap to set
274
         */
275
        private void setEndCap(int endCap) {
276
                this.endCap = endCap;
277
        }
278

    
279
        /**
280
         * @return the lineJoin
281
         */
282
        private int getLineJoin() {
283
                return lineJoin;
284
        }
285

    
286
        /**
287
         * @param lineJoin
288
         *            the lineJoin to set
289
         */
290
        private void setLineJoin(int lineJoin) {
291
                this.lineJoin = lineJoin;
292
        }
293

    
294
        /**
295
         * @return the miterlimit
296
         */
297
        private float getMiterlimit() {
298
                return miterlimit;
299
        }
300

    
301
        /**
302
         * @param miterlimit
303
         *            the miterlimit to set
304
         */
305
        private void setMiterlimit(float miterlimit) {
306
                this.miterlimit = miterlimit;
307
        }
308

    
309
        @Override
310
        public void loadFromState(PersistentState state)
311
                        throws PersistenceException {
312
                // Set parent style properties
313
                super.loadFromState(state);
314

    
315
                // Set own properties
316
                setArrowDecorator((ArrowDecoratorStyle) state
317
                                .get(FIELD_ARROW_DECORATOR));
318
                setDashArray(state.getFloatArray(FIELD_DASH_ARRAY));
319
                tempDashArray = state.getFloatArray(FIELD_TEMP_DASH_ARRAY);
320
                setDashPhase(state.getFloat(FIELD_DASH_PHASE));
321
                setEndCap(state.getInt(FIELD_END_CAP));
322
                setLineJoin(state.getInt(FIELD_LINE_JOIN));
323
                setLineWidth(state.getFloat(FIELD_LINE_WIDTH));
324
                setMiterlimit(state.getFloat(FIELD_MITER_LIMIT));
325
                setOffset(state.getDouble(FIELD_OFFSET));
326
                setUnit(state.getInt(FIELD_UNIT));
327
        }
328

    
329
        @Override
330
        public void saveToState(PersistentState state) throws PersistenceException {
331
                // Save parent fill symbol properties
332
                super.saveToState(state);
333

    
334
                // Save own properties
335
                state.set(FIELD_ARROW_DECORATOR, getArrowDecorator());
336
                state.set(FIELD_DASH_ARRAY, getDashArray());
337
                state.set(FIELD_TEMP_DASH_ARRAY, tempDashArray);
338
                state.set(FIELD_DASH_PHASE, getDashPhase());
339
                state.set(FIELD_END_CAP, getEndCap());
340
                state.set(FIELD_LINE_JOIN, getLineJoin());
341
                state.set(FIELD_LINE_WIDTH, getLineWidth());
342
                state.set(FIELD_MITER_LIMIT, getMiterlimit());
343
                state.set(FIELD_OFFSET, getOffset());
344
                state.set(FIELD_UNIT, getUnit());
345
        }
346

    
347
        public static class RegisterPersistence implements Callable {
348

    
349
                @Override
350
                public Object call() throws Exception {
351
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
352
                        if( manager.getDefinition(SIMPLE_LINE_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) {
353
                                DynStruct definition = manager.addDefinition(
354
                                                SimpleLineStyle.class,
355
                                                SIMPLE_LINE_STYLE_PERSISTENCE_DEFINITION_NAME,
356
                                                SIMPLE_LINE_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
357
                                                null, 
358
                                                null
359
                                );
360

    
361
                                // Extend the Style base definition
362
                                definition.extend(manager.getDefinition(STYLE_PERSISTENCE_DEFINITION_NAME));
363

    
364
                                // Arrow decorator
365
                                definition.addDynFieldObject(FIELD_ARROW_DECORATOR)
366
                                        .setClassOfValue(ArrowDecoratorStyle.class);
367
                                // Dash array
368
                                definition.addDynFieldArray(FIELD_DASH_ARRAY)
369
                                        .setClassOfItems(float.class);
370
                                // Temporal dash array
371
                                definition.addDynFieldArray(FIELD_TEMP_DASH_ARRAY)
372
                                        .setClassOfItems(float.class);
373
                                // Dash phase
374
                                definition.addDynFieldFloat(FIELD_DASH_PHASE).setMandatory(true);
375
                                // End cap
376
                                definition.addDynFieldInt(FIELD_END_CAP).setMandatory(true);
377
                                // Line join
378
                                definition.addDynFieldInt(FIELD_LINE_JOIN).setMandatory(true);
379
                                // Line width
380
                                definition.addDynFieldFloat(FIELD_LINE_WIDTH).setMandatory(true);
381
                                // Miter limit
382
                                definition.addDynFieldFloat(FIELD_MITER_LIMIT).setMandatory(true);
383
                                // Offset
384
                                definition.addDynFieldDouble(FIELD_OFFSET).setMandatory(true);
385
                                // Unit
386
                                definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
387
                        }
388
                        return Boolean.TRUE;
389
                }
390
                
391
        }
392

    
393
    @Override
394
    public void setCartographicContext(CartographicContext ctx) {
395
        super.setCartographicContext(ctx);
396
        if(arrowDecorator instanceof CartographicSupport){
397
            ((CartographicSupport)arrowDecorator).setCartographicContext(ctx);
398
        }
399
    }
400
        
401
        
402
}