Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / SymbologyFactory.java @ 29132

History | View | Annotate | Download (22.6 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: SymbologyFactory.java 29132 2009-06-02 09:57:46Z vcaballero $
45
* $Log$
46
* Revision 1.11  2007-09-20 09:33:15  jaume
47
* Refactored: fixed name of IPersistAnce to IPersistence
48
*
49
* Revision 1.10  2007/09/17 14:16:11  jaume
50
* multilayer symbols sizing bug fixed
51
*
52
* Revision 1.9  2007/09/17 09:32:05  jaume
53
* view refresh frame rate now configurable
54
*
55
* Revision 1.8  2007/07/18 06:54:34  jaume
56
* continuing with cartographic support
57
*
58
* Revision 1.7  2007/04/26 11:41:00  jaume
59
* attempting to let defining size in world units
60
*
61
* Revision 1.6  2007/03/27 09:28:40  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.5  2007/03/21 11:02:51  jaume
65
* javadoc
66
*
67
* Revision 1.4  2007/03/20 15:59:03  jaume
68
* improved factory toolkit
69
*
70
* Revision 1.3  2007/03/13 16:58:36  jaume
71
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
72
*
73
* Revision 1.2  2007/03/09 11:20:57  jaume
74
* Advanced symbology (start committing)
75
*
76
* Revision 1.1.2.2  2007/02/15 16:23:44  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.1.2.1  2007/02/12 15:15:20  jaume
80
* refactored interval legend and added graduated symbol legend
81
*
82
* Revision 1.1.2.3  2007/02/09 07:47:05  jaume
83
* Isymbol moved
84
*
85
* Revision 1.1.2.2  2007/02/01 11:42:47  jaume
86
* *** empty log message ***
87
*
88
* Revision 1.1.2.1  2007/01/26 13:48:05  jaume
89
* patch for opening old projects
90
*
91
* Revision 1.1  2007/01/10 16:39:41  jaume
92
* ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
93
*
94
* Revision 1.3  2006/11/06 16:06:52  jaume
95
* *** empty log message ***
96
*
97
* Revision 1.2  2006/11/06 07:33:54  jaume
98
* javadoc, source style
99
*
100
* Revision 1.1  2006/10/30 19:30:35  jaume
101
* *** empty log message ***
102
*
103
*
104
*/
105
package com.iver.cit.gvsig.fmap.core;
106

    
107
import java.awt.BasicStroke;
108
import java.awt.Color;
109
import java.awt.Font;
110
import java.awt.Graphics;
111
import java.awt.Graphics2D;
112
import java.awt.Rectangle;
113
import java.awt.Stroke;
114
import java.awt.geom.AffineTransform;
115
import java.awt.geom.Ellipse2D;
116
import java.io.File;
117
import java.io.IOException;
118

    
119
import javax.print.attribute.PrintRequestAttributeSet;
120
import javax.swing.JFrame;
121
import javax.swing.JPanel;
122

    
123
import org.apache.log4j.Logger;
124

    
125
import com.iver.cit.gvsig.fmap.Messages;
126
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
127
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle;
128
import com.iver.cit.gvsig.fmap.core.symbols.AbstractSymbol;
129
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
130
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
131
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
132
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
133
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
134
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
135
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol;
136
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerLineSymbol;
137
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerMarkerSymbol;
138
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
139
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
140
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
141
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
142
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
143
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
144
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
145
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
146
import com.iver.utiles.IPersistence;
147
import com.iver.utiles.NotExistInXMLEntity;
148
import com.iver.utiles.XMLEntity;
149
import com.iver.utiles.swing.threads.Cancellable;
150

    
151
/**
152
 * Factory for obtaining symbology of any kind from several sources like.
153
 * <ol>
154
 *         <li>
155
 *                 <b>XMLEntity's</b> that, at least, contains a full class name
156
 *                         string property that defines which class handles such symbol.
157
 *  </li>
158
 * </ol>
159
 *
160
 * @author jaume dominguez faus - jaume.dominguez@iver.es
161
 */
162
public class SymbologyFactory {
163

    
164
        /**
165
         * This Constants are only used in SymbologyPage (preferences page for symbology),
166
         * do not use in other context.
167
         */
168
        public static final Color FactoryDefaultSymbolColor = Color.GRAY;
169
        public static final Color FactoryDefaultFillSymbolColor = new Color(60, 235, 235);
170
        public static final Font FactoryDefaultTextFont =  new Font("Serif", Font.PLAIN, 8);
171
        public static final String FactorySymbolLibraryPath =
172
                System.getProperty("user.home") +
173
                File.separator +
174
                "gvSIG" +
175
                File.separator +
176
                "Symbols";
177

    
178
        public static final String FactoryStyleLibraryPath =
179
                System.getProperty("user.home") +
180
                File.separator +
181
                "gvSIG" +
182
                File.separator +
183
                "Styles";
184

    
185
        public static final Boolean FactoryDefaultAleatoryFillColor = false;
186

    
187
        public static Color DefaultSymbolColor = FactoryDefaultSymbolColor;
188
        public static Color DefaultFillSymbolColor = FactoryDefaultFillSymbolColor;
189
        public static Font DefaultTextFont = FactoryDefaultTextFont;
190
        public static String SymbolLibraryPath = FactorySymbolLibraryPath;
191
        public static String StyleLibraryPath = FactoryStyleLibraryPath;
192

    
193
        public static Boolean DefaultAleatoryFillColor = FactoryDefaultAleatoryFillColor;
194

    
195

    
196

    
197
        private static Logger logger = Logger.getLogger(SymbologyFactory.class.getName());
198
        private static WarningSymbol warningSymbol;
199

    
200
        /**
201
         * Factory that allows to create <b>ISymbol</b>'s from an ISymbol xml
202
         * descriptor. A barely specific XMLEntity object. The string passed in the
203
         * second argument is the description text that will be used in case no description
204
         * is supplied by the symbol's xml descriptor.
205
         *
206
         * @param xml, the symbol's xml descriptor
207
         * @param defaultDescription, a human readable description string for the symbol.
208
         * @return ISymbol
209
         */
210
        public static ISymbol createSymbolFromXML(XMLEntity xml, String defaultDescription) {
211
                if (!xml.contains("desc")) {
212
                        if (defaultDescription == null) defaultDescription = "";
213
                        xml.putProperty("desc", defaultDescription);
214
                }
215
                return (ISymbol) createFromXML(xml);
216
        }
217

    
218
        /**
219
         * Factory that allows to create <b>IStyle</b>'s from an <b>IStyle</b> xml descriptor. A barely
220
         * specific <b>XMLEntity</b> object. The string passed in the second argument is the
221
         * description text that will be used in case no description is supplied by the
222
         * style's xml descriptor.
223
         *
224
         * @param xml, the style's xml descriptor
225
         * @param defaultDescription, a human readable description string for the style
226
         * @return IStyle
227
         */
228
        public static IStyle createStyleFromXML(XMLEntity xml, String defaultDescription) {
229
                if (!xml.contains("desc")) {
230
                        if (defaultDescription == null)
231
                                defaultDescription = "";
232

    
233
                        xml.putProperty("desc", defaultDescription);
234
                }
235
                return (IStyle) createFromXML(xml);
236
        }
237

    
238
        /**
239
         * Creates an <b>Object</b> described by the <b>XMLEntity</b> xml, please reffer to the
240
         * XMLEntity definition contract to know what is the format of the xml argument.
241
         * The result of this method is an <b>Object</b> that you can cast to the type you were
242
         * looking for by means of the xml entity.
243
         *
244
         * @param xml
245
         * @return Object
246
         */
247
        private static Object createFromXML(XMLEntity xml) {
248
                String className = null;
249
                try {
250
                        className = xml.getStringProperty("className");
251
                } catch (NotExistInXMLEntity e) {
252
                        logger.error("Class name not set.\n" +
253
                                                " Maybe you forgot to add the" +
254
                                                " putProperty(\"className\", yourClassName)" +
255
                                                " call in the getXMLEntity method of your class", e);
256
                }
257

    
258

    
259
                Class clazz = null;
260
                IPersistence obj = null;
261
                String s = className;
262

    
263
                try {
264
                        clazz = Class.forName(className);
265

    
266
                        if (xml.contains("desc")) {
267
                                s += " \"" + xml.getStringProperty("desc") +"\"";
268
                        }
269
                        // TODO: Modify the patch the day we deprecate FSymbol
270
                        // begin patch
271
                        if (clazz.equals(FSymbol.class)){
272
                                obj = deriveFSymbol(FSymbol.createFromXML(xml));
273
                        } else {
274
                        // end patch
275

    
276

    
277
                                obj = (IPersistence) clazz.newInstance();
278
                                logger.info(Messages.getString("creating")+"....... "+s);
279
                                try {
280
                                        obj.setXMLEntity(xml);
281
                                } catch (NotExistInXMLEntity neiXML) {
282
                                        logger.error(Messages.getString("failed_creating_object")+": "+s);
283
                                        throw neiXML;
284
                                }
285

    
286
                        }
287

    
288
                } catch (InstantiationException e) {
289
                        logger.error("Trying to instantiate an interface" +
290
                                                " or abstract class + "+className, e);
291
                } catch (IllegalAccessException e) {
292
                        logger.error("IllegalAccessException: does your class have an" +
293
                                        " anonymous constructor?", e);
294
                } catch (ClassNotFoundException e) {
295
                        logger.error("No class called " + className +
296
                                        " was found.\nCheck the following.\n<br>" +
297
                                        "\t- The fullname of the class you're looking " +
298
                                                "for matches the value in the className " +
299
                                                "property of the XMLEntity ("+className+").\n<br>" +
300
                                        "\t- The jar file containing your symbol class is in" +
301
                                                "the application classpath<br>", e);
302
                }
303
                return obj;
304
        }
305

    
306
        private static ISymbol deriveFSymbol(FSymbol fSymbol) {
307
                ISymbol derivedSymbol;
308

    
309
                int symbolType = fSymbol.getSymbolType();
310
                Color color = fSymbol.getColor();
311
                double size = fSymbol.getSize();
312
                if (symbolType == FShape.LINE) {
313
                        ILineSymbol line = SymbologyFactory.createDefaultLineSymbol();
314
                        line.setLineColor(color);
315

    
316
                        SimpleLineStyle lineStyle = new SimpleLineStyle();
317
                        lineStyle.setUnit(-1);//En FSymbol solo estaban contemplados metros en los simbolos puntuales
318
                        lineStyle.setOffset(0);
319
                        lineStyle.setReferenceSystem(CartographicSupport.PAPER);
320
                        Stroke stroke = fSymbol.getStroke();
321
                        BasicStroke bStroke = null;
322
                        if(stroke instanceof BasicStroke){
323
                                bStroke = (BasicStroke) stroke;
324
                        }
325
                        if(bStroke != null){
326
                                lineStyle.setStroke(bStroke);
327
                                lineStyle.setLineWidth(bStroke.getLineWidth());
328
                        } else {
329
                                lineStyle.setLineWidth((float) size);
330
                        }
331

    
332
                        line.setLineStyle(lineStyle);
333
                        derivedSymbol = line;
334

    
335
                } else if (symbolType == FShape.POINT) {
336
                        int unit = fSymbol.isSizeInPixels() ? -1 : 1; // only meters or pixels
337
                        // were supported in
338
                        // FSymbol
339
                        int style = fSymbol.getStyle();
340
                        IMarkerSymbol marker = new SimpleMarkerSymbol();
341
                        SimpleMarkerSymbol sms = (SimpleMarkerSymbol) marker;
342
                        if (style == FConstant.SYMBOL_STYLE_MARKER_CIRCLE) {
343
                                sms.setStyle(SimpleMarkerSymbol.CIRCLE_STYLE);
344
                        } else if (style == FConstant.SYMBOL_STYLE_MARKER_CROSS) {
345
                                sms.setStyle(SimpleMarkerSymbol.CROSS_STYLE);
346
                        } else if (style == FConstant.SYMBOL_STYLE_MARKER_SQUARE) {
347
                                sms.setStyle(SimpleMarkerSymbol.SQUARE_STYLE);
348
                        } else if (style == FConstant.SYMBOL_STYLE_MARKER_TRIANGLE) {
349
                                sms.setStyle(SimpleMarkerSymbol.TRIANGLE_STYLE);
350
                        } else if (style == FConstant.SYMBOL_STYLE_MARKER_IMAGEN) {
351
                                //Como no tenemos en el core ningun simbolo de imagen devolvemos uno circular por defecto
352
                                sms.setStyle(SimpleMarkerSymbol.CIRCLE_STYLE);
353
                        }
354
                        Color outlineColor = fSymbol.getOutlineColor();
355
                        if (outlineColor != null) {
356
                                sms.setOutlined(true);
357
                                sms.setOutlineColor(outlineColor);
358
                        }
359
                        marker.setColor(color);
360
                        marker.setSize(size);
361
                        marker.setUnit(unit);
362
                        marker.setRotation(fSymbol.getRotation());
363
                        derivedSymbol = marker;
364

    
365
                } else if (symbolType == FShape.POLYGON) {
366
                        IFillSymbol fill;
367
                        int fSymbolStyle = fSymbol.getStyle();
368

    
369
                        fill = SymbologyFactory.createDefaultFillSymbol();
370
                        if (fSymbolStyle == FConstant.SYMBOL_STYLE_FILL_TRANSPARENT ||
371
                                        fSymbolStyle == FConstant.SYMBOL_STYLE_DGNSPECIAL) {
372
                                color = null;
373
                        }
374
                        // De momento no tenemos tipos simbolos apropiados en el core de fmap para estos.
375

    
376
                        fill.setUnit(-1);//En FSymbol solo estaban contemplados metros en los simbolos puntuales
377
                        fill.setFillColor(color);
378

    
379
                        if (fSymbol.isOutlined()) {
380
                                // Let's create the outline line symbol
381
                                fSymbol.setSymbolType(FShape.LINE);
382
                                ILineSymbol outline = (ILineSymbol) deriveFSymbol(fSymbol);
383

    
384
                                // restore the old value for symbol type (should be always
385
                                // FShape.POLYGON)
386
                                assert symbolType == FShape.POLYGON;
387
                                fSymbol.setSymbolType(symbolType);
388
                                outline.setLineColor(fSymbol.getOutlineColor());
389
                                fill.setOutline(outline);
390
                        }
391
                        derivedSymbol = fill;
392
                } else if (symbolType == FShape.MULTI) {
393
                        fSymbol.setSymbolType(FShape.LINE);
394
                        ILineSymbol line = (ILineSymbol) deriveFSymbol(fSymbol);
395
                        fSymbol.setSymbolType(FShape.POINT);
396
                        IMarkerSymbol marker = (IMarkerSymbol) deriveFSymbol(fSymbol);
397
                        fSymbol.setSymbolType(FShape.POLYGON);
398
                        IFillSymbol fill = (IFillSymbol) deriveFSymbol(fSymbol);
399
                        assert symbolType == FShape.MULTI;
400
                        fSymbol.setSymbolType(symbolType);
401
                        MultiShapeSymbol multiShapeSymbol = new MultiShapeSymbol();
402
                        multiShapeSymbol.setMarkerSymbol(marker);
403
                        multiShapeSymbol.setLineSymbol(line);
404
                        multiShapeSymbol.setFillSymbol(fill);
405

    
406
                        derivedSymbol = multiShapeSymbol;
407
                } else if (symbolType == FShape.TEXT) {
408
                        ITextSymbol textSym = SymbologyFactory.createDefaultTextSymbol();
409
                        textSym.setTextColor(color);
410
                        derivedSymbol = textSym;
411
                } else {
412
                        throw new Error("FSymbol of type "+symbolType+" cannot be imported yet!");
413
                }
414

    
415
                // establish the general description;
416
                if (derivedSymbol instanceof AbstractSymbol) {
417
                        AbstractSymbol symbol = (AbstractSymbol) derivedSymbol;
418
                        symbol.setIsShapeVisible(true);
419
                        symbol.setDescription(fSymbol.getDescription());
420
                }
421

    
422
                return derivedSymbol;
423
        }
424

    
425
        /**
426
         * Returns a new empty instance of a <b>IMultiLayer</b> that can be one of:
427
         * <b>MultiLayerMarkerSymbol</b>, <b>MultiLayerLineSymbol</b>, or
428
         * <b>MultiLayerFillSymbol</b> depending on the shape type passed.
429
         *
430
         * @param shapeType, one of FShape.POINT, FShape.LINE, or FShape.POLYGON
431
         * @return IMultiLayerSymbol
432
         */
433
        public static IMultiLayerSymbol createEmptyMultiLayerSymbol(int shapeType) {
434
                IMultiLayerSymbol mSym;
435
                switch (shapeType%FShape.Z) {
436
                case FShape.POINT:
437
                        mSym = new MultiLayerMarkerSymbol();
438
                        break;
439
                case FShape.LINE:
440
                        mSym = new MultiLayerLineSymbol();
441
                        break;
442
                case FShape.POLYGON:
443
                        mSym =new MultiLayerFillSymbol();
444
                        break;
445
                default:
446
                        throw new Error("Shape type not yet supported for multilayer symbols");
447
                }
448

    
449
                if (mSym instanceof CartographicSupport) {
450
                        CartographicSupport cs = (CartographicSupport) mSym;
451
                        cs.setUnit(CartographicSupportToolkit.DefaultMeasureUnit);
452
                        cs.setReferenceSystem(CartographicSupportToolkit.DefaultReferenceSystem);
453
                }
454
                return mSym;
455
        }
456

    
457
        /**
458
         * Returns a new instance of an <b>IMarkerSymbol</b>.
459
         * @return IMarkerSymbol, the default symbol for markers
460
         */
461
        public static IMarkerSymbol createDefaultMarkerSymbol() {
462
                SimpleMarkerSymbol sms = new SimpleMarkerSymbol();
463
                sms.setColor(DefaultSymbolColor);
464
                sms.setSize(4);
465
                toDefaultCartographicProperties(sms);
466
                return sms;
467
        }
468

    
469
        /**
470
         * Returns a new instance of an <b>ILineSymbol</b>. A black line.
471
         * @return ILineSymbol, the default symbol for lines.
472
         */
473
        public static ILineSymbol createDefaultLineSymbol() {
474
                SimpleLineSymbol sls = new SimpleLineSymbol();
475
                sls.setLineColor(DefaultSymbolColor);
476
                sls.setLineWidth(1);
477
                toDefaultCartographicProperties(sls);
478
                return sls;
479
        }
480

    
481
        private static void toDefaultCartographicProperties(CartographicSupport cs) {
482
                cs.setUnit(CartographicSupportToolkit.DefaultMeasureUnit);
483
                cs.setReferenceSystem(CartographicSupportToolkit.DefaultReferenceSystem);
484
        }
485

    
486
        /**
487
         * Returns a new instance of an <b>IFillSymbol</b>. Black outline,
488
         * and transparent fill.
489
         * @return IFillSymbol, the default symbol for polygons
490
         */
491
        public static IFillSymbol createDefaultFillSymbol() {
492

    
493
                SimpleFillSymbol sfs = new SimpleFillSymbol();
494

    
495
                // Default symbol for polygons
496
                sfs.setOutline(createDefaultLineSymbol());
497
                sfs.setFillColor(DefaultFillSymbolColor); // transparent fill
498
                toDefaultCartographicProperties(sfs);
499
                return sfs;
500
    }
501

    
502
        /**
503
         * Returns a new instance of an <b>ITextSymbol</b>.
504
         * @return ITextSymbol, the default symbol for texts
505
         */
506
        public static ITextSymbol createDefaultTextSymbol() {
507
                SimpleTextSymbol sts = new SimpleTextSymbol();
508

    
509
                sts.setFont(DefaultTextFont);
510
                sts.setTextColor(DefaultSymbolColor);
511
                toDefaultCartographicProperties(sts);
512

    
513
                return sts;
514
        }
515

    
516
        /**
517
         * Creates a new instance of the default symbol whose type is defined
518
         * by the parameter <b>shapeType</b>
519
         * @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
520
         * FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
521
         * @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
522
         */
523
        public static ISymbol createDefaultSymbolByShapeType(int shapeType) {
524
                if ((shapeType & FShape.POINT)!= 0){
525
                        return createDefaultMarkerSymbol();
526
                } else if ((shapeType & FShape.LINE) != 0){
527
                        return createDefaultLineSymbol();
528
                } else if ((shapeType & FShape.POLYGON) != 0){
529
                        return createDefaultFillSymbol();
530
                } else if ((shapeType & FShape.MULTIPOINT) != 0){
531
                        return createDefaultMarkerSymbol();
532
                } else if ((shapeType & FShape.TEXT) != 0){
533
                        return createDefaultTextSymbol();
534
                } else if ((shapeType & FShape.MULTI) != 0){
535
                        return new MultiShapeSymbol();
536
                } else if (shapeType == FShape.NULL){
537
                        return null;
538
                }
539
                throw new Error("shape type not yet supported");
540
        }
541

    
542
        /**
543
         * Creates a new instance of the default symbol whose type is defined
544
         * by the parameter <b>shapeType</b> and uses the color defined by the
545
         * parameter color.
546
         * @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
547
         * @param color, the color to be applied to the new ISymbol.
548
         *
549
         * FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
550
         * @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
551
         */
552
        public static ISymbol createDefaultSymbolByShapeType(int shapeType, Color color) {
553
                ISymbol sym = createDefaultSymbolByShapeType(shapeType);
554

    
555
                if (sym instanceof IMarkerSymbol) {
556
                        ((IMarkerSymbol) sym).setColor(color);
557
                }
558

    
559
                if (sym instanceof ILineSymbol) {
560
                        ((ILineSymbol) sym).setLineColor(color);
561
                }
562

    
563
                if (sym instanceof IFillSymbol) {
564
                        ((IFillSymbol) sym).setFillColor(color);
565
                }
566

    
567
                if (sym instanceof ITextSymbol) {
568
                        ((ITextSymbol) sym).setTextColor(color);
569
                }
570

    
571
                return sym;
572
        }
573

    
574

    
575
        public static ISymbol getWarningSymbol(String message, String symbolDesc, int symbolDrawExceptionType) {
576
                if (warningSymbol == null) {
577
                        warningSymbol = new WarningSymbol();
578
                }
579
                warningSymbol.setDescription(symbolDesc);
580
                warningSymbol.setMessage(message);
581
                warningSymbol.setDrawExceptionType(symbolDrawExceptionType);
582
                return warningSymbol;
583
        }
584

    
585
        private static class WarningSymbol extends MultiShapeSymbol {
586
                private String desc;
587
                private String message;
588
                private int exceptionType;
589
                private SimpleTextSymbol text;
590

    
591
                public static void main(String[] args) {
592
                        JFrame f = new JFrame();
593
                        final ISymbol warning = SymbologyFactory.getWarningSymbol(SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, "a description", SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
594
                        JPanel preview = new JPanel() {
595
                                @Override
596
                                protected void paintComponent(Graphics g) {
597
                                        // TODO Auto-generated method stub
598
                                        super.paintComponent(g);
599
                                        Graphics2D g2 = (Graphics2D)g;
600
                                        try {
601
                                                warning.drawInsideRectangle(g2, g2.getTransform(), getBounds(), null);
602
                                        } catch (SymbolDrawingException e) {
603
                                                // TODO Auto-generated catch block
604
                                                e.printStackTrace();
605
                                        }
606
                                }
607
                        };
608
                        f.setContentPane(preview);
609
                        f.pack();
610
                        f.setVisible(true);
611
                }
612

    
613
                public void draw(Graphics2D g, AffineTransform affineTransform,
614
                                FShape shp, Cancellable cancel) {
615
                        try {
616
                                drawInsideRectangle(g, g.getTransform(), shp.getBounds(), null);
617
                        } catch (SymbolDrawingException e) {
618
                                // IMPOSSIBLE
619
                        }
620
                }
621

    
622
                public void setDrawExceptionType(int symbolDrawExceptionType) {
623
                        this.exceptionType = symbolDrawExceptionType;
624
                }
625

    
626
                public void setMessage(String message) {
627
                        this.message = message;
628
                }
629

    
630
                public void drawInsideRectangle(Graphics2D g,
631
                                AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties)
632
                                throws SymbolDrawingException {
633
                        g.setClip(r);
634
                        if (message == null) {
635
                                message = "Symbol undrawable.\nPlease, check errors.";
636
                        }
637

    
638

    
639
                        String[] messageLines = message.split("\n");
640
                        int strokeWidth = (int) (Math.min(r.width, r.height)*.1);
641

    
642
                        if (strokeWidth == 0) strokeWidth = 1;
643

    
644
                        g.setColor(Color.red);
645
                        g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
646
                        int x = r.x+strokeWidth;
647
                        int y = r.x+strokeWidth;
648
                        int width = r.width-(strokeWidth+strokeWidth);
649
                        int height = r.height-(strokeWidth+strokeWidth);
650

    
651
                        double radius = Math.min(width, height)*.5;
652
                        double centerX = r.getCenterX();
653
                        double centerY = r.getCenterY();
654
                        Ellipse2D circle = new Ellipse2D.Double(centerX - radius, centerY - radius, 2*radius, 2*radius);
655
                        g.draw(circle);
656
                        g.setClip(circle);
657
                        double aux = Math.cos(Math.PI*0.25)*radius;
658
                        g.drawLine((int) (centerX - aux), (int) (centerY - aux), (int) (centerX + aux), (int) (centerY + aux));
659
                        int fontSize = 20;
660
                        g.setFont(new Font("Arial", fontSize, Font.PLAIN));
661
                        g.setColor(Color.black);
662
                        g.setClip(null);
663

    
664
                        if (text == null) {
665
                                text = new SimpleTextSymbol();
666
                                text.setAutoresizeEnabled(true);
667

    
668
                        }
669

    
670
                        double lineHeight = (r.getHeight()-6)/messageLines.length;
671
                        Rectangle textRect = new Rectangle(
672
                                        (int) r.getMinX(),
673
                                        (int) r.getMinY()+6,
674
                                        (int) r.getWidth(),
675
                                        (int) lineHeight);
676
                        for (int i =0; i < messageLines.length; i++) {
677
                                text.setText(messageLines[i]);
678
                                text.drawInsideRectangle(g, null, textRect, properties);
679
                                textRect.setLocation((int) r.getX(), (int) (r.getY()+r.getHeight()));
680
                        }
681
                }
682

    
683
                @Override
684
                public XMLEntity getXMLEntity() {
685
                        XMLEntity xml = new XMLEntity();
686
                        xml.putProperty("className", getClass().getName());
687
                        xml.putProperty("desc", desc);
688
                        xml.putProperty("exceptionType", exceptionType);
689
                        xml.putProperty("message", message);
690
                        return xml;
691
                }
692

    
693
                @Override
694
                public void setXMLEntity(XMLEntity xml) {
695
                        setDescription(xml.getStringProperty("desc"));
696
                        exceptionType = xml.getIntProperty("exceptionType");
697
                        message = xml.getStringProperty("message");
698
                }
699

    
700
        }
701

    
702

    
703
}