root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SimpleLabelStyle.java @ 11245
History | View | Annotate | Download (8.73 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 11245 2007-04-18 18:26:18Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.13 2007-04-18 18:26:18 jaume
|
47 |
* *** empty log message ***
|
48 |
*
|
49 |
* Revision 1.12 2007/04/18 15:35:11 jaume
|
50 |
* *** empty log message ***
|
51 |
*
|
52 |
* Revision 1.11 2007/04/12 14:28:43 jaume
|
53 |
* basic labeling support for lines
|
54 |
*
|
55 |
* Revision 1.10 2007/04/11 16:01:34 jaume
|
56 |
* try to fit the text in the correct place
|
57 |
*
|
58 |
* Revision 1.9 2007/04/10 16:34:01 jaume
|
59 |
* towards a styled labeling
|
60 |
*
|
61 |
* Revision 1.8 2007/04/05 16:07:14 jaume
|
62 |
* Styled labeling stuff
|
63 |
*
|
64 |
* Revision 1.7 2007/04/04 15:42:03 jaume
|
65 |
* *** empty log message ***
|
66 |
*
|
67 |
* Revision 1.6 2007/04/04 15:41:05 jaume
|
68 |
* *** empty log message ***
|
69 |
*
|
70 |
* Revision 1.5 2007/04/02 16:34:56 jaume
|
71 |
* Styled labeling (start commiting)
|
72 |
*
|
73 |
* Revision 1.4 2007/04/02 00:10:04 jaume
|
74 |
* *** empty log message ***
|
75 |
*
|
76 |
* Revision 1.3 2007/03/29 16:02:01 jaume
|
77 |
* *** empty log message ***
|
78 |
*
|
79 |
* Revision 1.2 2007/03/09 11:20:56 jaume
|
80 |
* Advanced symbology (start committing)
|
81 |
*
|
82 |
* Revision 1.1.2.2 2007/02/15 16:23:44 jaume
|
83 |
* *** empty log message ***
|
84 |
*
|
85 |
* Revision 1.1.2.1 2007/02/09 07:47:04 jaume
|
86 |
* Isymbol moved
|
87 |
*
|
88 |
*
|
89 |
*/
|
90 |
package com.iver.cit.gvsig.fmap.core.styles; |
91 |
|
92 |
import java.awt.Color; |
93 |
import java.awt.Graphics2D; |
94 |
import java.awt.Rectangle; |
95 |
import java.awt.geom.Point2D; |
96 |
import java.awt.geom.Rectangle2D; |
97 |
import java.util.ArrayList; |
98 |
|
99 |
import com.iver.cit.gvsig.fmap.Messages; |
100 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
101 |
import com.iver.cit.gvsig.fmap.core.FShape; |
102 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
103 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
104 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
105 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol; |
106 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol; |
107 |
import com.iver.utiles.XMLEntity; |
108 |
|
109 |
|
110 |
/**
|
111 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
112 |
*/
|
113 |
public class SimpleLabelStyle extends AbstractStyle implements ILabelStyle { |
114 |
private String text; |
115 |
private Point2D markerPoint = new Point2D.Double(); |
116 |
// conertir a?? a Rectangle2D[] ja que pot arribar a gastar-se massivament
|
117 |
// en el pintat
|
118 |
private ArrayList textFieldAreas = new ArrayList(); |
119 |
private BackgroundFileStyle background;
|
120 |
|
121 |
public int getFieldCount() { |
122 |
return 1; |
123 |
} |
124 |
|
125 |
public void setTextFields(String[] texts) { |
126 |
StringBuffer sb = new StringBuffer(); |
127 |
for (int i = 0; i < texts.length; i++) { |
128 |
sb.append(texts[i]); |
129 |
if (i<texts.length)
|
130 |
sb.append(" ");
|
131 |
} |
132 |
this.text = sb.toString();
|
133 |
} |
134 |
|
135 |
public String getTextAt(int i) { |
136 |
return text;
|
137 |
} |
138 |
|
139 |
public boolean isSuitableFor(ISymbol symbol) { |
140 |
return true; |
141 |
} |
142 |
|
143 |
public String getClassName() { |
144 |
return getClass().getName();
|
145 |
} |
146 |
|
147 |
public XMLEntity getXMLEntity() {
|
148 |
XMLEntity xml = new XMLEntity();
|
149 |
xml.putProperty("className", getClassName());
|
150 |
xml.putProperty("desc", getDescription());
|
151 |
xml.putProperty("text", text);
|
152 |
xml.putProperty("markerPointX", markerPoint.getX());
|
153 |
xml.putProperty("markerPointY", markerPoint.getY());
|
154 |
|
155 |
int size = getFieldCount();
|
156 |
String[] minx = new String[size]; |
157 |
String[] miny = new String[size]; |
158 |
String[] widths = new String[size]; |
159 |
String[] heights = new String[size]; |
160 |
|
161 |
Rectangle2D[] rects = getTextBounds(); |
162 |
for (int i = 0; i < rects.length; i++) { |
163 |
minx[i] = String.valueOf(rects[i].getMinX());
|
164 |
miny[i] = String.valueOf(rects[i].getMinY());
|
165 |
widths[i] = String.valueOf(rects[i].getHeight());
|
166 |
heights[i] = String.valueOf(rects[i].getWidth());
|
167 |
} |
168 |
|
169 |
xml.putProperty("minXArray", minx);
|
170 |
xml.putProperty("minYArray", miny);
|
171 |
xml.putProperty("widthArray", widths);
|
172 |
xml.putProperty("heightArray", heights);
|
173 |
return xml;
|
174 |
} |
175 |
|
176 |
public void setXMLEntity(XMLEntity xml) { |
177 |
setDescription(xml.getStringProperty("desc"));
|
178 |
text = xml.getStringProperty("text");
|
179 |
System.err.println(getDescription());
|
180 |
double x = xml.getDoubleProperty("markerPointX"); |
181 |
double y = xml.getDoubleProperty("markerPointY"); |
182 |
|
183 |
double[] minx = xml.getDoubleArrayProperty("minXArray"); |
184 |
double[] miny = xml.getDoubleArrayProperty("minYArray"); |
185 |
double[] widths = xml.getDoubleArrayProperty("widthArray"); |
186 |
double[] heights = xml.getDoubleArrayProperty("heightArray"); |
187 |
for (int i = 0; i < minx.length; i++) { |
188 |
addTextFieldArea(new Rectangle2D.Double(minx[i], miny[i], widths[i], heights[i])); |
189 |
} |
190 |
markerPoint.setLocation(x, y); |
191 |
background = (BackgroundFileStyle) SymbologyFactory.createStyleFromXML(xml.getChild(0), null); |
192 |
} |
193 |
|
194 |
public Rectangle2D[] getTextBounds() { |
195 |
return (Rectangle2D[]) textFieldAreas.toArray(new Rectangle2D[textFieldAreas.size()]); |
196 |
} |
197 |
|
198 |
public void drawInsideRectangle(Graphics2D g, Rectangle r) { |
199 |
g.translate(r.getX(), r.getY()); |
200 |
background.drawInsideRectangle(g, r); |
201 |
g.translate(-r.getX(), -r.getY()); |
202 |
} |
203 |
|
204 |
public Rectangle getBounds() { |
205 |
return background.getBounds();
|
206 |
} |
207 |
|
208 |
public Point2D getMarkerPoint() { |
209 |
return markerPoint;
|
210 |
} |
211 |
|
212 |
public void setMarkerPoint(Point2D p) throws IllegalArgumentException { |
213 |
if (p.getX()<0 || p.getX()>1) |
214 |
throw new IllegalArgumentException("X must be >=0 and <=1 ("+p.getX()+")"); |
215 |
if (p.getY()<0 || p.getY()>1) |
216 |
throw new IllegalArgumentException("Y must be >=0 and <=1 ("+p.getY()+")"); |
217 |
// the marker represents the point labeled in relative percent units
|
218 |
this.markerPoint = p;
|
219 |
} |
220 |
|
221 |
public void drawOutline(Graphics2D g, Rectangle r) { |
222 |
background.drawOutline(g, r); |
223 |
Rectangle labelSz = getBounds();
|
224 |
|
225 |
final double ratioLabel = labelSz.getWidth()/labelSz.getHeight(); |
226 |
final double ratioViewPort = r.getWidth() / r.getWidth(); |
227 |
|
228 |
// draw the pointer
|
229 |
{ |
230 |
int x;
|
231 |
int y;
|
232 |
if (ratioViewPort > ratioLabel) {
|
233 |
// size is defined by the viewport height
|
234 |
y = (int) (r.height*markerPoint.getY());
|
235 |
x = (int) ((0.5*r.width) - (0.5-markerPoint.getX())*(ratioLabel*r.height)); |
236 |
} else {
|
237 |
// size is defined by the viewport width
|
238 |
x = (int) (r.width * markerPoint.getX());
|
239 |
y = (int) ((0.5 * r.height) - (0.5-markerPoint.getY())*(r.width/ratioLabel)); |
240 |
} |
241 |
y = r.height - y; |
242 |
|
243 |
int size = 7; |
244 |
g.setColor(Color.ORANGE.darker());
|
245 |
g.fillOval(x, y, size, size); |
246 |
g.setColor(Color.BLACK.brighter());
|
247 |
g.drawString(Messages.getString("labeled_point"), x + size + 10, y + size); |
248 |
g.setColor(Color.BLACK);
|
249 |
g.drawLine(x-size, (int) (y+(size*0.5)), x+2*size-1, (int) (y+(size*0.5))); |
250 |
g.drawLine((int) (x+(size*0.5)), y-size, (int) (x+(size*0.5)), y+2*size-1); |
251 |
} |
252 |
|
253 |
// draw the text fields
|
254 |
if (textFieldAreas.size() > 0) { |
255 |
SimpleFillSymbol sym = new SimpleFillSymbol();
|
256 |
Color c = Color.blue.brighter().brighter(); |
257 |
|
258 |
sym.setFillColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100)); |
259 |
SimpleLineSymbol outline = new SimpleLineSymbol();
|
260 |
c = Color.BLACK;
|
261 |
outline.setLineColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100)); |
262 |
sym.setOutline(outline); |
263 |
for (int i = 0; i < textFieldAreas.size(); i++) { |
264 |
Rectangle2D textFieldArea = (Rectangle2D) textFieldAreas.get(i); |
265 |
|
266 |
int x= (int) (r.width * textFieldArea.getX()); |
267 |
int y= (int) (r.height * textFieldArea.getY()); |
268 |
int width= (int) (r.width * textFieldArea.getWidth()); |
269 |
int height= (int) (r.width * textFieldArea.getHeight()); |
270 |
|
271 |
Rectangle aux = new Rectangle(x, y, width, height); |
272 |
FShape shp = new FPolygon2D(new GeneralPathX(aux)); |
273 |
sym.draw(g, null, shp);
|
274 |
g.setColor(Color.BLACK);
|
275 |
g.drawString(String.valueOf(i), x+5, y + 10); |
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
public void setTextFieldArea(int index, Rectangle2D rect) { |
281 |
// patch, please remove it
|
282 |
if (textFieldAreas.size()==0) |
283 |
textFieldAreas.add(new Rectangle2D.Double()); |
284 |
// end patch
|
285 |
textFieldAreas.set(index, rect); |
286 |
} |
287 |
|
288 |
public void addTextFieldArea(Rectangle2D rect) { |
289 |
textFieldAreas.add(rect); |
290 |
} |
291 |
|
292 |
public void deleteTextFieldArea(int index) { |
293 |
textFieldAreas.remove(index); |
294 |
} |
295 |
|
296 |
} |