Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / MarkerFillSymbol.java @ 20768

History | View | Annotate | Download (15.4 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id: MarkerFillSymbol.java 16176 2007-11-08 16:07:26Z jdominguez $
45
* $Log$
46
* Revision 1.19  2007-09-21 12:25:32  jaume
47
* cancellation support extended down to the IGeometry and ISymbol level
48
*
49
* Revision 1.18  2007/09/20 11:53:11  jvidal
50
* bug solved
51
*
52
* Revision 1.16  2007/08/09 10:39:41  jaume
53
* first round of found bugs fixed
54
*
55
* Revision 1.15  2007/08/08 12:04:05  jvidal
56
* javadoc
57
*
58
* Revision 1.14  2007/08/03 09:22:09  jaume
59
* refactored class names
60
*
61
* Revision 1.13  2007/08/02 11:13:50  jaume
62
* char encoding fix
63
*
64
* Revision 1.12  2007/08/01 11:45:59  jaume
65
* passing general tests (drawing test yet missing)
66
*
67
* Revision 1.11  2007/07/23 06:52:25  jaume
68
* default selection color refactored, moved to MapContext
69
*
70
* Revision 1.10  2007/05/28 15:36:42  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.9  2007/05/08 08:47:40  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.8  2007/03/28 16:48:14  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.7  2007/03/26 14:25:17  jaume
80
* implements IPrintable
81
*
82
* Revision 1.6  2007/03/21 17:36:22  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.5  2007/03/13 16:58:36  jaume
86
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
87
*
88
* Revision 1.4  2007/03/09 11:20:57  jaume
89
* Advanced symbology (start committing)
90
*
91
* Revision 1.2.2.4  2007/02/16 10:54:12  jaume
92
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
93
*
94
* Revision 1.2.2.3  2007/02/15 16:23:44  jaume
95
* *** empty log message ***
96
*
97
* Revision 1.2.2.2  2007/02/12 15:15:20  jaume
98
* refactored interval legend and added graduated symbol legend
99
*
100
* Revision 1.2.2.1  2007/02/09 07:47:05  jaume
101
* Isymbol moved
102
*
103
* Revision 1.2  2007/01/10 16:39:41  jaume
104
* ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
105
*
106
* Revision 1.1  2007/01/10 16:31:36  jaume
107
* *** empty log message ***
108
*
109
* Revision 1.10  2006/11/09 18:39:05  jaume
110
* *** empty log message ***
111
*
112
* Revision 1.9  2006/11/09 10:22:50  jaume
113
* *** empty log message ***
114
*
115
* Revision 1.8  2006/11/08 13:05:51  jaume
116
* *** empty log message ***
117
*
118
* Revision 1.7  2006/11/08 10:56:47  jaume
119
* *** empty log message ***
120
*
121
* Revision 1.6  2006/11/07 08:52:30  jaume
122
* *** empty log message ***
123
*
124
* Revision 1.5  2006/11/06 17:08:45  jaume
125
* *** empty log message ***
126
*
127
* Revision 1.4  2006/11/06 16:06:52  jaume
128
* *** empty log message ***
129
*
130
* Revision 1.3  2006/11/06 07:33:54  jaume
131
* javadoc, source style
132
*
133
* Revision 1.2  2006/10/31 16:16:34  jaume
134
* *** empty log message ***
135
*
136
* Revision 1.1  2006/10/30 19:30:35  jaume
137
* *** empty log message ***
138
*
139
*
140
*/
141
package org.gvsig.symbology.fmap.symbols;
142

    
143
import java.awt.Color;
144
import java.awt.Graphics2D;
145
import java.awt.Paint;
146
import java.awt.Rectangle;
147
import java.awt.RenderingHints;
148
import java.awt.TexturePaint;
149
import java.awt.geom.AffineTransform;
150
import java.awt.geom.Point2D;
151
import java.awt.image.BufferedImage;
152
import java.util.Random;
153

    
154
import javax.print.attribute.PrintRequestAttributeSet;
155

    
156
import org.gvsig.symbology.fmap.styles.SimpleMarkerFillPropertiesStyle;
157

    
158
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
159
import com.iver.cit.gvsig.fmap.MapContext;
160
import com.iver.cit.gvsig.fmap.Messages;
161
import com.iver.cit.gvsig.fmap.ViewPort;
162
import com.iver.cit.gvsig.fmap.core.FPoint2D;
163
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
164
import com.iver.cit.gvsig.fmap.core.FShape;
165
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
166
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
167
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
168
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol;
169
import com.iver.cit.gvsig.fmap.core.symbols.AbstractMarkerSymbol;
170
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
171
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
172
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
173
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
174
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
175
import com.iver.utiles.StringUtilities;
176
import com.iver.utiles.XMLEntity;
177
import com.iver.utiles.swing.threads.Cancellable;
178
import com.vividsolutions.jts.geom.Geometry;
179

    
180
/**
181
 * Allows to define a marker symbol of any type as a path image to be used for a filled of a
182
 * polygon's padding
183
 *
184
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
185
 */
186
public class MarkerFillSymbol extends AbstractFillSymbol {
187
        public static final int RANDOM_FILL = 3;
188
        public static final int GRID_FILL = 1;
189
        public static final int SINGLE_CENTERED_SYMBOL = 2;
190
        public static int DefaultFillStyle = GRID_FILL;
191
        private MarkerFillSymbol selectionSymbol;
192
        private IMarkerFillPropertiesStyle markerFillProperties = new SimpleMarkerFillPropertiesStyle();
193
        private IMarkerSymbol markerSymbol = SymbologyFactory.createDefaultMarkerSymbol();
194

    
195
        public ISymbol getSymbolForSelection() {
196
                if (selectionSymbol == null) {
197
                        selectionSymbol = (MarkerFillSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), null);
198
                        selectionSymbol.setFillColor(MapContext.getSelectionColor());
199
                }
200

    
201
                return selectionSymbol;
202
        }
203

    
204
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
205
                switch (markerFillProperties.getFillStyle()) {
206
                case SINGLE_CENTERED_SYMBOL:
207
                        // case a single marker is used into a polygon shapetype
208
                        Geometry geom = FConverter.java2d_to_jts(shp);
209
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
210
                        FPoint2D p = new FPoint2D(new Point2D.Double(
211
                                        centroid.getX()+markerFillProperties.getXOffset(),
212
                                        centroid.getY()+markerFillProperties.getYOffset()));
213
                        markerSymbol.draw(g, affineTransform, p, null);
214
                        break;
215
                case GRID_FILL:
216
                        // case a grid fill is used
217
                        {
218
                        g.setClip(shp);
219
                        int size = (int) markerSymbol.getSize();
220
                        Rectangle rProv = new Rectangle();
221
                        rProv.setFrame(0, 0, size, size);
222
                        Paint resulPatternFill = null;
223

    
224
                        BufferedImage sample = null;
225
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
226
                        Graphics2D gAux = sample.createGraphics();
227

    
228
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
229
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
230
                        double xOffset = markerFillProperties.getXOffset();
231
                        double yOffset = markerFillProperties.getYOffset();
232

    
233

    
234
                        try {
235
                                markerSymbol.drawInsideRectangle(gAux, gAux.getTransform(), rProv);
236
                        } catch (SymbolDrawingException e) {
237
                                if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
238
                                        try {
239
                                                SymbologyFactory.getWarningSymbol(
240
                                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
241
                                                                "",
242
                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS).drawInsideRectangle(gAux, gAux.getTransform(), rProv);
243
                                        } catch (SymbolDrawingException e1) {
244
                                                // IMPOSSIBLE TO REACH THIS
245
                                        }
246
                                } else {
247
                                        // should be unreachable code
248
                                        throw new Error(Messages.getString("symbol_shapetype_mismatch"));
249
                                }
250
                        }
251
                        rProv.setRect(0, 0,
252
                                        rProv.getWidth() + xSeparation,
253
                                        rProv.getHeight() + ySeparation);
254

    
255
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
256
                        gAux = bi.createGraphics();
257
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
258

    
259
                        resulPatternFill = new TexturePaint(bi,rProv);
260

    
261
                        g.setColor(null);
262
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
263
                                RenderingHints.VALUE_ANTIALIAS_ON);
264

    
265
                        g.translate(xOffset, yOffset);
266
                        g.setPaint(resulPatternFill);
267
                        g.fill(shp);
268
                        g.translate(-xOffset, -yOffset);
269
                        g.setClip(null);
270

    
271
                        }
272
                        break;
273
                case RANDOM_FILL:
274
                        {
275

    
276
                        double s = markerSymbol.getSize();
277
                        Rectangle r = shp.getBounds();
278
                        int drawCount = (int) (Math.min(r.getWidth(), r.getHeight())/s);
279
                        Random random = new Random();
280

    
281
                        int minx = r.x;
282
                        int miny = r.y;
283
                        int width = r.width;
284
                        int height = r.height;
285

    
286
                        r = new Rectangle();
287
                        g.setClip(shp);
288

    
289
                        for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < drawCount; i++) {
290
                                int x = (int) Math.abs(random.nextDouble() * width);
291
                                int y = (int) Math.abs(random.nextDouble() * height);
292
                                x = x + minx;
293
                                y = y + miny;
294
                                markerSymbol.draw(g, new AffineTransform(), new FPoint2D(x, y), cancel);
295

    
296
                        }
297
                        g.setClip(null);
298
                        }
299
                        break;
300
                }
301
                if(getOutline()!= null){
302
                        getOutline().draw(g, affineTransform, shp, cancel);
303
                }
304
                
305
        }
306

    
307
        public XMLEntity getXMLEntity() {
308
                XMLEntity xml = new XMLEntity();
309
                xml.putProperty("className", getClassName());
310
                xml.putProperty("isShapeVisible", isShapeVisible());
311
                // color (necessite)
312
                if (getFillColor() !=null)
313
                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
314
                xml.putProperty("desc", getDescription());
315
                xml.putProperty("referenceSystem", getReferenceSystem());
316
                xml.putProperty("unit", getUnit());
317
                xml.addChild(markerSymbol.getXMLEntity());
318
                xml.addChild(markerFillProperties.getXMLEntity());
319
                
320
                if (getOutline()!=null) {
321
                        XMLEntity outlineXML = getOutline().getXMLEntity();
322
                        outlineXML.putProperty("id", "outline symbol");
323
                        xml.addChild(outlineXML);
324
                }
325
                xml.putProperty("hasOutline", hasOutline());
326
                return xml;
327
        }
328

    
329
        public int getSymbolType() {
330
                return FShape.POLYGON;
331
        }
332

    
333
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
334
                markerFillProperties.setSampleSymbol(markerSymbol);
335
                switch (markerFillProperties.getFillStyle()) {
336
                case SINGLE_CENTERED_SYMBOL:
337
                        FPoint2D p = new FPoint2D(r.getCenterX(), r.getCenterY());
338
                        markerSymbol.draw(g, null, p, null);
339
                        break;
340
                case GRID_FILL:
341
                {
342
                        g.setClip(r);
343
                        int size = (int) markerSymbol.getSize();
344
                        if (size <= 0 ) size = 1;
345
                        Rectangle rProv = new Rectangle();
346
                        rProv.setFrame(0, 0, size, size);
347
                        Paint resulPatternFill = null;
348

    
349
                        BufferedImage sample = null;
350
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
351
                        Graphics2D gAux = sample.createGraphics();
352

    
353
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
354
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
355
                        double xOffset = markerFillProperties.getXOffset();
356
                        double yOffset = markerFillProperties.getYOffset();
357

    
358
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
359

    
360
                        rProv.setRect(0, 0,
361
                                        rProv.getWidth() + xSeparation,
362
                                        rProv.getHeight() + ySeparation);
363

    
364
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
365
                        gAux = bi.createGraphics();
366
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
367

    
368

    
369
                        resulPatternFill = new TexturePaint(bi,rProv);
370
                        g.setColor(null);
371
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
372
                                        RenderingHints.VALUE_ANTIALIAS_ON);
373

    
374
                        g.translate(xOffset, yOffset);
375
                        g.setPaint(resulPatternFill);
376
                        g.fill(r);
377
                        g.translate(-xOffset, -yOffset);
378
                        g.setClip(null);
379
                }
380
                        break;
381
                case RANDOM_FILL:
382
                        g.setClip(r);
383
                        int x = r.x;
384
                        int y = r.y;
385
                        int width = r.width;
386
                        int height= r.height;
387
                        g.setBackground(null);
388

    
389
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.2), (y+height*0.8)), null);
390
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.634), (y+height*0.3)), null);
391
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.26), (y+height*0.35)), null);
392
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.45), (y+height*0.98)), null);
393
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.9), (y+height*0.54)), null);
394
                        markerSymbol.draw(g, null, new FPoint2D((x+width*1.1), (y+height*0.7)), null);
395
                        g.setClip(null);
396
                        break;
397
                }
398
                if(getOutline()!= null && hasOutline()){
399
                        getOutline().draw(g, scaleInstance, new FPolyline2D(new GeneralPathX(r)), null);
400
                }
401
        }
402

    
403

    
404
        public String getClassName() {
405
                return getClass().getName();
406
        }
407

    
408
        public void setXMLEntity(XMLEntity xml) {
409
                setDescription(xml.getStringProperty("desc"));
410
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
411

    
412
                markerSymbol = (AbstractMarkerSymbol) SymbologyFactory.
413
                                                        createSymbolFromXML(xml.getChild(0), null);
414
                markerFillProperties = (SimpleMarkerFillPropertiesStyle) SymbologyFactory.
415
                                                        createStyleFromXML(xml.getChild(1), null);
416
                
417
                if (xml.contains("unit")) { // remove this line when done
418
                // measure unit (for outline)
419
                setUnit(xml.getIntProperty("unit"));
420
                
421
                // reference system (for outline)
422
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
423
                }
424
                
425
                if(xml.contains("hasOutline")) {
426
                        XMLEntity outlineXML = xml.firstChild("id", "outline symbol");
427
                        if (outlineXML != null) {
428
                                setOutline((ILineSymbol) SymbologyFactory.createSymbolFromXML(outlineXML, "outline"));
429
                        }
430
                }
431
        }
432

    
433
        public void setMarker(IMarkerSymbol marker) {
434
                this.markerSymbol = marker;
435
        }
436

    
437
        public IMarkerSymbol getMarker() {
438
                return markerSymbol;
439
        }
440

    
441
        public Color getFillColor(){
442
                return markerSymbol.getColor();
443
        }
444

    
445
        public void setFillColor (Color color) {
446
                markerSymbol.setColor(color);
447
        }
448
        
449
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) throws ReadDriverException {
450
                // TODO Implement it
451
                throw new Error("Not yet implemented!");
452

    
453
        }
454
        /**
455
         * Sets the markerfillproperties to be used by the class
456
         *
457
         * @param markerFillStyle,IMarkerFillPropertiesStyle
458
         */
459
        public void setMarkerFillProperties(IMarkerFillPropertiesStyle markerFillStyle) {
460
                this.markerFillProperties = markerFillStyle;
461
        }
462

    
463
        /**
464
         * Returns the markerfillproperties that are used by the class
465
         *
466
         * @return markerFillProperties,IMarkerFillPropertiesStyle
467
         */
468
        public IMarkerFillPropertiesStyle getMarkerFillProperties() {
469
                return markerFillProperties;
470
        }
471

    
472
        
473
        @Override
474
        public void setUnit(int unitIndex) {
475
                super.setUnit(unitIndex);
476
                if (getMarker()!=null) {
477
                        getMarker().setUnit(unitIndex);
478
                }
479
        }
480
        
481
        @Override
482
        public void setReferenceSystem(int system) {
483
                super.setReferenceSystem(system);
484
                if (getMarker()!=null) {
485
                        getMarker().setReferenceSystem(system);
486
                }
487
        }
488
        
489
        public void setCartographicSize(double cartographicSize, FShape shp) {
490
                super.setCartographicSize(cartographicSize, shp);
491
                if (getMarker()!=null) {
492
                        getMarker().setCartographicSize(cartographicSize, shp);
493
                }
494
        }
495
        
496
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
497
                double s = super.toCartographicSize(viewPort, dpi, shp);
498
                if (getMarker()!=null) {
499
                        s = getMarker().toCartographicSize(viewPort, dpi, shp);
500
                }
501
                return s;
502
        }
503

    
504
}