Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / styling / labeling / LabelClass.java @ 28367

History | View | Annotate | Download (16.7 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 com.iver.cit.gvsig.fmap.rendering.styling.labeling;
106

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

    
116
import com.iver.cit.gvsig.fmap.ViewPort;
117
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
118
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
119
import com.iver.cit.gvsig.fmap.core.FPoint2D;
120
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
121
import com.iver.cit.gvsig.fmap.core.FShape;
122
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
123
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
124
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
125
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
126
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
127
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
128
import com.iver.utiles.IPersistence;
129
import com.iver.utiles.XMLEntity;
130

    
131
/**
132
 *
133
 * LabelClass is the model of the label in the new simbology of gvSIG. In this
134
 * class is contained its definition, the expresion that defines the text which
135
 * is going to be showed, if it will be visible or not, the text symbol that is
136
 * going to paint the label and the style for its background.
137
 *
138
 * @author jaume dominguez faus - jaume.dominguez@iver.es
139
 */
140
public class LabelClass implements IPersistence, CartographicSupport {
141
        private String name;
142
        private ITextSymbol textSymbol;
143
        private String[] labelExpressions;
144
        private boolean isVisible = true;
145
        private ILabelStyle labelStyle;
146
        private String[] texts;
147
        private int priority;
148
        private double scale = 1;
149
        private String sqlQuery;
150
        private boolean useSqlQuery = false;
151

    
152

    
153
        /**
154
         * Returns true if the label will be showed in the map
155
         *
156
         * @return isVisible boolean
157
         */
158
        public boolean isVisible() {
159
                return isVisible;
160
        }
161

    
162
        /**
163
         * Sets the visibility of the label in the map.
164
         *
165
         * @param isVisible boolean
166
         */
167
        public void setVisible(boolean isVisible) {
168
                this.isVisible = isVisible;
169
        }
170

    
171
        /**
172
         * Returns the expression that defines the text which will be showed in
173
         * the label
174
         *
175
         * @return labelExpressions String[]
176
         */
177
        public String[] getLabelExpressions() {
178
                return labelExpressions;
179
        }
180
        /**
181
         * Returns the expression that defines the text which will be showed in
182
         * the label ready to be used for the label parser
183
         *
184
         * @return labelExpression String
185
         */
186
        public String getStringLabelExpression(){
187
                String expr = "";
188

    
189
                if(labelExpressions != null && labelExpressions.length > 0){
190

    
191
                        for (int i = 0; i < labelExpressions.length-1; i++) {
192
                                expr += (String) labelExpressions[i] +  ":";//EOFIELD
193
                        }
194
                        expr += labelExpressions[labelExpressions.length - 1] +";";//EOEXPRESSION
195

    
196
                }
197
                else expr = ";";
198
                return expr;
199
        }
200

    
201

    
202
        /**
203
         * Stablishes the expresion that, when it is evaluated, returns the text
204
         * which will be showed by the label.
205
         *
206
         * @param labelExpression String
207
         */
208
        public void setLabelExpressions(String[] labelExpressions) {
209
                this.labelExpressions = labelExpressions;
210
        }
211

    
212
        /**
213
         * Returns the text symbol that is being used for the text(the font,
214
         * size,style,aligment)
215
         *
216
         * @return label ITextSymbol
217
         */
218
        public ITextSymbol getTextSymbol() {
219
                if (textSymbol == null) {
220
                        textSymbol = new SimpleTextSymbol();
221
                }
222
                return textSymbol;
223
        }
224

    
225
        private Dimension getSize() {
226
                if (labelStyle == null) {
227
                        if (texts!=null && texts.length >0) {
228
                                String t = "";
229
                                for (int i = 0; i < texts.length; i++) {
230
                                        t += texts[i];
231
                                }
232
                                getTextSymbol().setText(t);
233
                        }
234

    
235
                        Rectangle bounds = getTextSymbol().getBounds();
236
//                        bounds.setLocation(
237
//                                        (int) Math.round(bounds.getX()),
238
//                                        (int) Math.round(bounds.getY()+bounds.getHeight()));
239
                        return new Dimension(bounds.width, bounds.height);
240
                } else {
241
                        labelStyle.setTextFields(texts);
242
                        return labelStyle.getSize();
243
                }
244
        }
245
        /**
246
         * Stablishes the text symbol that is going to be used for the text(the
247
         * font,size,style,aligment)
248
         *
249
         * @param textSymbol ITextSymbol
250
         */
251
        public void setTextSymbol(ITextSymbol textSymbol) {
252
                this.textSymbol = textSymbol;
253
                if (textSymbol == null) {
254
                        this.textSymbol = new SimpleTextSymbol();
255
                }
256
                setReferenceSystem(referenceSystem);
257
                setUnit(unit);
258
        }
259

    
260
        /**
261
         * Stablishes the style for the label.
262
         *
263
         * @param labelStyle ILabelStyle
264
         */
265
        public void setLabelStyle(ILabelStyle labelStyle) {
266
                this.labelStyle = labelStyle;
267
        }
268

    
269
        /**
270
         * Returns the style of the label
271
         *
272
         */
273
        public ILabelStyle getLabelStyle() {
274
                return this.labelStyle;
275
        }
276

    
277
        /**
278
         * Returns the name of the label
279
         *
280
         */
281
        public String getName() {
282
                return name;
283
        }
284

    
285
        /**
286
         * Stablishes the name of the label
287
         * @param name
288
         */
289
        public void setName(String name) {
290
                this.name = name;
291
        }
292

    
293
        public String toString() {
294
                // for debugging
295
//                return name+"{label expression="+labelExpression+", visible="+isVisible+", priority="+priority+"}";
296
                return getName();
297
        }
298

    
299
        /**
300
         * Sets the text for the label
301
         *
302
         * @param texts String[]
303
         */
304
        public void setTexts(String[] texts) {
305
                this.texts = texts;
306

    
307
        }
308

    
309
        /**
310
         * Return the text for the label
311
         *
312
         * @param texts String[]
313
         */
314
        public String[] getTexts() {
315
                return this.texts;
316
        }
317

    
318

    
319
        /**
320
         * <p>
321
         * LabelLocationMetrics, contains the anchor point, rotation, and some
322
         * other geometric calculations computed by the PlacementManager.
323
         * </p>
324
         *
325
         * <p>
326
         * The shp argument is passed as an accessory for subclasses of this
327
         * class in case they need futher geometric calculations
328
         * </p>
329
         * @param graphics, graphics to use to paint the label.
330
         * @param llm, concrete settings of the placement of this layer
331
         * @param shp, the Shape over whose the label is painted
332
         */
333
        public void draw(Graphics2D graphics, LabelLocationMetrics llm, FShape shp) {
334
                if (scale == 0)
335
                        return;
336
                if (!isVisible)
337
                        return;
338
                Dimension size = getSize();
339
                int width = (int) Math.round(size.getWidth()*scale);
340
                if (width  < 1)
341
                        return;
342

    
343
                int height = (int) Math.round(size.getHeight()*scale);
344
                if (height < 1)
345
                        return;
346

    
347
                Rectangle r = new Rectangle(0,0, width, height);
348
                FPoint2D anchor = new FPoint2D(llm.getAnchor());
349
                double xAnchor = anchor.getX();
350
                double yAnchor = anchor.getY();
351
                double theta = llm.getRotation();
352

    
353
                float fontSizeBefore = textSymbol.getFont().getSize2D();
354
                textSymbol.setFontSize(fontSizeBefore*scale);
355
                graphics.translate(xAnchor, yAnchor);
356
                graphics.rotate(theta);
357
                try {
358
                        drawInsideRectangle(graphics, r);
359
                } catch (SymbolDrawingException e) {
360
                        // TODO Auto-generated catch block
361
                        e.printStackTrace();
362
                }
363
                graphics.rotate(-theta);
364
                graphics.translate(-xAnchor, -yAnchor);
365
                textSymbol.setFontSize(fontSizeBefore);
366
        }
367

    
368

    
369
        private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) {
370
                int x;
371
                int y;
372
                if (ratioViewPort > ratioLabel) {
373
                        // size is defined by the viewport height
374
                        y = (int) (r.height*xy[1]);
375
                        x = (int) ((0.5*r.width) - (0.5-xy[0])*(ratioLabel*r.height));
376
                } else {
377
                        // size is defined by the viewport width
378
                        x = (int) (r.width * xy[0]);
379
                        y = (int) ((0.5 * r.height) - (0.5-xy[1])*(r.width/ratioLabel));
380
                }
381
                xy[0] = x;
382
                xy[1] = y;
383
        }
384

    
385
        /**
386
         * Useful to render a Label with size inside little rectangles.
387
         *
388
         * @param graphics Graphics2D
389
         * @param bounds Rectangle
390
         * @throws SymbolDrawingException
391
         */
392
        public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds) throws SymbolDrawingException {
393
                if (labelStyle != null) {
394
                        labelStyle.drawInsideRectangle(graphics, bounds);
395
                        Rectangle2D[] textBounds = labelStyle.getTextBounds();
396
                        Dimension labelSz = getSize();
397
                        final double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
398
                        final double ratioViewPort = bounds.getWidth() / bounds.getHeight();
399
                        final double[] xy = new double[2];
400

    
401

    
402
                        // draw the text fields
403
                        if (textBounds.length > 0 && texts!=null) {
404
                                for (int i = 0; i < textBounds.length && i < texts.length; i++) {
405
                                        getTextSymbol().setText(texts[i]);
406
                                        Rectangle2D textFieldArea = textBounds[i];
407
                                        xy[0] = textFieldArea.getX();
408
                                        xy[1] = textFieldArea.getY();
409
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
410
                                        int x = (int) Math.round(xy[0]);
411
                                        int y = (int) Math.round(xy[1]);
412

    
413
                                        xy[0] = textFieldArea.getMaxX();
414
                                        xy[1] = textFieldArea.getMaxY();
415
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
416
                                        int width = (int) Math.round(xy[0]) -x;
417
                                        int height = (int) Math.round(xy[1] - y) ;
418

    
419
                                        Rectangle textRect = new Rectangle(x, y, width, height);
420
                                        Shape oldClip = graphics.getClip();
421
                                        graphics.setClip(textRect);
422
                                        getTextSymbol().drawInsideRectangle(graphics, null, textRect, null);
423
                                        graphics.setClip(oldClip);
424
                                }
425
                        }
426
                } else {
427

    
428
                        if (texts != null && texts.length>0)
429
                                getTextSymbol().setText(texts[0]);
430
                        getTextSymbol().drawInsideRectangle(graphics, null, bounds, null);
431
                }
432
        }
433

    
434
        public int getPriority() {
435
                return priority;
436
        }
437

    
438
        public void setPriority(int priority) {
439
                this.priority = priority;
440
        }
441

    
442
        public FShape getShape(LabelLocationMetrics llm) {
443
                Point2D anchor = llm.getAnchor();
444
                FPoint2D p = new FPoint2D(anchor);
445
                double theta = llm.getRotation();
446

    
447
                // 2. calculate the container shape
448
                FShape returnedValue;
449
                Rectangle bounds = getBounds();
450

    
451
                AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
452
                at.concatenate(AffineTransform.getRotateInstance(theta));
453

    
454
                returnedValue = new FPolygon2D(new GeneralPathX(bounds));
455

    
456
                returnedValue.transform(at);
457
                return returnedValue;
458
        }
459

    
460

    
461
        public String getClassName() {
462
                return getClass().getName();
463
        }
464

    
465
        public XMLEntity getXMLEntity() {
466
                XMLEntity xml = new XMLEntity();
467
                xml.putProperty("className", getClassName());
468
                xml.putProperty("isVisible", isVisible);
469
                xml.putProperty("name", name);
470
                if(labelExpressions != null)
471
                        xml.putProperty("labelExpressions", labelExpressions);
472
                xml.putProperty("unit", getUnit());
473
                xml.putProperty("referenceSystem", getReferenceSystem());
474
                xml.putProperty("priority", getPriority());
475
                xml.putProperty("useSqlQuery",isUseSqlQuery());
476

    
477
                if (texts != null) {
478
                        xml.putProperty("texts", texts);
479
                }
480
                if (sqlQuery != null) {
481
                        xml.putProperty("sqlQuery", getSQLQuery());
482
                }
483

    
484
                if (labelStyle!=null) {
485
                        XMLEntity labelStyleXML = labelStyle.getXMLEntity();
486
                        labelStyleXML.putProperty("id", "labelStyle");
487
                        xml.addChild(labelStyleXML);
488
                }
489

    
490
                XMLEntity textSymXML = getTextSymbol().getXMLEntity();
491
                textSymXML.putProperty("id", "TextSymbol");
492
                xml.addChild(textSymXML);
493

    
494
                return xml;
495
        }
496

    
497
        public void setXMLEntity(XMLEntity xml) {
498
                isVisible = xml.getBooleanProperty("isVisible");
499
                name = xml.getStringProperty("name");
500
                if(xml.contains("labelExpressions"))
501
                        labelExpressions = xml.getStringArrayProperty("labelExpressions");
502
                setUnit(xml.getIntProperty("unit"));
503
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
504
                setTextSymbol(
505
                                (ITextSymbol) SymbologyFactory.
506
                                createSymbolFromXML(xml.firstChild("id", "TextSymbol"), null));
507

    
508
                if(xml.contains("useSqlQuery")){
509
                        setUseSqlQuery(xml.getBooleanProperty("useSqlQuery"));
510
                }
511

    
512
                if (xml.contains("texts")) {
513
                        setTexts(xml.getStringArrayProperty("texts"));
514
                }
515
                if (xml.contains("sqlQuery")) {
516
                        setSQLQuery(xml.getStringProperty("sqlQuery"));
517
                }
518

    
519
                if (xml.contains("priority")) {
520
                        setPriority(xml.getIntProperty("priority"));
521
                }
522
                // labelStyle
523
                XMLEntity aux = xml.firstChild("id", "labelStyle");
524
                if (aux!= null) {
525
                        setLabelStyle((ILabelStyle) SymbologyFactory.
526
                                                        createStyleFromXML(aux, "labelStyle"));
527
                }
528
        }
529

    
530
        private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
531
        private int referenceSystem = CartographicSupportToolkit.DefaultReferenceSystem;
532

    
533
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
534
                Dimension sz = getSize();
535
                double width = sz.getWidth();
536
                double height = sz.getHeight();
537
                return CartographicSupportToolkit.
538
                        getCartographicLength(this,
539
                                                          Math.max(width, height),
540
                                                          viewPort,
541
                                                          dpi);
542
        }
543

    
544
        public int getReferenceSystem() {
545
                return referenceSystem;
546
        }
547

    
548
        public int getUnit() {
549
                return unit;
550
        }
551

    
552
        public void setCartographicSize(double cartographicSize, FShape shp) {
553
                Dimension sz = getSize();
554
                double width = sz.getWidth();
555
                double height = sz.getHeight();
556
                if (width >= height) {
557
                        scale = cartographicSize / width;
558
                } else {
559
                        scale = cartographicSize / height;
560
                }
561
        }
562

    
563
        public void setReferenceSystem(int referenceSystem) {
564
                this.referenceSystem = referenceSystem;
565
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
566
                        ((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
567
                }
568
        }
569

    
570
        public void setUnit(int unitIndex) {
571
                this.unit = unitIndex;
572
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
573
                        ((CartographicSupport) textSymbol).setUnit(unitIndex);
574
                }
575
        }
576

    
577
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
578
                setCartographicSize(getCartographicSize(
579
                                                                viewPort,
580
                                                                dpi,
581
                                                                shp),
582
                                                        shp);
583
                return 0;
584
        }
585

    
586
        public Rectangle getBounds() {
587
                Dimension cBounds = getSize();
588
                return new Rectangle(
589
                                0,
590
                                0,
591
                                (int) Math.round(cBounds.width*scale),
592
                                (int) Math.round(cBounds.height*scale));
593
        }
594

    
595
        public String getSQLQuery() {
596
                if (sqlQuery == null) sqlQuery = "";
597
                return sqlQuery;
598
        }
599

    
600
        public void setSQLQuery(String sqlQuery) {
601
                this.sqlQuery = sqlQuery;
602
        }
603

    
604
        public boolean isUseSqlQuery() {
605
                return useSqlQuery;
606
        }
607

    
608
        public void setUseSqlQuery(boolean useSqlQuery) {
609
                this.useSqlQuery = useSqlQuery;
610
        }
611
}