svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / legend / styling / LabelClass.java @ 42980
History | View | Annotate | Download (20.6 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40560 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40560 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40560 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | /* CVS MESSAGES:
|
||
25 | *
|
||
26 | * $Id: LabelClass.java 13953 2007-09-21 12:26:04Z jaume $
|
||
27 | * $Log$
|
||
28 | * Revision 1.14 2007-09-21 12:26:04 jaume
|
||
29 | * cancellation support extended down to the IGeometry and ISymbol level
|
||
30 | *
|
||
31 | * Revision 1.13 2007/09/17 14:16:11 jaume
|
||
32 | * multilayer symbols sizing bug fixed
|
||
33 | *
|
||
34 | * Revision 1.12 2007/08/22 09:48:13 jvidal
|
||
35 | * javadoc
|
||
36 | *
|
||
37 | * Revision 1.11 2007/05/09 11:04:58 jaume
|
||
38 | * refactored legend hierarchy
|
||
39 | *
|
||
40 | * Revision 1.10 2007/05/08 08:47:40 jaume
|
||
41 | * *** empty log message ***
|
||
42 | *
|
||
43 | * Revision 1.9 2007/04/26 11:41:00 jaume
|
||
44 | * attempting to let defining size in world units
|
||
45 | *
|
||
46 | * Revision 1.8 2007/04/18 15:35:11 jaume
|
||
47 | * *** empty log message ***
|
||
48 | *
|
||
49 | * Revision 1.7 2007/04/12 14:28:43 jaume
|
||
50 | * basic labeling support for lines
|
||
51 | *
|
||
52 | * Revision 1.6 2007/04/11 16:01:08 jaume
|
||
53 | * maybe a label placer refactor
|
||
54 | *
|
||
55 | * Revision 1.5 2007/04/10 16:34:01 jaume
|
||
56 | * towards a styled labeling
|
||
57 | *
|
||
58 | * Revision 1.4 2007/04/05 16:07:14 jaume
|
||
59 | * Styled labeling stuff
|
||
60 | *
|
||
61 | * Revision 1.3 2007/04/02 16:34:56 jaume
|
||
62 | * Styled labeling (start commiting)
|
||
63 | *
|
||
64 | * Revision 1.2 2007/03/09 08:33:43 jaume
|
||
65 | * *** empty log message ***
|
||
66 | *
|
||
67 | * Revision 1.1.2.6 2007/02/15 16:23:44 jaume
|
||
68 | * *** empty log message ***
|
||
69 | *
|
||
70 | * Revision 1.1.2.5 2007/02/09 07:47:05 jaume
|
||
71 | * Isymbol moved
|
||
72 | *
|
||
73 | * Revision 1.1.2.4 2007/02/02 16:21:24 jaume
|
||
74 | * start commiting labeling stuff
|
||
75 | *
|
||
76 | * Revision 1.1.2.3 2007/02/01 17:46:49 jaume
|
||
77 | * *** empty log message ***
|
||
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/30 18:10:45 jaume
|
||
83 | * start commiting labeling stuff
|
||
84 | *
|
||
85 | *
|
||
86 | */
|
||
87 | package org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling; |
||
88 | |||
89 | import java.awt.Dimension; |
||
90 | import java.awt.Graphics2D; |
||
91 | import java.awt.Rectangle; |
||
92 | import java.awt.Shape; |
||
93 | import java.awt.geom.AffineTransform; |
||
94 | import java.awt.geom.Point2D; |
||
95 | import java.awt.geom.Rectangle2D; |
||
96 | |||
97 | import org.gvsig.fmap.geom.Geometry; |
||
98 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
99 | import org.gvsig.fmap.geom.GeometryLocator; |
||
100 | import org.gvsig.fmap.geom.GeometryManager; |
||
101 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
102 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
103 | 42980 | fdiaz | import org.gvsig.fmap.geom.primitive.Polygon; |
104 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.ViewPort; |
105 | import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass; |
||
106 | import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelLocationMetrics; |
||
107 | import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport; |
||
108 | import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol; |
||
109 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
||
110 | import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle; |
||
111 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit; |
||
112 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.impl.SimpleTextSymbol; |
||
113 | import org.gvsig.tools.ToolsLocator; |
||
114 | import org.gvsig.tools.dynobject.DynStruct; |
||
115 | import org.gvsig.tools.persistence.PersistenceManager; |
||
116 | 41676 | jjdelcerro | import org.gvsig.tools.persistence.Persistent; |
117 | import org.gvsig.tools.persistence.PersistentContext; |
||
118 | 40435 | jjdelcerro | import org.gvsig.tools.persistence.PersistentState; |
119 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
120 | 41676 | jjdelcerro | import org.gvsig.tools.persistence.spi.PersistentContextServices; |
121 | 40435 | jjdelcerro | import org.gvsig.tools.util.Callable; |
122 | 42980 | fdiaz | |
123 | 40435 | jjdelcerro | import org.slf4j.Logger; |
124 | import org.slf4j.LoggerFactory; |
||
125 | |||
126 | |||
127 | /**
|
||
128 | *
|
||
129 | * LabelClass is the model of the label in the new simbology of gvSIG. In this
|
||
130 | * class is contained its definition, the expresion that defines the text which
|
||
131 | * is going to be showed, if it will be visible or not, the text symbol that is
|
||
132 | * going to paint the label and the style for its background.
|
||
133 | *
|
||
134 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
135 | */
|
||
136 | public class LabelClass implements ILabelClass { |
||
137 | 42980 | fdiaz | |
138 | 40435 | jjdelcerro | private static final String FIELD_REFERENCE_SYSTEM = "referenceSystem"; |
139 | private static final String FIELD_UNIT = "unit"; |
||
140 | private static final String FIELD_SQL_QUERY = "sqlQuery"; |
||
141 | private static final String FIELD_SCALE = "scale"; |
||
142 | private static final String FIELD_PRIORITY = "priority"; |
||
143 | private static final String FIELD_TEXTS = "texts"; |
||
144 | private static final String FIELD_LABEL_STYLE = "labelStyle"; |
||
145 | private static final String FIELD_VISIBLE = "visible"; |
||
146 | 40679 | jldominguez | private static final String FIELD_LABEL_EXPRESSIONS = "labelExpressions"; |
147 | 40435 | jjdelcerro | private static final String FIELD_TEXT_SYMBOL = "textSymbol"; |
148 | private static final String FIELD_NAME = "name"; |
||
149 | 40679 | jldominguez | private static final String FIELD_IS_USESQL = "useSQL"; |
150 | 40435 | jjdelcerro | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
151 | private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class); |
||
152 | private static final String LABEL_CLASS_PERSISTENCE_DEFINITION_NAME = "LabelClass"; |
||
153 | 41174 | jldominguez | private String theName = ""; |
154 | 40435 | jjdelcerro | private ITextSymbol textSymbol;
|
155 | 40679 | jldominguez | private String[] labelExpressions = new String[0]; |
156 | 40435 | jjdelcerro | private boolean isVisible = true; |
157 | private ILabelStyle labelStyle;
|
||
158 | private String[] texts; |
||
159 | private int priority; |
||
160 | private double scale = 1; |
||
161 | 42980 | fdiaz | |
162 | 40435 | jjdelcerro | private String sqlQuery; |
163 | 40679 | jldominguez | private boolean usesSQL; |
164 | 40435 | jjdelcerro | |
165 | private int unit = CartographicSupportToolkit.DefaultMeasureUnit; |
||
166 | private int referenceSystem = |
||
167 | CartographicSupportToolkit.DefaultReferenceSystem; |
||
168 | 42980 | fdiaz | |
169 | 40435 | jjdelcerro | /**
|
170 | * Returns true if the label will be showed in the map
|
||
171 | *
|
||
172 | * @return isVisible boolean
|
||
173 | */
|
||
174 | public boolean isVisible() { |
||
175 | return isVisible;
|
||
176 | } |
||
177 | |||
178 | /**
|
||
179 | 41676 | jjdelcerro | * Returns true if the label will be showed in the map
|
180 | *
|
||
181 | * @return isVisible boolean
|
||
182 | */
|
||
183 | public boolean isVisible(double scale) { |
||
184 | return isVisible();
|
||
185 | } |
||
186 | |||
187 | /**
|
||
188 | 40435 | jjdelcerro | * Sets the visibility of the label in the map.
|
189 | *
|
||
190 | * @param isVisible boolean
|
||
191 | */
|
||
192 | public void setVisible(boolean isVisible) { |
||
193 | this.isVisible = isVisible;
|
||
194 | } |
||
195 | |||
196 | /**
|
||
197 | * Returns the expression that defines the text which will be showed in
|
||
198 | * the label
|
||
199 | *
|
||
200 | * @return labelExpression String
|
||
201 | */
|
||
202 | 40679 | jldominguez | public String[] getLabelExpressions() { |
203 | return labelExpressions;
|
||
204 | 40435 | jjdelcerro | } |
205 | |||
206 | /**
|
||
207 | * Stablishes the expresion that, when it is evaluated, returns the text
|
||
208 | * which will be showed by the label.
|
||
209 | *
|
||
210 | * @param labelExpression String
|
||
211 | */
|
||
212 | 40679 | jldominguez | public void setLabelExpressions(String[] lbl_exps) { |
213 | if (lbl_exps == null) { |
||
214 | this.labelExpressions = new String[0]; |
||
215 | } else {
|
||
216 | this.labelExpressions = lbl_exps;
|
||
217 | } |
||
218 | 42980 | fdiaz | |
219 | 40435 | jjdelcerro | } |
220 | |||
221 | /**
|
||
222 | * Returns the text symbol that is being used for the text(the font,
|
||
223 | * size,style,aligment)
|
||
224 | *
|
||
225 | * @return label ITextSymbol
|
||
226 | */
|
||
227 | public ITextSymbol getTextSymbol() {
|
||
228 | if (textSymbol == null) { |
||
229 | textSymbol = new SimpleTextSymbol();
|
||
230 | } |
||
231 | return textSymbol;
|
||
232 | } |
||
233 | |||
234 | private Dimension getSize() { |
||
235 | if (labelStyle == null) { |
||
236 | if (texts!=null && texts.length >0) { |
||
237 | String t = ""; |
||
238 | for (int i = 0; i < texts.length; i++) { |
||
239 | t += texts[i]; |
||
240 | } |
||
241 | getTextSymbol().setText(t); |
||
242 | } |
||
243 | |||
244 | Rectangle bounds = getTextSymbol().getBounds();
|
||
245 | bounds.setLocation( |
||
246 | (int) Math.round(bounds.getX()), |
||
247 | (int) Math.round(bounds.getY()+bounds.getHeight())); |
||
248 | return new Dimension(bounds.width, bounds.height); |
||
249 | } else {
|
||
250 | labelStyle.setTextFields(texts); |
||
251 | return labelStyle.getSize();
|
||
252 | } |
||
253 | } |
||
254 | /**
|
||
255 | * Stablishes the text symbol that is going to be used for the text(the
|
||
256 | * font,size,style,aligment)
|
||
257 | *
|
||
258 | * @param textSymbol ITextSymbol
|
||
259 | */
|
||
260 | public void setTextSymbol(ITextSymbol textSymbol) { |
||
261 | this.textSymbol = textSymbol;
|
||
262 | if (textSymbol == null) { |
||
263 | this.textSymbol = new SimpleTextSymbol(); |
||
264 | } |
||
265 | setReferenceSystem(referenceSystem); |
||
266 | setUnit(unit); |
||
267 | } |
||
268 | |||
269 | /**
|
||
270 | * Stablishes the style for the label.
|
||
271 | *
|
||
272 | * @param labelStyle ILabelStyle
|
||
273 | */
|
||
274 | public void setLabelStyle(ILabelStyle labelStyle) { |
||
275 | this.labelStyle = labelStyle;
|
||
276 | } |
||
277 | |||
278 | /**
|
||
279 | * Returns the style of the label
|
||
280 | *
|
||
281 | */
|
||
282 | public ILabelStyle getLabelStyle() {
|
||
283 | return this.labelStyle; |
||
284 | } |
||
285 | |||
286 | /**
|
||
287 | * Returns the name of the label
|
||
288 | *
|
||
289 | */
|
||
290 | public String getName() { |
||
291 | 41174 | jldominguez | return theName;
|
292 | 40435 | jjdelcerro | } |
293 | |||
294 | /**
|
||
295 | * Stablishes the name of the label
|
||
296 | * @param name
|
||
297 | */
|
||
298 | public void setName(String name) { |
||
299 | 41174 | jldominguez | if (name == null) { |
300 | theName = "";
|
||
301 | } else {
|
||
302 | theName = name; |
||
303 | } |
||
304 | 40435 | jjdelcerro | } |
305 | |||
306 | public String toString() { |
||
307 | // for debugging
|
||
308 | // return name+"{label expression="+labelExpression+", visible="+isVisible+", priority="+priority+"}";
|
||
309 | return getName();
|
||
310 | } |
||
311 | |||
312 | /**
|
||
313 | * Sets the text for the label
|
||
314 | *
|
||
315 | * @param texts String[]
|
||
316 | */
|
||
317 | public void setTexts(String[] texts) { |
||
318 | 42172 | jbadia | this.texts = null; |
319 | 40435 | jjdelcerro | this.texts = texts;
|
320 | |||
321 | } |
||
322 | |||
323 | /**
|
||
324 | * Return the text for the label
|
||
325 | *
|
||
326 | * @param texts String[]
|
||
327 | */
|
||
328 | public String[] getTexts() { |
||
329 | return this.texts; |
||
330 | } |
||
331 | |||
332 | /**
|
||
333 | * <p>
|
||
334 | * LabelLocationMetrics, contains the anchor point, rotation, and some
|
||
335 | * other geometric calculations computed by the PlacementManager.
|
||
336 | * </p>
|
||
337 | *
|
||
338 | * <p>
|
||
339 | * The shp argument is passed as an accessory for subclasses of this
|
||
340 | * class in case they need futher geometric calculations
|
||
341 | * </p>
|
||
342 | * @param graphics, graphics to use to paint the label.
|
||
343 | * @param llm, concrete settings of the placement of this layer
|
||
344 | * @param shp, the Shape over whose the label is painted
|
||
345 | */
|
||
346 | public void draw(Graphics2D graphics, ILabelLocationMetrics llm, Geometry geom) { |
||
347 | if (scale == 0) |
||
348 | return;
|
||
349 | |||
350 | |||
351 | Dimension size = getSize();
|
||
352 | int width = (int) Math.round(size.getWidth()*scale); |
||
353 | if (width < 1) |
||
354 | return;
|
||
355 | |||
356 | int height = (int) Math.round(size.getHeight()*scale); |
||
357 | if (height < 1) |
||
358 | return;
|
||
359 | |||
360 | Rectangle r = new Rectangle(0,0, width, height); |
||
361 | org.gvsig.fmap.geom.primitive.Point anchor; |
||
362 | try {
|
||
363 | anchor = geomManager.createPoint(llm.getAnchor().getX(), llm.getAnchor().getY(), SUBTYPES.GEOM2D); |
||
364 | double xAnchor = anchor.getX();
|
||
365 | double yAnchor = anchor.getY();
|
||
366 | double theta = llm.getRotation();
|
||
367 | |||
368 | graphics.translate(xAnchor, yAnchor); |
||
369 | graphics.rotate(theta); |
||
370 | synchronized (this) { |
||
371 | float fontSizeBefore = textSymbol.getFont().getSize2D();
|
||
372 | try {
|
||
373 | textSymbol.setFontSize(fontSizeBefore*scale); |
||
374 | drawInsideRectangle(graphics, r); |
||
375 | textSymbol.setFontSize(fontSizeBefore); |
||
376 | } catch (SymbolDrawingException e) {
|
||
377 | 41085 | jjdelcerro | logger.warn("Error drawing", e);
|
378 | 40435 | jjdelcerro | } |
379 | } |
||
380 | graphics.rotate(-theta); |
||
381 | graphics.translate(-xAnchor, -yAnchor); |
||
382 | } catch (CreateGeometryException e1) {
|
||
383 | 41085 | jjdelcerro | logger.warn("Error creating a point", e1);
|
384 | 40435 | jjdelcerro | } |
385 | } |
||
386 | |||
387 | private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) { |
||
388 | int x;
|
||
389 | int y;
|
||
390 | if (ratioViewPort > ratioLabel) {
|
||
391 | // size is defined by the viewport height
|
||
392 | y = (int) (r.height*xy[1]); |
||
393 | x = (int) ((0.5*r.width) - (0.5-xy[0])*(ratioLabel*r.height)); |
||
394 | } else {
|
||
395 | // size is defined by the viewport width
|
||
396 | x = (int) (r.width * xy[0]); |
||
397 | y = (int) ((0.5 * r.height) - (0.5-xy[1])*(r.width/ratioLabel)); |
||
398 | } |
||
399 | xy[0] = x;
|
||
400 | xy[1] = y;
|
||
401 | } |
||
402 | |||
403 | /**
|
||
404 | * Useful to render a Label with size inside little rectangles.
|
||
405 | *
|
||
406 | * @param graphics Graphics2D
|
||
407 | * @param bounds Rectangle
|
||
408 | * @throws SymbolDrawingException
|
||
409 | */
|
||
410 | public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds) throws SymbolDrawingException { |
||
411 | if (labelStyle != null) { |
||
412 | labelStyle.drawInsideRectangle(graphics, bounds); |
||
413 | Rectangle2D[] textBounds = labelStyle.getTextBounds(); |
||
414 | Dimension labelSz = getSize();
|
||
415 | final double ratioLabel = labelSz.getWidth()/labelSz.getHeight(); |
||
416 | final double ratioViewPort = bounds.getWidth() / bounds.getHeight(); |
||
417 | final double[] xy = new double[2]; |
||
418 | |||
419 | |||
420 | // draw the text fields
|
||
421 | if (textBounds.length > 0 && texts!=null) { |
||
422 | for (int i = 0; i < textBounds.length && i < texts.length; i++) { |
||
423 | getTextSymbol().setText(texts[i]); |
||
424 | Rectangle2D textFieldArea = textBounds[i];
|
||
425 | xy[0] = textFieldArea.getX();
|
||
426 | xy[1] = textFieldArea.getY();
|
||
427 | relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort); |
||
428 | int x = (int) Math.round(xy[0]); |
||
429 | int y = (int) Math.round(xy[1]); |
||
430 | |||
431 | xy[0] = textFieldArea.getMaxX();
|
||
432 | xy[1] = textFieldArea.getMaxY();
|
||
433 | relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort); |
||
434 | int width = (int) Math.round(xy[0]) -x; |
||
435 | int height = (int) Math.round(xy[1] - y) ; |
||
436 | |||
437 | Rectangle textRect = new Rectangle(x, y, width, height); |
||
438 | Shape oldClip = graphics.getClip();
|
||
439 | graphics.setClip(textRect); |
||
440 | getTextSymbol().drawInsideRectangle(graphics, null, textRect, null); |
||
441 | graphics.setClip(oldClip); |
||
442 | } |
||
443 | } |
||
444 | } else {
|
||
445 | |||
446 | 42172 | jbadia | if (texts != null && texts.length>0){ |
447 | String fullText = ""; |
||
448 | for(int ig=0; ig<texts.length; ig++){ |
||
449 | fullText = fullText + texts[ig]; |
||
450 | } |
||
451 | getTextSymbol().setText(fullText); |
||
452 | } |
||
453 | 40435 | jjdelcerro | getTextSymbol().drawInsideRectangle(graphics, null, bounds, null); |
454 | } |
||
455 | } |
||
456 | |||
457 | public int getPriority() { |
||
458 | return priority;
|
||
459 | } |
||
460 | |||
461 | public void setPriority(int priority) { |
||
462 | this.priority = priority;
|
||
463 | } |
||
464 | |||
465 | public Geometry getShape(ILabelLocationMetrics llm) throws CreateGeometryException { |
||
466 | if (llm==null) |
||
467 | return null; |
||
468 | Point2D anchor = llm.getAnchor();
|
||
469 | double theta = llm.getRotation();
|
||
470 | |||
471 | // 2. calculate the container shape
|
||
472 | 42980 | fdiaz | Polygon returnedValue;
|
473 | 40435 | jjdelcerro | Rectangle bounds = getBounds();
|
474 | |||
475 | 42980 | fdiaz | AffineTransform at = AffineTransform.getTranslateInstance(anchor.getX(), anchor.getY()); |
476 | 40435 | jjdelcerro | at.concatenate(AffineTransform.getRotateInstance(theta));
|
477 | 42980 | fdiaz | returnedValue = geomManager.createPolygon(SUBTYPES.GEOM2D); |
478 | returnedValue.addVertex(bounds.getMinX(), bounds.getMinY()); |
||
479 | returnedValue.addVertex(bounds.getMinX(), bounds.getMaxY()); |
||
480 | returnedValue.addVertex(bounds.getMaxX(), bounds.getMaxY()); |
||
481 | returnedValue.addVertex(bounds.getMaxX(), bounds.getMinY()); |
||
482 | returnedValue.addVertex(bounds.getMinX(), bounds.getMinY()); |
||
483 | 40435 | jjdelcerro | |
484 | returnedValue.transform(at); |
||
485 | return returnedValue;
|
||
486 | } |
||
487 | |||
488 | public String getClassName() { |
||
489 | return getClass().getName();
|
||
490 | } |
||
491 | |||
492 | public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
493 | Dimension sz = getSize();
|
||
494 | double width = sz.getWidth();
|
||
495 | double height = sz.getHeight();
|
||
496 | return CartographicSupportToolkit.
|
||
497 | getCartographicLength(this,
|
||
498 | Math.max(width, height),
|
||
499 | viewPort, |
||
500 | dpi); |
||
501 | } |
||
502 | |||
503 | public int getReferenceSystem() { |
||
504 | return referenceSystem;
|
||
505 | } |
||
506 | |||
507 | public int getUnit() { |
||
508 | return unit;
|
||
509 | } |
||
510 | |||
511 | public void setCartographicSize(double cartographicSize, Geometry geom) { |
||
512 | Dimension sz = getSize();
|
||
513 | double width = sz.getWidth();
|
||
514 | double height = sz.getHeight();
|
||
515 | if (width >= height) {
|
||
516 | scale = cartographicSize / width; |
||
517 | } else {
|
||
518 | scale = cartographicSize / height; |
||
519 | } |
||
520 | } |
||
521 | |||
522 | public void setReferenceSystem(int referenceSystem) { |
||
523 | this.referenceSystem = referenceSystem;
|
||
524 | if (textSymbol != null && textSymbol instanceof CartographicSupport) { |
||
525 | ((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem); |
||
526 | } |
||
527 | } |
||
528 | |||
529 | public void setUnit(int unitIndex) { |
||
530 | this.unit = unitIndex;
|
||
531 | if (textSymbol != null && textSymbol instanceof CartographicSupport) { |
||
532 | ((CartographicSupport) textSymbol).setUnit(unitIndex); |
||
533 | } |
||
534 | } |
||
535 | |||
536 | public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
537 | setCartographicSize(getCartographicSize( |
||
538 | viewPort, |
||
539 | dpi, |
||
540 | geom), |
||
541 | geom); |
||
542 | return 0; |
||
543 | } |
||
544 | |||
545 | public Rectangle getBounds() { |
||
546 | Dimension cBounds = getSize();
|
||
547 | return new Rectangle( |
||
548 | 0,
|
||
549 | 0,
|
||
550 | (int) Math.round(cBounds.width*scale), |
||
551 | (int) Math.round(cBounds.height*scale)); |
||
552 | } |
||
553 | |||
554 | public String getSQLQuery() { |
||
555 | if (sqlQuery == null) sqlQuery = ""; |
||
556 | return sqlQuery;
|
||
557 | } |
||
558 | |||
559 | public void setSQLQuery(String sqlQuery) { |
||
560 | this.sqlQuery = sqlQuery;
|
||
561 | } |
||
562 | |||
563 | /*
|
||
564 | * (non-Javadoc)
|
||
565 | 42980 | fdiaz | *
|
566 | 40435 | jjdelcerro | * @seeorg.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.
|
567 | * persistence.PersistentState)
|
||
568 | */
|
||
569 | public void saveToState(PersistentState state) throws PersistenceException { |
||
570 | state.set(FIELD_NAME, getName()); |
||
571 | state.set(FIELD_TEXT_SYMBOL, getTextSymbol()); |
||
572 | 40679 | jldominguez | state.set(FIELD_LABEL_EXPRESSIONS, getLabelExpressions()); |
573 | 40435 | jjdelcerro | state.set(FIELD_VISIBLE, isVisible()); |
574 | 42980 | fdiaz | |
575 | 40711 | jldominguez | ILabelStyle sty = getLabelStyle(); |
576 | if (sty != null) { |
||
577 | state.set(FIELD_LABEL_STYLE, getLabelStyle()); |
||
578 | } |
||
579 | 42980 | fdiaz | |
580 | 40435 | jjdelcerro | state.set(FIELD_TEXTS, getTexts()); |
581 | state.set(FIELD_PRIORITY, getPriority()); |
||
582 | 40679 | jldominguez | |
583 | 40435 | jjdelcerro | state.set(FIELD_SCALE, scale); |
584 | state.set(FIELD_SQL_QUERY, getSQLQuery()); |
||
585 | 40679 | jldominguez | state.set(FIELD_IS_USESQL, isUseSqlQuery()); |
586 | 42980 | fdiaz | |
587 | 40435 | jjdelcerro | state.set(FIELD_UNIT, getUnit()); |
588 | state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem()); |
||
589 | } |
||
590 | |||
591 | /*
|
||
592 | * (non-Javadoc)
|
||
593 | 42980 | fdiaz | *
|
594 | 40435 | jjdelcerro | * @see
|
595 | * org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.
|
||
596 | * persistence.PersistentState)
|
||
597 | */
|
||
598 | public void loadFromState(PersistentState state) |
||
599 | throws PersistenceException {
|
||
600 | setName(state.getString(FIELD_NAME)); |
||
601 | setTextSymbol((ITextSymbol) state.get(FIELD_TEXT_SYMBOL)); |
||
602 | 40679 | jldominguez | setLabelExpressions(state.getStringArray(FIELD_LABEL_EXPRESSIONS)); |
603 | 40435 | jjdelcerro | setVisible(state.getBoolean(FIELD_VISIBLE)); |
604 | 42980 | fdiaz | |
605 | 40711 | jldominguez | if (state.hasValue(FIELD_LABEL_STYLE)) {
|
606 | 42980 | fdiaz | setLabelStyle((ILabelStyle) state.get(FIELD_LABEL_STYLE)); |
607 | 40711 | jldominguez | } |
608 | 42980 | fdiaz | |
609 | 40435 | jjdelcerro | setTexts((String[]) state.getArray(FIELD_TEXTS, String.class)); |
610 | setPriority(state.getInt(FIELD_PRIORITY)); |
||
611 | scale = state.getDouble(FIELD_SCALE); |
||
612 | 42980 | fdiaz | |
613 | 40679 | jldominguez | setUseSqlQuery(state.getBoolean(FIELD_IS_USESQL)); |
614 | 40435 | jjdelcerro | setSQLQuery(state.getString(FIELD_SQL_QUERY)); |
615 | 42980 | fdiaz | |
616 | 40435 | jjdelcerro | setUnit(state.getInt(FIELD_UNIT)); |
617 | setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM)); |
||
618 | } |
||
619 | |||
620 | public static class RegisterPersistence implements Callable { |
||
621 | |||
622 | public Object call() throws Exception { |
||
623 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
624 | if( manager.getDefinition(LABEL_CLASS_PERSISTENCE_DEFINITION_NAME)==null ) { |
||
625 | DynStruct definition = manager.addDefinition( |
||
626 | LabelClass.class, |
||
627 | LABEL_CLASS_PERSISTENCE_DEFINITION_NAME, |
||
628 | LABEL_CLASS_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
||
629 | 42980 | fdiaz | null,
|
630 | 40435 | jjdelcerro | null
|
631 | ); |
||
632 | |||
633 | definition.addDynFieldString(FIELD_NAME).setMandatory(true);
|
||
634 | definition.addDynFieldObject(FIELD_TEXT_SYMBOL).setMandatory(true).setClassOfValue(ITextSymbol.class);
|
||
635 | 42980 | fdiaz | |
636 | 40679 | jldominguez | definition.addDynFieldArray(FIELD_LABEL_EXPRESSIONS) |
637 | .setClassOfItems(String.class).setMandatory(true); |
||
638 | 42980 | fdiaz | |
639 | 40435 | jjdelcerro | definition.addDynFieldBoolean(FIELD_VISIBLE).setMandatory(true);
|
640 | 42980 | fdiaz | |
641 | 40711 | jldominguez | definition.addDynFieldObject(FIELD_LABEL_STYLE).setMandatory(false)
|
642 | .setClassOfValue(ILabelStyle.class); |
||
643 | 42980 | fdiaz | |
644 | 40435 | jjdelcerro | definition.addDynFieldList(FIELD_TEXTS).setMandatory(true).setClassOfItems(String.class); |
645 | definition.addDynFieldInt(FIELD_PRIORITY).setMandatory(true);
|
||
646 | definition.addDynFieldDouble(FIELD_SCALE).setMandatory(true);
|
||
647 | definition.addDynFieldString(FIELD_SQL_QUERY).setMandatory(true);
|
||
648 | 40679 | jldominguez | definition.addDynFieldBoolean(FIELD_IS_USESQL).setMandatory(true);
|
649 | 40435 | jjdelcerro | definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
|
650 | definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
|
||
651 | } |
||
652 | return Boolean.TRUE; |
||
653 | } |
||
654 | 42980 | fdiaz | |
655 | 40435 | jjdelcerro | } |
656 | |||
657 | 40679 | jldominguez | public void setUseSqlQuery(boolean use_sql) { |
658 | this.usesSQL = use_sql;
|
||
659 | } |
||
660 | 40435 | jjdelcerro | |
661 | 40679 | jldominguez | public boolean isUseSqlQuery() { |
662 | return this.usesSQL; |
||
663 | } |
||
664 | 40435 | jjdelcerro | |
665 | 40679 | jldominguez | public String getStringLabelExpression(){ |
666 | String expr = ""; |
||
667 | |||
668 | 40701 | jldominguez | if(labelExpressions != null && labelExpressions.length > 0) { |
669 | 40679 | jldominguez | |
670 | for (int i = 0; i < labelExpressions.length-1; i++) { |
||
671 | expr += (String) labelExpressions[i] + ":";//EOFIELD |
||
672 | } |
||
673 | 40701 | jldominguez | expr += labelExpressions[labelExpressions.length - 1];
|
674 | 40679 | jldominguez | |
675 | 40701 | jldominguez | } else {
|
676 | expr = "";
|
||
677 | 40679 | jldominguez | } |
678 | return expr;
|
||
679 | } |
||
680 | |||
681 | 41676 | jjdelcerro | public Object clone() throws CloneNotSupportedException { |
682 | 40679 | jldominguez | |
683 | 41676 | jjdelcerro | PersistenceManager persman = ToolsLocator.getPersistenceManager(); |
684 | |||
685 | PersistentContext context = null;
|
||
686 | Object resp = null; |
||
687 | PersistentState state = null;
|
||
688 | try {
|
||
689 | state = persman.getState(this, true); |
||
690 | } catch (PersistenceException e) {
|
||
691 | logger.warn("Can't clone, return me !!!!!!!!!!!!!!", e);
|
||
692 | return this; |
||
693 | } |
||
694 | context = state.getContext(); |
||
695 | if (context instanceof PersistentContextServices) { |
||
696 | /*
|
||
697 | * Ensure that previous instances are not used,
|
||
698 | * so objects are recreated
|
||
699 | */
|
||
700 | ((PersistentContextServices) context).clear(); |
||
701 | } |
||
702 | try {
|
||
703 | resp = persman.create(state); |
||
704 | } catch (Exception e) { |
||
705 | logger.warn("Can't clone, return me !!!!!!!!!!!!!!", e);
|
||
706 | return this; |
||
707 | } |
||
708 | return resp;
|
||
709 | |||
710 | } |
||
711 | |||
712 | |||
713 | 42980 | fdiaz | |
714 | |||
715 | 40435 | jjdelcerro | } |