Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / MultiShapeSymbol.java @ 28367

History | View | Annotate | Download (12.8 KB)

1
package com.iver.cit.gvsig.fmap.core.symbols;
2

    
3
import java.awt.Color;
4
import java.awt.Graphics2D;
5
import java.awt.Rectangle;
6
import java.awt.geom.AffineTransform;
7
import java.awt.geom.Point2D;
8
import java.util.logging.Logger;
9

    
10
import javax.print.attribute.PrintRequestAttributeSet;
11

    
12
import com.iver.cit.gvsig.fmap.ViewPort;
13
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
14
import com.iver.cit.gvsig.fmap.core.FShape;
15
import com.iver.cit.gvsig.fmap.core.IGeometry;
16
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
17
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
18
import com.iver.cit.gvsig.fmap.core.styles.IMask;
19
import com.iver.utiles.XMLEntity;
20
import com.iver.utiles.swing.threads.Cancellable;
21

    
22
/**
23
 * MultiShapeSymbol class allows to create a composition of several symbols with
24
 * different shapes and be treated as a single symbol.These shapes can be marker,line
25
 * or fill.
26
 *
27
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
28
 */
29

    
30
public class MultiShapeSymbol implements ILineSymbol, IMarkerSymbol, IFillSymbol {
31
        private IMarkerSymbol marker = SymbologyFactory.createDefaultMarkerSymbol();
32
        private ILineSymbol line = SymbologyFactory.createDefaultLineSymbol();
33
        private IFillSymbol fill = SymbologyFactory.createDefaultFillSymbol();
34
        private IMask mask;
35
        private String desc;
36
        private int referenceSystem;
37
        private MultiShapeSymbol symSelect;
38

    
39
        public Color getLineColor() {
40
                return line.getColor();
41
        }
42

    
43
        public void setLineColor(Color color) {
44
                line.setLineColor(color);
45
        }
46

    
47
        public ILineStyle getLineStyle() {
48
                return line.getLineStyle();
49
        }
50

    
51
        public void setLineStyle(ILineStyle lineStyle) {
52
                line.setLineStyle(lineStyle);
53
        }
54

    
55
        public void setLineWidth(double width) {
56
                line.setLineWidth(width);
57
        }
58

    
59
        public double getLineWidth() {
60
                return line.getLineWidth();
61
        }
62

    
63
        public int getAlpha() {
64
                return line.getAlpha();
65
        }
66

    
67
        public void setAlpha(int outlineAlpha) {
68
                line.setAlpha(outlineAlpha);
69
        }
70

    
71
        public ISymbol getSymbolForSelection() {
72
                if (symSelect == null) {
73
                        symSelect = new MultiShapeSymbol();
74
                }
75

    
76
                if (marker!=null){
77
                        symSelect.setMarkerSymbol((IMarkerSymbol) marker.getSymbolForSelection());
78
                }
79

    
80
                if (line!=null){
81
                        symSelect.setLineSymbol((ILineSymbol) line.getSymbolForSelection());
82
                }
83

    
84
                if (fill!=null ){
85
                        symSelect.setFillSymbol((IFillSymbol) fill.getSymbolForSelection());
86
                }
87

    
88
                return symSelect;
89

    
90
        }
91

    
92
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
93
                switch (shp.getShapeType()) {
94
                case FShape.POINT: //Tipo punto
95
        case FShape.POINT + FShape.Z:
96
                if (marker != null)
97
                        marker.draw(g, affineTransform, shp, cancel);
98
                        break;
99
                case FShape.LINE:
100
        case FShape.LINE + FShape.Z:
101
                case FShape.ARC:
102
                case FShape.ARC + FShape.Z:
103
                        if (line != null)
104
                                line.draw(g, affineTransform, shp, cancel);
105
                        break;
106

    
107
                case FShape.POLYGON:
108
        case FShape.POLYGON + FShape.Z:
109
                case FShape.ELLIPSE:
110
                case FShape.ELLIPSE + FShape.Z:
111
        case FShape.CIRCLE:
112
                case FShape.CIRCLE + FShape.Z:
113
                        if (fill != null)
114
                                fill.draw(g, affineTransform, shp, cancel);
115
                        break;
116
                }
117

    
118

    
119
        }
120

    
121
        public void getPixExtentPlus(FShape shp, float[] distances,
122
                        ViewPort viewPort, int dpi) {
123
                // TODO Implement it
124
                throw new Error("Not yet implemented!");
125

    
126
        }
127

    
128
        public int getOnePointRgb() {
129
                // will return a mixture of all symbol's getOnePointRgb() value
130

    
131
                int rMarker = 0;
132
                int gMarker = 0;
133
                int bMarker = 0;
134
                int aMarker = 0;
135

    
136
                if (marker!=null && marker.getColor() != null) {
137
                        rMarker = marker.getColor().getRed();
138
                        gMarker = marker.getColor().getGreen();
139
                        bMarker = marker.getColor().getBlue();
140
                        aMarker = marker.getColor().getAlpha();
141
                }
142

    
143
                int rLine = 0;
144
                int gLine = 0;
145
                int bLine = 0;
146
                int aLine = 0;
147

    
148
                if (line != null  && line.getColor() != null) {
149
                        rLine = line.getColor().getRed();
150
                        gLine = line.getColor().getGreen();
151
                        bLine = line.getColor().getBlue();
152
                        aLine = line.getColor().getAlpha();
153
                }
154

    
155
                int rFill = 0;
156
                int gFill = 0;
157
                int bFill = 0;
158
                int aFill = 0;
159

    
160
                if (fill != null ) {
161
                        Color colorOfFill = null;
162
                        if (fill.getOutline()!=null) {
163
                                colorOfFill = fill.getOutline().getColor();
164
                        } else if (fill.getFillColor()!=null) {
165
                                colorOfFill = fill.getFillColor();
166
                        }
167
                        if (colorOfFill != null) {
168
                                rFill = colorOfFill.getRed();
169
                                gFill = colorOfFill.getGreen();
170
                                bFill = colorOfFill.getBlue();
171
                                aFill = colorOfFill.getAlpha();
172

    
173
                        }
174
                }
175

    
176
                int red = (rMarker + rLine + rFill) / 3;
177
                int green = (gMarker + gLine + gFill) / 3;
178
                int blue = (bMarker + bLine + bFill) / 3;
179
                int alpha = (aMarker + aLine + aFill) / 3;
180

    
181
                return (alpha) << 24 + (red << 16) + (green << 8) + blue;
182
        }
183

    
184
        public XMLEntity getXMLEntity() {
185
                XMLEntity xml = new XMLEntity();
186
                xml.putProperty("className", getClassName());
187
                xml.putProperty("desc", getDescription());
188
                xml.putProperty("unit", getUnit());
189

    
190
                if (marker!=null) {
191
                        XMLEntity markerXML = marker.getXMLEntity();
192
                        markerXML.putProperty("id", "marker");
193
                        xml.addChild(markerXML);
194
                }
195

    
196
                if (line!=null) {
197
                        XMLEntity lineXML = line.getXMLEntity();
198
                        lineXML.putProperty("id", "line");
199
                        xml.addChild(lineXML);
200
                }
201

    
202
                if (fill!=null) {
203
                        XMLEntity fillXML = fill.getXMLEntity();
204
                        fillXML.putProperty("id", "fill");
205
                        xml.addChild(fillXML);
206

    
207

    
208
                }
209
                return xml;
210
        }
211

    
212
        public void setXMLEntity(XMLEntity xml) {
213
                setDescription(xml.getStringProperty("desc"));
214
                setUnit(xml.getIntProperty("unit"));
215

    
216
                XMLEntity myXML;
217
                myXML = xml.firstChild("id", "marker");
218
                if (myXML != null)
219
                        marker = (IMarkerSymbol) SymbologyFactory.
220
                                                createSymbolFromXML(myXML, null);
221

    
222
                myXML = xml.firstChild("id", "line");
223
                if (myXML != null)
224
                        line = (ILineSymbol) SymbologyFactory.
225
                                                createSymbolFromXML(myXML, null);
226

    
227
                myXML = xml.firstChild("id", "fill");
228
                if (myXML != null)
229
                        fill = (IFillSymbol) SymbologyFactory.
230
                                                createSymbolFromXML(myXML, null);
231

    
232

    
233
        }
234

    
235
        public String getDescription() {
236
                return desc;
237
        }
238

    
239
        public boolean isShapeVisible() {
240
                if (marker!=null) {
241
                        return marker.isShapeVisible();
242
                }
243

    
244
                if (line != null)
245
                        return line.isShapeVisible();
246

    
247
                if (fill != null)
248
                        fill.isShapeVisible();
249

    
250
                return false;
251
        }
252

    
253
        public void setDescription(String desc) {
254
                this.desc = desc ;
255
        }
256

    
257
        public int getSymbolType() {
258
                return FShape.MULTI;
259
        }
260

    
261
        public boolean isSuitableFor(IGeometry geom) {
262
                // suitable for everything (why else does it exist?)
263
                return true;
264
        }
265

    
266
        public void drawInsideRectangle(Graphics2D g,
267
                        AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException {
268
                double myWidth =  (r.getWidth()/3);
269

    
270
                Rectangle rect = new Rectangle(0, r.y, (int) myWidth, r.height);
271

    
272
                if (marker != null) {
273
                        g.translate(r.x, r.y);
274
                        marker.drawInsideRectangle(g, scaleInstance, rect, properties);
275
                        g.translate(-(r.x), -(r.y));
276
                }
277

    
278
                if (line != null) {
279
                        g.translate(r.x+myWidth, r.y);
280
                        line.drawInsideRectangle(g, scaleInstance, rect, properties);
281
                        g.translate(-(r.x+myWidth), -(r.y));
282
                }
283

    
284
                if (fill != null) {
285
                        g.translate(r.x+myWidth+myWidth, r.y);
286
                        fill.drawInsideRectangle(g, scaleInstance, rect, properties);
287
                        g.translate(-(r.x+myWidth+myWidth), -(r.y));
288

    
289
                }
290
        }
291

    
292
        public String getClassName() {
293
                return getClass().getName();
294
        }
295

    
296
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) {
297
                switch (shape.getShapeType()) {
298
                case FShape.POINT: //Tipo punto
299
        case FShape.POINT + FShape.Z:
300
                if (marker != null)
301
                        marker.print(g, at, shape, properties);
302
                        break;
303
                case FShape.LINE:
304
        case FShape.LINE + FShape.Z:
305
                case FShape.ARC:
306
                case FShape.ARC + FShape.Z:
307
                        if (line != null)
308
                                line.print(g, at, shape, properties);
309
                        break;
310

    
311
                case FShape.POLYGON:
312
        case FShape.POLYGON + FShape.Z:
313
                case FShape.ELLIPSE:
314
                case FShape.ELLIPSE + FShape.Z:
315
        case FShape.CIRCLE:
316
                case FShape.CIRCLE + FShape.Z:
317
                        if (fill != null)
318
                                fill.print(g, at, shape, properties);
319
                        break;
320
                }
321
        }
322

    
323
        public double getRotation() {
324
                if (marker != null)
325
                        return marker.getRotation();
326
                return 0;
327
        }
328

    
329
        public void setRotation(double rotation) {
330
                if (marker != null)
331
                        marker.setRotation(rotation);
332
        }
333

    
334
        public Point2D getOffset() {
335
                if (marker != null)
336
                        return marker.getOffset();
337
                return new Point2D.Double();
338
        }
339

    
340
        public void setOffset(Point2D offset) {
341
                if (marker != null)
342
                        marker.setOffset(offset);
343
        }
344

    
345
        public double getSize() {
346
                if (marker != null)
347
                        return marker.getSize();
348
                return 0;
349
        }
350

    
351
        public void setSize(double size) {
352
                if (marker != null)
353
                        marker.setSize(size);
354

    
355
        }
356

    
357
        public Color getColor() {
358
                if (marker != null)
359
                        return marker.getColor();
360
                return null;
361
        }
362

    
363
        public void setColor(Color color) {
364
                if (marker != null)
365
                        marker.setColor(color);
366
        }
367

    
368
        public void setFillColor(Color color) {
369
                if (fill != null)
370
                        fill.setFillColor(color);
371
        }
372

    
373
        public void setOutline(ILineSymbol outline) {
374
                if (fill != null)
375
                        fill.setOutline(outline);
376
        }
377

    
378
        public Color getFillColor() {
379
                if (fill != null)
380
                        return fill.getFillColor();
381
                return null;
382
        }
383

    
384
        public ILineSymbol getOutline() {
385
                if (fill != null)
386
                        return fill.getOutline();
387
                return null;
388
        }
389

    
390
        public int getFillAlpha() {
391
                if (fill != null)
392
                        return fill.getFillAlpha();
393
                return 255;
394
        }
395

    
396
        public IMask getMask() {
397
                return mask;
398
        }
399

    
400
        public void setUnit(int unitIndex) {
401
                if (marker != null)
402
                        marker.setUnit(unitIndex);
403
                if (line != null)
404
                        line.setUnit(unitIndex);
405
        }
406

    
407
        public int getUnit() {
408
                if (marker != null)
409
                        return marker.getUnit();
410
                if (line != null)
411
                        return line.getUnit();
412
                return -1;
413
        }
414

    
415
        public void setMask(IMask mask) {
416
                // TODO Implement it
417
                throw new Error("Not yet implemented!");
418

    
419
        }
420

    
421
        public int getReferenceSystem() {
422
                return this.referenceSystem;
423
        }
424

    
425
        public void setReferenceSystem(int system) {
426
                this.referenceSystem = system;
427

    
428
        }
429

    
430
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
431
                switch (shp.getShapeType()) {
432
                case FShape.POINT: //Tipo punto
433
        case FShape.POINT + FShape.Z:
434
                if (marker != null)
435
                        return marker.toCartographicSize(viewPort, dpi, shp);
436
                case FShape.LINE:
437
        case FShape.LINE + FShape.Z:
438
                case FShape.ARC:
439
                case FShape.ARC + FShape.Z:
440
                        if (line != null)
441
                                return line.toCartographicSize(viewPort, dpi, shp);
442
                case FShape.POLYGON:
443
        case FShape.POLYGON + FShape.Z:
444
                case FShape.ELLIPSE:
445
                case FShape.ELLIPSE + FShape.Z:
446
        case FShape.CIRCLE:
447
                case FShape.CIRCLE + FShape.Z:
448
                        Logger.getAnonymousLogger().warning("Cartographic size does not have any sense for fill symbols");
449

    
450
                }
451
                return -1;
452
        }
453

    
454
        public void setCartographicSize(double cartographicSize, FShape shp) {
455
                switch (shp.getShapeType()) {
456
                case FShape.POINT: //Tipo punto
457
        case FShape.POINT + FShape.Z:
458
                if (marker != null)
459
                    marker.setCartographicSize(cartographicSize, null);
460
                break;
461
                case FShape.LINE:
462
        case FShape.LINE + FShape.Z:
463
                case FShape.ARC:
464
                case FShape.ARC + FShape.Z:
465
                        if (line != null)
466
                                line.setCartographicSize(cartographicSize, null);
467
                break;
468
                case FShape.POLYGON:
469
        case FShape.POLYGON + FShape.Z:
470
                case FShape.ELLIPSE:
471
                case FShape.ELLIPSE + FShape.Z:
472
        case FShape.CIRCLE:
473
                case FShape.CIRCLE + FShape.Z:
474
                        Logger.getAnonymousLogger().warning("Cartographic size does not have any sense for fill symbols");
475
                }
476
        }
477

    
478

    
479
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
480
                switch (shp.getShapeType()) {
481
                case FShape.POINT: //Tipo punto
482
        case FShape.POINT + FShape.Z:
483
                return CartographicSupportToolkit.
484
                        getCartographicLength(marker,
485
                                                                  getSize(),
486
                                                                  viewPort,
487
                                                                  dpi);
488
                case FShape.LINE:
489
        case FShape.LINE + FShape.Z:
490
                case FShape.ARC:
491
                case FShape.ARC + FShape.Z:
492
                        return CartographicSupportToolkit.
493
                        getCartographicLength(line,
494
                                                                  getSize(),
495
                                                                  viewPort,
496
                                                                  dpi);
497
                case FShape.POLYGON:
498
        case FShape.POLYGON + FShape.Z:
499
                case FShape.ELLIPSE:
500
                case FShape.ELLIPSE + FShape.Z:
501
        case FShape.CIRCLE:
502
                case FShape.CIRCLE + FShape.Z:
503
                        Logger.getAnonymousLogger().warning("Cartographic size does not have any sense for fill symbols");
504
                }
505
                return -1;
506
        }
507

    
508
        public IMarkerSymbol getMarkerSymbol() {
509
                return marker;
510
        }
511

    
512
        public ILineSymbol getLineSymbol() {
513
                return line;
514
        }
515

    
516
        public IFillSymbol getFillSymbol() {
517
                return fill;
518
        }
519

    
520
        public void setMarkerSymbol(IMarkerSymbol markerSymbol) {
521
                this.marker = markerSymbol;
522
        }
523

    
524
        public void setLineSymbol(ILineSymbol lineSymbol) {
525
                this.line = lineSymbol;
526
        }
527

    
528
        public void setFillSymbol(IFillSymbol fillFillSymbol) {
529
                this.fill = fillFillSymbol;
530
        }
531

    
532
        public boolean hasFill() {
533
        if (fill == null) return false;
534
        return fill.hasFill();
535
        }
536

    
537
        public void setHasFill(boolean hasFill) {
538
                if (fill != null) {
539
                        fill.setHasFill(hasFill);
540
//                        this.hasFill = hasFill;
541
                }
542
        }
543

    
544
        public boolean hasOutline() {
545
                // TODO Auto-generated method stub
546
                return false;
547
        }
548

    
549
        public void setHasOutline(boolean hasOutline) {
550
                // TODO Auto-generated method stub
551

    
552
        }
553

    
554
}