svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / SymbologyFactory.java @ 27025
History | View | Annotate | Download (17.9 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 | 13913 | jaume | * 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 | 13749 | jaume | * multilayer symbols sizing bug fixed
|
51 | *
|
||
52 | * Revision 1.9 2007/09/17 09:32:05 jaume
|
||
53 | 13733 | jaume | * view refresh frame rate now configurable
|
54 | *
|
||
55 | * Revision 1.8 2007/07/18 06:54:34 jaume
|
||
56 | 12657 | jaume | * continuing with cartographic support
|
57 | *
|
||
58 | * Revision 1.7 2007/04/26 11:41:00 jaume
|
||
59 | 11372 | jaume | * attempting to let defining size in world units
|
60 | *
|
||
61 | * Revision 1.6 2007/03/27 09:28:40 jaume
|
||
62 | 10926 | jaume | * *** empty log message ***
|
63 | *
|
||
64 | * Revision 1.5 2007/03/21 11:02:51 jaume
|
||
65 | 10833 | jaume | * javadoc
|
66 | *
|
||
67 | * Revision 1.4 2007/03/20 15:59:03 jaume
|
||
68 | 10806 | jaume | * improved factory toolkit
|
69 | *
|
||
70 | * Revision 1.3 2007/03/13 16:58:36 jaume
|
||
71 | 10739 | jaume | * Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
|
72 | *
|
||
73 | * Revision 1.2 2007/03/09 11:20:57 jaume
|
||
74 | 10679 | jaume | * 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 | 18621 | jdominguez | import java.awt.BasicStroke; |
108 | 10806 | jaume | import java.awt.Color; |
109 | import java.awt.Font; |
||
110 | 18621 | jdominguez | import java.awt.Graphics; |
111 | import java.awt.Graphics2D; |
||
112 | import java.awt.Rectangle; |
||
113 | import java.awt.geom.AffineTransform; |
||
114 | 22639 | vcaballero | import java.awt.geom.Ellipse2D; |
115 | 15904 | jdominguez | import java.io.File; |
116 | 10806 | jaume | |
117 | 18621 | jdominguez | import javax.swing.JFrame; |
118 | import javax.swing.JPanel; |
||
119 | |||
120 | 10679 | jaume | import org.apache.log4j.Logger; |
121 | |||
122 | 13749 | jaume | import com.iver.cit.gvsig.fmap.Messages; |
123 | 10679 | jaume | import com.iver.cit.gvsig.fmap.core.styles.IStyle; |
124 | 10806 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol; |
125 | import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol; |
||
126 | import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol; |
||
127 | 10739 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol; |
128 | 10679 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
129 | 10806 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol; |
130 | 10739 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol; |
131 | import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerLineSymbol; |
||
132 | import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerMarkerSymbol; |
||
133 | 10806 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol; |
134 | import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol; |
||
135 | import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol; |
||
136 | import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol; |
||
137 | import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol; |
||
138 | 18621 | jdominguez | import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException; |
139 | 10679 | jaume | import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
140 | 13913 | jaume | import com.iver.utiles.IPersistence; |
141 | 10679 | jaume | import com.iver.utiles.NotExistInXMLEntity; |
142 | import com.iver.utiles.XMLEntity; |
||
143 | 18621 | jdominguez | import com.iver.utiles.swing.threads.Cancellable; |
144 | 10679 | jaume | |
145 | /**
|
||
146 | * Factory for obtaining symbology of any kind from several sources like.
|
||
147 | * <ol>
|
||
148 | * <li>
|
||
149 | * <b>XMLEntity's</b> that, at least, contains a full class name
|
||
150 | * string property that defines which class handles such symbol.
|
||
151 | * </li>
|
||
152 | * </ol>
|
||
153 | *
|
||
154 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
155 | */
|
||
156 | public class SymbologyFactory { |
||
157 | 22997 | vcaballero | |
158 | 18621 | jdominguez | /**
|
159 | 22997 | vcaballero | * This Constants are only used in SymbologyPage (preferences page for symbology),
|
160 | 18621 | jdominguez | * do not use in other context.
|
161 | */
|
||
162 | public static final Color FactoryDefaultSymbolColor = Color.GRAY; |
||
163 | public static final Color FactoryDefaultFillSymbolColor = new Color(60, 235, 235); |
||
164 | public static final Font FactoryDefaultTextFont = new Font("Serif", Font.PLAIN, 8); |
||
165 | public static final String FactorySymbolLibraryPath = |
||
166 | System.getProperty("user.home") + |
||
167 | File.separator +
|
||
168 | "gvSIG" +
|
||
169 | File.separator +
|
||
170 | "Symbols";
|
||
171 | 22997 | vcaballero | |
172 | 26050 | vcaballero | public static final String FactoryStyleLibraryPath = |
173 | System.getProperty("user.home") + |
||
174 | File.separator +
|
||
175 | "gvSIG" +
|
||
176 | File.separator +
|
||
177 | "Styles";
|
||
178 | |||
179 | 22997 | vcaballero | public static final Boolean FactoryDefaultAleatoryFillColor = false; |
180 | |||
181 | 18621 | jdominguez | public static Color DefaultSymbolColor = FactoryDefaultSymbolColor; |
182 | public static Color DefaultFillSymbolColor = FactoryDefaultFillSymbolColor; |
||
183 | public static Font DefaultTextFont = FactoryDefaultTextFont; |
||
184 | public static String SymbolLibraryPath = FactorySymbolLibraryPath; |
||
185 | 26050 | vcaballero | public static String StyleLibraryPath = FactoryStyleLibraryPath; |
186 | 22997 | vcaballero | |
187 | public static Boolean DefaultAleatoryFillColor = FactoryDefaultAleatoryFillColor; |
||
188 | |||
189 | |||
190 | |||
191 | 10833 | jaume | private static Logger logger = Logger.getLogger(SymbologyFactory.class.getName()); |
192 | 18621 | jdominguez | private static WarningSymbol warningSymbol; |
193 | 10806 | jaume | |
194 | 10679 | jaume | /**
|
195 | 10833 | jaume | * Factory that allows to create <b>ISymbol</b>'s from an ISymbol xml
|
196 | 10679 | jaume | * descriptor. A barely specific XMLEntity object. The string passed in the
|
197 | * second argument is the description text that will be used in case no description
|
||
198 | 10833 | jaume | * is supplied by the symbol's xml descriptor.
|
199 | *
|
||
200 | * @param xml, the symbol's xml descriptor
|
||
201 | * @param defaultDescription, a human readable description string for the symbol.
|
||
202 | * @return ISymbol
|
||
203 | 10679 | jaume | */
|
204 | public static ISymbol createSymbolFromXML(XMLEntity xml, String defaultDescription) { |
||
205 | 10833 | jaume | if (!xml.contains("desc")) { |
206 | if (defaultDescription == null) defaultDescription = ""; |
||
207 | 10679 | jaume | xml.putProperty("desc", defaultDescription);
|
208 | 10833 | jaume | } |
209 | 10679 | jaume | return (ISymbol) createFromXML(xml);
|
210 | } |
||
211 | |||
212 | 10833 | jaume | /**
|
213 | * Factory that allows to create <b>IStyle</b>'s from an <b>IStyle</b> xml descriptor. A barely
|
||
214 | * specific <b>XMLEntity</b> object. The string passed in the second argument is the
|
||
215 | * description text that will be used in case no description is supplied by the
|
||
216 | * style's xml descriptor.
|
||
217 | *
|
||
218 | * @param xml, the style's xml descriptor
|
||
219 | * @param defaultDescription, a human readable description string for the style
|
||
220 | * @return IStyle
|
||
221 | */
|
||
222 | 10679 | jaume | public static IStyle createStyleFromXML(XMLEntity xml, String defaultDescription) { |
223 | 22464 | vcaballero | if (!xml.contains("desc")) { |
224 | 22997 | vcaballero | if (defaultDescription == null) |
225 | 22464 | vcaballero | defaultDescription = "";
|
226 | |||
227 | 10679 | jaume | xml.putProperty("desc", defaultDescription);
|
228 | 22464 | vcaballero | } |
229 | 10679 | jaume | return (IStyle) createFromXML(xml);
|
230 | } |
||
231 | |||
232 | 10833 | jaume | /**
|
233 | * Creates an <b>Object</b> described by the <b>XMLEntity</b> xml, please reffer to the
|
||
234 | * XMLEntity definition contract to know what is the format of the xml argument.
|
||
235 | * The result of this method is an <b>Object</b> that you can cast to the type you were
|
||
236 | * looking for by means of the xml entity.
|
||
237 | *
|
||
238 | * @param xml
|
||
239 | * @return Object
|
||
240 | */
|
||
241 | 10679 | jaume | private static Object createFromXML(XMLEntity xml) { |
242 | String className = null; |
||
243 | try {
|
||
244 | className = xml.getStringProperty("className");
|
||
245 | } catch (NotExistInXMLEntity e) {
|
||
246 | 10833 | jaume | logger.error("Class name not set.\n" +
|
247 | 10679 | jaume | " Maybe you forgot to add the" +
|
248 | " putProperty(\"className\", yourClassName)" +
|
||
249 | 10833 | jaume | " call in the getXMLEntity method of your class", e);
|
250 | 10679 | jaume | } |
251 | |||
252 | |||
253 | Class clazz = null; |
||
254 | 13913 | jaume | IPersistence obj = null;
|
255 | 13749 | jaume | String s = className;
|
256 | 14449 | vcaballero | |
257 | 10679 | jaume | try {
|
258 | clazz = Class.forName(className);
|
||
259 | 10833 | jaume | |
260 | 13749 | jaume | if (xml.contains("desc")) { |
261 | s += " \"" + xml.getStringProperty("desc") +"\""; |
||
262 | 14449 | vcaballero | } |
263 | 10679 | jaume | // TODO remove the patch the day we deprecate FSymbol
|
264 | // begin patch
|
||
265 | if (clazz.equals(FSymbol.class))
|
||
266 | obj = FSymbol.createFromXML(xml); |
||
267 | else {
|
||
268 | // end patch
|
||
269 | 10833 | jaume | |
270 | |||
271 | 13913 | jaume | obj = (IPersistence) clazz.newInstance(); |
272 | 13749 | jaume | logger.info(Messages.getString("creating")+"....... "+s); |
273 | try {
|
||
274 | obj.setXMLEntity(xml); |
||
275 | } catch (NotExistInXMLEntity neiXML) {
|
||
276 | logger.error(Messages.getString("failed_creating_object")+": "+s); |
||
277 | throw neiXML;
|
||
278 | } |
||
279 | 14449 | vcaballero | |
280 | 10679 | jaume | } |
281 | |||
282 | } catch (InstantiationException e) { |
||
283 | logger.error("Trying to instantiate an interface" +
|
||
284 | " or abstract class + "+className, e);
|
||
285 | } catch (IllegalAccessException e) { |
||
286 | logger.error("IllegalAccessException: does your class have an" +
|
||
287 | " anonymous constructor?", e);
|
||
288 | } catch (ClassNotFoundException e) { |
||
289 | logger.error("No class called " + className +
|
||
290 | " was found.\nCheck the following.\n<br>" +
|
||
291 | "\t- The fullname of the class you're looking " +
|
||
292 | "for matches the value in the className " +
|
||
293 | "property of the XMLEntity ("+className+").\n<br>" + |
||
294 | "\t- The jar file containing your symbol class is in" +
|
||
295 | "the application classpath<br>", e);
|
||
296 | } |
||
297 | return obj;
|
||
298 | } |
||
299 | 10739 | jaume | |
300 | 10833 | jaume | /**
|
301 | * Returns a new empty instance of a <b>IMultiLayer</b> that can be one of:
|
||
302 | * <b>MultiLayerMarkerSymbol</b>, <b>MultiLayerLineSymbol</b>, or
|
||
303 | * <b>MultiLayerFillSymbol</b> depending on the shape type passed.
|
||
304 | *
|
||
305 | * @param shapeType, one of FShape.POINT, FShape.LINE, or FShape.POLYGON
|
||
306 | * @return IMultiLayerSymbol
|
||
307 | */
|
||
308 | 10739 | jaume | public static IMultiLayerSymbol createEmptyMultiLayerSymbol(int shapeType) { |
309 | 18621 | jdominguez | IMultiLayerSymbol mSym; |
310 | switch (shapeType) {
|
||
311 | 10739 | jaume | case FShape.POINT:
|
312 | 18621 | jdominguez | mSym = new MultiLayerMarkerSymbol();
|
313 | 16237 | jdominguez | break;
|
314 | 10739 | jaume | case FShape.LINE:
|
315 | 18621 | jdominguez | mSym = new MultiLayerLineSymbol();
|
316 | 16237 | jdominguez | break;
|
317 | 10739 | jaume | case FShape.POLYGON:
|
318 | 18621 | jdominguez | mSym =new MultiLayerFillSymbol();
|
319 | 16237 | jdominguez | break;
|
320 | 10739 | jaume | default:
|
321 | throw new Error("Shape type not yet supported for multilayer symbols"); |
||
322 | } |
||
323 | 22997 | vcaballero | |
324 | 18621 | jdominguez | if (mSym instanceof CartographicSupport) { |
325 | CartographicSupport cs = (CartographicSupport) mSym; |
||
326 | cs.setUnit(CartographicSupportToolkit.DefaultMeasureUnit); |
||
327 | cs.setReferenceSystem(CartographicSupportToolkit.DefaultReferenceSystem); |
||
328 | 16237 | jdominguez | } |
329 | 18621 | jdominguez | return mSym;
|
330 | 10739 | jaume | } |
331 | 10806 | jaume | |
332 | 10833 | jaume | /**
|
333 | * Returns a new instance of an <b>IMarkerSymbol</b>.
|
||
334 | * @return IMarkerSymbol, the default symbol for markers
|
||
335 | */
|
||
336 | 10806 | jaume | public static IMarkerSymbol createDefaultMarkerSymbol() { |
337 | SimpleMarkerSymbol sms = new SimpleMarkerSymbol();
|
||
338 | 10833 | jaume | sms.setColor(DefaultSymbolColor); |
339 | 15609 | jdominguez | sms.setSize(4);
|
340 | 18621 | jdominguez | toDefaultCartographicProperties(sms); |
341 | 10806 | jaume | return sms;
|
342 | } |
||
343 | |||
344 | 10833 | jaume | /**
|
345 | 18621 | jdominguez | * Returns a new instance of an <b>ILineSymbol</b>. A black line.
|
346 | 10833 | jaume | * @return ILineSymbol, the default symbol for lines.
|
347 | */
|
||
348 | 10806 | jaume | public static ILineSymbol createDefaultLineSymbol() { |
349 | SimpleLineSymbol sls = new SimpleLineSymbol();
|
||
350 | 10833 | jaume | sls.setLineColor(DefaultSymbolColor); |
351 | 13733 | jaume | sls.setLineWidth(1);
|
352 | 18621 | jdominguez | toDefaultCartographicProperties(sls); |
353 | 10806 | jaume | return sls;
|
354 | } |
||
355 | 18621 | jdominguez | |
356 | private static void toDefaultCartographicProperties(CartographicSupport cs) { |
||
357 | cs.setUnit(CartographicSupportToolkit.DefaultMeasureUnit); |
||
358 | cs.setReferenceSystem(CartographicSupportToolkit.DefaultReferenceSystem); |
||
359 | } |
||
360 | 22997 | vcaballero | |
361 | 10833 | jaume | /**
|
362 | * Returns a new instance of an <b>IFillSymbol</b>. Black outline,
|
||
363 | * and transparent fill.
|
||
364 | * @return IFillSymbol, the default symbol for polygons
|
||
365 | */
|
||
366 | 10806 | jaume | public static IFillSymbol createDefaultFillSymbol() { |
367 | |||
368 | SimpleFillSymbol sfs = new SimpleFillSymbol();
|
||
369 | |||
370 | // Default symbol for polygons
|
||
371 | 10833 | jaume | sfs.setOutline(createDefaultLineSymbol()); |
372 | 11372 | jaume | sfs.setFillColor(DefaultFillSymbolColor); // transparent fill
|
373 | 18621 | jdominguez | toDefaultCartographicProperties(sfs); |
374 | 10806 | jaume | return sfs;
|
375 | } |
||
376 | |||
377 | 10833 | jaume | /**
|
378 | * Returns a new instance of an <b>ITextSymbol</b>.
|
||
379 | * @return ITextSymbol, the default symbol for texts
|
||
380 | */
|
||
381 | 10806 | jaume | public static ITextSymbol createDefaultTextSymbol() { |
382 | 17438 | jdominguez | SimpleTextSymbol sts = new SimpleTextSymbol();
|
383 | 10806 | jaume | |
384 | 17438 | jdominguez | sts.setFont(DefaultTextFont); |
385 | sts.setTextColor(DefaultSymbolColor); |
||
386 | 18621 | jdominguez | toDefaultCartographicProperties(sts); |
387 | 10806 | jaume | |
388 | return sts;
|
||
389 | } |
||
390 | |||
391 | 10833 | jaume | /**
|
392 | * Creates a new instance of the default symbol whose type is defined
|
||
393 | * by the parameter <b>shapeType</b>
|
||
394 | * @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
|
||
395 | * FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
|
||
396 | * @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
|
||
397 | */
|
||
398 | 10806 | jaume | public static ISymbol createDefaultSymbolByShapeType(int shapeType) { |
399 | 18621 | jdominguez | switch (shapeType) {
|
400 | 10806 | jaume | case FShape.POINT:
|
401 | 24154 | jpiera | case FShape.POINT | FShape.M:
|
402 | 10806 | jaume | return createDefaultMarkerSymbol();
|
403 | case FShape.LINE:
|
||
404 | 24154 | jpiera | case FShape.LINE | FShape.M:
|
405 | 10806 | jaume | return createDefaultLineSymbol();
|
406 | case FShape.POLYGON:
|
||
407 | 24154 | jpiera | case FShape.POLYGON | FShape.M:
|
408 | 10806 | jaume | return createDefaultFillSymbol();
|
409 | case FShape.MULTIPOINT:
|
||
410 | 24154 | jpiera | case FShape.MULTIPOINT | FShape.M:
|
411 | 10806 | jaume | return createDefaultMarkerSymbol();
|
412 | case FShape.TEXT:
|
||
413 | return createDefaultTextSymbol();
|
||
414 | case FShape.MULTI:
|
||
415 | return new MultiShapeSymbol(); |
||
416 | 14449 | vcaballero | case FShape.NULL:
|
417 | return null; |
||
418 | 10806 | jaume | default:
|
419 | throw new Error("shape type not yet supported"); |
||
420 | } |
||
421 | } |
||
422 | |||
423 | 10833 | jaume | /**
|
424 | * Creates a new instance of the default symbol whose type is defined
|
||
425 | * by the parameter <b>shapeType</b> and uses the color defined by the
|
||
426 | * parameter color.
|
||
427 | * @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
|
||
428 | * @param color, the color to be applied to the new ISymbol.
|
||
429 | *
|
||
430 | * FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
|
||
431 | * @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
|
||
432 | */
|
||
433 | 10806 | jaume | public static ISymbol createDefaultSymbolByShapeType(int shapeType, Color color) { |
434 | ISymbol sym = createDefaultSymbolByShapeType(shapeType); |
||
435 | 22997 | vcaballero | |
436 | 10806 | jaume | if (sym instanceof IMarkerSymbol) { |
437 | ((IMarkerSymbol) sym).setColor(color); |
||
438 | } |
||
439 | |||
440 | if (sym instanceof ILineSymbol) { |
||
441 | ((ILineSymbol) sym).setLineColor(color); |
||
442 | } |
||
443 | |||
444 | if (sym instanceof IFillSymbol) { |
||
445 | ((IFillSymbol) sym).setFillColor(color); |
||
446 | } |
||
447 | |||
448 | if (sym instanceof ITextSymbol) { |
||
449 | ((ITextSymbol) sym).setTextColor(color); |
||
450 | } |
||
451 | |||
452 | return sym;
|
||
453 | } |
||
454 | |||
455 | |||
456 | 18621 | jdominguez | public static ISymbol getWarningSymbol(String message, String symbolDesc, int symbolDrawExceptionType) { |
457 | 17438 | jdominguez | if (warningSymbol == null) { |
458 | warningSymbol = new WarningSymbol();
|
||
459 | } |
||
460 | warningSymbol.setDescription(symbolDesc); |
||
461 | warningSymbol.setMessage(message); |
||
462 | warningSymbol.setDrawExceptionType(symbolDrawExceptionType); |
||
463 | return warningSymbol;
|
||
464 | } |
||
465 | 22997 | vcaballero | |
466 | 22639 | vcaballero | private static class WarningSymbol extends MultiShapeSymbol { |
467 | 17438 | jdominguez | private String desc; |
468 | private String message; |
||
469 | private int exceptionType; |
||
470 | 22639 | vcaballero | private SimpleTextSymbol text;
|
471 | 22997 | vcaballero | |
472 | 17438 | jdominguez | public static void main(String[] args) { |
473 | JFrame f = new JFrame(); |
||
474 | final ISymbol warning = SymbologyFactory.getWarningSymbol(SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, "a description", SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS); |
||
475 | JPanel preview = new JPanel() { |
||
476 | @Override
|
||
477 | protected void paintComponent(Graphics g) { |
||
478 | // TODO Auto-generated method stub
|
||
479 | super.paintComponent(g);
|
||
480 | Graphics2D g2 = (Graphics2D)g; |
||
481 | try {
|
||
482 | warning.drawInsideRectangle(g2, g2.getTransform(), getBounds()); |
||
483 | } catch (SymbolDrawingException e) {
|
||
484 | // TODO Auto-generated catch block
|
||
485 | e.printStackTrace(); |
||
486 | } |
||
487 | } |
||
488 | }; |
||
489 | f.setContentPane(preview); |
||
490 | f.pack(); |
||
491 | f.setVisible(true);
|
||
492 | } |
||
493 | |||
494 | public void draw(Graphics2D g, AffineTransform affineTransform, |
||
495 | FShape shp, Cancellable cancel) { |
||
496 | 22639 | vcaballero | try {
|
497 | drawInsideRectangle(g, g.getTransform(), shp.getBounds()); |
||
498 | } catch (SymbolDrawingException e) {
|
||
499 | // IMPOSSIBLE
|
||
500 | } |
||
501 | 17438 | jdominguez | } |
502 | |||
503 | public void setDrawExceptionType(int symbolDrawExceptionType) { |
||
504 | this.exceptionType = symbolDrawExceptionType;
|
||
505 | } |
||
506 | |||
507 | public void setMessage(String message) { |
||
508 | this.message = message;
|
||
509 | } |
||
510 | |||
511 | public void drawInsideRectangle(Graphics2D g, |
||
512 | AffineTransform scaleInstance, Rectangle r) |
||
513 | throws SymbolDrawingException {
|
||
514 | 22639 | vcaballero | g.setClip(r); |
515 | 17438 | jdominguez | if (message == null) { |
516 | message = "Symbol undrawable.\nPlease, check errors.";
|
||
517 | } |
||
518 | 22997 | vcaballero | |
519 | |||
520 | 17438 | jdominguez | String[] messageLines = message.split("\n"); |
521 | 22639 | vcaballero | int strokeWidth = (int) (Math.min(r.width, r.height)*.1); |
522 | 22997 | vcaballero | |
523 | 22639 | vcaballero | if (strokeWidth == 0) strokeWidth = 1; |
524 | 22997 | vcaballero | |
525 | 17438 | jdominguez | g.setColor(Color.red);
|
526 | g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND)); |
||
527 | int x = r.x+strokeWidth;
|
||
528 | int y = r.x+strokeWidth;
|
||
529 | int width = r.width-(strokeWidth+strokeWidth);
|
||
530 | int height = r.height-(strokeWidth+strokeWidth);
|
||
531 | 22997 | vcaballero | |
532 | 22639 | vcaballero | double radius = Math.min(width, height)*.5; |
533 | double centerX = r.getCenterX();
|
||
534 | double centerY = r.getCenterY();
|
||
535 | Ellipse2D circle = new Ellipse2D.Double(centerX - radius, centerY - radius, 2*radius, 2*radius); |
||
536 | g.draw(circle); |
||
537 | g.setClip(circle); |
||
538 | double aux = Math.cos(Math.PI*0.25)*radius; |
||
539 | g.drawLine((int) (centerX - aux), (int) (centerY - aux), (int) (centerX + aux), (int) (centerY + aux)); |
||
540 | 17438 | jdominguez | int fontSize = 20; |
541 | g.setFont(new Font("Arial", fontSize, Font.PLAIN)); |
||
542 | g.setColor(Color.black);
|
||
543 | 22639 | vcaballero | g.setClip(null);
|
544 | 22997 | vcaballero | |
545 | 22639 | vcaballero | if (text == null) { |
546 | text = new SimpleTextSymbol();
|
||
547 | text.setAutoresizeEnabled(true);
|
||
548 | 22997 | vcaballero | |
549 | 17438 | jdominguez | } |
550 | 22997 | vcaballero | |
551 | 22639 | vcaballero | double lineHeight = (r.getHeight()-6)/messageLines.length; |
552 | Rectangle textRect = new Rectangle( |
||
553 | 22997 | vcaballero | (int) r.getMinX(),
|
554 | 22639 | vcaballero | (int) r.getMinY()+6, |
555 | 22997 | vcaballero | (int) r.getWidth(),
|
556 | 22639 | vcaballero | (int) lineHeight);
|
557 | for (int i =0; i < messageLines.length; i++) { |
||
558 | text.setText(messageLines[i]); |
||
559 | text.drawInsideRectangle(g, null, textRect);
|
||
560 | textRect.setLocation((int) r.getX(), (int) (r.getY()+r.getHeight())); |
||
561 | } |
||
562 | 17438 | jdominguez | } |
563 | 22997 | vcaballero | |
564 | 22639 | vcaballero | @Override
|
565 | 17438 | jdominguez | public XMLEntity getXMLEntity() {
|
566 | 22639 | vcaballero | XMLEntity xml = new XMLEntity();
|
567 | xml.putProperty("className", getClass().getName());
|
||
568 | xml.putProperty("desc", desc);
|
||
569 | xml.putProperty("exceptionType", exceptionType);
|
||
570 | xml.putProperty("message", message);
|
||
571 | return xml;
|
||
572 | 17438 | jdominguez | } |
573 | 22997 | vcaballero | |
574 | 22639 | vcaballero | @Override
|
575 | public void setXMLEntity(XMLEntity xml) { |
||
576 | setDescription(xml.getStringProperty("desc"));
|
||
577 | exceptionType = xml.getIntProperty("exceptionType");
|
||
578 | message = xml.getStringProperty("message");
|
||
579 | } |
||
580 | 17438 | jdominguez | |
581 | 22997 | vcaballero | } |
582 | |||
583 | |||
584 | 10679 | jaume | } |