Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / styling / labeling / LabelClass.java @ 23917

History | View | Annotate | Download (16.4 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
        /**
311
         * <p>
312
         * LabelLocationMetrics, contains the anchor point, rotation, and some
313
         * other geometric calculations computed by the PlacementManager.
314
         * </p>
315
         *
316
         * <p>
317
         * The shp argument is passed as an accessory for subclasses of this
318
         * class in case they need futher geometric calculations
319
         * </p>
320
         * @param graphics, graphics to use to paint the label.
321
         * @param llm, concrete settings of the placement of this layer
322
         * @param shp, the Shape over whose the label is painted
323
         */
324
        public void draw(Graphics2D graphics, LabelLocationMetrics llm, FShape shp) {
325
                if (scale == 0)
326
                        return;
327
                if (!isVisible)
328
                        return;
329
                Dimension size = getSize();
330
                int width = (int) Math.round(size.getWidth()*scale);
331
                if (width  < 1)
332
                        return;
333

    
334
                int height = (int) Math.round(size.getHeight()*scale);
335
                if (height < 1)
336
                        return;
337

    
338
                Rectangle r = new Rectangle(0,0, width, height);
339
                FPoint2D anchor = new FPoint2D(llm.getAnchor());
340
                double xAnchor = anchor.getX();
341
                double yAnchor = anchor.getY();
342
                double theta = llm.getRotation();
343

    
344

    
345
                graphics.translate(xAnchor, yAnchor);
346
                graphics.rotate(theta);
347
                try {
348
                        drawInsideRectangle(graphics, r);
349
                } catch (SymbolDrawingException e) {
350
                        // TODO Auto-generated catch block
351
                        e.printStackTrace();
352
                }
353
                graphics.rotate(-theta);
354
                graphics.translate(-xAnchor, -yAnchor);
355
        }
356

    
357

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

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

    
390

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

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

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

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

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

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

    
431
        public FShape getShape(LabelLocationMetrics llm) {
432
                Point2D anchor = llm.getAnchor();
433
                FPoint2D p = new FPoint2D(anchor);
434
                double theta = llm.getRotation();
435

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

    
440
                AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
441
                at.concatenate(AffineTransform.getRotateInstance(theta));
442
                returnedValue = new FPolygon2D(new GeneralPathX(bounds));
443

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

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

    
452
        public XMLEntity getXMLEntity() {
453
                XMLEntity xml = new XMLEntity();
454
                xml.putProperty("className", getClassName());
455
                xml.putProperty("isVisible", isVisible);
456
                xml.putProperty("name", name);
457
                if(labelExpressions != null)
458
                        xml.putProperty("labelExpressions", labelExpressions);
459
                xml.putProperty("unit", getUnit());
460
                xml.putProperty("referenceSystem", getReferenceSystem());
461
                xml.putProperty("priority", getPriority());
462
                xml.putProperty("useSqlQuery",isUseSqlQuery());
463

    
464
                if (texts != null) {
465
                        xml.putProperty("texts", texts);
466
                }
467
                if (sqlQuery != null) {
468
                        xml.putProperty("sqlQuery", getSQLQuery());
469
                }
470

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

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

    
481
                return xml;
482
        }
483

    
484
        public void setXMLEntity(XMLEntity xml) {
485
                isVisible = xml.getBooleanProperty("isVisible");
486
                name = xml.getStringProperty("name");
487
                if(xml.contains("labelExpressions"))
488
                        labelExpressions = xml.getStringArrayProperty("labelExpressions");
489
                setUnit(xml.getIntProperty("unit"));
490
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
491
                setTextSymbol(
492
                                (ITextSymbol) SymbologyFactory.
493
                                createSymbolFromXML(xml.firstChild("id", "TextSymbol"), null));
494

    
495
                if(xml.contains("useSqlQuery")){
496
                        setUseSqlQuery(xml.getBooleanProperty("useSqlQuery"));
497
                }
498

    
499
                if (xml.contains("texts")) {
500
                        setTexts(xml.getStringArrayProperty("texts"));
501
                }
502
                if (xml.contains("sqlQuery")) {
503
                        setSQLQuery(xml.getStringProperty("sqlQuery"));
504
                }
505

    
506
                if (xml.contains("priority")) {
507
                        setPriority(xml.getIntProperty("priority"));
508
                }
509
                // labelStyle
510
                XMLEntity aux = xml.firstChild("id", "labelStyle");
511
                if (aux!= null) {
512
                        setLabelStyle((ILabelStyle) SymbologyFactory.
513
                                                        createStyleFromXML(aux, "labelStyle"));
514
                }
515
        }
516

    
517
        private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
518
        private int referenceSystem = CartographicSupportToolkit.DefaultReferenceSystem;
519

    
520
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
521
                Dimension sz = getSize();
522
                double width = sz.getWidth();
523
                double height = sz.getHeight();
524
                return CartographicSupportToolkit.
525
                        getCartographicLength(this,
526
                                                          Math.max(width, height),
527
                                                          viewPort,
528
                                                          dpi);
529
        }
530

    
531
        public int getReferenceSystem() {
532
                return referenceSystem;
533
        }
534

    
535
        public int getUnit() {
536
                return unit;
537
        }
538

    
539
        public void setCartographicSize(double cartographicSize, FShape shp) {
540
                Dimension sz = getSize();
541
                double width = sz.getWidth();
542
                double height = sz.getHeight();
543
                if (width >= height) {
544
                        scale = cartographicSize / width;
545
                } else {
546
                        scale = cartographicSize / height;
547
                }
548
        }
549

    
550
        public void setReferenceSystem(int referenceSystem) {
551
                this.referenceSystem = referenceSystem;
552
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
553
                        ((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
554
                }
555
        }
556

    
557
        public void setUnit(int unitIndex) {
558
                this.unit = unitIndex;
559
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
560
                        ((CartographicSupport) textSymbol).setUnit(unitIndex);
561
                }
562
        }
563

    
564
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
565
                setCartographicSize(getCartographicSize(
566
                                                                viewPort,
567
                                                                dpi,
568
                                                                shp),
569
                                                        shp);
570
                return 0;
571
        }
572

    
573
        public Rectangle getBounds() {
574
                Dimension cBounds = getSize();
575
                return new Rectangle(
576
                                0,
577
                                0,
578
                                (int) Math.round(cBounds.width*scale),
579
                                (int) Math.round(cBounds.height*scale));
580
        }
581

    
582
        public String getSQLQuery() {
583
                if (sqlQuery == null) sqlQuery = "";
584
                return sqlQuery;
585
        }
586

    
587
        public void setSQLQuery(String sqlQuery) {
588
                this.sqlQuery = sqlQuery;
589
        }
590

    
591
        public boolean isUseSqlQuery() {
592
                return useSqlQuery;
593
        }
594

    
595
        public void setUseSqlQuery(boolean useSqlQuery) {
596
                this.useSqlQuery = useSqlQuery;
597
        }
598
}