Statistics
| Revision:

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

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

    
80
import java.awt.Graphics2D;
81
import java.awt.Point;
82
import java.awt.Rectangle;
83
import java.awt.geom.AffineTransform;
84
import java.awt.geom.Point2D;
85
import java.awt.geom.Rectangle2D;
86

    
87
import com.iver.cit.gvsig.fmap.core.FPoint2D;
88
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
89
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
90
import com.iver.cit.gvsig.fmap.core.FShape;
91
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
92
import com.iver.cit.gvsig.fmap.core.IGeometry;
93
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
94
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
95
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
96

    
97
/**
98
 * @author jaume dominguez faus - jaume.dominguez@iver.es
99
 */
100
public final class LabelClass {
101
        private String name;
102

    
103
        private ITextSymbol label;
104

    
105
        private String labelExpression;
106

    
107
        private boolean isVisible;
108

    
109
        private ILabelStyle labelStyle;
110

    
111
        public boolean isVisible() {
112
                return isVisible;
113
        }
114

    
115
        public void setVisible(boolean isVisible) {
116
                this.isVisible = isVisible;
117
        }
118

    
119
        public String getLabelExpression() {
120
                return labelExpression;
121
        }
122

    
123
        public void setLabelExpression(String labelExpression) {
124
                this.labelExpression = labelExpression;
125
        }
126

    
127
        public ITextSymbol getLabelSymbol() {
128
                if (label == null) {
129
                        label = new SimpleTextSymbol();
130
                }
131
                return label;
132
        }
133

    
134
        public void setSymbol(ITextSymbol textSymbol) {
135
                label = textSymbol;
136
                if (label == null)
137
                        label = new SimpleTextSymbol();
138
        }
139

    
140
        public void setLabelStyle(ILabelStyle labelStyle) {
141
                this.labelStyle = labelStyle;
142
        }
143

    
144
        public ILabelStyle getLabelStyle() {
145
                return this.labelStyle;
146
        }
147

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

    
152
        public void setName(String name) {
153
                this.name = name;
154
        }
155

    
156
        public String toString() {
157
                return name;
158
        }
159

    
160
        public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds,
161
                        String[] texts) {
162
                if (labelStyle != null) {
163
                        labelStyle.drawInsideRectangle(graphics, bounds);
164
                        Point2D offset = new Point2D.Double(bounds.getMinX(), bounds
165
                                        .getMinY());
166
                        graphics.translate(offset.getX(), offset.getY());
167
                        for (int i = 0; i < texts.length; i++) {
168
                                getLabelSymbol().setText(texts[i]);
169
                                Rectangle2D textArea = labelStyle.getTextBounds()[i];
170

    
171
                                Rectangle textRect = new Rectangle((int) (bounds.x * textArea
172
                                                .getX()), (int) (bounds.y * textArea.getY()),
173
                                                (int) (bounds.width * textArea.getWidth()),
174
                                                (int) (bounds.height * textArea.getHeight()));
175

    
176
                                getLabelSymbol().drawInsideRectangle(graphics, null, textRect);
177
                        }
178
                        graphics.translate(-bounds.getX(), -bounds.getY());
179
                } else {
180
                        getLabelSymbol().setText(texts[0]);
181
                        getLabelSymbol().drawInsideRectangle(graphics, null, bounds);
182
                }
183
        }
184

    
185
        public FShape getShape(Graphics2D g, /* AffineTransform at, */IGeometry geom) {
186
                if (labelStyle == null) {
187
                        // shape is defined by the symbol
188
                        FShape shp = null;
189

    
190
                        switch (geom.getGeometryType()) {
191
                        case FShape.POINT:
192
                                shp = new FPoint2D((Point) geom.getInternalShape());
193
                                break;
194
                        case FShape.LINE:
195
                                shp = new FPolyline2D(new GeneralPathX(geom.getInternalShape()));
196
                                break;
197
                        case FShape.POLYGON:
198
                                shp = new FPolygon2D(new GeneralPathX(geom.getInternalShape()));
199
                                break;
200
                        }
201
                        return getLabelSymbol().getTextWrappingShape(g, shp);
202
                } else {
203
                        // shape is defined by the style
204
                        // maybe this is incorrect but for the moment we'll go with it
205
                        Rectangle bounds = labelStyle.getBounds();
206
                        return new FPolygon2D(new GeneralPathX(bounds));
207
                }
208
        }
209
}