Statistics
| Revision:

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

History | View | Annotate | Download (13 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(r.x, 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
                rect = new Rectangle((int)(r.x+myWidth), r.y, (int) myWidth, r.height);
278

    
279
                if (line != null) {
280
//                        g.translate(r.x+myWidth, r.y);
281
                        line.drawInsideRectangle(g, scaleInstance, rect, properties);
282
//                        g.translate(-(r.x+myWidth), -(r.y));
283
                }
284
                rect = new Rectangle((int)(r.x+myWidth+myWidth), r.y, (int) myWidth, r.height);
285

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

    
291
                }
292
        }
293

    
294
        public String getClassName() {
295
                return getClass().getName();
296
        }
297

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

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

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

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

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

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

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

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

    
357
        }
358

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

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

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

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

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

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

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

    
398
        public IMask getMask() {
399
                return mask;
400
        }
401

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

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

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

    
421
        }
422

    
423
        public int getReferenceSystem() {
424
                return this.referenceSystem;
425
        }
426

    
427
        public void setReferenceSystem(int system) {
428
                this.referenceSystem = system;
429

    
430
        }
431

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

    
452
                }
453
                return -1;
454
        }
455

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

    
480

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

    
510
        public IMarkerSymbol getMarkerSymbol() {
511
                return marker;
512
        }
513

    
514
        public ILineSymbol getLineSymbol() {
515
                return line;
516
        }
517

    
518
        public IFillSymbol getFillSymbol() {
519
                return fill;
520
        }
521

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

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

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

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

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

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

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

    
554
        }
555

    
556
}