Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / legend / styling / LabelClass.java @ 29973

History | View | Annotate | Download (15.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
 *
44
 * $Id: LabelClass.java 13953 2007-09-21 12:26:04Z jaume $
45
 * $Log$
46
 * Revision 1.14  2007-09-21 12:26:04  jaume
47
 * cancellation support extended down to the IGeometry and ISymbol level
48
 *
49
 * Revision 1.13  2007/09/17 14:16:11  jaume
50
 * multilayer symbols sizing bug fixed
51
 *
52
 * Revision 1.12  2007/08/22 09:48:13  jvidal
53
 * javadoc
54
 *
55
 * Revision 1.11  2007/05/09 11:04:58  jaume
56
 * refactored legend hierarchy
57
 *
58
 * Revision 1.10  2007/05/08 08:47:40  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.9  2007/04/26 11:41:00  jaume
62
 * attempting to let defining size in world units
63
 *
64
 * Revision 1.8  2007/04/18 15:35:11  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.7  2007/04/12 14:28:43  jaume
68
 * basic labeling support for lines
69
 *
70
 * Revision 1.6  2007/04/11 16:01:08  jaume
71
 * maybe a label placer refactor
72
 *
73
 * Revision 1.5  2007/04/10 16:34:01  jaume
74
 * towards a styled labeling
75
 *
76
 * Revision 1.4  2007/04/05 16:07:14  jaume
77
 * Styled labeling stuff
78
 *
79
 * Revision 1.3  2007/04/02 16:34:56  jaume
80
 * Styled labeling (start commiting)
81
 *
82
 * Revision 1.2  2007/03/09 08:33:43  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.1.2.6  2007/02/15 16:23:44  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.1.2.5  2007/02/09 07:47:05  jaume
89
 * Isymbol moved
90
 *
91
 * Revision 1.1.2.4  2007/02/02 16:21:24  jaume
92
 * start commiting labeling stuff
93
 *
94
 * Revision 1.1.2.3  2007/02/01 17:46:49  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.1.2.2  2007/02/01 11:42:47  jaume
98
 * *** empty log message ***
99
 *
100
 * Revision 1.1.2.1  2007/01/30 18:10:45  jaume
101
 * start commiting labeling stuff
102
 *
103
 *
104
 */
105
package org.gvsig.fmap.mapcontext.rendering.legend.styling;
106

    
107
import java.awt.Dimension;
108
import java.awt.Graphics2D;
109
import java.awt.Rectangle;
110
import java.awt.Shape;
111
import java.awt.geom.AffineTransform;
112
import java.awt.geom.Point2D;
113
import java.awt.geom.Rectangle2D;
114

    
115
import org.gvsig.fmap.geom.Geometry;
116
import org.gvsig.fmap.geom.GeometryLocator;
117
import org.gvsig.fmap.geom.GeometryManager;
118
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
119
import org.gvsig.fmap.geom.exception.CreateGeometryException;
120
import org.gvsig.fmap.geom.primitive.GeneralPathX;
121
import org.gvsig.fmap.geom.util.UtilFunctions;
122
import org.gvsig.fmap.mapcontext.ViewPort;
123
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
124
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupportToolkit;
125
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
126
import org.gvsig.fmap.mapcontext.rendering.symbols.SimpleTextSymbol;
127
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
128
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
129
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
130
import org.gvsig.utils.IPersistence;
131
import org.gvsig.utils.XMLEntity;
132
import org.gvsig.utils.XMLException;
133
import org.slf4j.Logger;
134
import org.slf4j.LoggerFactory;
135

    
136

    
137
/**
138
 *
139
 * LabelClass is the model of the label in the new simbology of gvSIG. In this
140
 * class is contained its definition, the expresion that defines the text which
141
 * is going to be showed, if it will be visible or not, the text symbol that is
142
 * going to paint the label and the style for its background.
143
 *
144
 * @author jaume dominguez faus - jaume.dominguez@iver.es
145
 */
146
public class LabelClass implements IPersistence, CartographicSupport {
147
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
148
        private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class);
149
        private String name;
150
        private ITextSymbol textSymbol;
151
        private String labelExpression;
152
        private boolean isVisible = true;
153
        private ILabelStyle labelStyle;
154
        private String[] texts;
155
        private int priority;
156
        private double scale = 1;
157
        private String sqlQuery;
158

    
159
        /**
160
         * Returns true if the label will be showed in the map
161
         *
162
         * @return isVisible boolean
163
         */
164
        public boolean isVisible() {
165
                return isVisible;
166
        }
167

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

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

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

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

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

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

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

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

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

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

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

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

    
292
        }
293

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

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

    
321

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

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

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

    
339

    
340
                        graphics.translate(xAnchor, yAnchor);
341
                        graphics.rotate(theta);
342
                        try {
343
                                drawInsideRectangle(graphics, r);
344
                        } catch (SymbolDrawingException e) {
345
                                // TODO Auto-generated catch block
346
                                e.printStackTrace();
347
                        }
348
                        graphics.rotate(-theta);
349
                        graphics.translate(-xAnchor, -yAnchor);
350

    
351
                } catch (CreateGeometryException e1) {
352
                        logger.error("Error creating a point", e1);
353
                        e1.printStackTrace();
354
                }
355
        }
356

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

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

    
389

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

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

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

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

    
422
        public int getPriority() {
423
                return priority;
424
        }
425

    
426
        public void setPriority(int priority) {
427
                this.priority = priority;
428
        }
429

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

    
437
                // 2. calculate the container shape
438
                Geometry returnedValue;
439
                Rectangle bounds = getBounds();
440

    
441
                AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
442
                at.concatenate(AffineTransform.getRotateInstance(theta));
443
                returnedValue = geomManager.createSurface(new GeneralPathX(bounds.getPathIterator(null)), SUBTYPES.GEOM2D);
444

    
445
                returnedValue.transform(at);
446
                return returnedValue;
447
        }
448

    
449
        public String getClassName() {
450
                return getClass().getName();
451
        }
452

    
453
        public XMLEntity getXMLEntity() throws XMLException {
454
                XMLEntity xml = new XMLEntity();
455
                xml.putProperty("className", getClassName());
456
                xml.putProperty("isVisible", isVisible);
457
                xml.putProperty("name", name);
458
                xml.putProperty("labelExpression", labelExpression);
459
                xml.putProperty("unit", getUnit());
460
                xml.putProperty("referenceSystem", getReferenceSystem());
461
                xml.putProperty("priority", getPriority());
462
                if (texts != null) {
463
                        xml.putProperty("texts", texts);
464
                }
465
                if (sqlQuery != null) {
466
                        xml.putProperty("sqlQuery", getSQLQuery());
467
                }
468

    
469
                if (labelStyle!=null) {
470
                        XMLEntity labelStyleXML = labelStyle.getXMLEntity();
471
                        labelStyleXML.putProperty("id", "labelStyle");
472
                        xml.addChild(labelStyleXML);
473
                }
474

    
475
                XMLEntity textSymXML = getTextSymbol().getXMLEntity();
476
                textSymXML.putProperty("id", "TextSymbol");
477
                xml.addChild(textSymXML);
478

    
479
                return xml;
480
        }
481

    
482
        public void setXMLEntity(XMLEntity xml) {
483
                isVisible = xml.getBooleanProperty("isVisible");
484
                name = xml.getStringProperty("name");
485
                labelExpression = xml.getStringProperty("labelExpression");
486
                setUnit(xml.getIntProperty("unit"));
487
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
488
                setTextSymbol(
489
                                (ITextSymbol) SymbologyFactory.
490
                                createSymbolFromXML(xml.firstChild("id", "TextSymbol"), null));
491
                if (xml.contains("texts")) {
492
                        setTexts(xml.getStringArrayProperty("texts"));
493
                }
494
                if (xml.contains("sqlQuery")) {
495
                        setSQLQuery(xml.getStringProperty("sqlQuery"));
496
                }
497

    
498
                if (xml.contains("priority")) {
499
                        setPriority(xml.getIntProperty("priority"));
500
                }
501
                // labelStyle
502
                XMLEntity aux = xml.firstChild("id", "labelStyle");
503
                if (aux!= null) {
504
                        setLabelStyle((ILabelStyle) SymbologyFactory.
505
                                        createStyleFromXML(aux, "labelStyle"));
506
                }
507
        }
508

    
509
        private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
510
        private int referenceSystem = CartographicSupportToolkit.DefaultReferenceSystem;
511

    
512
        public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
513
                Dimension sz = getSize();
514
                double width = sz.getWidth();
515
                double height = sz.getHeight();
516
                return CartographicSupportToolkit.
517
                getCartographicLength(this,
518
                                Math.max(width, height),
519
                                viewPort,
520
                                dpi);
521
        }
522

    
523
        public int getReferenceSystem() {
524
                return referenceSystem;
525
        }
526

    
527
        public int getUnit() {
528
                return unit;
529
        }
530

    
531
        public void setCartographicSize(double cartographicSize, Geometry geom) {
532
                Dimension sz = getSize();
533
                double width = sz.getWidth();
534
                double height = sz.getHeight();
535
                if (width >= height) {
536
                        scale = cartographicSize / width;
537
                } else {
538
                        scale = cartographicSize / height;
539
                }
540
        }
541

    
542
        public void setReferenceSystem(int referenceSystem) {
543
                this.referenceSystem = referenceSystem;
544
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
545
                        ((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
546
                }
547
        }
548

    
549
        public void setUnit(int unitIndex) {
550
                this.unit = unitIndex;
551
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
552
                        ((CartographicSupport) textSymbol).setUnit(unitIndex);
553
                }
554
        }
555

    
556
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
557
                setCartographicSize(getCartographicSize(
558
                                viewPort,
559
                                dpi,
560
                                geom),
561
                                geom);
562
                return 0;
563
        }
564

    
565
        public Rectangle getBounds() {
566
                Dimension cBounds = getSize();
567
                return new Rectangle(
568
                                0,
569
                                0,
570
                                (int) Math.round(cBounds.width*scale),
571
                                (int) Math.round(cBounds.height*scale));
572
        }
573

    
574
        public String getSQLQuery() {
575
                if (sqlQuery == null) sqlQuery = "";
576
                return sqlQuery;
577
        }
578

    
579
        public void setSQLQuery(String sqlQuery) {
580
                this.sqlQuery = sqlQuery;
581
        }
582
}