Statistics
| Revision:

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

History | View | Annotate | Download (17.5 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 org.apache.batik.ext.awt.geom.PathLength;
117

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

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

    
154

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

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

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

    
191
                if(labelExpressions != null && labelExpressions.length > 0){
192

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

    
198
                }
199
                else expr = ";";
200
                return expr;
201
        }
202

    
203

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

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

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

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

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

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

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

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

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

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

    
309
        }
310

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

    
320

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

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

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

    
355
                if (theta < 0) {
356
                        theta = theta + (2 * Math.PI);
357
                }
358

    
359
                if ((theta > (Math.PI / 2)) &&
360
                        (theta < ((3 * Math.PI) / 2))) {
361
                        theta = theta - (float) Math.PI;
362
                }
363

    
364

    
365
//                PathLength pathLen = new PathLength(shp);
366
//
367
//                // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
368
//                float midDistance = pathLen.lengthOfPath() / 2;
369
//                pAux = pathLen.pointAtLength(midDistance);
370
//                angle = pathLen.angleAtLength(midDistance);
371

    
372
//                if (angle < 0) {
373
//                        angle = angle + (float) (2 * Math.PI);
374
//                }
375
//
376
//                if ((angle > (Math.PI / 2)) &&
377
//                                (angle < ((3 * Math.PI) / 2))) {
378
//                        angle = angle - (float) Math.PI;
379
//                }
380
//
381
//                theLabel.setRotation(Math.toDegrees(angle));
382

    
383

    
384

    
385

    
386
                graphics.translate(xAnchor, yAnchor);
387
                graphics.rotate(theta);
388
                synchronized (this) {
389
                        float fontSizeBefore = textSymbol.getFont().getSize2D();
390
                        try {
391
                                textSymbol.setFontSize(fontSizeBefore*scale);
392
                                drawInsideRectangle(graphics, r);
393
                                textSymbol.setFontSize(fontSizeBefore);
394
                        } catch (SymbolDrawingException e) {
395
                                e.printStackTrace();
396
                        }
397
                }
398
                graphics.rotate(-theta);
399
                graphics.translate(-xAnchor, -yAnchor);
400

    
401
        }
402

    
403

    
404
        private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) {
405
                int x;
406
                int y;
407
                if (ratioViewPort > ratioLabel) {
408
                        // size is defined by the viewport height
409
                        y = (int) (r.height*xy[1]);
410
                        x = (int) ((0.5*r.width) - (0.5-xy[0])*(ratioLabel*r.height));
411
                } else {
412
                        // size is defined by the viewport width
413
                        x = (int) (r.width * xy[0]);
414
                        y = (int) ((0.5 * r.height) - (0.5-xy[1])*(r.width/ratioLabel));
415
                }
416
                xy[0] = x;
417
                xy[1] = y;
418
        }
419

    
420
        /**
421
         * Useful to render a Label with size inside little rectangles.
422
         *
423
         * @param graphics Graphics2D
424
         * @param bounds Rectangle
425
         * @throws SymbolDrawingException
426
         */
427
        public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds) throws SymbolDrawingException {
428
                if (labelStyle != null) {
429
                        labelStyle.drawInsideRectangle(graphics, bounds);
430
                        Rectangle2D[] textBounds = labelStyle.getTextBounds();
431
                        Dimension labelSz = getSize();
432
                        final double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
433
                        final double ratioViewPort = bounds.getWidth() / bounds.getHeight();
434
                        final double[] xy = new double[2];
435

    
436

    
437
                        // draw the text fields
438
                        if (textBounds.length > 0 && texts!=null) {
439
                                for (int i = 0; i < textBounds.length && i < texts.length; i++) {
440
                                        getTextSymbol().setText(texts[i]);
441
                                        Rectangle2D textFieldArea = textBounds[i];
442
                                        xy[0] = textFieldArea.getX();
443
                                        xy[1] = textFieldArea.getY();
444
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
445
                                        int x = (int) Math.round(xy[0]);
446
                                        int y = (int) Math.round(xy[1]);
447

    
448
                                        xy[0] = textFieldArea.getMaxX();
449
                                        xy[1] = textFieldArea.getMaxY();
450
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
451
                                        int width = (int) Math.round(xy[0]) -x;
452
                                        int height = (int) Math.round(xy[1] - y) ;
453

    
454
                                        Rectangle textRect = new Rectangle(x, y, width, height);
455
                                        Shape oldClip = graphics.getClip();
456
                                        graphics.setClip(textRect);
457
                                        getTextSymbol().drawInsideRectangle(graphics, null, textRect, null);
458
                                        graphics.setClip(oldClip);
459
                                }
460
                        }
461
                } else {
462

    
463
                        if (texts != null && texts.length>0)
464
                                getTextSymbol().setText(texts[0]);
465
                        getTextSymbol().drawInsideRectangle(graphics, null, bounds, null);
466
                }
467
        }
468

    
469
        public int getPriority() {
470
                return priority;
471
        }
472

    
473
        public void setPriority(int priority) {
474
                this.priority = priority;
475
        }
476

    
477
        public FShape getShape(LabelLocationMetrics llm) {
478
                if (llm==null)
479
                        return null;
480
                Point2D anchor = llm.getAnchor();
481
                FPoint2D p = new FPoint2D(anchor);
482
                double theta = llm.getRotation();
483

    
484
                // 2. calculate the container shape
485
                FShape returnedValue;
486
                Rectangle bounds = getBounds();
487

    
488
                AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
489
                at.concatenate(AffineTransform.getRotateInstance(theta));
490

    
491
                returnedValue = new FPolygon2D(new GeneralPathX(bounds));
492

    
493
                returnedValue.transform(at);
494
                return returnedValue;
495
        }
496

    
497

    
498
        public String getClassName() {
499
                return getClass().getName();
500
        }
501

    
502
        public XMLEntity getXMLEntity() {
503
                XMLEntity xml = new XMLEntity();
504
                xml.putProperty("className", getClassName());
505
                xml.putProperty("isVisible", isVisible);
506
                xml.putProperty("name", name);
507
                if(labelExpressions != null)
508
                        xml.putProperty("labelExpressions", labelExpressions);
509
                xml.putProperty("unit", getUnit());
510
                xml.putProperty("referenceSystem", getReferenceSystem());
511
                xml.putProperty("priority", getPriority());
512
                xml.putProperty("useSqlQuery",isUseSqlQuery());
513

    
514
                if (texts != null) {
515
                        xml.putProperty("texts", texts);
516
                }
517
                if (sqlQuery != null) {
518
                        xml.putProperty("sqlQuery", getSQLQuery());
519
                }
520

    
521
                if (labelStyle!=null) {
522
                        XMLEntity labelStyleXML = labelStyle.getXMLEntity();
523
                        labelStyleXML.putProperty("id", "labelStyle");
524
                        xml.addChild(labelStyleXML);
525
                }
526

    
527
                XMLEntity textSymXML = getTextSymbol().getXMLEntity();
528
                textSymXML.putProperty("id", "TextSymbol");
529
                xml.addChild(textSymXML);
530

    
531
                return xml;
532
        }
533

    
534
        public void setXMLEntity(XMLEntity xml) {
535
                isVisible = xml.getBooleanProperty("isVisible");
536
                name = xml.getStringProperty("name");
537
                if(xml.contains("labelExpressions"))
538
                        labelExpressions = xml.getStringArrayProperty("labelExpressions");
539
                setUnit(xml.getIntProperty("unit"));
540
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
541
                setTextSymbol(
542
                                (ITextSymbol) SymbologyFactory.
543
                                createSymbolFromXML(xml.firstChild("id", "TextSymbol"), null));
544

    
545
                if(xml.contains("useSqlQuery")){
546
                        setUseSqlQuery(xml.getBooleanProperty("useSqlQuery"));
547
                }
548

    
549
                if (xml.contains("texts")) {
550
                        setTexts(xml.getStringArrayProperty("texts"));
551
                }
552
                if (xml.contains("sqlQuery")) {
553
                        setSQLQuery(xml.getStringProperty("sqlQuery"));
554
                }
555

    
556
                if (xml.contains("priority")) {
557
                        setPriority(xml.getIntProperty("priority"));
558
                }
559
                // labelStyle
560
                XMLEntity aux = xml.firstChild("id", "labelStyle");
561
                if (aux!= null) {
562
                        setLabelStyle((ILabelStyle) SymbologyFactory.
563
                                                        createStyleFromXML(aux, "labelStyle"));
564
                }
565
        }
566

    
567
        private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
568
        private int referenceSystem = CartographicSupportToolkit.DefaultReferenceSystem;
569

    
570
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
571
                Dimension sz = getSize();
572
                double width = sz.getWidth();
573
                double height = sz.getHeight();
574
                return CartographicSupportToolkit.
575
                        getCartographicLength(this,
576
                                                          Math.max(width, height),
577
                                                          viewPort,
578
                                                          dpi);
579
        }
580

    
581
        public int getReferenceSystem() {
582
                return referenceSystem;
583
        }
584

    
585
        public int getUnit() {
586
                return unit;
587
        }
588

    
589
        public void setCartographicSize(double cartographicSize, FShape shp) {
590
                Dimension sz = getSize();
591
                double width = sz.getWidth();
592
                double height = sz.getHeight();
593
                if (width >= height) {
594
                        scale = cartographicSize / width;
595
                } else {
596
                        scale = cartographicSize / height;
597
                }
598
        }
599

    
600
        public void setReferenceSystem(int referenceSystem) {
601
                this.referenceSystem = referenceSystem;
602
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
603
                        ((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
604
                }
605
        }
606

    
607
        public void setUnit(int unitIndex) {
608
                this.unit = unitIndex;
609
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
610
                        ((CartographicSupport) textSymbol).setUnit(unitIndex);
611
                }
612
        }
613

    
614
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
615
                setCartographicSize(getCartographicSize(
616
                                                                viewPort,
617
                                                                dpi,
618
                                                                shp),
619
                                                        shp);
620
                return 0;
621
        }
622

    
623
        public Rectangle getBounds() {
624
                Dimension cBounds = getSize();
625
                return new Rectangle(
626
                                0,
627
                                0,
628
                                (int) Math.round(cBounds.width*scale),
629
                                (int) Math.round(cBounds.height*scale));
630
        }
631

    
632
        public String getSQLQuery() {
633
                if (sqlQuery == null) sqlQuery = "";
634
                return sqlQuery;
635
        }
636

    
637
        public void setSQLQuery(String sqlQuery) {
638
                this.sqlQuery = sqlQuery;
639
        }
640

    
641
        public boolean isUseSqlQuery() {
642
                return useSqlQuery;
643
        }
644

    
645
        public void setUseSqlQuery(boolean useSqlQuery) {
646
                this.useSqlQuery = useSqlQuery;
647
        }
648
}