Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / SymbologyFactory.java @ 13733

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

    
101
import java.awt.Color;
102
import java.awt.Font;
103

    
104
import org.apache.log4j.Logger;
105

    
106
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
107
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
108
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
109
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
110
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
111
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
112
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
113
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol;
114
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerLineSymbol;
115
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerMarkerSymbol;
116
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
117
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
118
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
119
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
120
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
121
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
122
import com.iver.utiles.IPersistance;
123
import com.iver.utiles.NotExistInXMLEntity;
124
import com.iver.utiles.XMLEntity;
125

    
126
/**
127
 * Factory for obtaining symbology of any kind from several sources like.
128
 * <ol>
129
 *         <li>
130
 *                 <b>XMLEntity's</b> that, at least, contains a full class name
131
 *                         string property that defines which class handles such symbol.
132
 *  </li>
133
 * </ol>
134
 *
135
 * @author jaume dominguez faus - jaume.dominguez@iver.es
136
 */
137
public class SymbologyFactory {
138
        public static Color DefaultSymbolColor = Color.DARK_GRAY;
139
        public static Color DefaultFillSymbolColor = new Color(60, 235, 235);
140
        public static Font DefaultTextFont = new Font("SansSerif", Font.PLAIN, 9);
141
        private static Logger logger = Logger.getLogger(SymbologyFactory.class.getName());
142

    
143
        /**
144
         * Factory that allows to create <b>ISymbol</b>'s from an ISymbol xml
145
         * descriptor. A barely specific XMLEntity object. The string passed in the
146
         * second argument is the description text that will be used in case no description
147
         * is supplied by the symbol's xml descriptor.
148
         *
149
         * @param xml, the symbol's xml descriptor
150
         * @param defaultDescription, a human readable description string for the symbol.
151
         * @return ISymbol
152
         */
153
        public static ISymbol createSymbolFromXML(XMLEntity xml, String defaultDescription) {
154
                if (!xml.contains("desc")) {
155
                        if (defaultDescription == null) defaultDescription = "";
156
                        xml.putProperty("desc", defaultDescription);
157
                }
158
                return (ISymbol) createFromXML(xml);
159
        }
160

    
161
        /**
162
         * Factory that allows to create <b>IStyle</b>'s from an <b>IStyle</b> xml descriptor. A barely
163
         * specific <b>XMLEntity</b> object. The string passed in the second argument is the
164
         * description text that will be used in case no description is supplied by the
165
         * style's xml descriptor.
166
         *
167
         * @param xml, the style's xml descriptor
168
         * @param defaultDescription, a human readable description string for the style
169
         * @return IStyle
170
         */
171
        public static IStyle createStyleFromXML(XMLEntity xml, String defaultDescription) {
172
                if (!xml.contains("desc"))
173
                        if (defaultDescription == null) defaultDescription = "";
174
                        xml.putProperty("desc", defaultDescription);
175
                return (IStyle) createFromXML(xml);
176
        }
177

    
178
        /**
179
         * Creates an <b>Object</b> described by the <b>XMLEntity</b> xml, please reffer to the
180
         * XMLEntity definition contract to know what is the format of the xml argument.
181
         * The result of this method is an <b>Object</b> that you can cast to the type you were
182
         * looking for by means of the xml entity.
183
         *
184
         * @param xml
185
         * @return Object
186
         */
187
        private static Object createFromXML(XMLEntity xml) {
188
                String className = null;
189
                try {
190
                        className = xml.getStringProperty("className");
191
                } catch (NotExistInXMLEntity e) {
192
                        logger.error("Class name not set.\n" +
193
                                                " Maybe you forgot to add the" +
194
                                                " putProperty(\"className\", yourClassName)" +
195
                                                " call in the getXMLEntity method of your class", e);
196
                }
197

    
198

    
199
                Class clazz = null;
200
                IPersistance obj = null;
201

    
202
                try {
203
                        clazz = Class.forName(className);
204

    
205
                        // TODO remove the patch the day we deprecate FSymbol
206
                        // begin patch
207
                        if (clazz.equals(FSymbol.class))
208
                                obj = FSymbol.createFromXML(xml);
209
                        else {
210
                        // end patch
211

    
212

    
213
                                obj = (IPersistance) clazz.newInstance();
214
                                obj.setXMLEntity(xml);
215
                        }
216

    
217
                } catch (InstantiationException e) {
218
                        logger.error("Trying to instantiate an interface" +
219
                                                " or abstract class + "+className, e);
220
                } catch (IllegalAccessException e) {
221
                        logger.error("IllegalAccessException: does your class have an" +
222
                                        " anonymous constructor?", e);
223
                } catch (ClassNotFoundException e) {
224
                        logger.error("No class called " + className +
225
                                        " was found.\nCheck the following.\n<br>" +
226
                                        "\t- The fullname of the class you're looking " +
227
                                                "for matches the value in the className " +
228
                                                "property of the XMLEntity ("+className+").\n<br>" +
229
                                        "\t- The jar file containing your symbol class is in" +
230
                                                "the application classpath<br>", e);
231
                }
232
                return obj;
233
        }
234

    
235
        /**
236
         * Returns a new empty instance of a <b>IMultiLayer</b> that can be one of:
237
         * <b>MultiLayerMarkerSymbol</b>, <b>MultiLayerLineSymbol</b>, or
238
         * <b>MultiLayerFillSymbol</b> depending on the shape type passed.
239
         *
240
         * @param shapeType, one of FShape.POINT, FShape.LINE, or FShape.POLYGON
241
         * @return IMultiLayerSymbol
242
         */
243
        public static IMultiLayerSymbol createEmptyMultiLayerSymbol(int shapeType) {
244
                switch (shapeType) {
245
                case FShape.POINT:
246
                        return new MultiLayerMarkerSymbol();
247
                case FShape.LINE:
248
                        return new MultiLayerLineSymbol();
249
                case FShape.POLYGON:
250
                        return new MultiLayerFillSymbol();
251
                default:
252
                        throw new Error("Shape type not yet supported for multilayer symbols");
253
                }
254
        }
255

    
256
        /**
257
         * Returns a new instance of an <b>IMarkerSymbol</b>.
258
         * @return IMarkerSymbol, the default symbol for markers
259
         */
260
        public static IMarkerSymbol createDefaultMarkerSymbol() {
261
                SimpleMarkerSymbol sms = new SimpleMarkerSymbol();
262
                sms.setColor(DefaultSymbolColor);
263
                sms.setSize(2);
264
                sms.setUnit(-1); // pixels
265
                return sms;
266
        }
267

    
268
        /**
269
         * Returns a new instance of an <b>ILineSymbol</b>. A black line.
270
         * @return ILineSymbol, the default symbol for lines.
271
         */
272
        public static ILineSymbol createDefaultLineSymbol() {
273
                SimpleLineSymbol sls = new SimpleLineSymbol();
274
                sls.setLineColor(DefaultSymbolColor);
275
                sls.setLineWidth(1);
276
                sls.setUnit(-1); // Pixels
277
                return sls;
278
        }
279

    
280
        /**
281
         * Returns a new instance of an <b>IFillSymbol</b>. Black outline,
282
         * and transparent fill.
283
         * @return IFillSymbol, the default symbol for polygons
284
         */
285
        public static IFillSymbol createDefaultFillSymbol() {
286

    
287
                SimpleFillSymbol sfs = new SimpleFillSymbol();
288

    
289
                // Default symbol for polygons
290
                sfs.setOutline(createDefaultLineSymbol());
291
                sfs.setFillColor(DefaultFillSymbolColor); // transparent fill
292
                return sfs;
293
    }
294

    
295
        /**
296
         * Returns a new instance of an <b>ITextSymbol</b>.
297
         * @return ITextSymbol, the default symbol for texts
298
         */
299
        public static ITextSymbol createDefaultTextSymbol() {
300
                 SimpleTextSymbol sts = new SimpleTextSymbol();
301

    
302
                 sts.setFont(DefaultTextFont);
303
                 sts.setTextColor(DefaultSymbolColor);
304

    
305
                return sts;
306
        }
307

    
308
        /**
309
         * Creates a new instance of the default symbol whose type is defined
310
         * by the parameter <b>shapeType</b>
311
         * @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
312
         * FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
313
         * @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
314
         */
315
        public static ISymbol createDefaultSymbolByShapeType(int shapeType) {
316
                switch (shapeType) {
317
                case FShape.POINT:
318
                        return createDefaultMarkerSymbol();
319
                case FShape.LINE:
320
                        return createDefaultLineSymbol();
321
                case FShape.POLYGON:
322
                        return createDefaultFillSymbol();
323
                case FShape.MULTIPOINT:
324
                        return createDefaultMarkerSymbol();
325
                case FShape.TEXT:
326
                        return createDefaultTextSymbol();
327
                case FShape.MULTI:
328
                        return new MultiShapeSymbol();
329
                default:
330
                        throw new Error("shape type not yet supported");
331
                }
332
        }
333

    
334
        /**
335
         * Creates a new instance of the default symbol whose type is defined
336
         * by the parameter <b>shapeType</b> and uses the color defined by the
337
         * parameter color.
338
         * @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
339
         * @param color, the color to be applied to the new ISymbol.
340
         *
341
         * FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
342
         * @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
343
         */
344
        public static ISymbol createDefaultSymbolByShapeType(int shapeType, Color color) {
345
                ISymbol sym = createDefaultSymbolByShapeType(shapeType);
346
                if (sym instanceof IMarkerSymbol) {
347
                        ((IMarkerSymbol) sym).setColor(color);
348
                }
349

    
350
                if (sym instanceof ILineSymbol) {
351
                        ((ILineSymbol) sym).setLineColor(color);
352
                }
353

    
354
                if (sym instanceof IFillSymbol) {
355
                        ((IFillSymbol) sym).setFillColor(color);
356
                }
357

    
358
                if (sym instanceof ITextSymbol) {
359
                        ((ITextSymbol) sym).setTextColor(color);
360
                }
361

    
362
                return sym;
363
        }
364

    
365

    
366
}