Statistics
| Revision:

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

History | View | Annotate | Download (14 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.Dimension;
108
import java.awt.Graphics2D;
109
import java.awt.Rectangle;
110
import java.awt.Shape;
111
import java.awt.geom.AffineTransform;
112
import java.awt.geom.Point2D;
113
import java.awt.geom.Rectangle2D;
114

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

    
130
/**
131
 *
132
 * LabelClass is the model of the label in the new simbology of gvSIG. In this
133
 * class is contained its definition, the expresion that defines the text which
134
 * is going to be showed, if it will be visible or not, the text symbol that is
135
 * going to paint the label and the style for its background.
136
 *
137
 * @author jaume dominguez faus - jaume.dominguez@iver.es
138
 */
139
public class LabelClass implements IPersistence, CartographicSupport {
140
        private String name;
141
        private ITextSymbol textSymbol;
142
        private String labelExpression;
143
        private boolean isVisible;
144
        private ILabelStyle labelStyle;
145
        private String[] texts;
146
        private int priority;
147
        private double scale = 1;
148
        private String sqlQuery;
149
        
150
        /**
151
         * Returns true if the label will be showed in the map
152
         *
153
         * @return isVisible boolean
154
         */
155
        public boolean isVisible() {
156
                return isVisible;
157
        }
158
        
159
        /**
160
         * Sets the visibility of the label in the map.
161
         *
162
         * @param isVisible boolean
163
         */
164
        public void setVisible(boolean isVisible) {
165
                this.isVisible = isVisible;
166
        }
167

    
168
        /**
169
         * Returns the expression that defines the text which will be showed in
170
         * the label
171
         *
172
         * @return labelExpression String
173
         */
174
        public String getLabelExpression() {
175
                return labelExpression;
176
        }
177

    
178
        /**
179
         * Stablishes the expresion that, when it is evaluated, returns the text
180
         * which will be showed by the label.
181
         *
182
         * @param labelExpression String
183
         */
184
        public void setLabelExpression(String labelExpression) {
185
                this.labelExpression = labelExpression;
186
        }
187

    
188
        /**
189
         * Returns the text symbol that is being used for the text(the font,
190
         * size,style,aligment)
191
         *
192
         * @return label ITextSymbol
193
         */
194
        public ITextSymbol getTextSymbol() {
195
                if (textSymbol == null) {
196
                        textSymbol = new SimpleTextSymbol();
197
                }
198
                return textSymbol;
199
        }
200
        
201
        private Dimension getSize() {
202
                if (labelStyle == null) {
203
                        getTextSymbol().setText(texts[0]);
204
                        Rectangle bounds = getTextSymbol().getBounds();
205
                        bounds.setLocation(
206
                                        (int) Math.round(bounds.getX()), 
207
                                        (int) Math.round(bounds.getY()+bounds.getHeight()));
208
                        return new Dimension(bounds.width, bounds.height);
209
                } else {
210
                        labelStyle.setTextFields(texts);
211
                        return labelStyle.getSize();
212
                }
213
        }
214
        /**
215
         * Stablishes the text symbol that is going to be used for the text(the
216
         * font,size,style,aligment)
217
         *
218
         * @param textSymbol ITextSymbol
219
         */
220
        public void setTextSymbol(ITextSymbol textSymbol) {
221
                this.textSymbol = textSymbol;
222
                if (textSymbol == null) {
223
                        this.textSymbol = new SimpleTextSymbol();
224
                }
225
                setReferenceSystem(referenceSystem);
226
                setUnit(unit);
227
        }
228
        
229
        /**
230
         * Stablishes the style for the label.
231
         *
232
         * @param labelStyle ILabelStyle
233
         */
234
        public void setLabelStyle(ILabelStyle labelStyle) {
235
                this.labelStyle = labelStyle;
236
        }
237
        
238
        /**
239
         * Returns the style of the label
240
         *
241
         */
242
        public ILabelStyle getLabelStyle() {
243
                return this.labelStyle;
244
        }
245
        
246
        /**
247
         * Returns the name of the label
248
         *
249
         */
250
        public String getName() {
251
                return name;
252
        }
253
        
254
        /**
255
         * Stablishes the name of the label
256
         * @param name
257
         */
258
        public void setName(String name) {
259
                this.name = name;
260
        }
261

    
262
        public String toString() {
263
                return name;
264
        }
265
        
266
        /**
267
         * Sets the text for the label
268
         *
269
         * @param texts String[]
270
         */
271
        public void setTexts(String[] texts) {
272
                this.texts = texts;
273
                
274
        }
275
        
276
        public void draw(Graphics2D graphics, LabelLocationMetrics llm, FShape shp) {
277
                if (scale == 0)
278
                        return;
279

    
280
                
281
                Dimension size = getSize();
282
                int width = (int) Math.round(size.getWidth()*scale);
283
                if (width  < 1)
284
                        return;
285
                
286
                int height = (int) Math.round(size.getHeight()*scale);
287
                if (height < 1)
288
                        return;
289
                
290
                Rectangle r = new Rectangle(0,0, width, height);
291
                FPoint2D anchor = new FPoint2D(llm.getAnchor());
292
                double xAnchor = anchor.getX();
293
                double yAnchor = anchor.getY();
294
                double theta = llm.getRotation();
295

    
296

    
297
                graphics.translate(xAnchor, yAnchor);
298
                graphics.rotate(theta);
299
                try {
300
                        drawInsideRectangle(graphics, r);
301
                } catch (SymbolDrawingException e) {
302
                        // TODO Auto-generated catch block
303
                        e.printStackTrace();
304
                }
305
                graphics.rotate(-theta);
306
                graphics.translate(-xAnchor, -yAnchor);
307
        }
308
        
309
        private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) {
310
                int x;
311
                int y;
312
                if (ratioViewPort > ratioLabel) {
313
                        // size is defined by the viewport height
314
                        y = (int) (r.height*xy[1]);
315
                        x = (int) ((0.5*r.width) - (0.5-xy[0])*(ratioLabel*r.height));
316
                } else {
317
                        // size is defined by the viewport width
318
                        x = (int) (r.width * xy[0]);
319
                        y = (int) ((0.5 * r.height) - (0.5-xy[1])*(r.width/ratioLabel));
320
                }
321
                xy[0] = x;
322
                xy[1] = y;
323
        }
324
        
325
        /**
326
         * Useful to render a Label with size inside little rectangles.
327
         *
328
         * @param graphics Graphics2D
329
         * @param bounds Rectangle
330
         * @throws SymbolDrawingException 
331
         */
332
        public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds) throws SymbolDrawingException {
333
                if (labelStyle != null) {
334
                        labelStyle.drawInsideRectangle(graphics, bounds);
335
                        Rectangle2D[] textBounds = labelStyle.getTextBounds();
336
                        Dimension labelSz = getSize();
337
                        final double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
338
                        final double ratioViewPort = bounds.getWidth() / bounds.getHeight();
339
                        final double[] xy = new double[2];
340
                                        
341

    
342
                        // draw the text fields
343
                        if (textBounds.length > 0) {
344
                                for (int i = 0; i < textBounds.length && i < texts.length; i++) {
345
                                        getTextSymbol().setText(texts[i]);
346
                                        Rectangle2D textFieldArea = textBounds[i];
347
                                        xy[0] = textFieldArea.getX();
348
                                        xy[1] = textFieldArea.getY();
349
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
350
                                        int x = (int) Math.round(xy[0]);
351
                                        int y = (int) Math.round(xy[1]);
352
                                        
353
                                        xy[0] = textFieldArea.getMaxX();
354
                                        xy[1] = textFieldArea.getMaxY();
355
                                        relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
356
                                        int width = (int) Math.round(xy[0]) -x;
357
                                        int height = (int) Math.round(xy[1] - y) ;
358

    
359
                                        Rectangle textRect = new Rectangle(x, y, width, height);
360
                                        Shape oldClip = graphics.getClip();
361
                                        graphics.setClip(textRect);
362
                                        getTextSymbol().drawInsideRectangle(graphics, null, textRect);
363
                                        graphics.setClip(oldClip);
364
                                }
365
                        }
366
                } else {
367
                        
368
                        if (texts != null && texts.length>0) 
369
                                getTextSymbol().setText(texts[0]);
370
                        getTextSymbol().draw(graphics, null, new FPoint2D(bounds.getX(), bounds.getY()), null);
371
                }
372
        }
373
        
374
        public int getPriority() {
375
                return priority;
376
        }
377
        
378
        public void setPriority(int priority) {
379
                this.priority = priority;
380
        }
381

    
382
        public FShape getShape(LabelLocationMetrics llm) {
383
                Point2D anchor = llm.getAnchor();
384
                FPoint2D p = new FPoint2D(anchor);
385
                double theta = llm.getRotation();
386

    
387
                // 2. calculate the container shape
388
                FShape returnedValue;
389
                Rectangle bounds = getBounds();
390
                
391
                AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
392
                at.concatenate(AffineTransform.getRotateInstance(theta));
393
                returnedValue = new FPolygon2D(new GeneralPathX(bounds));
394

    
395
                returnedValue.transform(at);
396
                return returnedValue;
397
        }
398

    
399
        public String getClassName() {
400
                return getClass().getName();
401
        }
402

    
403
        public XMLEntity getXMLEntity() {
404
                XMLEntity xml = new XMLEntity();
405
                xml.putProperty("className", getClassName());
406
                xml.putProperty("isVisible", isVisible);
407
                xml.putProperty("name", name);
408
                xml.putProperty("labelExpression", labelExpression);
409
                xml.putProperty("unit", getUnit());
410
                xml.putProperty("referenceSystem", getReferenceSystem());
411
                xml.putProperty("priority", getPriority());
412
                
413
                if (sqlQuery != null) {
414
                        xml.putProperty("sqlQuery", getSQLQuery());
415
                }
416
                
417
                if (labelStyle!=null) {
418
                        XMLEntity labelStyleXML = labelStyle.getXMLEntity();
419
                        labelStyleXML.putProperty("id", "labelStyle");
420
                        xml.addChild(labelStyleXML);
421
                }
422
                
423
                XMLEntity textSymXML = getTextSymbol().getXMLEntity();
424
                textSymXML.putProperty("id", "TextSymbol");
425
                xml.addChild(textSymXML);
426
                
427
                return xml;
428
        }
429

    
430
        public void setXMLEntity(XMLEntity xml) {
431
                isVisible = xml.getBooleanProperty("isVisible");
432
                name = xml.getStringProperty("name");
433
                labelExpression = xml.getStringProperty("labelExpression");
434
                setUnit(xml.getIntProperty("unit"));
435
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
436
                setTextSymbol(
437
                                (ITextSymbol) SymbologyFactory.
438
                                createSymbolFromXML(xml.firstChild("id", "TextSymbol"), null));
439
                
440
                if (xml.contains("sqlQuery")) {
441
                        setSQLQuery(xml.getStringProperty("sqlQuery"));
442
                }
443
                
444
                if (xml.contains("priority")) {
445
                        setPriority(xml.getIntProperty("priority"));
446
                }
447
                // labelStyle
448
                XMLEntity aux = xml.firstChild("id", "labelStyle");
449
                if (aux!= null) {
450
                        setLabelStyle((ILabelStyle) SymbologyFactory.
451
                                                        createStyleFromXML(aux, "labelStyle"));
452
                }
453
        }
454

    
455
        private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
456
        private int referenceSystem = CartographicSupportToolkit.DefaultReferenceSystem;
457
        
458
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
459
                Dimension sz = getSize();
460
                double width = sz.getWidth();
461
                double height = sz.getHeight();
462
                return CartographicSupportToolkit.
463
                        getCartographicLength(this,
464
                                                          Math.max(width, height),
465
                                                          viewPort,
466
                                                          dpi);
467
        }
468

    
469
        public int getReferenceSystem() {
470
                return referenceSystem;
471
        }
472

    
473
        public int getUnit() {
474
                return unit;
475
        }
476

    
477
        public void setCartographicSize(double cartographicSize, FShape shp) {
478
                Dimension sz = getSize();
479
                double width = sz.getWidth();
480
                double height = sz.getHeight();
481
                if (width >= height) {
482
                        scale = cartographicSize / width;
483
                } else {
484
                        scale = cartographicSize / height;
485
                }
486
        }
487

    
488
        public void setReferenceSystem(int referenceSystem) {
489
                this.referenceSystem = referenceSystem;
490
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
491
                        ((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
492
                }
493
        }
494

    
495
        public void setUnit(int unitIndex) {
496
                this.unit = unitIndex;
497
                if (textSymbol != null && textSymbol instanceof CartographicSupport) {
498
                        ((CartographicSupport) textSymbol).setUnit(unitIndex);
499
                }
500
        }
501

    
502
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
503
                setCartographicSize(getCartographicSize(
504
                                                                viewPort,
505
                                                                dpi,
506
                                                                shp),
507
                                                        shp);
508
                return 0;
509
        }
510

    
511
        public Rectangle getBounds() {
512
                Dimension cBounds = getSize();
513
                return new Rectangle(
514
                                0,
515
                                0,
516
                                (int) Math.round(cBounds.width*scale),
517
                                (int) Math.round(cBounds.height*scale));
518
        }
519

    
520
        public String getSQLQuery() {
521
                if (sqlQuery == null) sqlQuery = "";
522
                return sqlQuery;
523
        }
524

    
525
        public void setSQLQuery(String sqlQuery) {
526
                this.sqlQuery = sqlQuery;
527
        }
528
}