Statistics
| Revision:

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

History | View | Annotate | Download (5.12 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 11073 2007-04-05 16:08:34Z jaume $
45
* $Log$
46
* Revision 1.4  2007-04-05 16:07:14  jaume
47
* Styled labeling stuff
48
*
49
* Revision 1.3  2007/04/02 16:34:56  jaume
50
* Styled labeling (start commiting)
51
*
52
* Revision 1.2  2007/03/09 08:33:43  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.6  2007/02/15 16:23:44  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.1.2.5  2007/02/09 07:47:05  jaume
59
* Isymbol moved
60
*
61
* Revision 1.1.2.4  2007/02/02 16:21:24  jaume
62
* start commiting labeling stuff
63
*
64
* Revision 1.1.2.3  2007/02/01 17:46:49  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.2  2007/02/01 11:42:47  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.1.2.1  2007/01/30 18:10:45  jaume
71
* start commiting labeling stuff
72
*
73
*
74
*/
75
package com.iver.cit.gvsig.fmap.rendering.styling;
76

    
77
import java.awt.Graphics2D;
78
import java.awt.Point;
79
import java.awt.Rectangle;
80
import java.awt.geom.Rectangle2D;
81

    
82
import com.iver.cit.gvsig.fmap.core.FPoint2D;
83
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
84
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
85
import com.iver.cit.gvsig.fmap.core.FShape;
86
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
87
import com.iver.cit.gvsig.fmap.core.IGeometry;
88
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
89
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
90
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
91

    
92

    
93
/**
94
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
95
 */
96
public final class LabelClass {
97
        private String name;
98
        private ITextSymbol label;
99
        private String labelExpression;
100
        private boolean isVisible;
101
        private ILabelStyle labelStyle;
102

    
103
        public boolean isVisible() {
104
                return isVisible;
105
        }
106

    
107
        public void setVisible(boolean isVisible) {
108
                this.isVisible = isVisible;
109
        }
110

    
111
        public String getLabelExpression() {
112
                return labelExpression;
113
        }
114

    
115
        public void setLabelExpression(String labelExpression) {
116
                this.labelExpression = labelExpression;
117
        }
118

    
119
        public ITextSymbol getLabelSymbol() {
120
                if (label == null) {
121
                        label = new SimpleTextSymbol();
122
                }
123
                return label;
124
        }
125

    
126
        public void setSymbol(ITextSymbol textSymbol) {
127
                label =  textSymbol;
128
                if (label == null)
129
                        label = new SimpleTextSymbol();
130
        }
131

    
132
        public void setLabelStyle(ILabelStyle labelStyle) {
133
                this.labelStyle = labelStyle;
134
        }
135

    
136
        public ILabelStyle getLabelStyle() {
137
                return this.labelStyle;
138
        }
139

    
140
        public String getName() {
141
                return name;
142
        }
143

    
144
        public void setName(String name) {
145
                this.name = name;
146
        }
147

    
148
        public String toString() {
149
                return name;
150
        }
151

    
152
        public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds, String[] texts) {
153
                bounds.y = bounds.y - bounds.height;
154
                if (labelStyle != null) {
155
                        labelStyle.drawInsideRectangle(graphics, bounds);
156
                        for (int i = 0; i < texts.length; i++) {
157
                                getLabelSymbol().setText(texts[i]);
158
                                Rectangle2D textArea = labelStyle.getTextBounds()[i];
159
                                Rectangle textRect = new Rectangle(
160
                                                (int) (bounds.x * textArea.getX()),
161
                                                (int) (bounds.y * textArea.getY()),
162
                                                (int) (bounds.width * textArea.getWidth()),
163
                                                (int) (bounds.height * textArea.getHeight())
164
                                                );
165

    
166
                                getLabelSymbol().drawInsideRectangle(graphics, null, textRect);
167
                        }
168
                } else {
169
                        getLabelSymbol().setText(texts[0]);
170
                        getLabelSymbol().drawInsideRectangle(graphics, null, bounds);
171
                }
172
        }
173

    
174
        public FShape getShape(Graphics2D g, /*AffineTransform at,*/ IGeometry geom) {
175
                if (labelStyle == null) {
176
                        // shape is defined by the symbol
177
                        FShape shp = null;
178

    
179
                        switch (geom.getGeometryType()) {
180
                        case FShape.POINT:
181
                                shp = new FPoint2D((Point) geom.getInternalShape());
182
                                break;
183
                        case FShape.LINE:
184
                                shp = new FPolyline2D(new GeneralPathX(geom.getInternalShape()));
185
                                break;
186
                        case FShape.POLYGON:
187
                                shp = new FPolygon2D(new GeneralPathX(geom.getInternalShape()));
188
                                break;
189
                        }
190
                        return getLabelSymbol().getTextWrappingShape(g, shp);
191
                } else {
192
                        // shape is defined by the style
193
                        // maybe this is incorrect but for the moment we'll go with it
194
                        Rectangle bounds = labelStyle.getBounds();
195
                        return new FPolygon2D(new GeneralPathX(bounds));
196
                }
197
        }
198
}