Statistics
| Revision:

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

History | View | Annotate | Download (8.05 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 11372 2007-04-26 11:41:00Z jaume $
45
 * $Log$
46
 * Revision 1.9  2007-04-26 11:41:00  jaume
47
 * attempting to let defining size in world units
48
 *
49
 * Revision 1.8  2007/04/18 15:35:11  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.7  2007/04/12 14:28:43  jaume
53
 * basic labeling support for lines
54
 *
55
 * Revision 1.6  2007/04/11 16:01:08  jaume
56
 * maybe a label placer refactor
57
 *
58
 * Revision 1.5  2007/04/10 16:34:01  jaume
59
 * towards a styled labeling
60
 *
61
 * Revision 1.4  2007/04/05 16:07:14  jaume
62
 * Styled labeling stuff
63
 *
64
 * Revision 1.3  2007/04/02 16:34:56  jaume
65
 * Styled labeling (start commiting)
66
 *
67
 * Revision 1.2  2007/03/09 08:33:43  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.1.2.6  2007/02/15 16:23:44  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.1.2.5  2007/02/09 07:47:05  jaume
74
 * Isymbol moved
75
 *
76
 * Revision 1.1.2.4  2007/02/02 16:21:24  jaume
77
 * start commiting labeling stuff
78
 *
79
 * Revision 1.1.2.3  2007/02/01 17:46:49  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.1.2.2  2007/02/01 11:42:47  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.1.2.1  2007/01/30 18:10:45  jaume
86
 * start commiting labeling stuff
87
 *
88
 *
89
 */
90
package com.iver.cit.gvsig.fmap.rendering.styling;
91

    
92
import java.awt.Graphics2D;
93
import java.awt.Rectangle;
94
import java.awt.geom.AffineTransform;
95
import java.awt.geom.PathIterator;
96
import java.awt.geom.Rectangle2D;
97

    
98
import com.iver.cit.gvsig.fmap.core.FPoint2D;
99
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
100
import com.iver.cit.gvsig.fmap.core.FShape;
101
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
102
import com.iver.cit.gvsig.fmap.core.IGeometry;
103
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
104
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
105
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
106

    
107
/**
108
 * @author jaume dominguez faus - jaume.dominguez@iver.es
109
 */
110
public final class LabelClass {
111
        private String name;
112

    
113
        private ITextSymbol label;
114

    
115
        private String labelExpression;
116

    
117
        private boolean isVisible;
118

    
119
        private ILabelStyle labelStyle;
120

    
121
        private String[] texts;
122

    
123
        public boolean isVisible() {
124
                return isVisible;
125
        }
126

    
127
        public void setVisible(boolean isVisible) {
128
                this.isVisible = isVisible;
129
        }
130

    
131
        public String getLabelExpression() {
132
                return labelExpression;
133
        }
134

    
135
        public void setLabelExpression(String labelExpression) {
136
                this.labelExpression = labelExpression;
137
        }
138

    
139
        public ITextSymbol getLabelSymbol() {
140
                if (label == null) {
141
                        label = new SimpleTextSymbol();
142
                }
143
                return label;
144
        }
145

    
146
        public void setSymbol(ITextSymbol textSymbol) {
147
                label = textSymbol;
148
                if (label == null)
149
                        label = new SimpleTextSymbol();
150
        }
151

    
152
        public void setLabelStyle(ILabelStyle labelStyle) {
153
                this.labelStyle = labelStyle;
154
        }
155

    
156
        public ILabelStyle getLabelStyle() {
157
                return this.labelStyle;
158
        }
159

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

    
164
        public void setName(String name) {
165
                this.name = name;
166
        }
167

    
168
        public String toString() {
169
                return name;
170
        }
171

    
172
        public void setTexts(String[] texts) {
173
                this.texts = texts;
174
        }
175

    
176
        public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds) {
177
                if (labelStyle != null) {
178
                        // apply the offset to the markerPoint
179
                        System.out.println(bounds);
180
                        labelStyle.drawInsideRectangle(graphics, bounds);
181

    
182
                        for (int i = 0; i < texts.length; i++) {
183
                                getLabelSymbol().setText(texts[i]);
184

    
185
                                // textArea is the area for such text field in relative units
186
                                Rectangle2D textArea = labelStyle.getTextBounds()[i];
187

    
188
                                // let's build the text rectangle in screen units
189
                                double a = textArea.getX();
190
                                double b = textArea.getWidth();
191
                                double c = textArea.getY();
192
                                double d = textArea.getHeight();
193
                                System.out.println("INSIDE: "+bounds);
194
                                System.err        .println(a+","+b+","+c+","+d);
195

    
196
                                int x = (int) (bounds.getX() + a*bounds.getWidth());
197
                                int y = (int) (bounds.getY() + c*bounds.getHeight());
198
                                int width = (int) Math.abs((b+a) * bounds.getWidth());
199
                                int height = (int) Math.abs((d+c) * bounds.getHeight());
200

    
201
                                /*
202
                                 * Matem?ticament haurien d'?sser estes expressions, per? no entenc perqu? sume
203
                                 * els coeficients em compte de restar-los
204
                                 *        int width = (int) ((b-a) * bounds.getWidth());
205
                                 *        int height = (int) ((d-c) * bounds.getHeight());
206
                                 */
207
                                //TODO segueix calculant-se mal
208
//                                int x = (int) (bounds.getX() + textArea.getWidth()*textArea.getMinX());
209
//                                int y = (int) (bounds.getY() + textArea.getHeight()*textArea.getMinY());
210
//                                int width = (int) (bounds.getWidth() - (1-textArea.getWidth()));
211
//                                int height = (int) (bounds.getHeight() - (1-textArea.getHeight()));
212

    
213
                                Rectangle textRect = new Rectangle(x, y, width, height);
214
                                System.out.println("TEXT_RECT:"+textRect);
215
                                getLabelSymbol().drawInsideRectangle(graphics, null, textRect);
216
                        }
217
                } else {
218
                        getLabelSymbol().setText(texts[0]);
219
                        getLabelSymbol().draw(graphics, null, new FPoint2D(bounds.getX(), bounds.getY()));
220
                }
221
        }
222

    
223
        public FShape getShape(Graphics2D g, AffineTransform at, IGeometry geom) {
224
                // 1. locate the geometry starting point
225
                double[] coords = new double[6];
226
                PathIterator pi = geom.getPathIterator(at); // at?? or whatever?
227
                pi.currentSegment(coords);
228
                FPoint2D p = new FPoint2D(coords[0], coords[1]);
229

    
230
                p.transform(at);
231

    
232
                // 2. calculate the container shape
233
                if (labelStyle == null) {
234
                        // shape is defined by the symbol
235
                        ITextSymbol sym = getLabelSymbol();
236
                        if (texts != null) {
237
                                String lText = texts[0];
238
                                for (int i = 1; i < texts.length; i++) {
239
                                        lText += " "+ texts[i];
240
                                }
241
                                sym.setText(lText);
242
                        }
243
                        return getLabelSymbol().getTextWrappingShape(g, p);
244
                } else {
245
                        // shape is defined by the style
246
                        // maybe this is incorrect but for the moment we'll go with it
247
                        Rectangle bounds = labelStyle.getBounds();
248
                        bounds.translate((int) p.getX() /* TODO : + aGap */, (int) p.getY()-bounds.height /* TODO :- aGap */);
249
                        return new FPolygon2D(new GeneralPathX(bounds));
250
                }
251
        }
252

    
253

    
254
//        public FShape getShape(Graphics2D g, AffineTransform at, IGeometry geom) {
255
//                if (labelStyle == null) {
256
//                        // shape is defined by the symbol
257
//                        FShape shp = null;
258
//
259
//                        switch (geom.getGeometryType()) {
260
//                        case FShape.POINT:
261
//                                shp = (FShape) geom.getInternalShape();
262
////                                shp = new FPoint2D((Point2D) geom.getInternalShape());
263
//                                break;
264
//                        case FShape.LINE:
265
//                                shp = new FPolyline2D(new GeneralPathX(geom.getInternalShape()));
266
//                                break;
267
//                        case FShape.POLYGON:
268
//                                shp = new FPolygon2D(new GeneralPathX(geom.getInternalShape()));
269
//                                break;
270
//                        }
271
//                        shp.transform(at);
272
//                        System.err.println(shp);
273
//                        return getLabelSymbol().getTextWrappingShape(g, shp);
274
//                } else {
275
//                        // shape is defined by the style
276
//                        // maybe this is incorrect but for the moment we'll go with it
277
//                        Rectangle bounds = labelStyle.getBounds();
278
//                        FPoint2D p = new FPoint2D(geom.getBounds().getX(), geom.getBounds().getY());
279
//                        p.transform(at);
280
//                        bounds.translate((int) p.getX() /* TODO : + aGap */, (int) p.getY()-bounds.height /* TODO :- aGap */);
281
//                        return new FPolygon2D(new GeneralPathX(bounds));
282
//                }
283
//        }
284

    
285
}