Statistics
| Revision:

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

History | View | Annotate | Download (11 KB)

1 10679 jaume
/* 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$
45
* $Log$
46 10926 jaume
* Revision 1.6  2007-03-27 09:28:40  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.5  2007/03/21 11:02:51  jaume
50 10833 jaume
* javadoc
51
*
52
* Revision 1.4  2007/03/20 15:59:03  jaume
53 10806 jaume
* improved factory toolkit
54
*
55
* Revision 1.3  2007/03/13 16:58:36  jaume
56 10739 jaume
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
57
*
58
* Revision 1.2  2007/03/09 11:20:57  jaume
59 10679 jaume
* Advanced symbology (start committing)
60
*
61
* Revision 1.1.2.2  2007/02/15 16:23:44  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1.2.1  2007/02/12 15:15:20  jaume
65
* refactored interval legend and added graduated symbol legend
66
*
67
* Revision 1.1.2.3  2007/02/09 07:47:05  jaume
68
* Isymbol moved
69
*
70
* Revision 1.1.2.2  2007/02/01 11:42:47  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.1.2.1  2007/01/26 13:48:05  jaume
74
* patch for opening old projects
75
*
76
* Revision 1.1  2007/01/10 16:39:41  jaume
77
* ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
78
*
79
* Revision 1.3  2006/11/06 16:06:52  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.2  2006/11/06 07:33:54  jaume
83
* javadoc, source style
84
*
85
* Revision 1.1  2006/10/30 19:30:35  jaume
86
* *** empty log message ***
87
*
88
*
89
*/
90
package com.iver.cit.gvsig.fmap.core;
91
92 10806 jaume
import java.awt.Color;
93
import java.awt.Font;
94
95 10679 jaume
import org.apache.log4j.Logger;
96
97
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
98 10806 jaume
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
99
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
100
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
101 10739 jaume
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
102 10679 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
103 10806 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
104 10739 jaume
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol;
105
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerLineSymbol;
106
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerMarkerSymbol;
107 10806 jaume
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
108
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
109
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
110
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
111
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
112 10679 jaume
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
113
import com.iver.utiles.IPersistance;
114
import com.iver.utiles.NotExistInXMLEntity;
115
import com.iver.utiles.XMLEntity;
116
117
/**
118
 * Factory for obtaining symbology of any kind from several sources like.
119
 * <ol>
120
 *         <li>
121
 *                 <b>XMLEntity's</b> that, at least, contains a full class name
122
 *                         string property that defines which class handles such symbol.
123
 *  </li>
124
 * </ol>
125
 *
126
 * @author jaume dominguez faus - jaume.dominguez@iver.es
127
 */
128
public class SymbologyFactory {
129 10833 jaume
        public static Color DefaultSymbolColor = Color.BLACK;
130
        public static Font DefaultTextFont = new Font("SansSerif", Font.PLAIN, 9);
131
        private static Logger logger = Logger.getLogger(SymbologyFactory.class.getName());
132 10806 jaume
133 10679 jaume
        /**
134 10833 jaume
         * Factory that allows to create <b>ISymbol</b>'s from an ISymbol xml
135 10679 jaume
         * descriptor. A barely specific XMLEntity object. The string passed in the
136
         * second argument is the description text that will be used in case no description
137 10833 jaume
         * is supplied by the symbol's xml descriptor.
138
         *
139
         * @param xml, the symbol's xml descriptor
140
         * @param defaultDescription, a human readable description string for the symbol.
141
         * @return ISymbol
142 10679 jaume
         */
143
        public static ISymbol createSymbolFromXML(XMLEntity xml, String defaultDescription) {
144 10833 jaume
                if (!xml.contains("desc")) {
145
                        if (defaultDescription == null) defaultDescription = "";
146 10679 jaume
                        xml.putProperty("desc", defaultDescription);
147 10833 jaume
                }
148 10679 jaume
                return (ISymbol) createFromXML(xml);
149
        }
150
151 10833 jaume
        /**
152
         * Factory that allows to create <b>IStyle</b>'s from an <b>IStyle</b> xml descriptor. A barely
153
         * specific <b>XMLEntity</b> object. The string passed in the second argument is the
154
         * description text that will be used in case no description is supplied by the
155
         * style's xml descriptor.
156
         *
157
         * @param xml, the style's xml descriptor
158
         * @param defaultDescription, a human readable description string for the style
159
         * @return IStyle
160
         */
161 10679 jaume
        public static IStyle createStyleFromXML(XMLEntity xml, String defaultDescription) {
162
                if (!xml.contains("desc"))
163 10833 jaume
                        if (defaultDescription == null) defaultDescription = "";
164 10679 jaume
                        xml.putProperty("desc", defaultDescription);
165
                return (IStyle) createFromXML(xml);
166
        }
167
168 10833 jaume
        /**
169
         * Creates an <b>Object</b> described by the <b>XMLEntity</b> xml, please reffer to the
170
         * XMLEntity definition contract to know what is the format of the xml argument.
171
         * The result of this method is an <b>Object</b> that you can cast to the type you were
172
         * looking for by means of the xml entity.
173
         *
174
         * @param xml
175
         * @return Object
176
         */
177 10679 jaume
        private static Object createFromXML(XMLEntity xml) {
178
                String className = null;
179
                try {
180
                        className = xml.getStringProperty("className");
181
                } catch (NotExistInXMLEntity e) {
182 10833 jaume
                        logger.error("Class name not set.\n" +
183 10679 jaume
                                                " Maybe you forgot to add the" +
184
                                                " putProperty(\"className\", yourClassName)" +
185 10833 jaume
                                                " call in the getXMLEntity method of your class", e);
186 10679 jaume
                }
187
188
189
                Class clazz = null;
190
                IPersistance obj = null;
191
192
                try {
193
                        clazz = Class.forName(className);
194 10833 jaume
195 10679 jaume
                        // TODO remove the patch the day we deprecate FSymbol
196
                        // begin patch
197
                        if (clazz.equals(FSymbol.class))
198
                                obj = FSymbol.createFromXML(xml);
199
                        else {
200
                        // end patch
201 10833 jaume
202
203 10679 jaume
                                obj = (IPersistance) clazz.newInstance();
204
                                obj.setXMLEntity(xml);
205
                        }
206
207
                } catch (InstantiationException e) {
208
                        logger.error("Trying to instantiate an interface" +
209
                                                " or abstract class + "+className, e);
210
                } catch (IllegalAccessException e) {
211
                        logger.error("IllegalAccessException: does your class have an" +
212
                                        " anonymous constructor?", e);
213
                } catch (ClassNotFoundException e) {
214
                        logger.error("No class called " + className +
215
                                        " was found.\nCheck the following.\n<br>" +
216
                                        "\t- The fullname of the class you're looking " +
217
                                                "for matches the value in the className " +
218
                                                "property of the XMLEntity ("+className+").\n<br>" +
219
                                        "\t- The jar file containing your symbol class is in" +
220
                                                "the application classpath<br>", e);
221
                }
222
                return obj;
223
        }
224 10739 jaume
225 10833 jaume
        /**
226
         * Returns a new empty instance of a <b>IMultiLayer</b> that can be one of:
227
         * <b>MultiLayerMarkerSymbol</b>, <b>MultiLayerLineSymbol</b>, or
228
         * <b>MultiLayerFillSymbol</b> depending on the shape type passed.
229
         *
230
         * @param shapeType, one of FShape.POINT, FShape.LINE, or FShape.POLYGON
231
         * @return IMultiLayerSymbol
232
         */
233 10739 jaume
        public static IMultiLayerSymbol createEmptyMultiLayerSymbol(int shapeType) {
234
                switch (shapeType) {
235
                case FShape.POINT:
236
                        return new MultiLayerMarkerSymbol();
237
                case FShape.LINE:
238
                        return new MultiLayerLineSymbol();
239
                case FShape.POLYGON:
240
                        return new MultiLayerFillSymbol();
241
                default:
242
                        throw new Error("Shape type not yet supported for multilayer symbols");
243
                }
244
        }
245 10806 jaume
246 10833 jaume
        /**
247
         * Returns a new instance of an <b>IMarkerSymbol</b>.
248
         * @return IMarkerSymbol, the default symbol for markers
249
         */
250 10806 jaume
        public static IMarkerSymbol createDefaultMarkerSymbol() {
251
                SimpleMarkerSymbol sms = new SimpleMarkerSymbol();
252 10833 jaume
                sms.setColor(DefaultSymbolColor);
253 10926 jaume
                sms.setSize(2);
254 10806 jaume
                return sms;
255
        }
256
257 10833 jaume
        /**
258
         * Returns a new instance of an <b>ILineSymbol</b>. A black line.
259
         * @return ILineSymbol, the default symbol for lines.
260
         */
261 10806 jaume
        public static ILineSymbol createDefaultLineSymbol() {
262
                SimpleLineSymbol sls = new SimpleLineSymbol();
263 10833 jaume
                sls.setLineColor(DefaultSymbolColor);
264
                sls.setLineWidth(0.5);
265 10806 jaume
                return sls;
266
        }
267
268 10833 jaume
        /**
269
         * Returns a new instance of an <b>IFillSymbol</b>. Black outline,
270
         * and transparent fill.
271
         * @return IFillSymbol, the default symbol for polygons
272
         */
273 10806 jaume
        public static IFillSymbol createDefaultFillSymbol() {
274
275
                SimpleFillSymbol sfs = new SimpleFillSymbol();
276
277
                // Default symbol for polygons
278 10833 jaume
                sfs.setOutline(createDefaultLineSymbol());
279
                sfs.setFillColor(null); // transparent fill
280 10806 jaume
                return sfs;
281
    }
282
283 10833 jaume
        /**
284
         * Returns a new instance of an <b>ITextSymbol</b>.
285
         * @return ITextSymbol, the default symbol for texts
286
         */
287 10806 jaume
        public static ITextSymbol createDefaultTextSymbol() {
288
                 SimpleTextSymbol sts = new SimpleTextSymbol();
289
290 10833 jaume
                 sts.setFont(DefaultTextFont);
291
                 sts.setTextColor(DefaultSymbolColor);
292 10806 jaume
293
                return sts;
294
        }
295
296 10833 jaume
        /**
297
         * Creates a new instance of the default symbol whose type is defined
298
         * by the parameter <b>shapeType</b>
299
         * @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
300
         * FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
301
         * @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
302
         */
303 10806 jaume
        public static ISymbol createDefaultSymbolByShapeType(int shapeType) {
304
                switch (shapeType) {
305
                case FShape.POINT:
306
                        return createDefaultMarkerSymbol();
307
                case FShape.LINE:
308
                        return createDefaultLineSymbol();
309
                case FShape.POLYGON:
310
                        return createDefaultFillSymbol();
311
                case FShape.MULTIPOINT:
312
                        return createDefaultMarkerSymbol();
313
                case FShape.TEXT:
314
                        return createDefaultTextSymbol();
315
                case FShape.MULTI:
316
                        return new MultiShapeSymbol();
317
                default:
318
                        throw new Error("shape type not yet supported");
319
                }
320
        }
321
322 10833 jaume
        /**
323
         * Creates a new instance of the default symbol whose type is defined
324
         * by the parameter <b>shapeType</b> and uses the color defined by the
325
         * parameter color.
326
         * @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
327
         * @param color, the color to be applied to the new ISymbol.
328
         *
329
         * FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
330
         * @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
331
         */
332 10806 jaume
        public static ISymbol createDefaultSymbolByShapeType(int shapeType, Color color) {
333
                ISymbol sym = createDefaultSymbolByShapeType(shapeType);
334
                if (sym instanceof IMarkerSymbol) {
335
                        ((IMarkerSymbol) sym).setColor(color);
336
                }
337
338
                if (sym instanceof ILineSymbol) {
339
                        ((ILineSymbol) sym).setLineColor(color);
340
                }
341
342
                if (sym instanceof IFillSymbol) {
343
                        ((IFillSymbol) sym).setFillColor(color);
344
                }
345
346
                if (sym instanceof ITextSymbol) {
347
                        ((ITextSymbol) sym).setTextColor(color);
348
                }
349
350
                return sym;
351
        }
352
353
354 10679 jaume
}