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