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 / impl / MultiShapeSymbol.java @ 47477

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

    
26
import java.awt.Color;
27
import java.awt.Graphics2D;
28
import java.awt.Rectangle;
29
import java.awt.geom.AffineTransform;
30
import java.awt.geom.Point2D;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.aggregate.Aggregate;
34
import org.gvsig.fmap.geom.type.GeometryType;
35
import org.gvsig.fmap.mapcontext.MapContextLocator;
36
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
39
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.IMultiShapeSymbol;
40
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
41
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IMask;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.exception.NotYetImplemented;
48
import org.gvsig.tools.persistence.PersistenceManager;
49
import org.gvsig.tools.persistence.PersistentState;
50
import org.gvsig.tools.persistence.exception.PersistenceException;
51
import org.gvsig.tools.task.Cancellable;
52
import org.gvsig.tools.util.Callable;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

    
56
/**
57
 * MultiShapeSymbol class allows to create a composition of several symbols with
58
 * different shapes and be treated as a single symbol.These shapes can be
59
 * marker,line or fill.
60
 *
61
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
62
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
63
 */
64
public class MultiShapeSymbol extends AbstractSymbol implements ILineSymbol, IMarkerSymbol, IFillSymbol, IMultiShapeSymbol {
65

    
66
    private static final Logger LOG = LoggerFactory.getLogger(MultiShapeSymbol.class);
67

    
68
    public static final String MULTI_SHAPE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MultiShapeSymbol";
69

    
70
    private static final String FIELD_MARKER = "marker";
71
    private static final String FIELD_LINE = "line";
72
    private static final String FIELD_FILL = "fill";
73
    private static final String FIELD_DESCRIPTION = "description";
74

    
75
    @SuppressWarnings("FieldNameHidesFieldInSuperclass")
76
    public static final String SYMBOL_NAME = "multiShape";
77

    
78
    private IMarkerSymbol marker;
79
    private ILineSymbol line;
80
    private IFillSymbol fill;
81
    private IMask mask;
82
    private int referenceSystem;
83
    private MultiShapeSymbol symSelect;
84

    
85
    public MultiShapeSymbol() {
86
        super();
87
        marker = (IMarkerSymbol) createSymbol(IMarkerSymbol.SYMBOL_NAME);
88
        line = (ILineSymbol) createSymbol(ILineSymbol.SYMBOL_NAME);
89
        fill = (IFillSymbol) createSymbol(IFillSymbol.SYMBOL_NAME);
90
    }
91

    
92
    private ISymbol createSymbol(String symbolName) {
93
        SymbolManager manager = MapContextLocator.getSymbolManager();
94
        return manager.createSymbol(symbolName);
95
    }
96

    
97
    public Color getLineColor() {
98
        return line.getColor();
99
    }
100

    
101
    @Override
102
    public void setLineColor(Color color) {
103
        line.setLineColor(color);
104
    }
105

    
106
    @Override
107
    public ILineStyle getLineStyle() {
108
        return line.getLineStyle();
109
    }
110

    
111
    @Override
112
    public void setLineStyle(ILineStyle lineStyle) {
113
        line.setLineStyle(lineStyle);
114
    }
115

    
116
    @Override
117
    public void setLineWidth(double width) {
118
        line.setLineWidth(width);
119
    }
120

    
121
    @Override
122
    public double getLineWidth() {
123
        return line.getLineWidth();
124
    }
125

    
126
    @Override
127
    public int getAlpha() {
128
        return line.getAlpha();
129
    }
130

    
131
    @Override
132
    public void setAlpha(int outlineAlpha) {
133
        line.setAlpha(outlineAlpha);
134
    }
135

    
136
    @Override
137
    public ISymbol getSymbolForSelection() {
138
        if (symSelect == null) {
139
            symSelect = new MultiShapeSymbol();
140
        }
141

    
142
        if (marker != null) {
143
            symSelect.setMarkerSymbol((IMarkerSymbol) marker.getSymbolForSelection());
144
        }
145

    
146
        if (line != null) {
147
            symSelect.setLineSymbol((ILineSymbol) line.getSymbolForSelection());
148
        }
149

    
150
        if (fill != null) {
151
            symSelect.setFillSymbol((IFillSymbol) fill.getSymbolForSelection());
152
        }
153

    
154
        if (symSelect instanceof CartographicSupport) {
155
            ((CartographicSupport) symSelect).setUnit(this.getUnit());
156
        }
157
        return symSelect;
158

    
159
    }
160

    
161
    @Override
162
    public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel, Rectangle r) {
163
        if (r != null) {
164
            double myWidth = (r.getWidth() / 3);
165

    
166
            Rectangle rect = new Rectangle(0, 0, (int) myWidth, r.height);
167

    
168
            if (marker != null) {
169
                CartographicSupport.copyCartographicContext(this, marker);
170
                g.translate(r.x, r.y);
171
                marker.draw(g, affineTransform, geom, f, cancel, rect);
172
                g.translate(-(r.x), -(r.y));
173
            }
174

    
175
            if (line != null) {
176
                CartographicSupport.copyCartographicContext(this, line);
177
                g.translate(r.x + myWidth, r.y);
178
                line.draw(g, affineTransform, geom, f, cancel, rect);
179
                g.translate(-(r.x + myWidth), -(r.y));
180
            }
181

    
182
            if (fill != null) {
183
                CartographicSupport.copyCartographicContext(this, fill);
184
                g.translate(r.x + myWidth + myWidth, r.y);
185
                fill.draw(g, affineTransform, geom, f, cancel, rect);
186
                g.translate(-(r.x + myWidth + myWidth), -(r.y));
187
            }
188
        } else {
189
            GeometryType geometryType = geom.getGeometryType();
190
            if (geometryType.isTypeOf(Geometry.TYPES.POINT)) {
191
                if (marker != null) {
192
                    CartographicSupport.copyCartographicContext(this, marker);
193
                    marker.draw(g, affineTransform, geom, f, cancel, r);
194
                }
195
            } else if (geometryType.isTypeOf(Geometry.TYPES.CURVE)) {
196
                if (line != null) {
197
                    CartographicSupport.copyCartographicContext(this, line);
198
                    line.draw(g, affineTransform, geom, f, cancel, r);
199
                }
200
            } else if (geometryType.isTypeOf(Geometry.TYPES.SURFACE)) {
201
                if (fill != null) {
202
                    CartographicSupport.copyCartographicContext(this, fill);
203
                    fill.draw(g, affineTransform, geom, f, cancel, r);
204
                }
205
            } else if (geometryType.isTypeOf(Geometry.TYPES.AGGREGATE)) {
206
                Aggregate aggregate = (Aggregate) geom;
207
                for (int i = 0; i < aggregate.getPrimitivesNumber(); i++) {
208
                    draw(g, affineTransform, aggregate.getPrimitiveAt(i), f, cancel, r);
209
                }
210
            }
211
        }
212
    }
213

    
214
    @Override
215
    public boolean isShapeVisible() {
216
        if (marker != null) {
217
            return marker.isShapeVisible();
218
        }
219

    
220
        if (line != null) {
221
            return line.isShapeVisible();
222
        }
223

    
224
        if (fill != null) {
225
            fill.isShapeVisible();
226
        }
227

    
228
        return false;
229
    }
230

    
231
    @Override
232
    public int getSymbolType() {
233
        return Geometry.TYPES.GEOMETRY;
234
    }
235

    
236
    @Override
237
    public boolean isSuitableFor(Geometry geom) {
238
        // suitable for everything (why else does it exist?)
239
        return true;
240
    }
241

    
242
    @Override
243
    public double getRotation() {
244
        if (marker != null) {
245
            return marker.getRotation();
246
        }
247
        return 0;
248
    }
249

    
250
    @Override
251
    public void setRotation(double rotation) {
252
        if (marker != null) {
253
            marker.setRotation(rotation);
254
        }
255
    }
256

    
257
    @Override
258
    public Point2D getOffset() {
259
        if (marker != null) {
260
            return marker.getOffset();
261
        }
262
        return new Point2D.Double();
263
    }
264

    
265
    @Override
266
    public void setOffset(Point2D offset) {
267
        if (marker != null) {
268
            marker.setOffset(offset);
269
        }
270
    }
271

    
272
    @Override
273
    public double getSize() {
274
        if (marker != null) {
275
            return marker.getSize();
276
        }
277
        return 0;
278
    }
279

    
280
    @Override
281
    public void setSize(double size) {
282
        if (marker != null) {
283
            marker.setSize(size);
284
        }
285

    
286
    }
287

    
288
    @Override
289
    public Color getColor() {
290
        if (marker != null) {
291
            return marker.getColor();
292
        }
293
        return null;
294
    }
295

    
296
    @Override
297
    public void setColor(Color color) {
298
        if (marker != null) {
299
            marker.setColor(color);
300
        }
301
    }
302

    
303
    @Override
304
    public void setFillColor(Color color) {
305
        if (fill != null) {
306
            fill.setFillColor(color);
307
        }
308
    }
309

    
310
    @Override
311
    public void setOutline(ILineSymbol outline) {
312
        if (fill != null) {
313
            fill.setOutline(outline);
314
        }
315
    }
316

    
317
    @Override
318
    public Color getFillColor() {
319
        if (fill != null) {
320
            return fill.getFillColor();
321
        }
322
        return null;
323
    }
324

    
325
    @Override
326
    public ILineSymbol getOutline() {
327
        if (fill != null) {
328
            return fill.getOutline();
329
        }
330
        return null;
331
    }
332

    
333
    @Override
334
    public int getFillAlpha() {
335
        if (fill != null) {
336
            return fill.getFillAlpha();
337
        }
338
        return 255;
339
    }
340

    
341
    @Override
342
    public IMask getMask() {
343
        return mask;
344
    }
345

    
346
    @Override
347
    public void setUnit(int unitIndex) {
348
        if (marker != null) {
349
            marker.setUnit(unitIndex);
350
        }
351
        if (line != null) {
352
            line.setUnit(unitIndex);
353
        }
354
    }
355

    
356
    @Override
357
    public int getUnit() {
358
        if (marker != null) {
359
            return marker.getUnit();
360
        }
361
        if (line != null) {
362
            return line.getUnit();
363
        }
364
        return -1;
365
    }
366

    
367
    @Override
368
    public void setMask(IMask mask) {
369
        throw new NotYetImplemented();
370

    
371
    }
372

    
373
    @Override
374
    public int getReferenceSystem() {
375
        return this.referenceSystem;
376
    }
377

    
378
    @Override
379
    public void setReferenceSystem(int system) {
380
        this.referenceSystem = system;
381

    
382
    }
383

    
384
    @Override
385
    public IMarkerSymbol getMarkerSymbol() {
386
        return marker;
387
    }
388

    
389
    @Override
390
    public ILineSymbol getLineSymbol() {
391
        return line;
392
    }
393

    
394
    @Override
395
    public IFillSymbol getFillSymbol() {
396
        return fill;
397
    }
398

    
399
    @Override
400
    public void setMarkerSymbol(IMarkerSymbol markerSymbol) {
401
        this.marker = markerSymbol;
402
    }
403

    
404
    @Override
405
    public void setLineSymbol(ILineSymbol lineSymbol) {
406
        this.line = lineSymbol;
407
    }
408

    
409
    @Override
410
    public void setFillSymbol(IFillSymbol fillFillSymbol) {
411
        this.fill = fillFillSymbol;
412
    }
413

    
414
    @Override
415
    public boolean hasFill() {
416
        if (fill == null) {
417
            return false;
418
        }
419
        return fill.hasFill();
420
    }
421

    
422
    @Override
423
    public void setHasFill(boolean hasFill) {
424
        if (fill != null) {
425
            fill.setHasFill(hasFill);
426
        }
427
    }
428

    
429
    @Override
430
    public boolean hasOutline() {
431
        return false;
432
    }
433

    
434
    @Override
435
    public void setHasOutline(boolean hasOutline) {
436

    
437
    }
438

    
439
    @Override
440
    public Object clone() throws CloneNotSupportedException {
441
        MultiShapeSymbol copy = (MultiShapeSymbol) super.clone();
442

    
443
        // Clone inner symbols
444
        if (marker != null) {
445
            copy.marker = (IMarkerSymbol) marker.clone();
446
        }
447
        if (line != null) {
448
            copy.line = (ILineSymbol) line.clone();
449
        }
450
        if (fill != null) {
451
            copy.fill = (IFillSymbol) fill.clone();
452
        }
453

    
454
        if (mask != null) {
455
            copy.mask = (IMask) mask.clone();
456
        }
457

    
458
        // Clone selection symbol
459
        if (symSelect != null) {
460
            copy.symSelect = (MultiShapeSymbol) symSelect.clone();
461
        }
462

    
463
        return copy;
464
    }
465

    
466
    @Override
467
    public void loadFromState(PersistentState state)
468
            throws PersistenceException {
469
        setDescription(state.getString(FIELD_DESCRIPTION));
470
        setFillSymbol((IFillSymbol) state.get(FIELD_FILL));
471
        setMarkerSymbol((IMarkerSymbol) state.get(FIELD_MARKER));
472
        setLineSymbol((ILineSymbol) state.get(FIELD_LINE));
473
    }
474

    
475
    @Override
476
    public void saveToState(PersistentState state) throws PersistenceException {
477
        state.set(FIELD_DESCRIPTION, getDescription());
478
        state.set(FIELD_FILL, getFillSymbol());
479
        state.set(FIELD_MARKER, getMarkerSymbol());
480
        state.set(FIELD_LINE, getLineSymbol());
481
    }
482

    
483
    public static class RegisterPersistence implements Callable {
484

    
485
        @Override
486
        public Object call() throws Exception {
487
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
488
            if (manager.getDefinition(MULTI_SHAPE_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
489
                DynStruct definition = manager.addDefinition(
490
                        MultiShapeSymbol.class,
491
                        MULTI_SHAPE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
492
                        MULTI_SHAPE_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
493
                        null,
494
                        null
495
                );
496
                // Description
497
                definition.addDynFieldString(FIELD_DESCRIPTION).setMandatory(false);
498
                // Fill symbol
499
                definition.addDynFieldObject(FIELD_FILL).setClassOfValue(IFillSymbol.class).setMandatory(true);
500
                // Line symbol
501
                definition.addDynFieldObject(FIELD_LINE).setClassOfValue(ILineSymbol.class).setMandatory(true);
502
                // Marker symbol
503
                definition.addDynFieldObject(FIELD_MARKER).setClassOfValue(IMarkerSymbol.class).setMandatory(true);
504
            }
505
            return Boolean.TRUE;
506
        }
507

    
508
    }
509

    
510
    public static class RegisterSymbol implements Callable {
511

    
512
        @Override
513
        public Object call() throws Exception {
514
            int[] shapeTypes;
515
            SymbolManager manager = MapContextLocator.getSymbolManager();
516

    
517
            shapeTypes = new int[]{Geometry.TYPES.GEOMETRY};
518
            manager.registerSymbol(MultiShapeSymbol.SYMBOL_NAME,
519
                    shapeTypes,
520
                    MultiShapeSymbol.class
521
            );
522
            return Boolean.TRUE;
523
        }
524
    }
525
}