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 / LabelClass.java @ 40560

History | View | Annotate | Download (17.9 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
 *
26
 * $Id: LabelClass.java 13953 2007-09-21 12:26:04Z jaume $
27
 * $Log$
28
 * Revision 1.14  2007-09-21 12:26:04  jaume
29
 * cancellation support extended down to the IGeometry and ISymbol level
30
 *
31
 * Revision 1.13  2007/09/17 14:16:11  jaume
32
 * multilayer symbols sizing bug fixed
33
 *
34
 * Revision 1.12  2007/08/22 09:48:13  jvidal
35
 * javadoc
36
 *
37
 * Revision 1.11  2007/05/09 11:04:58  jaume
38
 * refactored legend hierarchy
39
 *
40
 * Revision 1.10  2007/05/08 08:47:40  jaume
41
 * *** empty log message ***
42
 *
43
 * Revision 1.9  2007/04/26 11:41:00  jaume
44
 * attempting to let defining size in world units
45
 *
46
 * Revision 1.8  2007/04/18 15:35:11  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.7  2007/04/12 14:28:43  jaume
50
 * basic labeling support for lines
51
 *
52
 * Revision 1.6  2007/04/11 16:01:08  jaume
53
 * maybe a label placer refactor
54
 *
55
 * Revision 1.5  2007/04/10 16:34:01  jaume
56
 * towards a styled labeling
57
 *
58
 * Revision 1.4  2007/04/05 16:07:14  jaume
59
 * Styled labeling stuff
60
 *
61
 * Revision 1.3  2007/04/02 16:34:56  jaume
62
 * Styled labeling (start commiting)
63
 *
64
 * Revision 1.2  2007/03/09 08:33:43  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.1.2.6  2007/02/15 16:23:44  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.1.2.5  2007/02/09 07:47:05  jaume
71
 * Isymbol moved
72
 *
73
 * Revision 1.1.2.4  2007/02/02 16:21:24  jaume
74
 * start commiting labeling stuff
75
 *
76
 * Revision 1.1.2.3  2007/02/01 17:46:49  jaume
77
 * *** empty log message ***
78
 *
79
 * Revision 1.1.2.2  2007/02/01 11:42:47  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.1.2.1  2007/01/30 18:10:45  jaume
83
 * start commiting labeling stuff
84
 *
85
 *
86
 */
87
package org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling;
88

    
89
import java.awt.Dimension;
90
import java.awt.Graphics2D;
91
import java.awt.Rectangle;
92
import java.awt.Shape;
93
import java.awt.geom.AffineTransform;
94
import java.awt.geom.Point2D;
95
import java.awt.geom.Rectangle2D;
96

    
97
import org.gvsig.fmap.geom.Geometry;
98
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
99
import org.gvsig.fmap.geom.GeometryLocator;
100
import org.gvsig.fmap.geom.GeometryManager;
101
import org.gvsig.fmap.geom.exception.CreateGeometryException;
102
import org.gvsig.fmap.geom.primitive.GeneralPathX;
103
import org.gvsig.fmap.mapcontext.ViewPort;
104
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
105
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelLocationMetrics;
106
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
107
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
108
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
109
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
110
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit;
111
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.impl.SimpleTextSymbol;
112
import org.gvsig.tools.ToolsLocator;
113
import org.gvsig.tools.dynobject.DynStruct;
114
import org.gvsig.tools.persistence.PersistenceManager;
115
import org.gvsig.tools.persistence.PersistentState;
116
import org.gvsig.tools.persistence.exception.PersistenceException;
117
import org.gvsig.tools.util.Callable;
118
import org.slf4j.Logger;
119
import org.slf4j.LoggerFactory;
120

    
121

    
122
/**
123
 *
124
 * LabelClass is the model of the label in the new simbology of gvSIG. In this
125
 * class is contained its definition, the expresion that defines the text which
126
 * is going to be showed, if it will be visible or not, the text symbol that is
127
 * going to paint the label and the style for its background.
128
 *
129
 * @author jaume dominguez faus - jaume.dominguez@iver.es
130
 */
131
public class LabelClass implements ILabelClass {
132
        private static final String FIELD_REFERENCE_SYSTEM = "referenceSystem";
133
        private static final String FIELD_UNIT = "unit";
134
        private static final String FIELD_SQL_QUERY = "sqlQuery";
135
        private static final String FIELD_SCALE = "scale";
136
        private static final String FIELD_PRIORITY = "priority";
137
        private static final String FIELD_TEXTS = "texts";
138
        private static final String FIELD_LABEL_STYLE = "labelStyle";
139
        private static final String FIELD_VISIBLE = "visible";
140
        private static final String FIELD_LABEL_EXPRESSION = "labelExpression";
141
        private static final String FIELD_TEXT_SYMBOL = "textSymbol";
142
        private static final String FIELD_NAME = "name";
143
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
144
        private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class);
145
        private static final String LABEL_CLASS_PERSISTENCE_DEFINITION_NAME = "LabelClass";
146
        private String name;
147
        private ITextSymbol textSymbol;
148
        private String labelExpression;
149
        private boolean isVisible = true;
150
        private ILabelStyle labelStyle;
151
        private String[] texts;
152
        private int priority;
153
        private double scale = 1;
154
        private String sqlQuery;
155

    
156
        private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
157
        private int referenceSystem =
158
                        CartographicSupportToolkit.DefaultReferenceSystem;
159
        
160
        /**
161
         * Returns true if the label will be showed in the map
162
         *
163
         * @return isVisible boolean
164
         */
165
        public boolean isVisible() {
166
                return isVisible;
167
        }
168

    
169
        /**
170
         * Sets the visibility of the label in the map.
171
         *
172
         * @param isVisible boolean
173
         */
174
        public void setVisible(boolean isVisible) {
175
                this.isVisible = isVisible;
176
        }
177

    
178
        /**
179
         * Returns the expression that defines the text which will be showed in
180
         * the label
181
         *
182
         * @return labelExpression String
183
         */
184
        public String getLabelExpression() {
185
                return labelExpression;
186
        }
187

    
188
        /**
189
         * Stablishes the expresion that, when it is evaluated, returns the text
190
         * which will be showed by the label.
191
         *
192
         * @param labelExpression String
193
         */
194
        public void setLabelExpression(String labelExpression) {
195
                this.labelExpression = labelExpression;
196
        }
197

    
198
        /**
199
         * Returns the text symbol that is being used for the text(the font,
200
         * size,style,aligment)
201
         *
202
         * @return label ITextSymbol
203
         */
204
        public ITextSymbol getTextSymbol() {
205
                if (textSymbol == null) {
206
                        textSymbol = new SimpleTextSymbol();
207
                }
208
                return textSymbol;
209
        }
210

    
211
        private Dimension getSize() {
212
                if (labelStyle == null) {
213
                        if (texts!=null && texts.length >0) {
214
                                String t = "";
215
                                for (int i = 0; i < texts.length; i++) {
216
                                        t += texts[i];
217
                                }
218
                                getTextSymbol().setText(t);
219
                        }
220

    
221
                        Rectangle bounds = getTextSymbol().getBounds();
222
                        bounds.setLocation(
223
                                        (int) Math.round(bounds.getX()),
224
                                        (int) Math.round(bounds.getY()+bounds.getHeight()));
225
                        return new Dimension(bounds.width, bounds.height);
226
                } else {
227
                        labelStyle.setTextFields(texts);
228
                        return labelStyle.getSize();
229
                }
230
        }
231
        /**
232
         * Stablishes the text symbol that is going to be used for the text(the
233
         * font,size,style,aligment)
234
         *
235
         * @param textSymbol ITextSymbol
236
         */
237
        public void setTextSymbol(ITextSymbol textSymbol) {
238
                this.textSymbol = textSymbol;
239
                if (textSymbol == null) {
240
                        this.textSymbol = new SimpleTextSymbol();
241
                }
242
                setReferenceSystem(referenceSystem);
243
                setUnit(unit);
244
        }
245

    
246
        /**
247
         * Stablishes the style for the label.
248
         *
249
         * @param labelStyle ILabelStyle
250
         */
251
        public void setLabelStyle(ILabelStyle labelStyle) {
252
                this.labelStyle = labelStyle;
253
        }
254

    
255
        /**
256
         * Returns the style of the label
257
         *
258
         */
259
        public ILabelStyle getLabelStyle() {
260
                return this.labelStyle;
261
        }
262

    
263
        /**
264
         * Returns the name of the label
265
         *
266
         */
267
        public String getName() {
268
                return name;
269
        }
270

    
271
        /**
272
         * Stablishes the name of the label
273
         * @param name
274
         */
275
        public void setName(String name) {
276
                this.name = name;
277
        }
278

    
279
        public String toString() {
280
                // for debugging
281
                //                return name+"{label expression="+labelExpression+", visible="+isVisible+", priority="+priority+"}";
282
                return getName();
283
        }
284

    
285
        /**
286
         * Sets the text for the label
287
         *
288
         * @param texts String[]
289
         */
290
        public void setTexts(String[] texts) {
291
                this.texts = texts;
292

    
293
        }
294

    
295
        /**
296
         * Return the text for the label
297
         *
298
         * @param texts String[]
299
         */
300
        public String[] getTexts() {
301
                return this.texts;
302
        }
303

    
304
        /**
305
         * <p>
306
         * LabelLocationMetrics, contains the anchor point, rotation, and some
307
         * other geometric calculations computed by the PlacementManager.
308
         * </p>
309
         *
310
         * <p>
311
         * The shp argument is passed as an accessory for subclasses of this
312
         * class in case they need futher geometric calculations
313
         * </p>
314
         * @param graphics, graphics to use to paint the label.
315
         * @param llm, concrete settings of the placement of this layer
316
         * @param shp, the Shape over whose the label is painted
317
         */
318
        public void draw(Graphics2D graphics, ILabelLocationMetrics llm, Geometry geom) {
319
                if (scale == 0)
320
                        return;
321

    
322

    
323
                Dimension size = getSize();
324
                int width = (int) Math.round(size.getWidth()*scale);
325
                if (width  < 1)
326
                        return;
327

    
328
                int height = (int) Math.round(size.getHeight()*scale);
329
                if (height < 1)
330
                        return;
331

    
332
                Rectangle r = new Rectangle(0,0, width, height);
333
                org.gvsig.fmap.geom.primitive.Point anchor;
334
                try {
335
                        anchor = geomManager.createPoint(llm.getAnchor().getX(), llm.getAnchor().getY(), SUBTYPES.GEOM2D);
336
                        double xAnchor = anchor.getX();
337
                        double yAnchor = anchor.getY();
338
                        double theta = llm.getRotation();
339

    
340
                        graphics.translate(xAnchor, yAnchor);
341
                        graphics.rotate(theta);
342
                        synchronized (this) {
343
                                float fontSizeBefore = textSymbol.getFont().getSize2D();
344
                                try {
345
                                        textSymbol.setFontSize(fontSizeBefore*scale);
346
                                        drawInsideRectangle(graphics, r);
347
                                        textSymbol.setFontSize(fontSizeBefore);
348
                                } catch (SymbolDrawingException e) {
349
                                        e.printStackTrace();
350
                                }
351
                        }
352
                        graphics.rotate(-theta);
353
                        graphics.translate(-xAnchor, -yAnchor);
354
                } catch (CreateGeometryException e1) {
355
                        logger.error("Error creating a point", e1);
356
                        e1.printStackTrace();
357
                }
358
        }
359

    
360
        private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) {
361
                int x;
362
                int y;
363
                if (ratioViewPort > ratioLabel) {
364
                        // size is defined by the viewport height
365
                        y = (int) (r.height*xy[1]);
366
                        x = (int) ((0.5*r.width) - (0.5-xy[0])*(ratioLabel*r.height));
367
                } else {
368
                        // size is defined by the viewport width
369
                        x = (int) (r.width * xy[0]);
370
                        y = (int) ((0.5 * r.height) - (0.5-xy[1])*(r.width/ratioLabel));
371
                }
372
                xy[0] = x;
373
                xy[1] = y;
374
        }
375

    
376
        /**
377
         * Useful to render a Label with size inside little rectangles.
378
         *
379
         * @param graphics Graphics2D
380
         * @param bounds Rectangle
381
         * @throws SymbolDrawingException
382
         */
383
        public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds) throws SymbolDrawingException {
384
                if (labelStyle != null) {
385
                        labelStyle.drawInsideRectangle(graphics, bounds);
386
                        Rectangle2D[] textBounds = labelStyle.getTextBounds();
387
                        Dimension labelSz = getSize();
388
                        final double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
389
                        final double ratioViewPort = bounds.getWidth() / bounds.getHeight();
390
                        final double[] xy = new double[2];
391

    
392

    
393
                        // draw the text fields
394
                        if (textBounds.length > 0 && texts!=null) {
395
                                for (int i = 0; i < textBounds.length && i < texts.length; i++) {
396
                                        getTextSymbol().setText(texts[i]);
397
                                        Rectangle2D textFieldArea = textBounds[i];
398
                                        xy[0] = textFieldArea.getX();
399
                                        xy[1] = textFieldArea.getY();
400
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
401
                                        int x = (int) Math.round(xy[0]);
402
                                        int y = (int) Math.round(xy[1]);
403

    
404
                                        xy[0] = textFieldArea.getMaxX();
405
                                        xy[1] = textFieldArea.getMaxY();
406
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
407
                                        int width = (int) Math.round(xy[0]) -x;
408
                                        int height = (int) Math.round(xy[1] - y) ;
409

    
410
                                        Rectangle textRect = new Rectangle(x, y, width, height);
411
                                        Shape oldClip = graphics.getClip();
412
                                        graphics.setClip(textRect);
413
                                        getTextSymbol().drawInsideRectangle(graphics, null, textRect, null);
414
                                        graphics.setClip(oldClip);
415
                                }
416
                        }
417
                } else {
418

    
419
                        if (texts != null && texts.length>0)
420
                                getTextSymbol().setText(texts[0]);
421
                        getTextSymbol().drawInsideRectangle(graphics, null, bounds, null);
422
                }
423
        }
424

    
425
        public int getPriority() {
426
                return priority;
427
        }
428

    
429
        public void setPriority(int priority) {
430
                this.priority = priority;
431
        }
432

    
433
        public Geometry getShape(ILabelLocationMetrics llm) throws CreateGeometryException {
434
                if (llm==null)
435
                        return null;
436
                Point2D anchor = llm.getAnchor();
437
                org.gvsig.fmap.geom.primitive.Point p = geomManager.createPoint(anchor.getX(), anchor.getY(), SUBTYPES.GEOM2D);
438
                double theta = llm.getRotation();
439

    
440
                // 2. calculate the container shape
441
                Geometry returnedValue;
442
                Rectangle bounds = getBounds();
443

    
444
                AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
445
                at.concatenate(AffineTransform.getRotateInstance(theta));
446
                returnedValue = geomManager.createSurface(new GeneralPathX(bounds
447
                                .getPathIterator(null)), SUBTYPES.GEOM2D);
448

    
449
                returnedValue.transform(at);
450
                return returnedValue;
451
        }
452

    
453
        public String getClassName() {
454
                return getClass().getName();
455
        }
456

    
457
        public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
458
                Dimension sz = getSize();
459
                double width = sz.getWidth();
460
                double height = sz.getHeight();
461
                return CartographicSupportToolkit.
462
                getCartographicLength(this,
463
                                Math.max(width, height),
464
                                viewPort,
465
                                dpi);
466
        }
467

    
468
        public int getReferenceSystem() {
469
                return referenceSystem;
470
        }
471

    
472
        public int getUnit() {
473
                return unit;
474
        }
475

    
476
        public void setCartographicSize(double cartographicSize, Geometry geom) {
477
                Dimension sz = getSize();
478
                double width = sz.getWidth();
479
                double height = sz.getHeight();
480
                if (width >= height) {
481
                        scale = cartographicSize / width;
482
                } else {
483
                        scale = cartographicSize / height;
484
                }
485
        }
486

    
487
        public void setReferenceSystem(int referenceSystem) {
488
                this.referenceSystem = referenceSystem;
489
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
490
                        ((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
491
                }
492
        }
493

    
494
        public void setUnit(int unitIndex) {
495
                this.unit = unitIndex;
496
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
497
                        ((CartographicSupport) textSymbol).setUnit(unitIndex);
498
                }
499
        }
500

    
501
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
502
                setCartographicSize(getCartographicSize(
503
                                viewPort,
504
                                dpi,
505
                                geom),
506
                                geom);
507
                return 0;
508
        }
509

    
510
        public Rectangle getBounds() {
511
                Dimension cBounds = getSize();
512
                return new Rectangle(
513
                                0,
514
                                0,
515
                                (int) Math.round(cBounds.width*scale),
516
                                (int) Math.round(cBounds.height*scale));
517
        }
518

    
519
        public String getSQLQuery() {
520
                if (sqlQuery == null) sqlQuery = "";
521
                return sqlQuery;
522
        }
523

    
524
        public void setSQLQuery(String sqlQuery) {
525
                this.sqlQuery = sqlQuery;
526
        }
527

    
528
        /*
529
         * (non-Javadoc)
530
         * 
531
         * @seeorg.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.
532
         * persistence.PersistentState)
533
         */
534
        public void saveToState(PersistentState state) throws PersistenceException {
535
                state.set(FIELD_NAME, getName());
536
                state.set(FIELD_TEXT_SYMBOL, getTextSymbol());
537
                state.set(FIELD_LABEL_EXPRESSION, getLabelExpression());
538
                state.set(FIELD_VISIBLE, isVisible());
539
                state.set(FIELD_LABEL_STYLE, getLabelStyle());
540
                state.set(FIELD_TEXTS, getTexts());
541
                state.set(FIELD_PRIORITY, getPriority());
542
                state.set(FIELD_SCALE, scale);
543
                state.set(FIELD_SQL_QUERY, getSQLQuery());
544
                state.set(FIELD_UNIT, getUnit());
545
                state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem());
546
        }
547

    
548
        /*
549
         * (non-Javadoc)
550
         * 
551
         * @see
552
         * org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.
553
         * persistence.PersistentState)
554
         */
555
        public void loadFromState(PersistentState state)
556
                        throws PersistenceException {
557
                setName(state.getString(FIELD_NAME));
558
                setTextSymbol((ITextSymbol) state.get(FIELD_TEXT_SYMBOL));
559
                setLabelExpression(state.getString(FIELD_LABEL_EXPRESSION));
560
                setVisible(state.getBoolean(FIELD_VISIBLE));
561
                setLabelStyle((ILabelStyle) state.get(FIELD_LABEL_STYLE));
562
                setTexts((String[]) state.getArray(FIELD_TEXTS, String.class));
563
                setPriority(state.getInt(FIELD_PRIORITY));
564
                scale = state.getDouble(FIELD_SCALE);
565
                setSQLQuery(state.getString(FIELD_SQL_QUERY));
566
                setUnit(state.getInt(FIELD_UNIT));
567
                setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM));
568
        }
569

    
570
        public static class RegisterPersistence implements Callable {
571

    
572
                public Object call() throws Exception {
573
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
574
                        if( manager.getDefinition(LABEL_CLASS_PERSISTENCE_DEFINITION_NAME)==null ) {
575
                                DynStruct definition = manager.addDefinition(
576
                                                LabelClass.class,
577
                                                LABEL_CLASS_PERSISTENCE_DEFINITION_NAME,
578
                                                LABEL_CLASS_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
579
                                                null, 
580
                                                null
581
                                );
582

    
583
                                definition.addDynFieldString(FIELD_NAME).setMandatory(true);
584
                                definition.addDynFieldObject(FIELD_TEXT_SYMBOL).setMandatory(true).setClassOfValue(ITextSymbol.class);
585
                                definition.addDynFieldString(FIELD_LABEL_EXPRESSION).setMandatory(true);
586
                                definition.addDynFieldBoolean(FIELD_VISIBLE).setMandatory(true);
587
                                definition.addDynFieldObject(FIELD_LABEL_STYLE).setMandatory(true).setClassOfValue(ILabelStyle.class);
588
                                definition.addDynFieldList(FIELD_TEXTS).setMandatory(true).setClassOfItems(String.class);
589
                                definition.addDynFieldInt(FIELD_PRIORITY).setMandatory(true);
590
                                definition.addDynFieldDouble(FIELD_SCALE).setMandatory(true);
591
                                definition.addDynFieldString(FIELD_SQL_QUERY).setMandatory(true);
592
                                definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
593
                                definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
594
                        }
595
                        return Boolean.TRUE;
596
                }
597
                
598
        }
599

    
600

    
601

    
602
        
603
}