svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / SymbologyFactory.java @ 37950
History | View | Annotate | Download (22.4 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/* CVS MESSAGES:
|
24 |
*
|
25 |
* $Id: SymbologyFactory.java 37950 2012-02-15 12:32:28Z fpenarrubia $
|
26 |
* $Log$
|
27 |
* Revision 1.11 2007-09-20 09:33:15 jaume
|
28 |
* Refactored: fixed name of IPersistAnce to IPersistence
|
29 |
*
|
30 |
* Revision 1.10 2007/09/17 14:16:11 jaume
|
31 |
* multilayer symbols sizing bug fixed
|
32 |
*
|
33 |
* Revision 1.9 2007/09/17 09:32:05 jaume
|
34 |
* view refresh frame rate now configurable
|
35 |
*
|
36 |
* Revision 1.8 2007/07/18 06:54:34 jaume
|
37 |
* continuing with cartographic support
|
38 |
*
|
39 |
* Revision 1.7 2007/04/26 11:41:00 jaume
|
40 |
* attempting to let defining size in world units
|
41 |
*
|
42 |
* Revision 1.6 2007/03/27 09:28:40 jaume
|
43 |
* *** empty log message ***
|
44 |
*
|
45 |
* Revision 1.5 2007/03/21 11:02:51 jaume
|
46 |
* javadoc
|
47 |
*
|
48 |
* Revision 1.4 2007/03/20 15:59:03 jaume
|
49 |
* improved factory toolkit
|
50 |
*
|
51 |
* Revision 1.3 2007/03/13 16:58:36 jaume
|
52 |
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
|
53 |
*
|
54 |
* Revision 1.2 2007/03/09 11:20:57 jaume
|
55 |
* Advanced symbology (start committing)
|
56 |
*
|
57 |
* Revision 1.1.2.2 2007/02/15 16:23:44 jaume
|
58 |
* *** empty log message ***
|
59 |
*
|
60 |
* Revision 1.1.2.1 2007/02/12 15:15:20 jaume
|
61 |
* refactored interval legend and added graduated symbol legend
|
62 |
*
|
63 |
* Revision 1.1.2.3 2007/02/09 07:47:05 jaume
|
64 |
* Isymbol moved
|
65 |
*
|
66 |
* Revision 1.1.2.2 2007/02/01 11:42:47 jaume
|
67 |
* *** empty log message ***
|
68 |
*
|
69 |
* Revision 1.1.2.1 2007/01/26 13:48:05 jaume
|
70 |
* patch for opening old projects
|
71 |
*
|
72 |
* Revision 1.1 2007/01/10 16:39:41 jaume
|
73 |
* ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
|
74 |
*
|
75 |
* Revision 1.3 2006/11/06 16:06:52 jaume
|
76 |
* *** empty log message ***
|
77 |
*
|
78 |
* Revision 1.2 2006/11/06 07:33:54 jaume
|
79 |
* javadoc, source style
|
80 |
*
|
81 |
* Revision 1.1 2006/10/30 19:30:35 jaume
|
82 |
* *** empty log message ***
|
83 |
*
|
84 |
*
|
85 |
*/
|
86 |
package com.iver.cit.gvsig.fmap.core; |
87 |
|
88 |
import java.awt.BasicStroke; |
89 |
import java.awt.Color; |
90 |
import java.awt.Font; |
91 |
import java.awt.Graphics; |
92 |
import java.awt.Graphics2D; |
93 |
import java.awt.Rectangle; |
94 |
import java.awt.Stroke; |
95 |
import java.awt.geom.AffineTransform; |
96 |
import java.awt.geom.Ellipse2D; |
97 |
|
98 |
import javax.print.attribute.PrintRequestAttributeSet; |
99 |
import javax.swing.JFrame; |
100 |
import javax.swing.JPanel; |
101 |
|
102 |
import org.apache.log4j.Logger; |
103 |
|
104 |
import com.iver.cit.gvsig.fmap.Messages; |
105 |
import com.iver.cit.gvsig.fmap.core.styles.IStyle; |
106 |
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle; |
107 |
import com.iver.cit.gvsig.fmap.core.symbols.AbstractSymbol; |
108 |
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol; |
109 |
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol; |
110 |
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol; |
111 |
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol; |
112 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
113 |
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol; |
114 |
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol; |
115 |
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerLineSymbol; |
116 |
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerMarkerSymbol; |
117 |
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol; |
118 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol; |
119 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol; |
120 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol; |
121 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol; |
122 |
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException; |
123 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
124 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
125 |
import com.iver.utiles.FileUtils; |
126 |
import com.iver.utiles.IPersistence; |
127 |
import com.iver.utiles.NotExistInXMLEntity; |
128 |
import com.iver.utiles.XMLEntity; |
129 |
import com.iver.utiles.swing.threads.Cancellable; |
130 |
|
131 |
/**
|
132 |
* Factory for obtaining symbology of any kind from several sources like.
|
133 |
* <ol>
|
134 |
* <li>
|
135 |
* <b>XMLEntity's</b> that, at least, contains a full class name
|
136 |
* string property that defines which class handles such symbol.
|
137 |
* </li>
|
138 |
* </ol>
|
139 |
*
|
140 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
141 |
*/
|
142 |
|
143 |
public class SymbologyFactory { |
144 |
|
145 |
/**
|
146 |
* This Constants are only used in SymbologyPage (preferences page for symbology),
|
147 |
* do not use in other context.
|
148 |
*/
|
149 |
public static final Color FactoryDefaultSymbolColor = Color.GRAY; |
150 |
public static final Color FactoryDefaultFillSymbolColor = new Color(60, 235, 235); |
151 |
public static final Font FactoryDefaultTextFont = new Font("Serif", Font.PLAIN, 8); |
152 |
|
153 |
public static final String FactorySymbolLibraryPath = FileUtils.getAppHomeDir() + "Symbols"; |
154 |
public static final String FactoryStyleLibraryPath = FileUtils.getAppHomeDir() + "Styles"; |
155 |
|
156 |
public static final Boolean FactoryDefaultAleatoryFillColor = false; |
157 |
|
158 |
public static Color DefaultSymbolColor = FactoryDefaultSymbolColor; |
159 |
public static Color DefaultFillSymbolColor = FactoryDefaultFillSymbolColor; |
160 |
public static Font DefaultTextFont = FactoryDefaultTextFont; |
161 |
public static String SymbolLibraryPath = FactorySymbolLibraryPath; |
162 |
public static String StyleLibraryPath = FactoryStyleLibraryPath; |
163 |
|
164 |
public static Boolean DefaultAleatoryFillColor = FactoryDefaultAleatoryFillColor; |
165 |
|
166 |
|
167 |
|
168 |
private static Logger logger = Logger.getLogger(SymbologyFactory.class.getName()); |
169 |
private static WarningSymbol warningSymbol; |
170 |
|
171 |
/**
|
172 |
* Factory that allows to create <b>ISymbol</b>'s from an ISymbol xml
|
173 |
* descriptor. A barely specific XMLEntity object. The string passed in the
|
174 |
* second argument is the description text that will be used in case no description
|
175 |
* is supplied by the symbol's xml descriptor.
|
176 |
*
|
177 |
* @param xml, the symbol's xml descriptor
|
178 |
* @param defaultDescription, a human readable description string for the symbol.
|
179 |
* @return ISymbol
|
180 |
*/
|
181 |
public static ISymbol createSymbolFromXML(XMLEntity xml, String defaultDescription) { |
182 |
if (!xml.contains("desc")) { |
183 |
if (defaultDescription == null) defaultDescription = ""; |
184 |
xml.putProperty("desc", defaultDescription);
|
185 |
} |
186 |
return (ISymbol) createFromXML(xml);
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* Factory that allows to create <b>IStyle</b>'s from an <b>IStyle</b> xml descriptor. A barely
|
191 |
* specific <b>XMLEntity</b> object. The string passed in the second argument is the
|
192 |
* description text that will be used in case no description is supplied by the
|
193 |
* style's xml descriptor.
|
194 |
*
|
195 |
* @param xml, the style's xml descriptor
|
196 |
* @param defaultDescription, a human readable description string for the style
|
197 |
* @return IStyle
|
198 |
*/
|
199 |
public static IStyle createStyleFromXML(XMLEntity xml, String defaultDescription) { |
200 |
if (!xml.contains("desc")) { |
201 |
if (defaultDescription == null) |
202 |
defaultDescription = "";
|
203 |
|
204 |
xml.putProperty("desc", defaultDescription);
|
205 |
} |
206 |
return (IStyle) createFromXML(xml);
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* Creates an <b>Object</b> described by the <b>XMLEntity</b> xml, please reffer to the
|
211 |
* XMLEntity definition contract to know what is the format of the xml argument.
|
212 |
* The result of this method is an <b>Object</b> that you can cast to the type you were
|
213 |
* looking for by means of the xml entity.
|
214 |
*
|
215 |
* @param xml
|
216 |
* @return Object
|
217 |
*/
|
218 |
private static Object createFromXML(XMLEntity xml) { |
219 |
String className = null; |
220 |
try {
|
221 |
className = xml.getStringProperty("className");
|
222 |
} catch (NotExistInXMLEntity e) {
|
223 |
logger.error("Class name not set.\n" +
|
224 |
" Maybe you forgot to add the" +
|
225 |
" putProperty(\"className\", yourClassName)" +
|
226 |
" call in the getXMLEntity method of your class", e);
|
227 |
} |
228 |
|
229 |
|
230 |
Class clazz = null; |
231 |
IPersistence obj = null;
|
232 |
String s = className;
|
233 |
|
234 |
try {
|
235 |
clazz = Class.forName(className);
|
236 |
|
237 |
if (xml.contains("desc")) { |
238 |
s += " \"" + xml.getStringProperty("desc") +"\""; |
239 |
} |
240 |
// TODO: Modify the patch the day we deprecate FSymbol
|
241 |
// begin patch
|
242 |
if (clazz.equals(FSymbol.class)){
|
243 |
obj = deriveFSymbol(FSymbol.createFromXML(xml)); |
244 |
} else {
|
245 |
// end patch
|
246 |
|
247 |
|
248 |
obj = (IPersistence) clazz.newInstance(); |
249 |
// logger.info(Messages.getString("creating")+"....... "+s);
|
250 |
try {
|
251 |
obj.setXMLEntity(xml); |
252 |
} catch (NotExistInXMLEntity neiXML) {
|
253 |
logger.error(Messages.getString("failed_creating_object")+": "+s); |
254 |
throw neiXML;
|
255 |
} |
256 |
|
257 |
} |
258 |
|
259 |
} catch (InstantiationException e) { |
260 |
logger.error("Trying to instantiate an interface" +
|
261 |
" or abstract class + "+className, e);
|
262 |
} catch (IllegalAccessException e) { |
263 |
logger.error("IllegalAccessException: does your class have an" +
|
264 |
" anonymous constructor?", e);
|
265 |
} catch (ClassNotFoundException e) { |
266 |
logger.error("No class called " + className +
|
267 |
" was found.\nCheck the following.\n<br>" +
|
268 |
"\t- The fullname of the class you're looking " +
|
269 |
"for matches the value in the className " +
|
270 |
"property of the XMLEntity ("+className+").\n<br>" + |
271 |
"\t- The jar file containing your symbol class is in" +
|
272 |
"the application classpath<br>", e);
|
273 |
} |
274 |
return obj;
|
275 |
} |
276 |
|
277 |
private static ISymbol deriveFSymbol(FSymbol fSymbol) { |
278 |
ISymbol derivedSymbol; |
279 |
|
280 |
int symbolType = fSymbol.getSymbolType();
|
281 |
Color color = fSymbol.getColor();
|
282 |
double size = fSymbol.getSize();
|
283 |
if (symbolType == FShape.LINE) {
|
284 |
ILineSymbol line = SymbologyFactory.createDefaultLineSymbol(); |
285 |
line.setLineColor(color); |
286 |
|
287 |
SimpleLineStyle lineStyle = new SimpleLineStyle();
|
288 |
lineStyle.setUnit(-1);//En FSymbol solo estaban contemplados metros en los simbolos puntuales |
289 |
lineStyle.setOffset(0);
|
290 |
lineStyle.setReferenceSystem(CartographicSupport.PAPER); |
291 |
Stroke stroke = fSymbol.getStroke();
|
292 |
BasicStroke bStroke = null; |
293 |
if(stroke instanceof BasicStroke){ |
294 |
bStroke = (BasicStroke) stroke;
|
295 |
} |
296 |
if(bStroke != null){ |
297 |
lineStyle.setStroke(bStroke); |
298 |
lineStyle.setLineWidth(bStroke.getLineWidth()); |
299 |
} else {
|
300 |
lineStyle.setLineWidth((float) size);
|
301 |
} |
302 |
|
303 |
line.setLineStyle(lineStyle); |
304 |
derivedSymbol = line; |
305 |
|
306 |
} else if (symbolType == FShape.POINT) { |
307 |
int unit = fSymbol.isSizeInPixels() ? -1 : 1; // only meters or pixels |
308 |
// were supported in
|
309 |
// FSymbol
|
310 |
int style = fSymbol.getStyle();
|
311 |
IMarkerSymbol marker = new SimpleMarkerSymbol();
|
312 |
SimpleMarkerSymbol sms = (SimpleMarkerSymbol) marker; |
313 |
if (style == FConstant.SYMBOL_STYLE_MARKER_CIRCLE) {
|
314 |
sms.setStyle(SimpleMarkerSymbol.CIRCLE_STYLE); |
315 |
} else if (style == FConstant.SYMBOL_STYLE_MARKER_CROSS) { |
316 |
sms.setStyle(SimpleMarkerSymbol.CROSS_STYLE); |
317 |
} else if (style == FConstant.SYMBOL_STYLE_MARKER_SQUARE) { |
318 |
sms.setStyle(SimpleMarkerSymbol.SQUARE_STYLE); |
319 |
} else if (style == FConstant.SYMBOL_STYLE_MARKER_TRIANGLE) { |
320 |
sms.setStyle(SimpleMarkerSymbol.TRIANGLE_STYLE); |
321 |
} else if (style == FConstant.SYMBOL_STYLE_MARKER_IMAGEN) { |
322 |
//Como no tenemos en el core ningun simbolo de imagen devolvemos uno circular por defecto
|
323 |
sms.setStyle(SimpleMarkerSymbol.CIRCLE_STYLE); |
324 |
} |
325 |
Color outlineColor = fSymbol.getOutlineColor();
|
326 |
if (outlineColor != null) { |
327 |
sms.setOutlined(true);
|
328 |
sms.setOutlineColor(outlineColor); |
329 |
} |
330 |
marker.setColor(color); |
331 |
marker.setSize(size); |
332 |
marker.setUnit(unit); |
333 |
marker.setRotation(fSymbol.getRotation()); |
334 |
derivedSymbol = marker; |
335 |
|
336 |
} else if (symbolType == FShape.POLYGON) { |
337 |
IFillSymbol fill; |
338 |
int fSymbolStyle = fSymbol.getStyle();
|
339 |
|
340 |
fill = SymbologyFactory.createDefaultFillSymbol(); |
341 |
if (fSymbolStyle == FConstant.SYMBOL_STYLE_FILL_TRANSPARENT ||
|
342 |
fSymbolStyle == FConstant.SYMBOL_STYLE_DGNSPECIAL) { |
343 |
color = null;
|
344 |
} |
345 |
// De momento no tenemos tipos simbolos apropiados en el core de fmap para estos.
|
346 |
|
347 |
fill.setUnit(-1);//En FSymbol solo estaban contemplados metros en los simbolos puntuales |
348 |
fill.setFillColor(color); |
349 |
|
350 |
if (fSymbol.isOutlined()) {
|
351 |
// Let's create the outline line symbol
|
352 |
fSymbol.setSymbolType(FShape.LINE); |
353 |
ILineSymbol outline = (ILineSymbol) deriveFSymbol(fSymbol); |
354 |
|
355 |
// restore the old value for symbol type (should be always
|
356 |
// FShape.POLYGON)
|
357 |
assert symbolType == FShape.POLYGON;
|
358 |
fSymbol.setSymbolType(symbolType); |
359 |
outline.setLineColor(fSymbol.getOutlineColor()); |
360 |
fill.setOutline(outline); |
361 |
} |
362 |
derivedSymbol = fill; |
363 |
} else if (symbolType == FShape.MULTI) { |
364 |
fSymbol.setSymbolType(FShape.LINE); |
365 |
ILineSymbol line = (ILineSymbol) deriveFSymbol(fSymbol); |
366 |
fSymbol.setSymbolType(FShape.POINT); |
367 |
IMarkerSymbol marker = (IMarkerSymbol) deriveFSymbol(fSymbol); |
368 |
fSymbol.setSymbolType(FShape.POLYGON); |
369 |
IFillSymbol fill = (IFillSymbol) deriveFSymbol(fSymbol); |
370 |
assert symbolType == FShape.MULTI;
|
371 |
fSymbol.setSymbolType(symbolType); |
372 |
MultiShapeSymbol multiShapeSymbol = new MultiShapeSymbol();
|
373 |
multiShapeSymbol.setMarkerSymbol(marker); |
374 |
multiShapeSymbol.setLineSymbol(line); |
375 |
multiShapeSymbol.setFillSymbol(fill); |
376 |
|
377 |
derivedSymbol = multiShapeSymbol; |
378 |
} else if (symbolType == FShape.TEXT || symbolType == FConstant.SYMBOL_TYPE_TEXT) { |
379 |
ITextSymbol textSym = SymbologyFactory.createDefaultTextSymbol(); |
380 |
textSym.setTextColor(color); |
381 |
derivedSymbol = textSym; |
382 |
} else {
|
383 |
throw new Error("FSymbol of type "+symbolType+" cannot be imported yet!"); |
384 |
} |
385 |
|
386 |
// establish the general description;
|
387 |
if (derivedSymbol instanceof AbstractSymbol) { |
388 |
AbstractSymbol symbol = (AbstractSymbol) derivedSymbol; |
389 |
symbol.setIsShapeVisible(fSymbol.isShapeVisible()); |
390 |
symbol.setDescription(fSymbol.getDescription()); |
391 |
} |
392 |
|
393 |
if (derivedSymbol instanceof MultiShapeSymbol ) { |
394 |
MultiShapeSymbol symbol = (MultiShapeSymbol) derivedSymbol; |
395 |
symbol.setDescription(fSymbol.getDescription()); |
396 |
} |
397 |
|
398 |
return derivedSymbol;
|
399 |
} |
400 |
|
401 |
/**
|
402 |
* Returns a new empty instance of a <b>IMultiLayer</b> that can be one of:
|
403 |
* <b>MultiLayerMarkerSymbol</b>, <b>MultiLayerLineSymbol</b>, or
|
404 |
* <b>MultiLayerFillSymbol</b> depending on the shape type passed.
|
405 |
*
|
406 |
* @param shapeType, one of FShape.POINT, FShape.LINE, or FShape.POLYGON
|
407 |
* @return IMultiLayerSymbol
|
408 |
*/
|
409 |
public static IMultiLayerSymbol createEmptyMultiLayerSymbol(int shapeType) { |
410 |
IMultiLayerSymbol mSym; |
411 |
switch (shapeType%FShape.Z) {
|
412 |
case FShape.POINT:
|
413 |
mSym = new MultiLayerMarkerSymbol();
|
414 |
break;
|
415 |
case FShape.LINE:
|
416 |
mSym = new MultiLayerLineSymbol();
|
417 |
break;
|
418 |
case FShape.POLYGON:
|
419 |
mSym =new MultiLayerFillSymbol();
|
420 |
break;
|
421 |
default:
|
422 |
throw new Error("Shape type not yet supported for multilayer symbols"); |
423 |
} |
424 |
|
425 |
if (mSym instanceof CartographicSupport) { |
426 |
CartographicSupport cs = (CartographicSupport) mSym; |
427 |
cs.setUnit(CartographicSupportToolkit.DefaultMeasureUnit); |
428 |
cs.setReferenceSystem(CartographicSupportToolkit.DefaultReferenceSystem); |
429 |
} |
430 |
return mSym;
|
431 |
} |
432 |
|
433 |
/**
|
434 |
* Returns a new instance of an <b>IMarkerSymbol</b>.
|
435 |
* @return IMarkerSymbol, the default symbol for markers
|
436 |
*/
|
437 |
public static IMarkerSymbol createDefaultMarkerSymbol() { |
438 |
SimpleMarkerSymbol sms = new SimpleMarkerSymbol();
|
439 |
sms.setColor(DefaultSymbolColor); |
440 |
sms.setSize(4);
|
441 |
toDefaultCartographicProperties(sms); |
442 |
return sms;
|
443 |
} |
444 |
|
445 |
/**
|
446 |
* Returns a new instance of an <b>ILineSymbol</b>. A black line.
|
447 |
* @return ILineSymbol, the default symbol for lines.
|
448 |
*/
|
449 |
public static ILineSymbol createDefaultLineSymbol() { |
450 |
SimpleLineSymbol sls = new SimpleLineSymbol();
|
451 |
sls.setLineColor(DefaultSymbolColor); |
452 |
sls.setLineWidth(1);
|
453 |
toDefaultCartographicProperties(sls); |
454 |
return sls;
|
455 |
} |
456 |
|
457 |
private static void toDefaultCartographicProperties(CartographicSupport cs) { |
458 |
cs.setUnit(CartographicSupportToolkit.DefaultMeasureUnit); |
459 |
cs.setReferenceSystem(CartographicSupportToolkit.DefaultReferenceSystem); |
460 |
} |
461 |
|
462 |
/**
|
463 |
* Returns a new instance of an <b>IFillSymbol</b>. Black outline,
|
464 |
* and transparent fill.
|
465 |
* @return IFillSymbol, the default symbol for polygons
|
466 |
*/
|
467 |
public static IFillSymbol createDefaultFillSymbol() { |
468 |
|
469 |
SimpleFillSymbol sfs = new SimpleFillSymbol();
|
470 |
|
471 |
// Default symbol for polygons
|
472 |
sfs.setOutline(createDefaultLineSymbol()); |
473 |
sfs.setFillColor(DefaultFillSymbolColor); // transparent fill
|
474 |
toDefaultCartographicProperties(sfs); |
475 |
return sfs;
|
476 |
} |
477 |
|
478 |
/**
|
479 |
* Returns a new instance of an <b>ITextSymbol</b>.
|
480 |
* @return ITextSymbol, the default symbol for texts
|
481 |
*/
|
482 |
public static ITextSymbol createDefaultTextSymbol() { |
483 |
SimpleTextSymbol sts = new SimpleTextSymbol();
|
484 |
|
485 |
sts.setFont(DefaultTextFont); |
486 |
sts.setTextColor(DefaultSymbolColor); |
487 |
toDefaultCartographicProperties(sts); |
488 |
|
489 |
return sts;
|
490 |
} |
491 |
|
492 |
/**
|
493 |
* Creates a new instance of the default symbol whose type is defined
|
494 |
* by the parameter <b>shapeType</b>
|
495 |
* @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
|
496 |
* FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
|
497 |
* @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
|
498 |
*/
|
499 |
public static ISymbol createDefaultSymbolByShapeType(int shapeType) { |
500 |
if ((shapeType & FShape.POINT)!= 0){ |
501 |
return createDefaultMarkerSymbol();
|
502 |
} else if ((shapeType & FShape.LINE) != 0){ |
503 |
return createDefaultLineSymbol();
|
504 |
} else if ((shapeType & FShape.POLYGON) != 0){ |
505 |
return createDefaultFillSymbol();
|
506 |
} else if ((shapeType & FShape.MULTIPOINT) != 0){ |
507 |
return createDefaultMarkerSymbol();
|
508 |
} else if ((shapeType & FShape.TEXT) != 0){ |
509 |
return createDefaultTextSymbol();
|
510 |
} else if ((shapeType & FShape.MULTI) != 0){ |
511 |
return new MultiShapeSymbol(); |
512 |
} else if (shapeType == FShape.NULL){ |
513 |
return null; |
514 |
} |
515 |
throw new Error("shape type not yet supported"); |
516 |
} |
517 |
|
518 |
/**
|
519 |
* Creates a new instance of the default symbol whose type is defined
|
520 |
* by the parameter <b>shapeType</b> and uses the color defined by the
|
521 |
* parameter color.
|
522 |
* @param shapeType, one of FShape.POINT, FShape.LINE, FShape.POLYGON,
|
523 |
* @param color, the color to be applied to the new ISymbol.
|
524 |
*
|
525 |
* FShape.MULTIPOINT, FShape.TEXT, or FShape.MULTI.
|
526 |
* @return ISymbol, the default symbol for the shape type defined by <b>shapeType</b>
|
527 |
*/
|
528 |
public static ISymbol createDefaultSymbolByShapeType(int shapeType, Color color) { |
529 |
ISymbol sym = createDefaultSymbolByShapeType(shapeType); |
530 |
|
531 |
if (sym instanceof IMarkerSymbol) { |
532 |
((IMarkerSymbol) sym).setColor(color); |
533 |
} |
534 |
|
535 |
if (sym instanceof ILineSymbol) { |
536 |
((ILineSymbol) sym).setLineColor(color); |
537 |
} |
538 |
|
539 |
if (sym instanceof IFillSymbol) { |
540 |
((IFillSymbol) sym).setFillColor(color); |
541 |
} |
542 |
|
543 |
if (sym instanceof ITextSymbol) { |
544 |
((ITextSymbol) sym).setTextColor(color); |
545 |
} |
546 |
|
547 |
return sym;
|
548 |
} |
549 |
|
550 |
|
551 |
public static ISymbol getWarningSymbol(String message, String symbolDesc, int symbolDrawExceptionType) { |
552 |
if (warningSymbol == null) { |
553 |
warningSymbol = new WarningSymbol();
|
554 |
} |
555 |
warningSymbol.setDescription(symbolDesc); |
556 |
warningSymbol.setMessage(message); |
557 |
warningSymbol.setDrawExceptionType(symbolDrawExceptionType); |
558 |
return warningSymbol;
|
559 |
} |
560 |
|
561 |
private static class WarningSymbol extends MultiShapeSymbol { |
562 |
private String desc; |
563 |
private String message; |
564 |
private int exceptionType; |
565 |
private SimpleTextSymbol text;
|
566 |
|
567 |
public static void main(String[] args) { |
568 |
JFrame f = new JFrame(); |
569 |
final ISymbol warning = SymbologyFactory.getWarningSymbol(SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, "a description", SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS); |
570 |
JPanel preview = new JPanel() { |
571 |
@Override
|
572 |
protected void paintComponent(Graphics g) { |
573 |
// TODO Auto-generated method stub
|
574 |
super.paintComponent(g);
|
575 |
Graphics2D g2 = (Graphics2D)g; |
576 |
try {
|
577 |
warning.drawInsideRectangle(g2, g2.getTransform(), getBounds(), null);
|
578 |
} catch (SymbolDrawingException e) {
|
579 |
// TODO Auto-generated catch block
|
580 |
e.printStackTrace(); |
581 |
} |
582 |
} |
583 |
}; |
584 |
f.setContentPane(preview); |
585 |
f.pack(); |
586 |
f.setVisible(true);
|
587 |
} |
588 |
|
589 |
public void draw(Graphics2D g, AffineTransform affineTransform, |
590 |
FShape shp, Cancellable cancel) { |
591 |
try {
|
592 |
drawInsideRectangle(g, g.getTransform(), shp.getBounds(), null);
|
593 |
} catch (SymbolDrawingException e) {
|
594 |
// IMPOSSIBLE
|
595 |
} |
596 |
} |
597 |
|
598 |
public void setDrawExceptionType(int symbolDrawExceptionType) { |
599 |
this.exceptionType = symbolDrawExceptionType;
|
600 |
} |
601 |
|
602 |
public void setMessage(String message) { |
603 |
this.message = message;
|
604 |
} |
605 |
|
606 |
public void drawInsideRectangle(Graphics2D g, |
607 |
AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) |
608 |
throws SymbolDrawingException {
|
609 |
g.setClip(r); |
610 |
if (message == null) { |
611 |
message = "Symbol undrawable.\nPlease, check errors.";
|
612 |
} |
613 |
|
614 |
|
615 |
String[] messageLines = message.split("\n"); |
616 |
int strokeWidth = (int) (Math.min(r.width, r.height)*.1); |
617 |
|
618 |
if (strokeWidth == 0) strokeWidth = 1; |
619 |
|
620 |
g.setColor(Color.red);
|
621 |
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND)); |
622 |
int x = r.x+strokeWidth;
|
623 |
int y = r.x+strokeWidth;
|
624 |
int width = r.width-(strokeWidth+strokeWidth);
|
625 |
int height = r.height-(strokeWidth+strokeWidth);
|
626 |
|
627 |
double radius = Math.min(width, height)*.5; |
628 |
double centerX = r.getCenterX();
|
629 |
double centerY = r.getCenterY();
|
630 |
Ellipse2D circle = new Ellipse2D.Double(centerX - radius, centerY - radius, 2*radius, 2*radius); |
631 |
g.draw(circle); |
632 |
g.setClip(circle); |
633 |
double aux = Math.cos(Math.PI*0.25)*radius; |
634 |
g.drawLine((int) (centerX - aux), (int) (centerY - aux), (int) (centerX + aux), (int) (centerY + aux)); |
635 |
int fontSize = 20; |
636 |
g.setFont(new Font("Arial", fontSize, Font.PLAIN)); |
637 |
g.setColor(Color.black);
|
638 |
g.setClip(null);
|
639 |
|
640 |
if (text == null) { |
641 |
text = new SimpleTextSymbol();
|
642 |
text.setAutoresizeEnabled(true);
|
643 |
|
644 |
} |
645 |
|
646 |
double lineHeight = (r.getHeight()-6)/messageLines.length; |
647 |
Rectangle textRect = new Rectangle( |
648 |
(int) r.getMinX(),
|
649 |
(int) r.getMinY()+6, |
650 |
(int) r.getWidth(),
|
651 |
(int) lineHeight);
|
652 |
for (int i =0; i < messageLines.length; i++) { |
653 |
text.setText(messageLines[i]); |
654 |
text.drawInsideRectangle(g, null, textRect, properties);
|
655 |
textRect.setLocation((int) r.getX(), (int) (r.getY()+r.getHeight())); |
656 |
} |
657 |
} |
658 |
|
659 |
@Override
|
660 |
public XMLEntity getXMLEntity() {
|
661 |
XMLEntity xml = new XMLEntity();
|
662 |
xml.putProperty("className", getClass().getName());
|
663 |
xml.putProperty("desc", desc);
|
664 |
xml.putProperty("exceptionType", exceptionType);
|
665 |
xml.putProperty("message", message);
|
666 |
return xml;
|
667 |
} |
668 |
|
669 |
@Override
|
670 |
public void setXMLEntity(XMLEntity xml) { |
671 |
setDescription(xml.getStringProperty("desc"));
|
672 |
exceptionType = xml.getIntProperty("exceptionType");
|
673 |
message = xml.getStringProperty("message");
|
674 |
} |
675 |
|
676 |
} |
677 |
|
678 |
|
679 |
} |