Revision 22833
trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/styles/SimpleLabelStyle.java | ||
---|---|---|
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 org.gvsig.symbology.fmap.styles; |
|
103 |
|
|
104 |
import java.awt.Color; |
|
105 |
import java.awt.Dimension; |
|
106 |
import java.awt.Graphics2D; |
|
107 |
import java.awt.Rectangle; |
|
108 |
import java.awt.geom.Point2D; |
|
109 |
import java.awt.geom.Rectangle2D; |
|
110 |
import java.util.ArrayList; |
|
111 |
|
|
112 |
import org.apache.log4j.Logger; |
|
113 |
|
|
114 |
import com.iver.cit.gvsig.fmap.Messages; |
|
115 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
|
116 |
import com.iver.cit.gvsig.fmap.core.FShape; |
|
117 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
|
118 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
|
119 |
import com.iver.cit.gvsig.fmap.core.styles.AbstractStyle; |
|
120 |
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle; |
|
121 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
|
122 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol; |
|
123 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol; |
|
124 |
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException; |
|
125 |
import com.iver.utiles.XMLEntity; |
|
126 |
|
|
127 |
|
|
128 |
/** |
|
129 |
* Implements a style for the creation of simple labels. |
|
130 |
* |
|
131 |
* @author jaume dominguez faus - jaume.dominguez@iver.es |
|
132 |
*/ |
|
133 |
public class SimpleLabelStyle extends AbstractStyle implements ILabelStyle { |
|
134 |
private Point2D markerPoint = new Point2D.Double(); |
|
135 |
// conertir a?? a Rectangle2D[] ja que pot arribar a gastar-se massivament |
|
136 |
// en el pintat |
|
137 |
private ArrayList<Rectangle2D> textFieldAreas = new ArrayList<Rectangle2D>(); |
|
138 |
private BackgroundFileStyle background; |
|
139 |
private Dimension sz; |
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
public int getFieldCount() { |
|
144 |
return textFieldAreas.size(); |
|
145 |
} |
|
146 |
|
|
147 |
public void setTextFields(String[] texts) { |
|
148 |
StringBuffer sb = new StringBuffer(); |
|
149 |
for (int i = 0; texts != null && i < texts.length; i++) { |
|
150 |
sb.append(texts[i]); |
|
151 |
if (i<texts.length) |
|
152 |
sb.append(" "); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
public boolean isSuitableFor(ISymbol symbol) { |
|
157 |
return true; |
|
158 |
} |
|
159 |
|
|
160 |
public String getClassName() { |
|
161 |
return getClass().getName(); |
|
162 |
} |
|
163 |
|
|
164 |
public XMLEntity getXMLEntity() { |
|
165 |
XMLEntity xml = new XMLEntity(); |
|
166 |
xml.putProperty("className", getClassName()); |
|
167 |
xml.putProperty("desc", getDescription()); |
|
168 |
xml.putProperty("markerPointX", markerPoint.getX()); |
|
169 |
xml.putProperty("markerPointY", markerPoint.getY()); |
|
170 |
|
|
171 |
int size = getFieldCount(); |
|
172 |
String[] minx = new String[size]; |
|
173 |
String[] miny = new String[size]; |
|
174 |
String[] widths = new String[size]; |
|
175 |
String[] heights = new String[size]; |
|
176 |
|
|
177 |
Rectangle2D[] rects = getTextBounds(); |
|
178 |
for (int i = 0; i < rects.length; i++) { |
|
179 |
minx[i] = String.valueOf(rects[i].getMinX()); |
|
180 |
miny[i] = String.valueOf(rects[i].getMinY()); |
|
181 |
widths[i] = String.valueOf(rects[i].getWidth()); |
|
182 |
heights[i] = String.valueOf(rects[i].getHeight()); |
|
183 |
} |
|
184 |
|
|
185 |
xml.putProperty("minXArray", minx); |
|
186 |
xml.putProperty("minYArray", miny); |
|
187 |
xml.putProperty("widthArray", widths); |
|
188 |
xml.putProperty("heightArray", heights); |
|
189 |
XMLEntity bgXML = background.getXMLEntity(); |
|
190 |
bgXML.putProperty("id", "LabelStyle"); |
|
191 |
xml.addChild(bgXML); |
|
192 |
return xml; |
|
193 |
} |
|
194 |
|
|
195 |
public void setXMLEntity(XMLEntity xml) { |
|
196 |
setDescription(xml.getStringProperty("desc")); |
|
197 |
|
|
198 |
double x = xml.getDoubleProperty("markerPointX"); |
|
199 |
double y = xml.getDoubleProperty("markerPointY"); |
|
200 |
|
|
201 |
double[] minx = xml.getDoubleArrayProperty("minXArray"); |
|
202 |
double[] miny = xml.getDoubleArrayProperty("minYArray"); |
|
203 |
double[] widths = xml.getDoubleArrayProperty("widthArray"); |
|
204 |
double[] heights = xml.getDoubleArrayProperty("heightArray"); |
|
205 |
for (int i = 0; i < minx.length; i++) { |
|
206 |
addTextFieldArea(new Rectangle2D.Double( |
|
207 |
minx[i], |
|
208 |
miny[i], |
|
209 |
widths[i], |
|
210 |
heights[i])); |
|
211 |
} |
|
212 |
markerPoint.setLocation(x, y); |
|
213 |
XMLEntity bgXML = xml.firstChild("id", "LabelStyle"); |
|
214 |
if (bgXML!=null) { |
|
215 |
background = (BackgroundFileStyle) SymbologyFactory.createStyleFromXML(xml.getChild(0), null); |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
public Rectangle2D[] getTextBounds() { |
|
220 |
return (Rectangle2D[]) textFieldAreas.toArray(new Rectangle2D[textFieldAreas.size()]); |
|
221 |
} |
|
222 |
|
|
223 |
public void drawInsideRectangle(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
|
224 |
background.drawInsideRectangle(g, r); |
|
225 |
} |
|
226 |
|
|
227 |
|
|
228 |
public Dimension getSize() { |
|
229 |
if (sz == null) { |
|
230 |
Rectangle bgBounds = background.getBounds(); |
|
231 |
setSize(bgBounds.getWidth(), bgBounds.getHeight()); |
|
232 |
} |
|
233 |
return sz; |
|
234 |
} |
|
235 |
|
|
236 |
public Point2D getMarkerPoint() { |
|
237 |
return markerPoint; |
|
238 |
} |
|
239 |
|
|
240 |
public void setMarkerPoint(Point2D p) throws IllegalArgumentException { |
|
241 |
if (p.getX()<0 || p.getX()>1) |
|
242 |
throw new IllegalArgumentException("X must be >=0 and <=1 ("+p.getX()+")"); |
|
243 |
if (p.getY()<0 || p.getY()>1) |
|
244 |
throw new IllegalArgumentException("Y must be >=0 and <=1 ("+p.getY()+")"); |
|
245 |
// the marker represents the point labeled in relative percent units |
|
246 |
this.markerPoint = p; |
|
247 |
} |
|
248 |
|
|
249 |
private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) { |
|
250 |
int x; |
|
251 |
int y; |
|
252 |
if (ratioViewPort > ratioLabel) { |
|
253 |
// size is defined by the viewport height |
|
254 |
y = (int) (r.height*xy[1]); |
|
255 |
x = (int) ((0.5*r.width) - (0.5-xy[0])*(ratioLabel*r.height)); |
|
256 |
} else { |
|
257 |
// size is defined by the viewport width |
|
258 |
x = (int) (r.width * xy[0]); |
|
259 |
y = (int) ((0.5 * r.height) - (0.5-xy[1])*(r.width/ratioLabel)); |
|
260 |
} |
|
261 |
xy[0] = x; |
|
262 |
xy[1] = y; |
|
263 |
} |
|
264 |
|
|
265 |
public void drawOutline(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
|
266 |
background.drawOutline(g, r); |
|
267 |
Dimension labelSz = getSize(); |
|
268 |
final double ratioLabel = labelSz.getWidth()/labelSz.getHeight(); |
|
269 |
final double ratioViewPort = r.getWidth() / r.getHeight(); |
|
270 |
final double[] xy = new double[2]; |
|
271 |
// draw the pointer |
|
272 |
{ |
|
273 |
xy[0] = markerPoint.getX(); |
|
274 |
xy[1] = markerPoint.getY(); |
|
275 |
relativeToAbsolute(xy, r, labelSz, ratioLabel, ratioViewPort); |
|
276 |
int x = (int) Math.round(xy[0]); |
|
277 |
int y = (int) Math.round(r.height - xy[1]); |
|
278 |
|
|
279 |
int size = 7; |
|
280 |
g.setColor(Color.ORANGE.darker()); |
|
281 |
g.fillOval(x, y, size, size); |
|
282 |
g.setColor(Color.BLACK.brighter()); |
|
283 |
g.drawString(Messages.getString("labeled_point"), x + size + 10, y + size); |
|
284 |
g.setColor(Color.BLACK); |
|
285 |
g.drawLine(x-size, (int) (y+(size*0.5)), x+2*size-1, (int) (y+(size*0.5))); |
|
286 |
g.drawLine((int) (x+(size*0.5)), y-size, (int) (x+(size*0.5)), y+2*size-1); |
|
287 |
} |
|
288 |
|
|
289 |
// draw the text fields |
|
290 |
if (textFieldAreas.size() > 0) { |
|
291 |
SimpleFillSymbol sym = new SimpleFillSymbol(); |
|
292 |
Color c = Color.blue.brighter().brighter(); |
|
293 |
|
|
294 |
sym.setFillColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100)); |
|
295 |
SimpleLineSymbol outline = new SimpleLineSymbol(); |
|
296 |
c = Color.BLACK; |
|
297 |
outline.setLineColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100)); |
|
298 |
sym.setOutline(outline); |
|
299 |
for (int i = 0; i < textFieldAreas.size(); i++) { |
|
300 |
Rectangle2D textFieldArea = (Rectangle2D) textFieldAreas.get(i); |
|
301 |
xy[0] = textFieldArea.getX(); |
|
302 |
xy[1] = textFieldArea.getY(); |
|
303 |
relativeToAbsolute(xy, r, labelSz, ratioLabel, ratioViewPort); |
|
304 |
int x = (int) Math.round(xy[0]); |
|
305 |
int y = (int) Math.round(xy[1]); |
|
306 |
|
|
307 |
xy[0] = textFieldArea.getMaxX(); |
|
308 |
xy[1] = textFieldArea.getMaxY(); |
|
309 |
relativeToAbsolute(xy, r, labelSz, ratioLabel, ratioViewPort); |
|
310 |
int width = (int) Math.round(xy[0]) -x; |
|
311 |
int height = (int) Math.round(xy[1] - y) ; |
|
312 |
|
|
313 |
Rectangle aux = new Rectangle(x, y, width, height); |
|
314 |
FShape shp = new FPolygon2D(new GeneralPathX(aux)); |
|
315 |
sym.draw(g, null, shp, null); |
|
316 |
g.setColor(Color.BLACK); |
|
317 |
g.drawString(String.valueOf(i+1), x+5, y + 10); // start with 1 |
|
318 |
} |
|
319 |
} |
|
320 |
} |
|
321 |
|
|
322 |
public void setTextFieldArea(int index, Rectangle2D rect) { |
|
323 |
textFieldAreas.set(index, rect); |
|
324 |
} |
|
325 |
|
|
326 |
public void addTextFieldArea(Rectangle2D rect) { |
|
327 |
textFieldAreas.add(rect); |
|
328 |
} |
|
329 |
|
|
330 |
public void deleteTextFieldArea(int index) { |
|
331 |
textFieldAreas.remove(index); |
|
332 |
} |
|
333 |
|
|
334 |
public void setSize(double width, double height) { |
|
335 |
sz = new Dimension( (int) Math.round(width), (int) Math.round(height)); |
|
336 |
} |
|
337 |
} |
|
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 org.gvsig.symbology.fmap.styles; |
|
103 |
|
|
104 |
import java.awt.Color; |
|
105 |
import java.awt.Dimension; |
|
106 |
import java.awt.Graphics2D; |
|
107 |
import java.awt.Rectangle; |
|
108 |
import java.awt.geom.Point2D; |
|
109 |
import java.awt.geom.Rectangle2D; |
|
110 |
import java.util.ArrayList; |
|
111 |
|
|
112 |
import org.apache.log4j.Logger; |
|
113 |
|
|
114 |
import com.iver.cit.gvsig.fmap.Messages; |
|
115 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
|
116 |
import com.iver.cit.gvsig.fmap.core.FShape; |
|
117 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
|
118 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
|
119 |
import com.iver.cit.gvsig.fmap.core.styles.AbstractStyle; |
|
120 |
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle; |
|
121 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
|
122 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol; |
|
123 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol; |
|
124 |
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException; |
|
125 |
import com.iver.utiles.XMLEntity; |
|
126 |
|
|
127 |
|
|
128 |
/** |
|
129 |
* Implements a style for the creation of simple labels. |
|
130 |
* |
|
131 |
* @author jaume dominguez faus - jaume.dominguez@iver.es |
|
132 |
*/ |
|
133 |
public class SimpleLabelStyle extends AbstractStyle implements ILabelStyle { |
|
134 |
private Point2D markerPoint = new Point2D.Double(); |
|
135 |
// conertir a?? a Rectangle2D[] ja que pot arribar a gastar-se massivament |
|
136 |
// en el pintat |
|
137 |
private ArrayList<Rectangle2D> textFieldAreas = new ArrayList<Rectangle2D>(); |
|
138 |
private BackgroundFileStyle background; |
|
139 |
private Dimension sz; |
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
public int getFieldCount() { |
|
144 |
return textFieldAreas.size(); |
|
145 |
} |
|
146 |
|
|
147 |
public void setTextFields(String[] texts) { |
|
148 |
StringBuffer sb = new StringBuffer(); |
|
149 |
for (int i = 0; texts != null && i < texts.length; i++) { |
|
150 |
sb.append(texts[i]); |
|
151 |
if (i<texts.length) |
|
152 |
sb.append(" "); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
public boolean isSuitableFor(ISymbol symbol) { |
|
157 |
return true; |
|
158 |
} |
|
159 |
|
|
160 |
public String getClassName() { |
|
161 |
return getClass().getName(); |
|
162 |
} |
|
163 |
|
|
164 |
public XMLEntity getXMLEntity() { |
|
165 |
XMLEntity xml = new XMLEntity(); |
|
166 |
xml.putProperty("className", getClassName()); |
|
167 |
xml.putProperty("desc", getDescription()); |
|
168 |
xml.putProperty("markerPointX", markerPoint.getX()); |
|
169 |
xml.putProperty("markerPointY", markerPoint.getY()); |
|
170 |
|
|
171 |
int size = getFieldCount(); |
|
172 |
String[] minx = new String[size]; |
|
173 |
String[] miny = new String[size]; |
|
174 |
String[] widths = new String[size]; |
|
175 |
String[] heights = new String[size]; |
|
176 |
|
|
177 |
Rectangle2D[] rects = getTextBounds(); |
|
178 |
for (int i = 0; i < rects.length; i++) { |
|
179 |
minx[i] = String.valueOf(rects[i].getMinX()); |
|
180 |
miny[i] = String.valueOf(rects[i].getMinY()); |
|
181 |
widths[i] = String.valueOf(rects[i].getWidth()); |
|
182 |
heights[i] = String.valueOf(rects[i].getHeight()); |
|
183 |
} |
|
184 |
|
|
185 |
xml.putProperty("minXArray", minx); |
|
186 |
xml.putProperty("minYArray", miny); |
|
187 |
xml.putProperty("widthArray", widths); |
|
188 |
xml.putProperty("heightArray", heights); |
|
189 |
XMLEntity bgXML = background.getXMLEntity(); |
|
190 |
bgXML.putProperty("id", "LabelStyle"); |
|
191 |
xml.addChild(bgXML); |
|
192 |
return xml; |
|
193 |
} |
|
194 |
|
|
195 |
public void setXMLEntity(XMLEntity xml) { |
|
196 |
setDescription(xml.getStringProperty("desc")); |
|
197 |
|
|
198 |
double x = xml.getDoubleProperty("markerPointX"); |
|
199 |
double y = xml.getDoubleProperty("markerPointY"); |
|
200 |
|
|
201 |
double[] minx = xml.getDoubleArrayProperty("minXArray"); |
|
202 |
double[] miny = xml.getDoubleArrayProperty("minYArray"); |
|
203 |
double[] widths = xml.getDoubleArrayProperty("widthArray"); |
|
204 |
double[] heights = xml.getDoubleArrayProperty("heightArray"); |
|
205 |
for (int i = 0; i < minx.length; i++) { |
|
206 |
addTextFieldArea(new Rectangle2D.Double( |
|
207 |
minx[i], |
|
208 |
miny[i], |
|
209 |
widths[i], |
|
210 |
heights[i])); |
|
211 |
} |
|
212 |
markerPoint.setLocation(x, y); |
|
213 |
XMLEntity bgXML = xml.firstChild("id", "LabelStyle"); |
|
214 |
if (bgXML!=null) { |
|
215 |
background = (BackgroundFileStyle) SymbologyFactory.createStyleFromXML(xml.getChild(0), null); |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
public Rectangle2D[] getTextBounds() { |
|
220 |
return (Rectangle2D[]) textFieldAreas.toArray(new Rectangle2D[textFieldAreas.size()]); |
|
221 |
} |
|
222 |
|
|
223 |
public void drawInsideRectangle(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
|
224 |
background.drawInsideRectangle(g, r); |
|
225 |
} |
|
226 |
|
|
227 |
|
|
228 |
public Dimension getSize() { |
|
229 |
if (sz == null) { |
|
230 |
Rectangle bgBounds = background.getBounds(); |
|
231 |
setSize(bgBounds.getWidth(), bgBounds.getHeight()); |
|
232 |
} |
|
233 |
return sz; |
|
234 |
} |
|
235 |
|
|
236 |
public Point2D getMarkerPoint() { |
|
237 |
return markerPoint; |
|
238 |
} |
|
239 |
|
|
240 |
public void setMarkerPoint(Point2D p) throws IllegalArgumentException { |
|
241 |
if (p.getX()<0 || p.getX()>1) |
|
242 |
throw new IllegalArgumentException("X must be >=0 and <=1 ("+p.getX()+")"); |
|
243 |
if (p.getY()<0 || p.getY()>1) |
|
244 |
throw new IllegalArgumentException("Y must be >=0 and <=1 ("+p.getY()+")"); |
|
245 |
// the marker represents the point labeled in relative percent units |
|
246 |
this.markerPoint = p; |
|
247 |
} |
|
248 |
|
|
249 |
private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) { |
|
250 |
double x, y; |
|
251 |
|
|
252 |
x = (r.width * xy[0]); |
|
253 |
y = (r.height * xy[1]); |
|
254 |
|
|
255 |
xy[0] = x; |
|
256 |
xy[1] = y; |
|
257 |
|
|
258 |
} |
|
259 |
|
|
260 |
public void drawOutline(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
|
261 |
background.drawOutline(g, r); |
|
262 |
Dimension labelSz = getSize(); |
|
263 |
final double ratioLabel = labelSz.getWidth()/labelSz.getHeight(); |
|
264 |
final double ratioViewPort = r.getWidth() / r.getHeight(); |
|
265 |
final double[] xy = new double[2]; |
|
266 |
// draw the pointer |
|
267 |
{ |
|
268 |
xy[0] = markerPoint.getX(); |
|
269 |
xy[1] = markerPoint.getY(); |
|
270 |
relativeToAbsolute(xy, r, labelSz, ratioLabel, ratioViewPort); |
|
271 |
int x = (int) Math.round(xy[0]); |
|
272 |
int y = (int) Math.round(xy[1]); |
|
273 |
|
|
274 |
int size = 7; |
|
275 |
g.setColor(Color.ORANGE.darker()); |
|
276 |
g.fillOval(x, y, size, size); |
|
277 |
g.setColor(Color.BLACK.brighter()); |
|
278 |
g.drawString(Messages.getString("labeled_point"), x + size + 10, y + size); |
|
279 |
g.setColor(Color.BLACK); |
|
280 |
g.drawLine(x-size, (int) (y+(size*0.5)), x+2*size-1, (int) (y+(size*0.5))); |
|
281 |
g.drawLine((int) (x+(size*0.5)), y-size, (int) (x+(size*0.5)), y+2*size-1); |
|
282 |
} |
|
283 |
|
|
284 |
// draw the text fields |
|
285 |
if (textFieldAreas.size() > 0) { |
|
286 |
SimpleFillSymbol sym = new SimpleFillSymbol(); |
|
287 |
Color c = Color.blue.brighter().brighter(); |
|
288 |
|
|
289 |
sym.setFillColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100)); |
|
290 |
SimpleLineSymbol outline = new SimpleLineSymbol(); |
|
291 |
c = Color.BLACK; |
|
292 |
outline.setLineColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100)); |
|
293 |
sym.setOutline(outline); |
|
294 |
for (int i = 0; i < textFieldAreas.size(); i++) { |
|
295 |
Rectangle2D textFieldArea = (Rectangle2D) textFieldAreas.get(i); |
|
296 |
xy[0] = textFieldArea.getX(); |
|
297 |
xy[1] = textFieldArea.getY(); |
|
298 |
relativeToAbsolute(xy, r, labelSz, ratioLabel, ratioViewPort); |
|
299 |
int x = (int) Math.round(xy[0]); |
|
300 |
int y = (int) Math.round(xy[1]); |
|
301 |
|
|
302 |
xy[0] = textFieldArea.getMaxX(); |
|
303 |
xy[1] = textFieldArea.getMaxY(); |
|
304 |
relativeToAbsolute(xy, r, labelSz, ratioLabel, ratioViewPort); |
|
305 |
int width = (int) Math.round(xy[0]) -x; |
|
306 |
int height = (int) Math.round(xy[1] - y) ; |
|
307 |
|
|
308 |
Rectangle aux = new Rectangle(x, y, width, height); |
|
309 |
FShape shp = new FPolygon2D(new GeneralPathX(aux)); |
|
310 |
sym.draw(g, null, shp, null); |
|
311 |
g.setColor(Color.BLACK); |
|
312 |
g.drawString(String.valueOf(i+1), x+5, y + 10); // start with 1 |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
|
|
317 |
public void setTextFieldArea(int index, Rectangle2D rect) { |
|
318 |
textFieldAreas.set(index, rect); |
|
319 |
} |
|
320 |
|
|
321 |
public void addTextFieldArea(Rectangle2D rect) { |
|
322 |
textFieldAreas.add(rect); |
|
323 |
} |
|
324 |
|
|
325 |
public void deleteTextFieldArea(int index) { |
|
326 |
textFieldAreas.remove(index); |
|
327 |
} |
|
328 |
|
|
329 |
public void setSize(double width, double height) { |
|
330 |
sz = new Dimension( (int) Math.round(width), (int) Math.round(height)); |
|
331 |
} |
|
332 |
} |
Also available in: Unified diff