Statistics
| Revision:

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

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.hardcode.gdbms.driver.exceptions.ReadDriverException;
13
import com.iver.cit.gvsig.fmap.ViewPort;
14
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
15
import com.iver.cit.gvsig.fmap.core.FShape;
16
import com.iver.cit.gvsig.fmap.core.IGeometry;
17
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
18
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
19
import com.iver.cit.gvsig.fmap.core.styles.IMask;
20
import com.iver.utiles.XMLEntity;
21
import com.iver.utiles.swing.threads.Cancellable;
22

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

    
31
public class MultiShapeSymbol implements ILineSymbol, IMarkerSymbol, IFillSymbol {
32
        private IMarkerSymbol marker = SymbologyFactory.createDefaultMarkerSymbol();
33
        private ILineSymbol line = SymbologyFactory.createDefaultLineSymbol();
34
        private IFillSymbol fill = SymbologyFactory.createDefaultFillSymbol();
35
        private IMask mask;
36
        private String desc;
37
        private int referenceSystem;
38
        private MultiShapeSymbol symSelect;
39
        
40
        public Color getLineColor() {
41
                return line.getColor();
42
        }
43

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

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

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

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

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

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

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

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

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

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

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

    
89
                return symSelect;
90

    
91
        }
92

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

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

    
119

    
120
        }
121

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

    
127
        }
128

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

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

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

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

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

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

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

    
174
                        }
175
                }
176

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

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

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

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

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

    
203
                if (fill!=null) {
204
                        XMLEntity fillXML = fill.getXMLEntity();
205
                        fillXML.putProperty("id", "fill");
206
                        xml.addChild(fillXML);
207
                        
208
        
209
                }
210
                return xml;
211
        }
212

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

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

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

    
228
                myXML = xml.firstChild("id", "fill");
229
                if (myXML != null)
230
                        fill = (IFillSymbol) SymbologyFactory.
231
                                                createSymbolFromXML(myXML, null);
232
                
233
                
234
        }
235

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

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

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

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

    
251
                return false;
252
        }
253

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

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

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

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

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

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

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

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

    
290
                }
291
        }
292

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

    
297
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
298
                        throws ReadDriverException {
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
}