Statistics
| Revision:

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

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

    
77
import java.awt.Color;
78
import java.awt.Graphics2D;
79
import java.awt.Rectangle;
80
import java.awt.geom.Point2D;
81
import java.awt.geom.Rectangle2D;
82
import java.util.ArrayList;
83

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

    
93

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

    
104
        public int getFieldCount() {
105
                return 1;
106
        }
107

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

    
118
        public String getTextAt(int i) {
119
                return text;
120
        }
121

    
122
        public boolean isSuitableFor(ISymbol symbol) {
123
                return true;
124
        }
125

    
126
        public String getClassName() {
127
                return getClass().getName();
128
        }
129

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

    
138
                int size = getFieldCount();
139
                String[] minx = new String[size];
140
                String[] miny = new String[size];
141
                String[] widths = new String[size];
142
                String[] heights = new String[size];
143

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

    
152
                xml.putProperty("minXArray", minx);
153
                xml.putProperty("minYArray", miny);
154
                xml.putProperty("widthArray", widths);
155
                xml.putProperty("heightArray", heights);
156
                return xml;
157
        }
158

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

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

    
176
        public Rectangle2D[] getTextBounds() {
177
                return (Rectangle2D[]) textFieldAreas.toArray(new Rectangle2D[textFieldAreas.size()]);
178
        }
179

    
180
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
181
                g.translate(r.getX(), r.getY());
182
                super.drawInsideRectangle(g, r);
183
                g.translate(-r.getX(), -r.getY());
184
        }
185

    
186
        public Rectangle getBounds() {
187
                return super.getBounds();
188
        }
189

    
190
        public Point2D getMarkerPoint() {
191
                return markerPoint;
192
        }
193

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

    
203
        public void drawOutline(Graphics2D g, Rectangle r) {
204
                super.drawOutline(g, r);
205
                Rectangle labelSz = getBounds();
206

    
207
                double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
208
                double ratioViewPort = r.getWidth() / r.getWidth();
209

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

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

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

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

    
248
                                int x = (int) (r.width * textFieldArea.getX());
249
                                int y = (int) (r.height * textFieldArea.getY());
250
                                int width = (int) (r.width * textFieldArea.getWidth());
251
                                int height = (int) (r.width * textFieldArea.getHeight());
252
                                Rectangle aux = new Rectangle(x, y, width, height);
253
                                FShape shp = new FPolygon2D(new GeneralPathX(aux));
254
                                sym.draw(g, null, shp);
255
                                g.setColor(Color.BLACK);
256
                                g.drawString(String.valueOf(i), x+5, y + 10);
257
                        }
258
                }
259
        }
260
        public void setTextFieldArea(int index, Rectangle2D rect) {
261
                // patch, please remove it
262
                if (textFieldAreas.size()==0)
263
                        textFieldAreas.add(new Rectangle2D.Double());
264
                // end patch
265
                textFieldAreas.set(index, rect);
266
        }
267

    
268
        public void addTextFieldArea(Rectangle2D rect) {
269
                textFieldAreas.add(rect);
270
        }
271

    
272
        public void deleteTextFieldArea(int index) {
273
                textFieldAreas.remove(index);
274
        }
275

    
276
}