Statistics
| Revision:

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

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

    
83
import java.awt.Color;
84
import java.awt.Graphics2D;
85
import java.awt.Point;
86
import java.awt.Rectangle;
87
import java.awt.geom.AffineTransform;
88
import java.awt.geom.Point2D;
89
import java.awt.geom.Rectangle2D;
90

    
91
import com.iver.cit.gvsig.fmap.core.FPoint2D;
92
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
93
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
94
import com.iver.cit.gvsig.fmap.core.FShape;
95
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
96
import com.iver.cit.gvsig.fmap.core.IGeometry;
97
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
98
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
99
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
100

    
101
/**
102
 * @author jaume dominguez faus - jaume.dominguez@iver.es
103
 */
104
public final class LabelClass {
105
        private String name;
106

    
107
        private ITextSymbol label;
108

    
109
        private String labelExpression;
110

    
111
        private boolean isVisible;
112

    
113
        private ILabelStyle labelStyle;
114

    
115
        public boolean isVisible() {
116
                return isVisible;
117
        }
118

    
119
        public void setVisible(boolean isVisible) {
120
                this.isVisible = isVisible;
121
        }
122

    
123
        public String getLabelExpression() {
124
                return labelExpression;
125
        }
126

    
127
        public void setLabelExpression(String labelExpression) {
128
                this.labelExpression = labelExpression;
129
        }
130

    
131
        public ITextSymbol getLabelSymbol() {
132
                if (label == null) {
133
                        label = new SimpleTextSymbol();
134
                }
135
                return label;
136
        }
137

    
138
        public void setSymbol(ITextSymbol textSymbol) {
139
                label = textSymbol;
140
                if (label == null)
141
                        label = new SimpleTextSymbol();
142
        }
143

    
144
        public void setLabelStyle(ILabelStyle labelStyle) {
145
                this.labelStyle = labelStyle;
146
        }
147

    
148
        public ILabelStyle getLabelStyle() {
149
                return this.labelStyle;
150
        }
151

    
152
        public String getName() {
153
                return name;
154
        }
155

    
156
        public void setName(String name) {
157
                this.name = name;
158
        }
159

    
160
        public String toString() {
161
                return name;
162
        }
163

    
164
        public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds,
165
                        String[] texts) {
166
                if (labelStyle != null) {
167
                        // apply the offset to the markerPoint
168

    
169
                        graphics.setColor(Color.RED);
170
                        graphics.draw(bounds);
171
                        labelStyle.drawInsideRectangle(graphics, bounds);
172

    
173
                        for (int i = 0; i < texts.length; i++) {
174
                                getLabelSymbol().setText(texts[i]);
175

    
176
                                // textArea is the area for such text field in relative units
177
                                Rectangle2D textArea = labelStyle.getTextBounds()[i];
178

    
179
                                // let's build the text rectangle in screen units
180

    
181
                                //TODO segueix calculant-se mal
182
                                int x = (int) (bounds.getX() + textArea.getWidth()*textArea.getMinX());
183
                                int y = (int) (bounds.getY() + textArea.getHeight()*textArea.getMinY());
184
                                int width = (int) (bounds.getWidth() - (1-textArea.getWidth()));
185
                                int height = (int) (bounds.getHeight() - (1-textArea.getHeight()));
186

    
187
                                Rectangle textRect = new Rectangle(x, y, width, height);
188
                                getLabelSymbol().drawInsideRectangle(graphics, null, textRect);
189
                        }
190
                } else {
191
                        getLabelSymbol().setText(texts[0]);
192
                        getLabelSymbol().drawInsideRectangle(graphics, null, bounds);
193
                }
194
        }
195

    
196
        public FShape getShape(Graphics2D g, /* AffineTransform at, */IGeometry geom) {
197
                if (labelStyle == null) {
198
                        // shape is defined by the symbol
199
                        FShape shp = null;
200

    
201
                        switch (geom.getGeometryType()) {
202
                        case FShape.POINT:
203
                                shp = new FPoint2D((Point) geom.getInternalShape());
204
                                break;
205
                        case FShape.LINE:
206
                                shp = new FPolyline2D(new GeneralPathX(geom.getInternalShape()));
207
                                break;
208
                        case FShape.POLYGON:
209
                                shp = new FPolygon2D(new GeneralPathX(geom.getInternalShape()));
210
                                break;
211
                        }
212
                        return getLabelSymbol().getTextWrappingShape(g, shp);
213
                } else {
214
                        // shape is defined by the style
215
                        // maybe this is incorrect but for the moment we'll go with it
216
                        Rectangle bounds = labelStyle.getBounds();
217
                        return new FPolygon2D(new GeneralPathX(bounds));
218
                }
219
        }
220
}