Statistics
| Revision:

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 @ 47464

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