Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SimpleLabelStyle.java @ 11139

History | View | Annotate | Download (8.27 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 11139 2007-04-11 16:01:34Z jaume $
45
* $Log$
46
* Revision 1.10  2007-04-11 16:01:34  jaume
47
* try to fit the text in the correct place
48
*
49
* Revision 1.9  2007/04/10 16:34:01  jaume
50
* towards a styled labeling
51
*
52
* Revision 1.8  2007/04/05 16:07:14  jaume
53
* Styled labeling stuff
54
*
55
* Revision 1.7  2007/04/04 15:42:03  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.6  2007/04/04 15:41:05  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.5  2007/04/02 16:34:56  jaume
62
* Styled labeling (start commiting)
63
*
64
* Revision 1.4  2007/04/02 00:10:04  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.3  2007/03/29 16:02:01  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.2  2007/03/09 11:20:56  jaume
71
* Advanced symbology (start committing)
72
*
73
* Revision 1.1.2.2  2007/02/15 16:23:44  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.1.2.1  2007/02/09 07:47:04  jaume
77
* Isymbol moved
78
*
79
*
80
*/
81
package com.iver.cit.gvsig.fmap.core.styles;
82

    
83
import java.awt.Color;
84
import java.awt.Graphics2D;
85
import java.awt.Rectangle;
86
import java.awt.geom.Point2D;
87
import java.awt.geom.Rectangle2D;
88
import java.util.ArrayList;
89

    
90
import com.iver.cit.gvsig.fmap.Messages;
91
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
92
import com.iver.cit.gvsig.fmap.core.FShape;
93
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
94
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
95
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
96
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
97
import com.iver.utiles.XMLEntity;
98

    
99

    
100
/**
101
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
102
 */
103
public class SimpleLabelStyle extends SVGStyle implements ILabelStyle {
104
        private String text;
105
        private Point2D markerPoint = new Point2D.Double();
106
        // conertir a?? a Rectangle2D[] ja que pot arribar a gastar-se masivament
107
        // en el pintat
108
        private ArrayList textFieldAreas = new ArrayList();
109

    
110
        public int getFieldCount() {
111
                return 1;
112
        }
113

    
114
        public void setTextFields(String[] texts) {
115
                StringBuffer sb = new StringBuffer();
116
                for (int i = 0; i < texts.length; i++) {
117
                        sb.append(texts[i]);
118
                        if (i<texts.length)
119
                                sb.append(" ");
120
                }
121
                this.text = sb.toString();
122
        }
123

    
124
        public String getTextAt(int i) {
125
                return text;
126
        }
127

    
128
        public boolean isSuitableFor(ISymbol symbol) {
129
                return true;
130
        }
131

    
132
        public String getClassName() {
133
                return getClass().getName();
134
        }
135

    
136
        public XMLEntity getXMLEntity() {
137
                XMLEntity xml = super.getXMLEntity();
138
                xml.putProperty("className", getClassName());
139
                xml.putProperty("desc", getDescription());
140
                xml.putProperty("text", text);
141
                xml.putProperty("markerPointX", markerPoint.getX());
142
                xml.putProperty("markerPointY", markerPoint.getY());
143

    
144
                int size = getFieldCount();
145
                String[] minx = new String[size];
146
                String[] miny = new String[size];
147
                String[] widths = new String[size];
148
                String[] heights = new String[size];
149

    
150
                Rectangle2D[] rects = getTextBounds();
151
                for (int i = 0; i < rects.length; i++) {
152
                        minx[i] = String.valueOf(rects[i].getMinX());
153
                        miny[i] = String.valueOf(rects[i].getMinY());
154
                        widths[i] = String.valueOf(rects[i].getHeight());
155
                        heights[i] = String.valueOf(rects[i].getWidth());
156
                }
157

    
158
                xml.putProperty("minXArray", minx);
159
                xml.putProperty("minYArray", miny);
160
                xml.putProperty("widthArray", widths);
161
                xml.putProperty("heightArray", heights);
162
                return xml;
163
        }
164

    
165
        public void setXMLEntity(XMLEntity xml) {
166
                super.setXMLEntity(xml);
167
                setDescription(xml.getStringProperty("desc"));
168
                text = xml.getStringProperty("text");
169
                double x = xml.getDoubleProperty("markerPointX");
170
                double y = xml.getDoubleProperty("markerPointY");
171

    
172
                double[] minx = xml.getDoubleArrayProperty("minXArray");
173
                double[] miny = xml.getDoubleArrayProperty("minYArray");
174
                double[] widths = xml.getDoubleArrayProperty("widthArray");
175
                double[] heights = xml.getDoubleArrayProperty("heightArray");
176
                for (int i = 0; i < minx.length; i++) {
177
                        addTextFieldArea(new Rectangle2D.Double(minx[i], miny[i], widths[i], heights[i]));
178
                }
179
                markerPoint.setLocation(x, y);
180
        }
181

    
182
        public Rectangle2D[] getTextBounds() {
183
                return (Rectangle2D[]) textFieldAreas.toArray(new Rectangle2D[textFieldAreas.size()]);
184
        }
185

    
186
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
187
                g.translate(r.getX(), r.getY());
188
                super.drawInsideRectangle(g, r);
189
                g.translate(-r.getX(), -r.getY());
190
        }
191

    
192
        public Rectangle getBounds() {
193
                return super.getBounds();
194
        }
195

    
196
        public Point2D getMarkerPoint() {
197
                return markerPoint;
198
        }
199

    
200
        public void setMarkerPoint(Point2D p) throws IllegalArgumentException {
201
                if (p.getX()<0 || p.getX()>1)
202
                        throw new IllegalArgumentException("X must be >=0 and <=1 ("+p.getX()+")");
203
                if (p.getY()<0 || p.getY()>1)
204
                        throw new IllegalArgumentException("Y must be >=0 and <=1 ("+p.getY()+")");
205
                // the marker represents the point labeled in relative percent units
206
                this.markerPoint = p;
207
        }
208

    
209
        public void drawOutline(Graphics2D g, Rectangle r) {
210
                super.drawOutline(g, r);
211
                Rectangle labelSz = getBounds();
212

    
213
                final double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
214
                final double ratioViewPort = r.getWidth() / r.getWidth();
215

    
216
                // draw the pointer
217
                {
218
                        int x;
219
                        int y;
220
                        if (ratioViewPort > ratioLabel) {
221
                                // size is defined by the viewport height
222
                                y = (int) (r.height*markerPoint.getY());
223
                                x = (int) ((0.5*r.width) - (0.5-markerPoint.getX())*(ratioLabel*r.height));
224
                        } else {
225
                                // size is defined by the viewport width
226
                                x = (int) (r.width * markerPoint.getX());
227
                                y = (int) ((0.5 * r.height) - (0.5-markerPoint.getY())*(r.width/ratioLabel));
228
                        }
229
                        y = r.height - y;
230

    
231
                        int size = 7;
232
                        g.setColor(Color.ORANGE.darker());
233
                        g.fillOval(x, y, size, size);
234
                        g.setColor(Color.BLACK.brighter());
235
                        g.drawString(Messages.getString("labeled_point"), x + size + 10, y + size);
236
                        g.setColor(Color.BLACK);
237
                        g.drawLine(x-size, (int) (y+(size*0.5)), x+2*size-1, (int) (y+(size*0.5)));
238
                        g.drawLine((int) (x+(size*0.5)), y-size, (int) (x+(size*0.5)), y+2*size-1);
239
                }
240

    
241
                // draw the text fields
242
                if (textFieldAreas.size() > 0) {
243
                        SimpleFillSymbol sym = new SimpleFillSymbol();
244
                        Color c = Color.YELLOW;
245

    
246
                        sym.setFillColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100));
247
                        SimpleLineSymbol outline = new SimpleLineSymbol();
248
                        c = Color.BLACK;
249
                        outline.setLineColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100));
250
                        sym.setOutline(outline);
251
                        for (int i = 0; i < textFieldAreas.size(); i++) {
252
                                Rectangle2D textFieldArea = (Rectangle2D) textFieldAreas.get(i);
253

    
254
                                int x= (int) (r.width * textFieldArea.getX());
255
                                int y= (int) (r.height * textFieldArea.getY());
256
                                int width= (int) (r.width * textFieldArea.getWidth());
257
                                int height= (int) (r.width * textFieldArea.getHeight());
258

    
259
                                Rectangle aux = new Rectangle(x, y, width, height);
260
                                FShape shp = new FPolygon2D(new GeneralPathX(aux));
261
                                sym.draw(g, null, shp);
262
                                g.setColor(Color.BLACK);
263
                                g.drawString(String.valueOf(i), x+5, y + 10);
264
                        }
265
                }
266
        }
267

    
268
        public void setTextFieldArea(int index, Rectangle2D rect) {
269
                // patch, please remove it
270
                if (textFieldAreas.size()==0)
271
                        textFieldAreas.add(new Rectangle2D.Double());
272
                // end patch
273
                textFieldAreas.set(index, rect);
274
        }
275

    
276
        public void addTextFieldArea(Rectangle2D rect) {
277
                textFieldAreas.add(rect);
278
        }
279

    
280
        public void deleteTextFieldArea(int index) {
281
                textFieldAreas.remove(index);
282
        }
283

    
284
}