Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SimpleLabelStyle.java @ 19509

History | View | Annotate | Download (10.3 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: SimpleLabelStyle.java 13953 2007-09-21 12:26:04Z jaume $
45
* $Log$
46
* Revision 1.18  2007-09-21 12:22:36  jaume
47
* cancellation support extended down to the IGeometry and ISymbol level
48
*
49
* Revision 1.17  2007/09/17 09:32:52  jaume
50
* removed system.out....
51
*
52
* Revision 1.16  2007/08/21 09:30:42  jvidal
53
* javadoc
54
*
55
* Revision 1.15  2007/05/08 08:47:39  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.14  2007/04/19 14:21:30  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.12  2007/04/18 15:35:11  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.11  2007/04/12 14:28:43  jaume
65
* basic labeling support for lines
66
*
67
* Revision 1.10  2007/04/11 16:01:34  jaume
68
* try to fit the text in the correct place
69
*
70
* Revision 1.9  2007/04/10 16:34:01  jaume
71
* towards a styled labeling
72
*
73
* Revision 1.8  2007/04/05 16:07:14  jaume
74
* Styled labeling stuff
75
*
76
* Revision 1.7  2007/04/04 15:42:03  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.6  2007/04/04 15:41:05  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.5  2007/04/02 16:34:56  jaume
83
* Styled labeling (start commiting)
84
*
85
* Revision 1.4  2007/04/02 00:10:04  jaume
86
* *** empty log message ***
87
*
88
* Revision 1.3  2007/03/29 16:02:01  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.2  2007/03/09 11:20:56  jaume
92
* Advanced symbology (start committing)
93
*
94
* Revision 1.1.2.2  2007/02/15 16:23:44  jaume
95
* *** empty log message ***
96
*
97
* Revision 1.1.2.1  2007/02/09 07:47:04  jaume
98
* Isymbol moved
99
*
100
*
101
*/
102
package com.iver.cit.gvsig.fmap.core.styles;
103

    
104
import java.awt.Color;
105
import java.awt.Graphics2D;
106
import java.awt.Rectangle;
107
import java.awt.geom.AffineTransform;
108
import java.awt.geom.Point2D;
109
import java.awt.geom.Rectangle2D;
110
import java.util.ArrayList;
111

    
112
import com.iver.cit.gvsig.fmap.Messages;
113
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
114
import com.iver.cit.gvsig.fmap.core.FShape;
115
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
116
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
117
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
118
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
119
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
120
import com.iver.utiles.XMLEntity;
121

    
122

    
123
/**
124
 * Implements a style for the creation of simple labels.
125
 *
126
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
127
 */
128
public class SimpleLabelStyle extends AbstractStyle implements ILabelStyle {
129
        private String text;
130
        private Point2D markerPoint = new Point2D.Double();
131
        // conertir a?? a Rectangle2D[] ja que pot arribar a gastar-se massivament
132
        // en el pintat
133
        private ArrayList textFieldAreas = new ArrayList();
134
        private BackgroundFileStyle background;
135
        private double width = -1;
136
        private double height = -1;
137
        private double unitScaleFactor = 0;
138

    
139
        public int getFieldCount() {
140
                return 1;
141
        }
142

    
143
        public void setTextFields(String[] texts) {
144
                StringBuffer sb = new StringBuffer();
145
                for (int i = 0; i < texts.length; i++) {
146
                        sb.append(texts[i]);
147
                        if (i<texts.length)
148
                                sb.append(" ");
149
                }
150
                this.text = sb.toString();
151
        }
152

    
153
        public String getTextAt(int i) {
154
                return text;
155
        }
156

    
157
        public boolean isSuitableFor(ISymbol symbol) {
158
                return true;
159
        }
160

    
161
        public String getClassName() {
162
                return getClass().getName();
163
        }
164

    
165
        public XMLEntity getXMLEntity() {
166
                XMLEntity xml = new XMLEntity();
167
                xml.putProperty("className", getClassName());
168
                xml.putProperty("desc", getDescription());
169
                xml.putProperty("text", text);
170
                xml.putProperty("markerPointX", markerPoint.getX());
171
                xml.putProperty("markerPointY", markerPoint.getY());
172

    
173
                int size = getFieldCount();
174
                String[] minx = new String[size];
175
                String[] miny = new String[size];
176
                String[] widths = new String[size];
177
                String[] heights = new String[size];
178

    
179
                Rectangle2D[] rects = getTextBounds();
180
                for (int i = 0; i < rects.length; i++) {
181
                        minx[i] = String.valueOf(rects[i].getMinX());
182
                        miny[i] = String.valueOf(rects[i].getMinY());
183
                        widths[i] = String.valueOf(rects[i].getHeight());
184
                        heights[i] = String.valueOf(rects[i].getWidth());
185
                }
186

    
187
                xml.putProperty("minXArray", minx);
188
                xml.putProperty("minYArray", miny);
189
                xml.putProperty("widthArray", widths);
190
                xml.putProperty("heightArray", heights);
191
                xml.addChild(background.getXMLEntity());
192
                return xml;
193
        }
194

    
195
        public void setXMLEntity(XMLEntity xml) {
196
                setDescription(xml.getStringProperty("desc"));
197
                text = xml.getStringProperty("text");
198

    
199
                double x = xml.getDoubleProperty("markerPointX");
200
                double y = xml.getDoubleProperty("markerPointY");
201

    
202
                double[] minx = xml.getDoubleArrayProperty("minXArray");
203
                double[] miny = xml.getDoubleArrayProperty("minYArray");
204
                double[] widths = xml.getDoubleArrayProperty("widthArray");
205
                double[] heights = xml.getDoubleArrayProperty("heightArray");
206
                for (int i = 0; i < minx.length; i++) {
207
                        addTextFieldArea(new Rectangle2D.Double(
208
                                        minx[i],
209
                                        miny[i],
210
                                        widths[i],
211
                                        heights[i]));
212
                }
213
                markerPoint.setLocation(x, y);
214
                background = (BackgroundFileStyle) SymbologyFactory.createStyleFromXML(xml.getChild(0), null);
215
        }
216

    
217
        public Rectangle2D[] getTextBounds() {
218
                return (Rectangle2D[]) textFieldAreas.toArray(new Rectangle2D[textFieldAreas.size()]);
219
        }
220

    
221
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
222
                g.translate(r.getX(), r.getY());
223
                background.drawInsideRectangle(g, r);
224
                g.translate(-r.getX(), -r.getY());
225
        }
226

    
227
        public Rectangle getBounds(AffineTransform scaleInstance) {
228
                if (width >0 && height >0) {
229
                        double myWidth = width;
230
                        double myHeight = height;
231
                        if (scaleInstance == null) scaleInstance = AffineTransform.getScaleInstance(1,1);
232
                        if (unitScaleFactor >0) {
233
                                double scaleX = scaleInstance.getScaleX() * unitScaleFactor;
234
                                double scaleY = scaleInstance.getScaleY() * unitScaleFactor;
235
                                myWidth *= scaleX;
236
                                myHeight *= scaleY;
237
                        }
238
                        return new Rectangle(0, 0, (int) myWidth, (int) myHeight);
239
                }
240
                return background.getBounds();
241
        }
242

    
243
        public Point2D getMarkerPoint() {
244
                return markerPoint;
245
        }
246

    
247
        public void setMarkerPoint(Point2D p) throws IllegalArgumentException {
248
                if (p.getX()<0 || p.getX()>1)
249
                        throw new IllegalArgumentException("X must be >=0 and <=1 ("+p.getX()+")");
250
                if (p.getY()<0 || p.getY()>1)
251
                        throw new IllegalArgumentException("Y must be >=0 and <=1 ("+p.getY()+")");
252
                // the marker represents the point labeled in relative percent units
253
                this.markerPoint = p;
254
        }
255

    
256
        public void drawOutline(Graphics2D g, Rectangle r) {
257
                background.drawOutline(g, r);
258
                Rectangle labelSz = getBounds(null);
259
                final double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
260
                final double ratioViewPort = r.getWidth() / r.getWidth();
261

    
262
                // draw the pointer
263
                {
264
                        int x;
265
                        int y;
266
                        if (ratioViewPort > ratioLabel) {
267
                                // size is defined by the viewport height
268
                                y = (int) (r.height*markerPoint.getY());
269
                                x = (int) ((0.5*r.width) - (0.5-markerPoint.getX())*(ratioLabel*r.height));
270
                        } else {
271
                                // size is defined by the viewport width
272
                                x = (int) (r.width * markerPoint.getX());
273
                                y = (int) ((0.5 * r.height) - (0.5-markerPoint.getY())*(r.width/ratioLabel));
274
                        }
275
                        y = r.height - y;
276

    
277
                        int size = 7;
278
                        g.setColor(Color.ORANGE.darker());
279
                        g.fillOval(x, y, size, size);
280
                        g.setColor(Color.BLACK.brighter());
281
                        g.drawString(Messages.getString("labeled_point"), x + size + 10, y + size);
282
                        g.setColor(Color.BLACK);
283
                        g.drawLine(x-size, (int) (y+(size*0.5)), x+2*size-1, (int) (y+(size*0.5)));
284
                        g.drawLine((int) (x+(size*0.5)), y-size, (int) (x+(size*0.5)), y+2*size-1);
285
                }
286

    
287
                // draw the text fields
288
                if (textFieldAreas.size() > 0) {
289
                        SimpleFillSymbol sym = new SimpleFillSymbol();
290
                        Color c = Color.blue.brighter().brighter();
291

    
292
                        sym.setFillColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100));
293
                        SimpleLineSymbol outline = new SimpleLineSymbol();
294
                        c = Color.BLACK;
295
                        outline.setLineColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100));
296
                        sym.setOutline(outline);
297
                        for (int i = 0; i < textFieldAreas.size(); i++) {
298
                                Rectangle2D textFieldArea = (Rectangle2D) textFieldAreas.get(i);
299

    
300
                                int x= (int) (r.width * textFieldArea.getX());
301
                                int y= (int) (r.height * textFieldArea.getY());
302
                                int width= (int) (r.width * textFieldArea.getWidth());
303
                                int height= (int) (r.width * textFieldArea.getHeight());
304

    
305
                                Rectangle aux = new Rectangle(x, y, width, height);
306
                                FShape shp = new FPolygon2D(new GeneralPathX(aux));
307
                                sym.draw(g, null, shp, null);
308
                                g.setColor(Color.BLACK);
309
                                g.drawString(String.valueOf(i), x+5, y + 10);
310
                        }
311
                }
312
        }
313

    
314
        public void setTextFieldArea(int index, Rectangle2D rect) {
315
                // patch, please remove it
316
                if (textFieldAreas.size()==0)
317
                        textFieldAreas.add(new Rectangle2D.Double());
318
                // end patch
319
                textFieldAreas.set(index, rect);
320
        }
321

    
322
        public void addTextFieldArea(Rectangle2D rect) {
323
                textFieldAreas.add(rect);
324
        }
325

    
326
        public void deleteTextFieldArea(int index) {
327
                textFieldAreas.remove(index);
328
        }
329

    
330
        public void setSize(double width, double height, double unitScaleFactor) {
331
                this.width = width;
332
                this.height = height;
333
                this.unitScaleFactor = unitScaleFactor;
334
        }
335
}