Statistics
| Revision:

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

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

    
95
import java.awt.Color;
96
import java.awt.Font;
97

    
98
import org.apache.log4j.Logger;
99

    
100
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
101
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
102
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
103
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
104
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
105
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
106
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
107
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol;
108
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerLineSymbol;
109
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerMarkerSymbol;
110
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
111
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
112
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
113
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
114
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
115
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
116
import com.iver.utiles.IPersistance;
117
import com.iver.utiles.NotExistInXMLEntity;
118
import com.iver.utiles.XMLEntity;
119

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

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

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

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

    
192

    
193
                Class clazz = null;
194
                IPersistance obj = null;
195

    
196
                try {
197
                        clazz = Class.forName(className);
198

    
199
                        // TODO remove the patch the day we deprecate FSymbol
200
                        // begin patch
201
                        if (clazz.equals(FSymbol.class))
202
                                obj = FSymbol.createFromXML(xml);
203
                        else {
204
                        // end patch
205

    
206

    
207
                                obj = (IPersistance) clazz.newInstance();
208
                                obj.setXMLEntity(xml);
209
                        }
210

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

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

    
250
        /**
251
         * Returns a new instance of an <b>IMarkerSymbol</b>.
252
         * @return IMarkerSymbol, the default symbol for markers
253
         */
254
        public static IMarkerSymbol createDefaultMarkerSymbol() {
255
                SimpleMarkerSymbol sms = new SimpleMarkerSymbol();
256
                sms.setColor(DefaultSymbolColor);
257
                sms.setSize(2);
258
                return sms;
259
        }
260

    
261
        /**
262
         * Returns a new instance of an <b>ILineSymbol</b>. A black line.
263
         * @return ILineSymbol, the default symbol for lines.
264
         */
265
        public static ILineSymbol createDefaultLineSymbol() {
266
                SimpleLineSymbol sls = new SimpleLineSymbol();
267
                sls.setLineColor(DefaultSymbolColor);
268
                sls.setLineWidth(0.5);
269
                return sls;
270
        }
271

    
272
        /**
273
         * Returns a new instance of an <b>IFillSymbol</b>. Black outline,
274
         * and transparent fill.
275
         * @return IFillSymbol, the default symbol for polygons
276
         */
277
        public static IFillSymbol createDefaultFillSymbol() {
278

    
279
                SimpleFillSymbol sfs = new SimpleFillSymbol();
280

    
281
                // Default symbol for polygons
282
                sfs.setOutline(createDefaultLineSymbol());
283
                sfs.setFillColor(DefaultFillSymbolColor); // transparent fill
284
                return sfs;
285
    }
286

    
287
        /**
288
         * Returns a new instance of an <b>ITextSymbol</b>.
289
         * @return ITextSymbol, the default symbol for texts
290
         */
291
        public static ITextSymbol createDefaultTextSymbol() {
292
                 SimpleTextSymbol sts = new SimpleTextSymbol();
293

    
294
                 sts.setFont(DefaultTextFont);
295
                 sts.setTextColor(DefaultSymbolColor);
296

    
297
                return sts;
298
        }
299

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

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

    
342
                if (sym instanceof ILineSymbol) {
343
                        ((ILineSymbol) sym).setLineColor(color);
344
                }
345

    
346
                if (sym instanceof IFillSymbol) {
347
                        ((IFillSymbol) sym).setFillColor(color);
348
                }
349

    
350
                if (sym instanceof ITextSymbol) {
351
                        ((ITextSymbol) sym).setTextColor(color);
352
                }
353

    
354
                return sym;
355
        }
356

    
357

    
358
}