Statistics
| Revision:

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

History | View | Annotate | Download (17.6 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
        private boolean changing= false;
154

    
155

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

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

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

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

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

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

    
204

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

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

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

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

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

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

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

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

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

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

    
310
        }
311

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

    
321

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

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

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

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

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

    
365

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

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

    
384

    
385

    
386

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

    
402
                graphics.rotate(-theta);
403
                graphics.translate(-xAnchor, -yAnchor);
404

    
405
        }
406

    
407

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

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

    
440

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

    
452
                                        xy[0] = textFieldArea.getMaxX();
453
                                        xy[1] = textFieldArea.getMaxY();
454
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
455
                                        int width = (int) Math.round(xy[0]) -x;
456
                                        int height = (int) Math.round(xy[1] - y) ;
457

    
458
                                        Rectangle textRect = new Rectangle(x, y, width, height);
459
                                        Shape oldClip = graphics.getClip();
460
                                        graphics.setClip(textRect);
461
                                        getTextSymbol().drawInsideRectangle(graphics, null, textRect, null);
462
                                        graphics.setClip(oldClip);
463
                                }
464
                        }
465
                } else {
466

    
467
                        if (texts != null && texts.length>0)
468
                                getTextSymbol().setText(texts[0]);
469
                        getTextSymbol().drawInsideRectangle(graphics, null, bounds, null);
470
                }
471
        }
472

    
473
        public int getPriority() {
474
                return priority;
475
        }
476

    
477
        public void setPriority(int priority) {
478
                this.priority = priority;
479
        }
480

    
481
        public FShape getShape(LabelLocationMetrics llm) {
482
                if (llm==null)
483
                        return null;
484
                Point2D anchor = llm.getAnchor();
485
                FPoint2D p = new FPoint2D(anchor);
486
                double theta = llm.getRotation();
487

    
488
                // 2. calculate the container shape
489
                FShape returnedValue;
490
                Rectangle bounds = getBounds();
491

    
492
                AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
493
                at.concatenate(AffineTransform.getRotateInstance(theta));
494

    
495
                returnedValue = new FPolygon2D(new GeneralPathX(bounds));
496

    
497
                returnedValue.transform(at);
498
                return returnedValue;
499
        }
500

    
501

    
502
        public String getClassName() {
503
                return getClass().getName();
504
        }
505

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

    
518
                if (texts != null) {
519
                        xml.putProperty("texts", texts);
520
                }
521
                if (sqlQuery != null) {
522
                        xml.putProperty("sqlQuery", getSQLQuery());
523
                }
524

    
525
                if (labelStyle!=null) {
526
                        XMLEntity labelStyleXML = labelStyle.getXMLEntity();
527
                        labelStyleXML.putProperty("id", "labelStyle");
528
                        xml.addChild(labelStyleXML);
529
                }
530

    
531
                XMLEntity textSymXML = getTextSymbol().getXMLEntity();
532
                textSymXML.putProperty("id", "TextSymbol");
533
                xml.addChild(textSymXML);
534

    
535
                return xml;
536
        }
537

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

    
549
                if(xml.contains("useSqlQuery")){
550
                        setUseSqlQuery(xml.getBooleanProperty("useSqlQuery"));
551
                }
552

    
553
                if (xml.contains("texts")) {
554
                        setTexts(xml.getStringArrayProperty("texts"));
555
                }
556
                if (xml.contains("sqlQuery")) {
557
                        setSQLQuery(xml.getStringProperty("sqlQuery"));
558
                }
559

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

    
571
        private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
572
        private int referenceSystem = CartographicSupportToolkit.DefaultReferenceSystem;
573

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

    
585
        public int getReferenceSystem() {
586
                return referenceSystem;
587
        }
588

    
589
        public int getUnit() {
590
                return unit;
591
        }
592

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

    
604
        public void setReferenceSystem(int referenceSystem) {
605
                this.referenceSystem = referenceSystem;
606
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
607
                        ((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
608
                }
609
        }
610

    
611
        public void setUnit(int unitIndex) {
612
                this.unit = unitIndex;
613
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
614
                        ((CartographicSupport) textSymbol).setUnit(unitIndex);
615
                }
616
        }
617

    
618
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
619
                setCartographicSize(getCartographicSize(
620
                                                                viewPort,
621
                                                                dpi,
622
                                                                shp),
623
                                                        shp);
624
                return 0;
625
        }
626

    
627
        public Rectangle getBounds() {
628
                Dimension cBounds = getSize();
629
                return new Rectangle(
630
                                0,
631
                                0,
632
                                (int) Math.round(cBounds.width*scale),
633
                                (int) Math.round(cBounds.height*scale));
634
        }
635

    
636
        public String getSQLQuery() {
637
                if (sqlQuery == null) sqlQuery = "";
638
                return sqlQuery;
639
        }
640

    
641
        public void setSQLQuery(String sqlQuery) {
642
                this.sqlQuery = sqlQuery;
643
        }
644

    
645
        public boolean isUseSqlQuery() {
646
                return useSqlQuery;
647
        }
648

    
649
        public void setUseSqlQuery(boolean useSqlQuery) {
650
                this.useSqlQuery = useSqlQuery;
651
        }
652
}