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

History | View | Annotate | Download (17 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.ViewPort;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2;
40
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
41
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.IMultiShapeSymbol;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol_v2;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IMask;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.dynobject.DynStruct;
50
import org.gvsig.tools.exception.NotYetImplemented;
51
import org.gvsig.tools.persistence.PersistenceManager;
52
import org.gvsig.tools.persistence.PersistentState;
53
import org.gvsig.tools.persistence.exception.PersistenceException;
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
 * MultiShapeSymbol class allows to create a composition of several symbols with
61
 * different shapes and be treated as a single symbol.These shapes can be
62
 * marker,line or fill.
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 MultiShapeSymbol extends AbstractSymbol implements ILineSymbol, IMarkerSymbol, IFillSymbol, IMultiShapeSymbol {
68

    
69
        private static final Logger LOG = LoggerFactory.getLogger(MultiShapeSymbol.class); 
70

    
71
        public static final String MULTI_SHAPE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MultiShapeSymbol";
72
        
73
        private static final String FIELD_MARKER = "marker";
74
        private static final String FIELD_LINE = "line";
75
        private static final String FIELD_FILL = "fill";
76
        private static final String FIELD_DESCRIPTION = "description";
77

    
78
        @SuppressWarnings("FieldNameHidesFieldInSuperclass")
79
        public static final String SYMBOL_NAME = "multiShape";
80
        
81
        private IMarkerSymbol marker;
82
        private ILineSymbol line;
83
        private IFillSymbol fill;
84
        private IMask mask;
85
        private int referenceSystem;
86
        private MultiShapeSymbol symSelect;
87
        
88
        public MultiShapeSymbol() {
89
                super();
90
                marker = (IMarkerSymbol) createSymbol(IMarkerSymbol.SYMBOL_NAME);
91
                line = (ILineSymbol) createSymbol(ILineSymbol.SYMBOL_NAME);
92
                fill = (IFillSymbol) createSymbol(IFillSymbol.SYMBOL_NAME);
93
        }
94
        
95
        private ISymbol createSymbol(String symbolName) {
96
                SymbolManager manager = MapContextLocator.getSymbolManager();
97
                return manager.createSymbol(symbolName);
98
        }
99

    
100
        public Color getLineColor() {
101
                return line.getColor();
102
        }
103

    
104
        @Override
105
        public void setLineColor(Color color) {
106
                line.setLineColor(color);
107
        }
108

    
109
        @Override
110
        public ILineStyle getLineStyle() {
111
                return line.getLineStyle();
112
        }
113

    
114
        @Override
115
        public void setLineStyle(ILineStyle lineStyle) {
116
                line.setLineStyle(lineStyle);
117
        }
118

    
119
        @Override
120
        public void setLineWidth(double width) {
121
                line.setLineWidth(width);
122
        }
123

    
124
        @Override
125
        public double getLineWidth() {
126
                return line.getLineWidth();
127
        }
128

    
129
        @Override
130
        public int getAlpha() {
131
                return line.getAlpha();
132
        }
133

    
134
        @Override
135
        public void setAlpha(int outlineAlpha) {
136
                line.setAlpha(outlineAlpha);
137
        }
138

    
139
        @Override
140
        public ISymbol getSymbolForSelection() {
141
                if (symSelect == null) {
142
                        symSelect = new MultiShapeSymbol();
143
                }
144

    
145
                if (marker!=null){
146
                        symSelect.setMarkerSymbol((IMarkerSymbol) marker.getSymbolForSelection());
147
                }
148

    
149
                if (line!=null){
150
                        symSelect.setLineSymbol((ILineSymbol) line.getSymbolForSelection());
151
                }
152

    
153
                if (fill!=null ){
154
                        symSelect.setFillSymbol((IFillSymbol) fill.getSymbolForSelection());
155
                }
156

    
157
                if (symSelect instanceof CartographicSupport) {
158
                    ((CartographicSupport) symSelect).setUnit(this.getUnit());
159
                }
160
                return symSelect;
161

    
162
        }
163

    
164
        @Override
165
    public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel, Rectangle r) {
166
            GeometryType geometryType = geom.getGeometryType();
167
            if (geometryType.isTypeOf(Geometry.TYPES.POINT)){
168
                if (marker != null) {
169
                    if(marker instanceof CartographicSupport){
170
                        marker.setCartographicContext(this.getCartographicContext());
171
                    }
172
                    if(marker instanceof IMarkerSymbol_v2){
173
                        ((IMarkerSymbol_v2)marker).draw(g, affineTransform, geom, f, cancel, r);
174
                    } else {
175
                        marker.draw(g, affineTransform, geom, f, cancel);
176
                    }
177
                }
178
            }else if (geometryType.isTypeOf(Geometry.TYPES.CURVE)){
179
                if (line != null) {
180
                    if(line instanceof CartographicSupport){
181
                        line.setCartographicContext(this.getCartographicContext());
182
                    }
183
                    if(line instanceof ISymbol_v2){
184
                        ((ISymbol_v2)line).draw(g, affineTransform, geom, f, cancel, r);
185
                    } else {
186
                        line.draw(g, affineTransform, geom, f, cancel);
187
                    }
188
                }
189
            }else if (geometryType.isTypeOf(Geometry.TYPES.SURFACE)){
190
                if (fill != null) {
191
                    if(fill instanceof CartographicSupport){
192
                        fill.setCartographicContext(this.getCartographicContext());
193
                    }
194
                    if(fill instanceof ISymbol_v2){
195
                        ((ISymbol_v2)fill).draw(g, affineTransform, geom, f, cancel, r);
196
                    } else {
197
                        fill.draw(g, affineTransform, geom, f, cancel);
198
                    }
199
                }                        
200
            }else if (geometryType.isTypeOf(Geometry.TYPES.AGGREGATE)){
201
                Aggregate aggregate = (Aggregate)geom;
202
                for (int i=0 ; i<aggregate.getPrimitivesNumber() ; i++){
203
                    draw(g, affineTransform, aggregate.getPrimitiveAt(i), f, cancel, r);
204
                }
205
            }
206
        }
207
        
208
//    @Override
209
//        public boolean isOneDotOrPixel(Geometry geom,
210
//                        double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) {
211
//                
212
//        GeometryType geomType = geom.getGeometryType();
213
//        if( geomType.isSubTypeOf(Geometry.TYPES.POINT) ) {
214
//                   if (marker != null) {
215
//                                return marker.isOneDotOrPixel(geom, positionOfDotOrPixel, viewPort, dpi);
216
//                        }
217
//
218
//        } else if( geomType.isSubTypeOf(Geometry.TYPES.CURVE) ) {
219
//            if (line != null) {
220
//                return line.isOneDotOrPixel(geom, positionOfDotOrPixel, viewPort, dpi);
221
//                        }
222
//            
223
//        } else if( geomType.isSubTypeOf(Geometry.TYPES.SURFACE) ) {
224
//                        if (fill != null) {
225
//                                return fill.isOneDotOrPixel(geom, positionOfDotOrPixel, viewPort, dpi);
226
//                        }
227
//            
228
//        }
229
//                if (!geom.isSimple()){
230
//                        Aggregate aggregate = (Aggregate)geom;                        
231
//                        for (int i=0 ; i<aggregate.getPrimitivesNumber() ; i++){
232
//                                if (!isOneDotOrPixel(aggregate.getPrimitiveAt(i), positionOfDotOrPixel, viewPort, dpi)){
233
//                                        return false;
234
//                                }
235
//                        }
236
//                        return true;
237
//                }
238
//                return true;
239
//        }
240

    
241
//        @Override
242
//        public void getPixExtentPlus(Geometry geom, float[] distances,
243
//                        ViewPort viewPort, int dpi) {
244
//                // TODO Implement it
245
//                throw new Error("Not yet implemented!");
246
//
247
//        }
248

    
249
//        @Override
250
//        public int getOnePointRgb() {
251
//                // will return a mixture of all symbol's getOnePointRgb() value
252
//
253
//                int rMarker = 0;
254
//                int gMarker = 0;
255
//                int bMarker = 0;
256
//                int aMarker = 0;
257
//
258
//                if (marker!=null && marker.getColor() != null) {
259
//                        rMarker = marker.getColor().getRed();
260
//                        gMarker = marker.getColor().getGreen();
261
//                        bMarker = marker.getColor().getBlue();
262
//                        aMarker = marker.getColor().getAlpha();
263
//                }
264
//
265
//                int rLine = 0;
266
//                int gLine = 0;
267
//                int bLine = 0;
268
//                int aLine = 0;
269
//
270
//                if (line != null  && line.getColor() != null) {
271
//                        rLine = line.getColor().getRed();
272
//                        gLine = line.getColor().getGreen();
273
//                        bLine = line.getColor().getBlue();
274
//                        aLine = line.getColor().getAlpha();
275
//                }
276
//
277
//                int rFill = 0;
278
//                int gFill = 0;
279
//                int bFill = 0;
280
//                int aFill = 0;
281
//
282
//                if (fill != null ) {
283
//                        Color colorOfFill = null;
284
//                        if (fill.getOutline()!=null) {
285
//                                colorOfFill = fill.getOutline().getColor();
286
//                        } else if (fill.getFillColor()!=null) {
287
//                                colorOfFill = fill.getFillColor();
288
//                        }
289
//                        if (colorOfFill != null) {
290
//                                rFill = colorOfFill.getRed();
291
//                                gFill = colorOfFill.getGreen();
292
//                                bFill = colorOfFill.getBlue();
293
//                                aFill = colorOfFill.getAlpha();
294
//
295
//                        }
296
//                }
297
//
298
//                int red = (rMarker + rLine + rFill) / 3;
299
//                int green = (gMarker + gLine + gFill) / 3;
300
//                int blue = (bMarker + bLine + bFill) / 3;
301
//                int alpha = (aMarker + aLine + aFill) / 3;
302
//
303
//                return (alpha) << 24 + (red << 16) + (green << 8) + blue;
304
//        }
305

    
306
        @Override
307
        public boolean isShapeVisible() {
308
                if (marker!=null) {
309
                        return marker.isShapeVisible();
310
                }
311

    
312
                if (line != null) {
313
                        return line.isShapeVisible();
314
                }
315

    
316
                if (fill != null) {
317
                        fill.isShapeVisible();
318
                }
319

    
320
                return false;
321
        }
322

    
323
        @Override
324
        public int getSymbolType() {
325
                return Geometry.TYPES.GEOMETRY;
326
        }
327

    
328
        @Override
329
        public boolean isSuitableFor(Geometry geom) {
330
                // suitable for everything (why else does it exist?)
331
                return true;
332
        }
333

    
334
    @Override
335
        public double getRotation() {
336
                if (marker != null) {
337
                        return marker.getRotation();
338
                }
339
                return 0;
340
        }
341

    
342
        @Override
343
        public void setRotation(double rotation) {
344
                if (marker != null) {
345
                        marker.setRotation(rotation);
346
                }
347
        }
348

    
349
        @Override
350
        public Point2D getOffset() {
351
                if (marker != null) {
352
                        return marker.getOffset();
353
                }
354
                return new Point2D.Double();
355
        }
356

    
357
        @Override
358
        public void setOffset(Point2D offset) {
359
                if (marker != null) {
360
                        marker.setOffset(offset);
361
                }
362
        }
363

    
364
        @Override
365
        public double getSize() {
366
                if (marker != null) {
367
                        return marker.getSize();
368
                }
369
                return 0;
370
        }
371

    
372
        @Override
373
        public void setSize(double size) {
374
                if (marker != null) {
375
                        marker.setSize(size);
376
                }
377

    
378
        }
379

    
380
        @Override
381
        public Color getColor() {
382
                if (marker != null) {
383
                        return marker.getColor();
384
                }
385
                return null;
386
        }
387

    
388
        @Override
389
        public void setColor(Color color) {
390
                if (marker != null) {
391
                        marker.setColor(color);
392
                }
393
        }
394

    
395
        @Override
396
        public void setFillColor(Color color) {
397
                if (fill != null) {
398
                        fill.setFillColor(color);
399
                }
400
        }
401

    
402
        @Override
403
        public void setOutline(ILineSymbol outline) {
404
                if (fill != null) {
405
                        fill.setOutline(outline);
406
                }
407
        }
408

    
409
        @Override
410
        public Color getFillColor() {
411
                if (fill != null) {
412
                        return fill.getFillColor();
413
                }
414
                return null;
415
        }
416

    
417
        @Override
418
        public ILineSymbol getOutline() {
419
                if (fill != null) {
420
                        return fill.getOutline();
421
                }
422
                return null;
423
        }
424

    
425
        @Override
426
        public int getFillAlpha() {
427
                if (fill != null) {
428
                        return fill.getFillAlpha();
429
                }
430
                return 255;
431
        }
432

    
433
        @Override
434
        public IMask getMask() {
435
                return mask;
436
        }
437

    
438
        @Override
439
        public void setUnit(int unitIndex) {
440
                if (marker != null) {
441
                        marker.setUnit(unitIndex);
442
                }
443
                if (line != null) {
444
                        line.setUnit(unitIndex);
445
                }
446
        }
447

    
448
    @Override
449
        public int getUnit() {
450
                if (marker != null) {
451
                        return marker.getUnit();
452
                }
453
                if (line != null) {
454
                        return line.getUnit();
455
                }
456
                return -1;
457
        }
458

    
459
    @Override
460
        public void setMask(IMask mask) {
461
                throw new NotYetImplemented();
462

    
463
        }
464

    
465
    @Override
466
        public int getReferenceSystem() {
467
                return this.referenceSystem;
468
        }
469

    
470
    @Override
471
        public void setReferenceSystem(int system) {
472
                this.referenceSystem = system;
473

    
474
        }
475

    
476
    @Override
477
        public IMarkerSymbol getMarkerSymbol() {
478
                return marker;
479
        }
480

    
481
    @Override
482
        public ILineSymbol getLineSymbol() {
483
                return line;
484
        }
485

    
486
    @Override
487
        public IFillSymbol getFillSymbol() {
488
                return fill;
489
        }
490

    
491
    @Override
492
        public void setMarkerSymbol(IMarkerSymbol markerSymbol) {
493
                this.marker = markerSymbol;
494
        }
495

    
496
    @Override
497
        public void setLineSymbol(ILineSymbol lineSymbol) {
498
                this.line = lineSymbol;
499
        }
500

    
501
    @Override
502
        public void setFillSymbol(IFillSymbol fillFillSymbol) {
503
                this.fill = fillFillSymbol;
504
        }
505

    
506
    @Override
507
        public boolean hasFill() {
508
        if (fill == null) {
509
                return false;
510
        }
511
        return fill.hasFill();
512
        }
513

    
514
    @Override
515
        public void setHasFill(boolean hasFill) {
516
                if (fill != null) {
517
                        fill.setHasFill(hasFill);
518
                }
519
        }
520

    
521
    @Override
522
        public boolean hasOutline() {
523
                return false;
524
        }
525

    
526
    @Override
527
        public void setHasOutline(boolean hasOutline) {
528

    
529
        }
530

    
531
    @Override
532
        public Object clone() throws CloneNotSupportedException {
533
                MultiShapeSymbol copy = (MultiShapeSymbol) super.clone();
534

    
535
                // Clone inner symbols
536
                if (marker != null) {
537
                        copy.marker = (IMarkerSymbol) marker.clone();
538
                }
539
                if (line != null) {
540
                        copy.line = (ILineSymbol) line.clone();
541
                }
542
                if (fill != null) {
543
                        copy.fill = (IFillSymbol) fill.clone();
544
                }
545
                
546
                if (mask != null) {
547
                        copy.mask = (IMask) mask.clone();
548
                }
549
                
550
                // Clone selection symbol
551
                if (symSelect != null) {
552
                        copy.symSelect = (MultiShapeSymbol) symSelect.clone();
553
                }
554
                
555
                return copy;
556
        }
557

    
558
    @Override
559
        public void loadFromState(PersistentState state)
560
                        throws PersistenceException {
561
                setDescription(state.getString(FIELD_DESCRIPTION));
562
                setFillSymbol((IFillSymbol) state.get(FIELD_FILL));
563
                setMarkerSymbol((IMarkerSymbol) state.get(FIELD_MARKER));
564
                setLineSymbol((ILineSymbol) state.get(FIELD_LINE));
565
        }
566

    
567
    @Override
568
        public void saveToState(PersistentState state) throws PersistenceException {
569
                state.set(FIELD_DESCRIPTION, getDescription());
570
                state.set(FIELD_FILL, getFillSymbol());
571
                state.set(FIELD_MARKER, getMarkerSymbol());
572
                state.set(FIELD_LINE, getLineSymbol());
573
        }
574

    
575
        public static class RegisterPersistence implements Callable {
576

    
577
        @Override
578
                public Object call() throws Exception {
579
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
580
                        if( manager.getDefinition(MULTI_SHAPE_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
581
                                DynStruct definition = manager.addDefinition(
582
                                                MultiShapeSymbol.class,
583
                                                MULTI_SHAPE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
584
                                                MULTI_SHAPE_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
585
                                                null, 
586
                                                null
587
                                );
588
                                // Description
589
                                definition.addDynFieldString(FIELD_DESCRIPTION).setMandatory(false);
590
                                // Fill symbol
591
                                definition.addDynFieldObject(FIELD_FILL).setClassOfValue(IFillSymbol.class).setMandatory(true);
592
                                // Line symbol
593
                                definition.addDynFieldObject(FIELD_LINE).setClassOfValue(ILineSymbol.class).setMandatory(true);
594
                                // Marker symbol
595
                                definition.addDynFieldObject(FIELD_MARKER).setClassOfValue(IMarkerSymbol.class).setMandatory(true);
596
                        }
597
                        return Boolean.TRUE;
598
                }
599
                
600
        }
601

    
602
        public static class RegisterSymbol implements Callable {
603

    
604
        @Override
605
                public Object call() throws Exception {
606
                int[] shapeTypes;
607
                SymbolManager manager = MapContextLocator.getSymbolManager();
608
                
609
                shapeTypes = new int[] { Geometry.TYPES.GEOMETRY };
610
                manager.registerSymbol(MultiShapeSymbol.SYMBOL_NAME,
611
                    shapeTypes,
612
                    MultiShapeSymbol.class
613
                );
614
                        return Boolean.TRUE;
615
                }
616
        }
617
}