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 / AttrInTableLabelingStrategy.java @ 41676
History | View | Annotate | Download (30.7 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 41394 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40560 | jjdelcerro | *
|
11 | 41394 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40560 | jjdelcerro | *
|
16 | 41394 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40560 | jjdelcerro | *
|
20 | 41394 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40560 | jjdelcerro | */
|
23 | 41394 | jjdelcerro | package org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling; |
24 | |||
25 | import java.awt.Color; |
||
26 | import java.awt.Font; |
||
27 | import java.awt.Graphics2D; |
||
28 | 41133 | jldominguez | import java.awt.geom.NoninvertibleTransformException; |
29 | import java.awt.geom.Point2D; |
||
30 | 41394 | jjdelcerro | import java.awt.image.BufferedImage; |
31 | import java.util.ArrayList; |
||
32 | import java.util.List; |
||
33 | |||
34 | 41133 | jldominguez | import org.apache.batik.ext.awt.geom.PathLength; |
35 | 41394 | jjdelcerro | import org.cresques.cts.ICoordTrans; |
36 | import org.cresques.cts.IProjection; |
||
37 | import org.slf4j.LoggerFactory; |
||
38 | |||
39 | import org.gvsig.compat.print.PrintAttributes; |
||
40 | import org.gvsig.fmap.dal.exception.DataException; |
||
41 | import org.gvsig.fmap.dal.exception.ReadException; |
||
42 | import org.gvsig.fmap.dal.feature.Feature; |
||
43 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
44 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
45 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
46 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
47 | import org.gvsig.fmap.geom.Geometry; |
||
48 | 41133 | jldominguez | import org.gvsig.fmap.geom.GeometryLocator; |
49 | 41394 | jjdelcerro | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
50 | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
||
51 | import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
||
52 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
53 | import org.gvsig.fmap.geom.primitive.Point; |
||
54 | 41133 | jldominguez | import org.gvsig.fmap.geom.type.GeometryType; |
55 | 41394 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContext; |
56 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
57 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
58 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
59 | import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsEnvelopeEvaluator; |
||
60 | import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod; |
||
61 | import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints; |
||
62 | import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints; |
||
63 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit; |
||
64 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.impl.SimpleTextSymbol; |
||
65 | import org.gvsig.tools.ToolsLocator; |
||
66 | import org.gvsig.tools.dispose.DisposableIterator; |
||
67 | import org.gvsig.tools.dynobject.DynStruct; |
||
68 | import org.gvsig.tools.exception.BaseException; |
||
69 | import org.gvsig.tools.persistence.PersistenceManager; |
||
70 | import org.gvsig.tools.persistence.PersistentState; |
||
71 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
72 | import org.gvsig.tools.task.Cancellable; |
||
73 | import org.gvsig.tools.util.Callable; |
||
74 | import org.slf4j.Logger; |
||
75 | |||
76 | /**
|
||
77 | * LabelingStrategy used when the user wants to use label sizes, rotations, etc.
|
||
78 | * from the values included in fields of the datasource's table
|
||
79 | *
|
||
80 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
81 | *
|
||
82 | */
|
||
83 | public class AttrInTableLabelingStrategy implements IAttrInTableLabelingStrategy { |
||
84 | |||
85 | private static final String FIELD_LABELING_METHOD = "labelingMethod"; |
||
86 | private static final String FIELD_ZOOM_CONSTRAINTS = "zoomConstraints"; |
||
87 | private static final String FIELD_COLOR_FONT = "colorFont"; |
||
88 | private static final String FIELD_REFERENCE_SYSTEM = "referenceSystem"; |
||
89 | private static final String FIELD_FIXED_COLOR = "fixedColor"; |
||
90 | private static final String FIELD_USE_FIXED_COLOR = "useFixedColor"; |
||
91 | private static final String FIELD_FIXED_SIZE = "fixedSize"; |
||
92 | private static final String FIELD_USE_FIXED_SIZE = "useFixedSize"; |
||
93 | private static final String FIELD_FONT = "font"; |
||
94 | private static final String FIELD_UNIT = "Unit"; |
||
95 | private static final String FIELD_ROTATION = "RotationField"; |
||
96 | private static final String FIELD_COLOR = "ColorField"; |
||
97 | private static final String FIELD_HEIGHT = "HeightField"; |
||
98 | private static final String FIELD_TEXT = "TextField"; |
||
99 | private static final Logger logger = LoggerFactory.getLogger(AttrInTableLabelingStrategy.class); |
||
100 | public static final double MIN_TEXT_SIZE = 3; |
||
101 | private static final String ATTR_IN_TABLE_LABELING_STRATEGY_PERSISTENCE_DEFINITION_NAME |
||
102 | = "AttrInTableLabelingStrategy";
|
||
103 | private ILabelingMethod method = new DefaultLabelingMethod(); |
||
104 | private IZoomConstraints zoom;
|
||
105 | private FLyrVect layer;
|
||
106 | // private double unitFactor = 1D;
|
||
107 | private double fixedSize = 10; |
||
108 | private Color fixedColor; |
||
109 | private int unit = -1; //(pixel) |
||
110 | private boolean useFixedSize; |
||
111 | private boolean useFixedColor; |
||
112 | private int referenceSystem; |
||
113 | // private boolean isPrinting;
|
||
114 | private String[] usedFields = null; |
||
115 | private Font font; |
||
116 | private Color colorFont; |
||
117 | private String textFieldName; |
||
118 | private String rotationFieldName; |
||
119 | private String heightFieldName; |
||
120 | private String colorFieldName; |
||
121 | |||
122 | public ILabelingMethod getLabelingMethod() {
|
||
123 | return this.method; |
||
124 | } |
||
125 | |||
126 | public void setLabelingMethod(ILabelingMethod method) { |
||
127 | this.method = method;
|
||
128 | } |
||
129 | |||
130 | public IPlacementConstraints getPlacementConstraints() {
|
||
131 | return null; // (automatically handled by the driver) |
||
132 | } |
||
133 | |||
134 | public void setPlacementConstraints(IPlacementConstraints constraints) { |
||
135 | // nothing
|
||
136 | } |
||
137 | |||
138 | public IZoomConstraints getZoomConstraints() {
|
||
139 | return zoom;
|
||
140 | } |
||
141 | |||
142 | public void setZoomConstraints(IZoomConstraints constraints) { |
||
143 | this.zoom = constraints;
|
||
144 | } |
||
145 | |||
146 | 41676 | jjdelcerro | // private double getScale(ViewPort vp, PrintAttributes patts)
|
147 | // throws ReadException {
|
||
148 | //
|
||
149 | // double dpi = 0;
|
||
150 | // if (patts != null) {
|
||
151 | // int len = PrintAttributes.PRINT_QUALITY_DPI.length;
|
||
152 | // int priq = patts.getPrintQuality();
|
||
153 | // if (priq < 0 || priq >= len) {
|
||
154 | // dpi = vp.getDPI();
|
||
155 | // } else {
|
||
156 | // dpi = PrintAttributes.PRINT_QUALITY_DPI[priq];
|
||
157 | // }
|
||
158 | // } else {
|
||
159 | // dpi = vp.getDPI();
|
||
160 | // }
|
||
161 | //
|
||
162 | // // ============== We have dpi now ====================
|
||
163 | // IProjection proj = vp.getProjection();
|
||
164 | //
|
||
165 | // if (vp.getImageSize() == null || vp.getAdjustedEnvelope() == null) {
|
||
166 | // throw new ReadException("",
|
||
167 | // new Exception("Viewport does not have image size or envelope"));
|
||
168 | // }
|
||
169 | //
|
||
170 | // double[] trans2Meter = MapContext.getDistanceTrans2Meter();
|
||
171 | // if (proj == null) {
|
||
172 | // double wmeters = ((vp.getImageSize().width / dpi) * 0.0254);
|
||
173 | // return (long) ((trans2Meter[vp.getMapUnits()] * vp.getAdjustedEnvelope().getLength(0))
|
||
174 | // / wmeters);
|
||
175 | // } else {
|
||
176 | // return Math.round(proj.getScale(
|
||
177 | // vp.getAdjustedEnvelope().getMinimum(0)
|
||
178 | // * trans2Meter[vp.getMapUnits()],
|
||
179 | // vp.getAdjustedEnvelope().getMaximum(0)
|
||
180 | // * trans2Meter[vp.getMapUnits()],
|
||
181 | // vp.getImageSize().width,
|
||
182 | // dpi));
|
||
183 | // }
|
||
184 | //
|
||
185 | // }
|
||
186 | 41394 | jjdelcerro | |
187 | 41676 | jjdelcerro | private void draw(BufferedImage image, Graphics2D g, double scale, ViewPort viewPort, |
188 | 41394 | jjdelcerro | Cancellable cancel, PrintAttributes props) throws ReadException {
|
189 | 41133 | jldominguez | |
190 | 41676 | jjdelcerro | // double scale = getScale(viewPort, props);
|
191 | 41394 | jjdelcerro | |
192 | SimpleTextSymbol sym = new SimpleTextSymbol();
|
||
193 | sym.setFont(getFont()); |
||
194 | sym.setUnit(unit); |
||
195 | sym.setReferenceSystem(referenceSystem); |
||
196 | if (zoom == null |
||
197 | || (zoom.isUserDefined() && (scale >= zoom.getMaxScale()) |
||
198 | && (scale <= zoom.getMinScale()))) { |
||
199 | FeatureSet set = null;
|
||
200 | DisposableIterator iterator = null;
|
||
201 | try {
|
||
202 | FilteredLogger logger = new FilteredLogger(AttrInTableLabelingStrategy.logger, 10); |
||
203 | |||
204 | // limit the labeling to the visible extent
|
||
205 | List<String> fields = new ArrayList<String>(); |
||
206 | int heightPos = -1; |
||
207 | int rotationPos = -1; |
||
208 | int textPos = -1; |
||
209 | int colorPos = -1; |
||
210 | |||
211 | if (!this.usesFixedSize()) { |
||
212 | if (getHeightField() != null) { |
||
213 | heightPos = fields.size(); |
||
214 | fields.add(getHeightField()); |
||
215 | } |
||
216 | } |
||
217 | if (getRotationField() != null) { |
||
218 | rotationPos = fields.size(); |
||
219 | fields.add(getRotationField()); |
||
220 | } |
||
221 | if (getTextField() != null) { |
||
222 | textPos = fields.size(); |
||
223 | fields.add(getTextField()); |
||
224 | } |
||
225 | |||
226 | if (!this.usesFixedColor() && getColorField() != null) { |
||
227 | colorPos = fields.size(); |
||
228 | fields.add(getColorField()); |
||
229 | } |
||
230 | |||
231 | FeatureStore featureStore = layer.getFeatureStore(); |
||
232 | ICoordTrans ct = layer.getCoordTrans(); |
||
233 | |||
234 | String geomName = featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName();
|
||
235 | fields.add(geomName); |
||
236 | |||
237 | if (viewPort.getAdjustedEnvelope().contains(
|
||
238 | layer.getFullEnvelope())) { |
||
239 | /*
|
||
240 | * viewport contains layer completely
|
||
241 | */
|
||
242 | set = featureStore.getFeatureSet(); |
||
243 | } else {
|
||
244 | /*
|
||
245 | * view port does not contain layer completely
|
||
246 | */
|
||
247 | IntersectsEnvelopeEvaluator iee = null;
|
||
248 | |||
249 | IProjection data_proj = null;
|
||
250 | Envelope env_in_store_crs = null;
|
||
251 | |||
252 | if (ct == null) { |
||
253 | env_in_store_crs = viewPort.getAdjustedEnvelope(); |
||
254 | data_proj = viewPort.getProjection(); |
||
255 | } else {
|
||
256 | env_in_store_crs = viewPort.getAdjustedEnvelope().convert( |
||
257 | ct.getInverted()); |
||
258 | data_proj = ct.getPOrig(); |
||
259 | } |
||
260 | |||
261 | iee = new IntersectsEnvelopeEvaluator(
|
||
262 | env_in_store_crs, |
||
263 | data_proj, |
||
264 | featureStore.getDefaultFeatureType(), |
||
265 | geomName); |
||
266 | FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
||
267 | featureQuery.setAttributeNames((String[]) fields.toArray(new String[fields.size()])); |
||
268 | featureQuery.setFilter(iee); |
||
269 | set = featureStore.getFeatureSet(featureQuery); |
||
270 | } |
||
271 | |||
272 | /*
|
||
273 | * 'set' now has the features that have to be labeled
|
||
274 | */
|
||
275 | iterator = set.fastIterator(); |
||
276 | while (iterator.hasNext()) {
|
||
277 | if (cancel.isCanceled()) {
|
||
278 | return;
|
||
279 | } |
||
280 | try {
|
||
281 | Feature feature = (Feature) iterator.next(); |
||
282 | Geometry geom = feature.getDefaultGeometry(); |
||
283 | if( geom == null ) { |
||
284 | continue;
|
||
285 | } |
||
286 | |||
287 | double size = 0; |
||
288 | Color color = null; |
||
289 | if (useFixedSize) {
|
||
290 | // uses fixed size
|
||
291 | size = fixedSize;// * fontScaleFactor;
|
||
292 | } else if (heightFieldName != null) { |
||
293 | // text size is defined in the table
|
||
294 | try {
|
||
295 | size = feature.getDouble(fields.get(heightPos)); |
||
296 | } catch (Exception e) { |
||
297 | logger.warn("Null text height value for text '"+ feature.getString(fields.get(textPos))+"'."); |
||
298 | continue;
|
||
299 | } |
||
300 | } else {
|
||
301 | // otherwise will use the size in the symbol
|
||
302 | size = sym.getFont().getSize(); |
||
303 | } |
||
304 | |||
305 | 41419 | jjdelcerro | double the_dpi = viewPort.getDPI();
|
306 | 41394 | jjdelcerro | if (props != null) { |
307 | int len = PrintAttributes.PRINT_QUALITY_DPI.length;
|
||
308 | int priq = props.getPrintQuality();
|
||
309 | if (priq < 0 || priq >= len) { |
||
310 | throw new ReadException(featureStore.getName(), |
||
311 | new Exception("Bad print quality value: "+ priq)); |
||
312 | } else {
|
||
313 | the_dpi = PrintAttributes.PRINT_QUALITY_DPI[priq]; |
||
314 | } |
||
315 | } |
||
316 | |||
317 | size = CartographicSupportToolkit.getCartographicLength(this,
|
||
318 | size, |
||
319 | viewPort, |
||
320 | the_dpi); |
||
321 | if (size <= MIN_TEXT_SIZE) {
|
||
322 | // label is too small to be readable, will be skipped
|
||
323 | // this speeds up the rendering in wider zooms
|
||
324 | continue;
|
||
325 | } |
||
326 | |||
327 | 41133 | jldominguez | /*
|
328 | 41394 | jjdelcerro | * ym.setFontSize(int) assumes dpi = 72
|
329 | 41133 | jldominguez | */
|
330 | 41394 | jjdelcerro | double font_size = size * 72 / the_dpi; |
331 | sym.setFontSize(font_size); |
||
332 | |||
333 | if (useFixedColor) {
|
||
334 | color = fixedColor; |
||
335 | } else if (colorFieldName != null) { |
||
336 | // text size is defined in the table
|
||
337 | try {
|
||
338 | color = new Color(feature.getInt(fields.get(colorPos))); |
||
339 | } catch (ClassCastException ccEx) { |
||
340 | if (feature.get(fields.get(colorPos)) != null) { |
||
341 | throw new ReadException("Unknown", ccEx); |
||
342 | } |
||
343 | logger.warn("Null color value for text '"+feature.getString(textFieldName)+"'."); |
||
344 | continue;
|
||
345 | } |
||
346 | } else {
|
||
347 | color = sym.getTextColor(); |
||
348 | } |
||
349 | |||
350 | sym.setTextColor(color); |
||
351 | |||
352 | if (ct != null) { |
||
353 | /*
|
||
354 | * Reproject from store CRS to view CRS
|
||
355 | */
|
||
356 | geom = geom.cloneGeometry(); |
||
357 | geom.reProject(ct); |
||
358 | } |
||
359 | |||
360 | double rotation = 0D; |
||
361 | Point p = null; |
||
362 | if (rotationFieldName != null) { |
||
363 | // text rotation is defined in the table
|
||
364 | rotation = -Math.toRadians(((Number) feature.get(fields.get(rotationPos))).doubleValue()); |
||
365 | p = createLabelPoint(geom); |
||
366 | } else {
|
||
367 | GeometryType gty = geom.getGeometryType(); |
||
368 | if (gty.isTypeOf(Geometry.TYPES.CURVE)
|
||
369 | || gty.isTypeOf(Geometry.TYPES.MULTICURVE)) { |
||
370 | |||
371 | Geometry aux_geom = geom.cloneGeometry(); |
||
372 | aux_geom.transform(viewPort.getAffineTransform()); |
||
373 | |||
374 | PathLength pathLen = new PathLength(aux_geom);
|
||
375 | float length = pathLen.lengthOfPath();
|
||
376 | float distance = (float) (length * 0.4); |
||
377 | rotation = pathLen.angleAtLength(distance); |
||
378 | if (rotation < (-Math.PI / 2)) { |
||
379 | rotation = rotation + Math.PI;
|
||
380 | } else {
|
||
381 | if (rotation > (Math.PI / 2)) { |
||
382 | rotation = rotation - Math.PI;
|
||
383 | } |
||
384 | } |
||
385 | double norm = -rotation + Math.PI / 2; |
||
386 | |||
387 | Point2D p2 = pathLen.pointAtLength(distance);
|
||
388 | Point2D p2_anchor = new Point2D.Double(); |
||
389 | |||
390 | try {
|
||
391 | viewPort.getAffineTransform().inverseTransform( |
||
392 | p2, p2_anchor); |
||
393 | } catch (NoninvertibleTransformException e) { |
||
394 | logger.warn("While computing anchor point", e);
|
||
395 | } |
||
396 | double font_offset = 0.8 * font_size |
||
397 | / viewPort.getAffineTransform().getScaleX(); |
||
398 | p2_anchor.setLocation( |
||
399 | /*
|
||
400 | * Move away from line
|
||
401 | */
|
||
402 | p2_anchor.getX() + font_offset * Math.cos(norm),
|
||
403 | p2_anchor.getY() + font_offset * Math.sin(norm));
|
||
404 | |||
405 | p = GeometryLocator.getGeometryManager().createPoint( |
||
406 | p2_anchor.getX(), |
||
407 | p2_anchor.getY(), Geometry.SUBTYPES.GEOM2D); |
||
408 | |||
409 | } else {
|
||
410 | p = createLabelPoint(geom); |
||
411 | } |
||
412 | |||
413 | } |
||
414 | |||
415 | sym.setText(feature.getString(fields.get(textPos))); |
||
416 | sym.setRotation(rotation); |
||
417 | |||
418 | if (p != null) { |
||
419 | if (props == null) { |
||
420 | sym.draw(g, viewPort.getAffineTransform(), p, feature, cancel); |
||
421 | } else {
|
||
422 | sym.print(g, viewPort.getAffineTransform(), p, props); |
||
423 | } |
||
424 | } |
||
425 | } catch(Exception ex) { |
||
426 | logger.warn("",ex);
|
||
427 | 41133 | jldominguez | } |
428 | 41394 | jjdelcerro | } |
429 | 41133 | jldominguez | |
430 | 41394 | jjdelcerro | } catch (BaseException e) {
|
431 | throw new ReadException( |
||
432 | "Could not draw annotation in the layer.", e);
|
||
433 | } finally {
|
||
434 | if (iterator != null) { |
||
435 | iterator.dispose(); |
||
436 | } |
||
437 | if (set != null) { |
||
438 | set.dispose(); |
||
439 | } |
||
440 | |||
441 | } |
||
442 | |||
443 | } |
||
444 | } |
||
445 | |||
446 | static class FilteredLogger { |
||
447 | private int count = 0; |
||
448 | private final Logger logger; |
||
449 | private final int max; |
||
450 | |||
451 | public FilteredLogger(Logger logger, int max) { |
||
452 | this.max = max;
|
||
453 | this.logger = logger;
|
||
454 | } |
||
455 | public void warn(String msg, Throwable th) { |
||
456 | if( ++this.count < this.max ) { |
||
457 | this.logger.warn(msg,th);
|
||
458 | } else if( this.count == this.max ) { |
||
459 | this.logger.warn(msg,th);
|
||
460 | this.logger.warn("Too many errors, don't dump more in this process."); |
||
461 | } |
||
462 | } |
||
463 | public void warn(String msg) { |
||
464 | if( ++this.count < this.max ) { |
||
465 | this.logger.warn(msg);
|
||
466 | } else if( this.count == this.max ) { |
||
467 | this.logger.warn(msg);
|
||
468 | this.logger.warn("Too many errors, don't dump more in this process."); |
||
469 | } |
||
470 | } |
||
471 | public void info(String msg) { |
||
472 | if( ++this.count < this.max ) { |
||
473 | this.logger.info(msg);
|
||
474 | } else if( this.count == this.max ) { |
||
475 | this.logger.info(msg);
|
||
476 | this.logger.info("Too many errors, don't dump more in this process."); |
||
477 | } |
||
478 | } |
||
479 | } |
||
480 | |||
481 | 41676 | jjdelcerro | public void draw(BufferedImage image, Graphics2D g, double scale, ViewPort viewPort, |
482 | 41394 | jjdelcerro | Cancellable cancel, double dpi) throws ReadException { |
483 | 41676 | jjdelcerro | draw(image, g, scale, viewPort, cancel, null);
|
484 | 41394 | jjdelcerro | } |
485 | |||
486 | private org.gvsig.fmap.geom.primitive.Point createLabelPoint(org.gvsig.fmap.geom.Geometry geom)
|
||
487 | throws CreateGeometryException {
|
||
488 | |||
489 | try {
|
||
490 | return geom.centroid();
|
||
491 | } catch (GeometryOperationNotSupportedException e) {
|
||
492 | return null; |
||
493 | } catch (GeometryOperationException e) {
|
||
494 | return null; |
||
495 | } |
||
496 | } |
||
497 | |||
498 | // public String getClassName() {
|
||
499 | // return getClass().getName();
|
||
500 | // }
|
||
501 | public String getRotationField() { |
||
502 | return this.rotationFieldName; |
||
503 | } |
||
504 | |||
505 | public String getTextField() { |
||
506 | return this.textFieldName; |
||
507 | } |
||
508 | |||
509 | public String getHeightField() { |
||
510 | return this.heightFieldName; |
||
511 | } |
||
512 | |||
513 | public String getColorField() { |
||
514 | return this.colorFieldName; |
||
515 | } |
||
516 | |||
517 | public void setTextField(String textFieldName) { |
||
518 | this.textFieldName = textFieldName;
|
||
519 | this.usedFields = null; |
||
520 | } |
||
521 | |||
522 | public void setRotationField(String rotationFieldName) { |
||
523 | this.rotationFieldName = rotationFieldName;
|
||
524 | this.usedFields = null; |
||
525 | } |
||
526 | |||
527 | /**
|
||
528 | * Sets the field that contains the size of the text. The size is computed
|
||
529 | * in meters. To use any other unit, call setUnit(int) with the scale factor
|
||
530 | * from meters (for centimeters, call <b>setUnitFactor(0.01))</b>.
|
||
531 | *
|
||
532 | * @param heightFieldName
|
||
533 | */
|
||
534 | public void setHeightField(String heightFieldName) { |
||
535 | this.heightFieldName = heightFieldName;
|
||
536 | this.usedFields = null; |
||
537 | } |
||
538 | |||
539 | public void setColorField(String colorFieldName) { |
||
540 | this.colorFieldName = colorFieldName;
|
||
541 | } |
||
542 | |||
543 | 41676 | jjdelcerro | public void print(Graphics2D g, double scale, ViewPort viewPort, Cancellable cancel, |
544 | 41394 | jjdelcerro | PrintAttributes props) throws ReadException {
|
545 | 41676 | jjdelcerro | draw(null, g, scale, viewPort, cancel, props);
|
546 | 41394 | jjdelcerro | } |
547 | |||
548 | public void setUsesFixedSize(boolean b) { |
||
549 | useFixedSize = b; |
||
550 | this.usedFields = null; |
||
551 | } |
||
552 | |||
553 | public boolean usesFixedSize() { |
||
554 | return useFixedSize;
|
||
555 | } |
||
556 | |||
557 | public double getFixedSize() { |
||
558 | return fixedSize;
|
||
559 | } |
||
560 | |||
561 | public void setFixedSize(double fixedSize) { |
||
562 | this.fixedSize = fixedSize;
|
||
563 | this.usedFields = null; |
||
564 | } |
||
565 | |||
566 | public void setUsesFixedColor(boolean b) { |
||
567 | useFixedColor = b; |
||
568 | this.usedFields = null; |
||
569 | } |
||
570 | |||
571 | public boolean usesFixedColor() { |
||
572 | return useFixedColor;
|
||
573 | } |
||
574 | |||
575 | public Color getFixedColor() { |
||
576 | return fixedColor;
|
||
577 | } |
||
578 | |||
579 | public void setFixedColor(Color fixedColor) { |
||
580 | this.fixedColor = fixedColor;
|
||
581 | } |
||
582 | |||
583 | public void setUnit(int unitIndex) { |
||
584 | unit = unitIndex; |
||
585 | |||
586 | } |
||
587 | |||
588 | public int getUnit() { |
||
589 | return unit;
|
||
590 | } |
||
591 | |||
592 | public String[] getUsedFields() { |
||
593 | if (this.usedFields == null) { |
||
594 | List<String> v = new ArrayList<String>(4); |
||
595 | if (!this.usesFixedSize()) { |
||
596 | if (getHeightField() != null) { |
||
597 | v.add(getHeightField()); |
||
598 | } |
||
599 | } |
||
600 | if (getRotationField() != null) { |
||
601 | v.add(getRotationField()); |
||
602 | } |
||
603 | if (getTextField() != null) { |
||
604 | v.add(getTextField()); |
||
605 | } |
||
606 | |||
607 | if (!this.usesFixedColor() && getColorField() != null) { |
||
608 | v.add(getColorField()); |
||
609 | } |
||
610 | this.usedFields = (String[]) v.toArray(new String[v.size()]); |
||
611 | } |
||
612 | return this.usedFields; |
||
613 | } |
||
614 | |||
615 | public int getReferenceSystem() { |
||
616 | return referenceSystem;
|
||
617 | } |
||
618 | |||
619 | public void setReferenceSystem(int referenceSystem) { |
||
620 | this.referenceSystem = referenceSystem;
|
||
621 | } |
||
622 | |||
623 | public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
624 | // not required here
|
||
625 | throw new Error("Undefined in this context"); |
||
626 | } |
||
627 | |||
628 | public void setCartographicSize(double cartographicSize, Geometry geom) { |
||
629 | // not required here
|
||
630 | throw new Error("Undefined in this context"); |
||
631 | } |
||
632 | |||
633 | public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
634 | // not required here
|
||
635 | throw new Error("Undefined in this context"); |
||
636 | |||
637 | } |
||
638 | |||
639 | public void setLayer(FLayer layer) { |
||
640 | this.layer = (FLyrVect) layer;
|
||
641 | if (layer == null) { |
||
642 | return;
|
||
643 | } |
||
644 | FeatureType type; |
||
645 | try {
|
||
646 | type = this.layer.getFeatureStore().getDefaultFeatureType();
|
||
647 | } catch (DataException e) {
|
||
648 | throw new RuntimeException(this.getClass().getName(), e); |
||
649 | } |
||
650 | |||
651 | if (textFieldName != null) { |
||
652 | if (type.getIndex(textFieldName) < 0) { |
||
653 | // FIXME exception ??
|
||
654 | } |
||
655 | } |
||
656 | if (rotationFieldName != null) { |
||
657 | if (type.getIndex(rotationFieldName) < 0) { |
||
658 | // FIXME exception ??
|
||
659 | } |
||
660 | } |
||
661 | if (heightFieldName != null) { |
||
662 | if (type.getIndex(heightFieldName) < 0) { |
||
663 | // FIXME exception ??
|
||
664 | } |
||
665 | } |
||
666 | if (colorFieldName != null) { |
||
667 | if (type.getIndex(colorFieldName) < 0) { |
||
668 | // FIXME exception ??
|
||
669 | } |
||
670 | } |
||
671 | } |
||
672 | |||
673 | public boolean shouldDrawLabels(double scale) { |
||
674 | return layer.isWithinScale(scale);
|
||
675 | } |
||
676 | |||
677 | public Color getColorFont() { |
||
678 | return colorFont;
|
||
679 | } |
||
680 | |||
681 | public void setColorFont(Color colorFont) { |
||
682 | this.colorFont = colorFont;
|
||
683 | } |
||
684 | |||
685 | public Font getFont() { |
||
686 | return font;
|
||
687 | } |
||
688 | |||
689 | public void setFont(Font selFont) { |
||
690 | this.font = selFont;
|
||
691 | } |
||
692 | |||
693 | /* (non-Javadoc)
|
||
694 | * @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
|
||
695 | */
|
||
696 | public void loadFromState(PersistentState state) |
||
697 | throws PersistenceException {
|
||
698 | |||
699 | setTextField(state.getString(FIELD_TEXT)); |
||
700 | setHeightField(state.getString(FIELD_HEIGHT)); |
||
701 | setColorField(state.getString(FIELD_COLOR)); |
||
702 | setRotationField(state.getString(FIELD_ROTATION)); |
||
703 | setUnit(state.getInt(FIELD_UNIT)); |
||
704 | setFont((Font) state.get(FIELD_FONT));
|
||
705 | useFixedSize = state.getBoolean(FIELD_USE_FIXED_SIZE); |
||
706 | setFixedSize(state.getDouble(FIELD_FIXED_SIZE)); |
||
707 | useFixedColor = state.getBoolean(FIELD_USE_FIXED_COLOR); |
||
708 | setFixedColor((Color) state.get(FIELD_FIXED_COLOR));
|
||
709 | setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM)); |
||
710 | setColorFont((Color) state.get(FIELD_COLOR_FONT));
|
||
711 | setZoomConstraints((IZoomConstraints) state.get(FIELD_ZOOM_CONSTRAINTS)); |
||
712 | setLabelingMethod((ILabelingMethod) state.get(FIELD_LABELING_METHOD)); |
||
713 | } |
||
714 | |||
715 | /* (non-Javadoc)
|
||
716 | * @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
|
||
717 | */
|
||
718 | public void saveToState(final PersistentState state) throws PersistenceException { |
||
719 | state.set(FIELD_TEXT, getTextField()); |
||
720 | state.set(FIELD_HEIGHT, getHeightField()); |
||
721 | state.set(FIELD_COLOR, getColorField()); |
||
722 | state.set(FIELD_ROTATION, getRotationField()); |
||
723 | state.set(FIELD_UNIT, getUnit()); |
||
724 | state.set(FIELD_FONT, getFont()); |
||
725 | state.set(FIELD_USE_FIXED_SIZE, useFixedSize); |
||
726 | state.set(FIELD_FIXED_SIZE, getFixedSize()); |
||
727 | state.set(FIELD_USE_FIXED_COLOR, useFixedColor); |
||
728 | state.set(FIELD_FIXED_COLOR, getFixedColor()); |
||
729 | state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem()); |
||
730 | state.set(FIELD_COLOR_FONT, getColorFont()); |
||
731 | state.set(FIELD_ZOOM_CONSTRAINTS, getZoomConstraints()); |
||
732 | state.set(FIELD_LABELING_METHOD, getLabelingMethod()); |
||
733 | } |
||
734 | |||
735 | public static class RegisterPersistence implements Callable { |
||
736 | |||
737 | public Object call() throws Exception { |
||
738 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
739 | if (manager.getDefinition(ATTR_IN_TABLE_LABELING_STRATEGY_PERSISTENCE_DEFINITION_NAME) == null) { |
||
740 | DynStruct definition = manager.addDefinition( |
||
741 | AttrInTableLabelingStrategy.class, |
||
742 | ATTR_IN_TABLE_LABELING_STRATEGY_PERSISTENCE_DEFINITION_NAME, |
||
743 | ATTR_IN_TABLE_LABELING_STRATEGY_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
|
||
744 | null,
|
||
745 | null
|
||
746 | ); |
||
747 | definition.addDynFieldString(FIELD_TEXT).setMandatory(true);
|
||
748 | definition.addDynFieldString(FIELD_HEIGHT).setMandatory(false);
|
||
749 | definition.addDynFieldString(FIELD_COLOR).setMandatory(false);
|
||
750 | definition.addDynFieldString(FIELD_ROTATION).setMandatory(false);
|
||
751 | definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
|
||
752 | definition.addDynFieldObject(FIELD_FONT).setMandatory(false).setClassOfValue(Font.class); |
||
753 | definition.addDynFieldBoolean(FIELD_USE_FIXED_SIZE).setMandatory(true);
|
||
754 | definition.addDynFieldDouble(FIELD_FIXED_SIZE).setMandatory(false);
|
||
755 | definition.addDynFieldBoolean(FIELD_USE_FIXED_COLOR).setMandatory(true);
|
||
756 | definition.addDynFieldObject(FIELD_FIXED_COLOR).setMandatory(false).setClassOfValue(Color.class); |
||
757 | definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
|
||
758 | definition.addDynFieldObject(FIELD_COLOR_FONT).setMandatory(false).setClassOfValue(Color.class); |
||
759 | definition.addDynFieldObject(FIELD_ZOOM_CONSTRAINTS).setMandatory(false).setClassOfValue(IZoomConstraints.class);
|
||
760 | definition.addDynFieldObject(FIELD_LABELING_METHOD).setMandatory(true).setClassOfValue(ILabelingMethod.class);
|
||
761 | } |
||
762 | return Boolean.TRUE; |
||
763 | } |
||
764 | |||
765 | } |
||
766 | } |