Statistics
| Revision:

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

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

    
80
import java.awt.Color;
81
import java.awt.Graphics2D;
82
import java.awt.Rectangle;
83
import java.awt.geom.Point2D;
84
import java.awt.geom.Rectangle2D;
85
import java.util.ArrayList;
86

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

    
96

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

    
107
        public int getFieldCount() {
108
                return 1;
109
        }
110

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

    
121
        public String getTextAt(int i) {
122
                return text;
123
        }
124

    
125
        public boolean isSuitableFor(ISymbol symbol) {
126
                return true;
127
        }
128

    
129
        public String getClassName() {
130
                return getClass().getName();
131
        }
132

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

    
141
                int size = getFieldCount();
142
                String[] minx = new String[size];
143
                String[] miny = new String[size];
144
                String[] widths = new String[size];
145
                String[] heights = new String[size];
146

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

    
155
                xml.putProperty("minXArray", minx);
156
                xml.putProperty("minYArray", miny);
157
                xml.putProperty("widthArray", widths);
158
                xml.putProperty("heightArray", heights);
159
                return xml;
160
        }
161

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

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

    
179
        public Rectangle2D[] getTextBounds() {
180
                return (Rectangle2D[]) textFieldAreas.toArray(new Rectangle2D[textFieldAreas.size()]);
181
        }
182

    
183
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
184
                g.translate(r.getX(), r.getY());
185
                super.drawInsideRectangle(g, r);
186
                g.translate(-r.getX(), -r.getY());
187
        }
188

    
189
        public Rectangle getBounds() {
190
                return super.getBounds();
191
        }
192

    
193
        public Point2D getMarkerPoint() {
194
                return markerPoint;
195
        }
196

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

    
206
        public void drawOutline(Graphics2D g, Rectangle r) {
207
                super.drawOutline(g, r);
208
                Rectangle labelSz = getBounds();
209

    
210
                double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
211
                double ratioViewPort = r.getWidth() / r.getWidth();
212

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

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

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

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

    
251
                                int x = (int) (r.width * textFieldArea.getX());
252
                                int y = (int) (r.height * textFieldArea.getY());
253
                                int width = (int) (r.width * textFieldArea.getWidth());
254
                                int height = (int) (r.width * textFieldArea.getHeight());
255
                                Rectangle aux = new Rectangle(x, y, width, height);
256
                                FShape shp = new FPolygon2D(new GeneralPathX(aux));
257
                                sym.draw(g, null, shp);
258
                                g.setColor(Color.BLACK);
259
                                g.drawString(String.valueOf(i), x+5, y + 10);
260
                        }
261
                }
262
        }
263

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

    
272
        public void addTextFieldArea(Rectangle2D rect) {
273
                textFieldAreas.add(rect);
274
        }
275

    
276
        public void deleteTextFieldArea(int index) {
277
                textFieldAreas.remove(index);
278
        }
279

    
280
}