Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / legend / styling / AttrInTableLabelingStrategy.java @ 33205

History | View | Annotate | Download (22.4 KB)

1
package org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling;
2

    
3
import java.awt.Color;
4
import java.awt.Font;
5
import java.awt.Graphics2D;
6
import java.awt.geom.Point2D;
7
import java.awt.geom.Rectangle2D;
8
import java.awt.image.BufferedImage;
9
import java.util.ArrayList;
10
import java.util.List;
11

    
12
import org.apache.batik.ext.awt.geom.PathLength;
13
import org.gvsig.compat.print.PrintAttributes;
14
import org.gvsig.fmap.dal.exception.DataException;
15
import org.gvsig.fmap.dal.exception.ReadException;
16
import org.gvsig.tools.dispose.DisposableIterator;
17
import org.gvsig.fmap.dal.feature.Feature;
18
import org.gvsig.fmap.dal.feature.FeatureQuery;
19
import org.gvsig.fmap.dal.feature.FeatureSet;
20
import org.gvsig.fmap.dal.feature.FeatureStore;
21
import org.gvsig.fmap.dal.feature.FeatureType;
22
import org.gvsig.fmap.geom.Geometry;
23
import org.gvsig.fmap.geom.GeometryLocator;
24
import org.gvsig.fmap.geom.GeometryManager;
25
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
26
import org.gvsig.fmap.geom.aggregate.MultiPoint;
27
import org.gvsig.fmap.geom.exception.CreateGeometryException;
28
import org.gvsig.fmap.geom.primitive.Point;
29
import org.gvsig.fmap.geom.util.Converter;
30
import org.gvsig.fmap.mapcontext.MapContext;
31
import org.gvsig.fmap.mapcontext.ViewPort;
32
import org.gvsig.fmap.mapcontext.layers.FLayer;
33
import org.gvsig.fmap.mapcontext.layers.vectorial.ContainsEnvelopeEvaluator;
34
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
35
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
36
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
37
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
38
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
40
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit;
41
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.impl.SimpleTextSymbol;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dynobject.DynStruct;
44
import org.gvsig.tools.exception.BaseException;
45
import org.gvsig.tools.persistence.PersistenceManager;
46
import org.gvsig.tools.persistence.PersistentState;
47
import org.gvsig.tools.persistence.exception.PersistenceException;
48
import org.gvsig.tools.task.Cancellable;
49
import org.slf4j.LoggerFactory;
50

    
51

    
52

    
53
/**
54
 * LabelingStrategy used when the user wants to use label sizes, rotations, etc. from
55
 * the values included in fields of the datasource's table
56
 *
57
 * @author jaume dominguez faus - jaume.dominguez@iver.es
58
 *
59
 */
60
public class AttrInTableLabelingStrategy implements ILabelingStrategy, CartographicSupport {
61
        private static final String FIELD_LABELING_METHOD = "labelingMethod";
62
        private static final String FIELD_ZOOM_CONSTRAINTS = "zoomConstraints";
63
        private static final String FIELD_COLOR_FONT = "colorFont";
64
        private static final String FIELD_LAYER = "layer";
65
        private static final String FIELD_REFERENCE_SYSTEM = "referenceSystem";
66
        private static final String FIELD_FIXED_COLOR = "fixedColor";
67
        private static final String FIELD_USE_FIXED_COLOR = "useFixedColor";
68
        private static final String FIELD_FIXED_SIZE = "fixedSize";
69
        private static final String FIELD_USE_FIXED_SIZE = "useFixedSize";
70
        private static final String FIELD_FONT = "font";
71
        private static final String FIELD_UNIT = "Unit";
72
        private static final String FIELD_ROTATION = "RotationField";
73
        private static final String FIELD_COLOR = "ColorField";
74
        private static final String FIELD_HEIGHT = "HeightField";
75
        private static final String FIELD_TEXT = "TextField";
76
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
77
        private static final org.slf4j.Logger logger = LoggerFactory.getLogger(AttrInTableLabelingStrategy.class);
78
        public static final double MIN_TEXT_SIZE = 3;
79
        private static final String ATTR_IN_TABLE_LABELING_STRATEGY_DYNCLASS_NAME =
80
                        "AttrInTableLabelingStrategy";
81
        private ILabelingMethod method = new DefaultLabelingMethod();
82
        private IZoomConstraints zoom;
83
        private FLyrVect layer;
84
//        private double unitFactor = 1D;
85
        private double fixedSize=10;
86
        private Color fixedColor;
87
        private int unit = -1; //(pixel)
88
        private boolean useFixedSize;
89
        private boolean useFixedColor;
90
        private int referenceSystem;
91
//        private boolean isPrinting;
92
        private String[] usedFields = null;
93
        private Font font;
94
        private Color colorFont;
95
        private String textFieldName;
96
        private String rotationFieldName;
97
        private String heightFieldName;
98
        private String colorFieldName;
99

    
100
        public ILabelingMethod getLabelingMethod() {
101
                return this.method;
102
        }
103

    
104
        public void setLabelingMethod(ILabelingMethod method) {
105
                this.method = method;
106
        }
107

    
108
        public IPlacementConstraints getPlacementConstraints() {
109
                return null; // (automatically handled by the driver)
110
        }
111

    
112
        public void setPlacementConstraints(IPlacementConstraints constraints) {
113
                // nothing
114
        }
115

    
116
        public IZoomConstraints getZoomConstraints() {
117
                return zoom;
118
        }
119

    
120
        public void setZoomConstraints(IZoomConstraints constraints) {
121
                this.zoom = constraints;
122
        }
123

    
124
        private void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
125
                        Cancellable cancel, PrintAttributes props) throws ReadException {
126
                double scale = viewPort.getScale();
127
//                double fontScaleFactor = FConstant.FONT_HEIGHT_SCALE_FACTOR;
128

    
129
                SimpleTextSymbol sym = new SimpleTextSymbol();
130

    
131
                sym.setFont(getFont());
132

    
133
                sym.setUnit(unit);
134
                sym.setReferenceSystem(referenceSystem);
135
                if (zoom==null ||
136
                        ( zoom.isUserDefined() && (scale >= zoom.getMaxScale())
137
                        && (scale <= zoom.getMinScale()) ) ) {
138
                        FeatureSet set = null;
139
                        DisposableIterator iterator = null;
140
                        try {
141
                                // limit the labeling to the visible extent
142
                                List<String> fields = new ArrayList<String>();
143
                                int heightPos =-1;
144
                                int rotationPos =-1;
145
                                int textPos = -1;
146
                                int colorPos = -1;
147
//                                int geomPos = -1;
148

    
149
                                if (!this.usesFixedSize()) {
150
                                        if (getHeightField() != null) {
151
                                                heightPos = fields.size();
152
                                                fields.add(getHeightField());
153
                                        }
154
                                }
155
                                if (getRotationField() != null) {
156
                                        rotationPos = fields.size();
157
                                        fields.add(getRotationField());
158
                                }
159
                                if (getTextField() != null) {
160
                                        textPos = fields.size();
161
                                        fields.add(getTextField());
162
                                }
163

    
164
                                if (!this.usesFixedColor() && getColorField() != null) {
165
                                        colorPos = fields.size();
166
                                        fields.add(getColorField());
167
                                }
168

    
169
                                FeatureStore featureStore=layer.getFeatureStore();
170

    
171
//                                geomPos = fields.size();
172
                                String geomName=featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName();
173
                                fields.add(geomName);
174

    
175
                                FeatureQuery featureQuery=featureStore.createFeatureQuery();
176

    
177
                                featureQuery.setAttributeNames((String[]) fields.toArray(new String[fields.size()]));
178
                                // TODO no set filter y layer is contained totaly in viewPort
179
                                ContainsEnvelopeEvaluator iee=new ContainsEnvelopeEvaluator(viewPort.getAdjustedEnvelope(),viewPort.getProjection(),featureStore.getDefaultFeatureType(),geomName);
180
                                featureQuery.setFilter(iee);
181

    
182

    
183

    
184
                                set = featureStore
185
                                                .getFeatureSet(featureQuery);
186

    
187
//                                ReadableVectorial source = layer.getSource();
188
//                                SelectableDataSource recordSet = source.getRecordset();
189
                                iterator = set.fastIterator();
190
//                                CreateLabelsOperationContext cloc=new CreateLabelsOperationContext();
191
//                                cloc.setDublicates(true);
192
//                                cloc.setPosition(0);
193
                                while(iterator.hasNext()){
194
                                        if (cancel.isCanceled()){
195
                                                return;
196
                                        }
197
                                        Feature feature=(Feature)iterator.next();
198
//                                for(int i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
199
//                                        Value[] vv = recordSet.getRow(i);
200
                                        double size;
201
                                        Color color = null;
202
                                        if (useFixedSize){
203
                                                // uses fixed size
204
                                                size = fixedSize;// * fontScaleFactor;
205
//                                        } else if (idHeightField != -1) {
206
                                        } else if (heightFieldName != null) {
207
                                                // text size is defined in the table
208
                                                try {
209
////                                                        Object obj=feature.get(idHeightField);
210
//                                                        Object obj=feature.get(heightPos);
211
//                                                        if (obj!=null) {
212
//                                                                size = ((Number) obj).doubleValue();// * fontScaleFactor;
213
//                                                        } else {
214
//                                                                size=0;
215
//                                                        }
216
                                                        size = feature.getDouble(heightPos);
217
                                                } catch (ClassCastException ccEx) {
218
//                                                        if (!NullValue.class.equals(feature.get(idHeightField).getClass())) {
219
//                                                        if (!NullValue.class.equals(feature.get(heightPos).getClass())) {
220
//
221
//                                                                throw new ReadException("Unknown", ccEx);
222
//                                                        }
223
                                                        // a null value
224
//                                                        Logger.getAnonymousLogger().
225
//                                                                warning("Null text height value for text '"+feature.get(idTextField).toString()+"'");
226
                                                        
227
                                                        logger.
228
                                                                warn("Null text height value for text '{}'", feature.get(textPos).toString());
229

    
230
                                                        continue;
231
                                                }
232
                                        } else {
233
                                                // otherwise will use the size in the symbol
234
                                                size = sym.getFont().getSize();
235
                                        }
236

    
237
                                        size = CartographicSupportToolkit.
238
                                                                getCartographicLength(this,
239
                                                                                                          size,
240
                                                                                                          viewPort,
241
                                                                                                          MapContext.getScreenDPI());
242
//                                                                                                          dpi);
243
//                                                                toScreenUnitYAxis(this,
244
//                                                                                                  size,
245
//                                                                                                  viewPort
246
//                                                                                                 );
247

    
248
                                        if (size <= MIN_TEXT_SIZE) {
249
                                                // label is too small to be readable, will be skipped
250
                                                // this speeds up the rendering in wider zooms
251
                                                continue;
252
                                        }
253

    
254
                                        sym.setFontSize(size);
255

    
256
                                        if (useFixedColor){
257
                                                color = fixedColor;
258
//                                        } else if (idColorField != -1) {
259
                                        } else if (colorFieldName != null) {
260
                                                // text size is defined in the table
261
                                                try {
262
//                                                        color = new Color(feature.getInt(idColorField));
263
                                                        color = new Color(feature.getInt(colorPos));
264
                                                } catch (ClassCastException ccEx) {
265
//                                                        if (feature.get(idColorField) != null) {
266
                                                        if (feature.get(colorPos) != null) {
267
                                                                throw new ReadException("Unknown", ccEx);
268
                                                        }
269
                                                        // a null value
270
//                                                        Logger.getAnonymousLogger().
271
//                                                                warning(
272
//                                                                        "Null color value for text '"
273
//                                                                                        + feature.getString(idTextField)
274
//                                                                                        + "'");
275
                                                        logger.warn(
276
                                                                        "Null color value for text '{}'", 
277
                                                                        feature.getString(textFieldName));
278

    
279
                                                        continue;
280
                                                }
281
                                        } else {
282
                                                color = sym.getTextColor();
283
                                        }
284

    
285
                                        sym.setTextColor(color);
286

    
287
                                        double rotation = 0D;
288
//                                        if (idRotationField!= -1) {
289
                                        if (rotationFieldName != null) {
290
                                                // text rotation is defined in the table
291
//                                                rotation = -Math.toRadians(((Number) feature.get(idRotationField)).doubleValue());
292
                                                rotation = -Math.toRadians(((Number) feature.get(rotationPos)).doubleValue());
293
                                        }
294

    
295
                                        Geometry geom = feature.getDefaultGeometry();
296
//                                        Object obj=feature.get(idTextField);
297
//                                        Object obj = feature.get(textPos);
298
//                                        if (obj!=null) {
299
//                                                sym.setText(obj.toString());
300
//                                        }
301
                                        sym.setText(feature.getString(textPos));
302
                                        sym.setRotation(rotation);
303
                        
304
                                        Point2D labelPoint = createLabelPoint(geom);
305
                    Point p = geomManager.createPoint(labelPoint.getX(), labelPoint.getY(), SUBTYPES.GEOM2D);
306
                    p.transform(viewPort.getAffineTransform());
307
                                        if (props == null) {
308
                        sym.draw(g, null, p, feature, cancel);
309
                    } else {
310
                                                sym.print(g, null, p, props);
311
                    }
312

    
313
//                                        FLabel[] aux =(FLabel[])geom.invokeOperation(CreateLabels.CODE,cloc);
314
////                                        FLabel[] aux = geom.createLabels(0, true);
315
//                                        for (int j = 0; j < aux.length; j++) {
316
//                                                Point p = geomManager.createPoint(aux[j].getOrig().getX(), aux[j].getOrig().getY(), SUBTYPES.GEOM2D);
317
//                                                p.transform(viewPort.getAffineTransform());
318
//                                                if (properties==null) {
319
//                                                        sym.draw(g, null, p, cancel);
320
//                                                } else {
321
//                                                        sym.print(g, null, p, properties);
322
//                                                }
323
//                                        }
324
                                }
325

    
326
//                        } catch (GeometryOperationNotSupportedException e) {
327
//                                throw new ReadException(
328
//                                                "Could not draw annotation in the layer.", e);
329
//                        } catch (GeometryOperationException e) {
330
//                                throw new ReadException(
331
//                                                "Could not draw annotation in the layer.", e);
332
                        } catch (BaseException e) {
333
                                throw new ReadException(
334
                                                "Could not draw annotation in the layer.", e);
335
                        } finally {
336
                                if (iterator != null) {
337
                                        iterator.dispose();
338
                                }
339
                                if (set != null) {
340
                                        set.dispose();
341
                                }
342

    
343
                        }
344

    
345
                }
346
        }
347
        
348
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
349
                        Cancellable cancel, double dpi) throws ReadException {
350
                draw(image, g, viewPort, cancel, null);
351
        }
352

    
353
        private Point2D createLabelPoint(org.gvsig.fmap.geom.Geometry geom)
354
                        throws CreateGeometryException {
355
                Point2D pAux = null;
356
                switch (geom.getType()) {
357
                case org.gvsig.fmap.geom.Geometry.TYPES.POINT:
358
                        pAux = new Point2D.Double(
359
                                        ((org.gvsig.fmap.geom.primitive.Point) geom).getX(),
360
                                        ((org.gvsig.fmap.geom.primitive.Point) geom).getY());
361
                        return pAux;
362

    
363
                case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
364
                        PathLength pathLen = new PathLength(geom);
365

    
366
                        // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
367
                        float midDistance = pathLen.lengthOfPath() / 2;
368
                        pAux = pathLen.pointAtLength(midDistance);
369
                        return pAux;
370

    
371
                case org.gvsig.fmap.geom.Geometry.TYPES.SURFACE:
372
                        com.vividsolutions.jts.geom.Geometry geo = Converter
373
                                        .geometryToJts(geom);
374

    
375
                        if (geo == null) {
376
                                return null;
377
                        }
378

    
379
                        com.vividsolutions.jts.geom.Point pJTS = geo.getCentroid();
380

    
381
                        if (pJTS != null) {
382
                                org.gvsig.fmap.geom.Geometry pLabel = Converter
383
                                                .jtsToGeometry(pJTS);
384
                                return new Point2D.Double(
385
                                                ((org.gvsig.fmap.geom.primitive.Point) pLabel).getX(),
386
                                                ((org.gvsig.fmap.geom.primitive.Point) pLabel).getY());
387
                        }
388
                        break;
389
                case org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT:
390
                        int num = ((MultiPoint) geom).getPrimitivesNumber();
391
                        Rectangle2D r = null;
392
                        if (num > 0) {
393
                                r = ((MultiPoint) geom).getPointAt(0).getBounds2D();
394
                                for (int i = 1; i < num; i++) {
395
                                        org.gvsig.fmap.geom.primitive.Point fp = ((MultiPoint) geom)
396
                                                        .getPointAt(i);
397
                                        r.add(new Point2D.Double(fp.getX(), fp.getY()));
398
                                }
399
                        }
400
                        if (r != null)
401
                                return new Point2D.Double(r.getCenterX(), r.getCenterY());
402
                        break;
403
                } // switch
404
                return null;
405
        }
406

    
407
        public String getClassName() {
408
                return getClass().getName();
409
        }
410

    
411
        public String getRotationField() throws DataException {
412
                return this.rotationFieldName;
413
        }
414

    
415
        public String getTextField() throws DataException {
416
                return this.textFieldName;
417
        }
418

    
419
        public String getHeightField() throws DataException {
420
                return this.heightFieldName;
421
        }
422

    
423
        public String getColorField() throws DataException {
424
                return this.colorFieldName;
425
        }
426

    
427
        public void setTextField(String textFieldName) {
428
                this.textFieldName=textFieldName;
429
                this.usedFields = null;
430
        }
431

    
432
        public void setRotationField(String rotationFieldName) {
433
                this.rotationFieldName = rotationFieldName;
434
                this.usedFields = null;
435
        }
436

    
437
        /**
438
         * Sets the field that contains the size of the text. The size is computed
439
         * in meters. To use any other unit, call setUnit(int) with the scale factor from meters
440
         * (for centimeters, call <b>setUnitFactor(0.01))</b>.
441
         * @param heightFieldName
442
         */
443
        public void setHeightField(String heightFieldName) {
444
                this.heightFieldName = heightFieldName;
445
                this.usedFields = null;
446
        }
447

    
448

    
449
        public void setColorField(String colorFieldName) {
450
                this.colorFieldName = colorFieldName;
451
        }
452

    
453
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
454
                        PrintAttributes props) throws ReadException {
455
                draw(null, g, viewPort, cancel, props);
456
        }
457

    
458
        public void setUsesFixedSize(boolean b) {
459
                useFixedSize = b;
460
                this.usedFields = null;
461
        }
462

    
463
        public boolean usesFixedSize() {
464
                return useFixedSize;
465
        }
466

    
467
        public double getFixedSize() {
468
                return fixedSize;
469
        }
470

    
471
        public void setFixedSize(double fixedSize) {
472
                this.fixedSize = fixedSize;
473
                this.usedFields = null;
474
        }
475

    
476
        public void setUsesFixedColor(boolean b) {
477
                useFixedColor = b;
478
                this.usedFields = null;
479
        }
480

    
481
        public boolean usesFixedColor() {
482
                return useFixedColor;
483
        }
484

    
485
        public Color getFixedColor() {
486
                return fixedColor;
487
        }
488

    
489
        public void setFixedColor(Color fixedColor) {
490
                this.fixedColor = fixedColor;
491
        }
492

    
493

    
494
        public void setUnit(int unitIndex) {
495
                unit = unitIndex;
496

    
497
        }
498

    
499
        public int getUnit() {
500
                return unit;
501
        }
502

    
503
        public String[] getUsedFields() {
504
                if (this.usedFields == null) {
505
                        List<String> v = new ArrayList<String>(4);
506
                        try {
507
                                if (!this.usesFixedSize()) {
508
                                        if (getHeightField() != null) {
509
                                                v.add(getHeightField());
510
                                        }
511
                                }
512
                                if (getRotationField() != null) {
513
                                        v.add(getRotationField());
514
                                }
515
                                if (getTextField() != null) {
516
                                        v.add(getTextField());
517
                                }
518

    
519
                                if (!this.usesFixedColor() && getColorField() != null) {
520
                                        v.add(getColorField());
521
                                }
522
                        } catch (DataException e) {
523
                                logger.error("Error", e);
524
                        }
525
                        this.usedFields = (String[]) v.toArray(new String[v.size()]);
526
                }
527
                return this.usedFields;
528
        }
529

    
530
        public int getReferenceSystem() {
531
                return referenceSystem;
532
        }
533

    
534
        public void setReferenceSystem(int referenceSystem) {
535
                this.referenceSystem = referenceSystem;
536
        }
537

    
538
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
539
                // not required here
540
                throw new Error("Undefined in this context");
541
        }
542

    
543
        public void setCartographicSize(double cartographicSize, Geometry geom) {
544
                // not required here
545
                throw new Error("Undefined in this context");
546
        }
547

    
548
        public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
549
                // not required here
550
                throw new Error("Undefined in this context");
551

    
552
        }
553

    
554
        public void setLayer(FLayer layer) throws ReadException {
555
                this.layer = (FLyrVect) layer;
556
                if (layer == null) {
557
                        return;
558
                }
559
                FeatureType type;
560
                try {
561
                        type = this.layer.getFeatureStore().getDefaultFeatureType();
562
                } catch (DataException e) {
563
                        // FIXME Throw DataException
564
                        throw new ReadException(this.getClass().getName(), e);
565
                }
566

    
567
                if (textFieldName != null) {
568
                        if (type.getIndex(textFieldName) < 0) {
569
                                // FIXME exception ??
570
                        }
571
                }
572
                if (rotationFieldName != null) {
573
                        if (type.getIndex(rotationFieldName) < 0) {
574
                                // FIXME exception ??
575
                        }
576
                }
577
                if (heightFieldName != null) {
578
                        if (type.getIndex(heightFieldName) < 0) {
579
                                // FIXME exception ??
580
                        }
581
                }
582
                if (colorFieldName != null) {
583
                        if (type.getIndex(colorFieldName) < 0) {
584
                                // FIXME exception ??
585
                        }
586
                }
587
        }
588

    
589
        public boolean shouldDrawLabels(double scale) {
590
                return layer.isWithinScale(scale);
591
        }
592
        public Color getColorFont() {
593
                return colorFont;
594
        }
595

    
596
        public void setColorFont(Color colorFont) {
597
                this.colorFont = colorFont;
598
        }
599

    
600
        public Font getFont() {
601
                return font;
602
        }
603

    
604
        public void setFont(Font selFont) {
605
                this.font = selFont;
606
        }
607

    
608
        /* (non-Javadoc)
609
         * @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
610
         */
611
        public void loadFromState(PersistentState state)
612
                        throws PersistenceException {
613

    
614
                setTextField(state.getString(FIELD_TEXT));
615
                setHeightField(state.getString(FIELD_HEIGHT));
616
                setColorField(state.getString(FIELD_COLOR));
617
                setRotationField(state.getString(FIELD_ROTATION));
618
                setUnit(state.getInt(FIELD_UNIT));
619
                setFont((Font) state.get(FIELD_FONT));
620
                useFixedSize = state.getBoolean(FIELD_USE_FIXED_SIZE);
621
                setFixedSize(state.getDouble(FIELD_FIXED_SIZE));
622
                useFixedColor = state.getBoolean(FIELD_USE_FIXED_COLOR);
623
                setFixedColor((Color) state.get(FIELD_FIXED_COLOR));
624
                setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM));
625
                
626
                try {
627
                        setLayer((FLayer) state.get(FIELD_LAYER));
628
                } catch (ReadException e) {
629
                        PersistenceException ex =
630
                                        createPersistenceException("Error loading state from persistence");
631
                        ex.add(e);
632
                        throw ex;
633
                }
634
                setColorFont((Color) state.get(FIELD_COLOR_FONT));
635
                setZoomConstraints((IZoomConstraints) state.get(FIELD_ZOOM_CONSTRAINTS));
636
                setLabelingMethod((ILabelingMethod) state.get(FIELD_LABELING_METHOD));
637
        }
638

    
639
        public static void registerPersistence() {
640
                // Add the DynClass definition.
641
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
642
                DynStruct definition = manager.addDefinition(
643
                                DefaultLabelingMethod.class,
644
                                ATTR_IN_TABLE_LABELING_STRATEGY_DYNCLASS_NAME,
645
                                ATTR_IN_TABLE_LABELING_STRATEGY_DYNCLASS_NAME+" Persistence definition",
646
                                null, 
647
                                null
648
                );
649
                definition.addDynFieldString(FIELD_TEXT).setMandatory(true);
650
                definition.addDynFieldString(FIELD_HEIGHT).setMandatory(true);
651
                definition.addDynFieldString(FIELD_COLOR).setMandatory(true);
652
                definition.addDynFieldString(FIELD_ROTATION).setMandatory(true);
653
                definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
654
                definition.addDynFieldObject(FIELD_FONT).setMandatory(true);
655
                definition.addDynFieldBoolean(FIELD_USE_FIXED_SIZE).setMandatory(true);
656
                definition.addDynFieldDouble(FIELD_FIXED_SIZE).setMandatory(true);
657
                definition.addDynFieldBoolean(FIELD_USE_FIXED_COLOR).setMandatory(true);
658
                definition.addDynFieldObject(FIELD_FIXED_COLOR).setMandatory(true);
659
                definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
660
                definition.addDynFieldObject(FIELD_LAYER).setMandatory(true);
661
                definition.addDynFieldObject(FIELD_COLOR_FONT).setMandatory(true);
662
                definition.addDynFieldObject(FIELD_ZOOM_CONSTRAINTS).setMandatory(true);
663
                definition.addDynFieldObject(FIELD_LABELING_METHOD).setMandatory(true);
664
        }
665

    
666
        /* (non-Javadoc)
667
         * @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
668
         */
669
        public void saveToState(PersistentState state) throws PersistenceException {
670
                PersistenceException ex = null;
671

    
672
                try {
673
                        state.set(FIELD_TEXT, getTextField());
674
                } catch (DataException e) {
675
                        if (ex == null) {
676
                                ex = createPersistenceException("Error saving to state");
677
                        }
678
                        ex.add(e);
679
                }
680
                try {
681
                        state.set(FIELD_HEIGHT, getHeightField());
682
                } catch (DataException e) {
683
                        if (ex == null) {
684
                                ex = createPersistenceException("Error saving to state");
685
                        }
686
                        ex.add(e);
687
                }
688
                try {
689
                        state.set(FIELD_COLOR, getColorField());
690
                } catch (DataException e) {
691
                        if (ex == null) {
692
                                ex = createPersistenceException("Error saving to state");
693
                        }
694
                        ex.add(e);
695
                }
696
                try {
697
                        state.set(FIELD_ROTATION, getRotationField());
698
                } catch (DataException e) {
699
                        if (ex == null) {
700
                                ex =
701
                                                createPersistenceException("Error saving to state for persistence");
702
                        }
703
                        ex.add(e);
704
                }
705
                state.set(FIELD_UNIT, getUnit());
706
                state.set(FIELD_FONT, getFont());
707
                state.set(FIELD_USE_FIXED_SIZE, useFixedSize);
708
                state.set(FIELD_FIXED_SIZE, getFixedSize());
709
                state.set(FIELD_USE_FIXED_COLOR, useFixedColor);
710
                state.set(FIELD_FIXED_COLOR, getFixedColor());
711
                state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem());
712
                state.set(FIELD_LAYER, layer);
713
                state.set(FIELD_COLOR_FONT, getColorFont());
714
                state.set(FIELD_ZOOM_CONSTRAINTS, getZoomConstraints());
715
                state.set(FIELD_LABELING_METHOD, getLabelingMethod());
716

    
717
                if (ex != null) {
718
                        throw ex;
719
                }
720
        }
721

    
722
        private PersistenceException createPersistenceException(String message) {
723
                PersistenceException ex = new PersistenceException(message);
724
                ex.fillInStackTrace();
725
                return ex;
726
        }
727
}