root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / fmap / core / symbols / CharacterMarkerSymbol.java @ 8458
History | View | Annotate | Download (7.3 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: CharacterMarkerSymbol.java 8458 2006-10-31 16:18:03Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.1 2006-10-31 16:16:34 jaume
|
47 |
* *** empty log message ***
|
48 |
*
|
49 |
* Revision 1.4 2006/10/30 19:30:35 jaume
|
50 |
* *** empty log message ***
|
51 |
*
|
52 |
* Revision 1.3 2006/10/29 23:53:49 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
* Revision 1.2 2006/10/26 16:27:33 jaume
|
56 |
* support for composite marker symbols (not tested)
|
57 |
*
|
58 |
* Revision 1.1 2006/10/25 10:50:41 jaume
|
59 |
* movement of classes and gui stuff
|
60 |
*
|
61 |
* Revision 1.3 2006/10/24 19:54:16 jaume
|
62 |
* added IPersistence
|
63 |
*
|
64 |
* Revision 1.2 2006/10/24 08:02:51 jaume
|
65 |
* *** empty log message ***
|
66 |
*
|
67 |
* Revision 1.1 2006/10/18 07:54:06 jaume
|
68 |
* *** empty log message ***
|
69 |
*
|
70 |
*
|
71 |
*/
|
72 |
package com.iver.cit.gvsig.fmap.core.symbols; |
73 |
|
74 |
import java.awt.Color; |
75 |
import java.awt.Font; |
76 |
import java.awt.Graphics2D; |
77 |
import java.awt.Point; |
78 |
import java.awt.Rectangle; |
79 |
import java.awt.Shape; |
80 |
import java.awt.geom.AffineTransform; |
81 |
import java.awt.geom.Point2D; |
82 |
|
83 |
import org.apache.batik.ext.awt.geom.PathLength; |
84 |
|
85 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
86 |
import com.iver.cit.gvsig.fmap.core.FShape; |
87 |
import com.iver.cit.gvsig.fmap.core.ISymbol; |
88 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
89 |
import com.iver.utiles.StringUtilities; |
90 |
import com.iver.utiles.XMLEntity; |
91 |
|
92 |
|
93 |
// TODO quitar batik-util.jar del classpath de extGraph cuando movamos esto a FMap
|
94 |
/**
|
95 |
* Symbol that manages symbols from a TrueType font source
|
96 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
97 |
*/
|
98 |
public class CharacterMarkerSymbol extends AbstractMarkerSymbol { |
99 |
private Font font; |
100 |
private Color color = Color.BLACK; |
101 |
private int symbol; |
102 |
|
103 |
/**
|
104 |
* Creates a new instance of CharacterMarker with default values
|
105 |
*
|
106 |
*/
|
107 |
public CharacterMarkerSymbol() {
|
108 |
super();
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* Creates a new instance of CharacterMarker specifying the marker source
|
113 |
* font, the character code corresponding to the symbol, and the color that
|
114 |
* will be used in rendering time.
|
115 |
*
|
116 |
* @param font -
|
117 |
* src Font
|
118 |
* @param charCode -
|
119 |
* character code of the symbol for this font
|
120 |
* @param color -
|
121 |
* color to be used in when rendering.
|
122 |
*/
|
123 |
public CharacterMarkerSymbol(Font font, int charCode, Color color) { |
124 |
super();
|
125 |
this.font = font;
|
126 |
symbol = charCode; |
127 |
this.color = color;
|
128 |
} |
129 |
|
130 |
public Font getFont() { |
131 |
if (font == null) { |
132 |
font = new Font("Arial", Font.PLAIN, 20); |
133 |
} |
134 |
return font;
|
135 |
} |
136 |
|
137 |
public void setFont(Font font) { |
138 |
this.font = font;
|
139 |
} |
140 |
|
141 |
public ISymbol getSymbolForSelection() {
|
142 |
// TODO Auto-generated method stub
|
143 |
throw new Error("Not yet implemented!"); |
144 |
} |
145 |
|
146 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) { |
147 |
g.setFont(getFont()); |
148 |
g.setColor(color); |
149 |
g.rotate(getRotation()); |
150 |
Point2D p = null; |
151 |
switch (shp.getShapeType()) {
|
152 |
case FShape.POINT:
|
153 |
p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp) |
154 |
.getY()); |
155 |
// p = affineTransform.transform(p, null);
|
156 |
break;
|
157 |
case FShape.LINE:
|
158 |
PathLength pathLen = new PathLength(shp);
|
159 |
float midDistance = pathLen.lengthOfPath() / 2; |
160 |
p = pathLen.pointAtLength(midDistance); |
161 |
// p = affineTransform.transform(p, null);
|
162 |
break;
|
163 |
case FShape.POLYGON:
|
164 |
/*Geometry geom = FConverter.java2d_to_jts(shp);
|
165 |
Point centroid = geom.getCentroid();
|
166 |
p = new Point2D.Double(centroid.getX(), centroid.getY());
|
167 |
*/
|
168 |
} |
169 |
|
170 |
char[] text = new char[] { (char) symbol }; |
171 |
g.drawChars(text, 0, text.length, (int) p.getX(), (int) p.getY()); |
172 |
} |
173 |
|
174 |
public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, |
175 |
Shape shp) {
|
176 |
// TODO Auto-generated method stub
|
177 |
throw new Error("Not yet implemented!"); |
178 |
} |
179 |
|
180 |
public int getOnePointRgb() { |
181 |
return color.getRGB();
|
182 |
} |
183 |
|
184 |
public XMLEntity getXMLEntity() {
|
185 |
XMLEntity xml = new XMLEntity();
|
186 |
|
187 |
// the class name
|
188 |
xml.putProperty("className", getClassName());
|
189 |
|
190 |
// color
|
191 |
xml.putProperty("color", StringUtilities.color2String(color));
|
192 |
|
193 |
// font
|
194 |
xml.putProperty("font", font.getFontName());
|
195 |
|
196 |
// font style
|
197 |
xml.putProperty("fontStyle", font.getStyle());
|
198 |
|
199 |
// symbol code
|
200 |
xml.putProperty("symbolCode", symbol);
|
201 |
|
202 |
// description
|
203 |
xml.putProperty("desc", desc);
|
204 |
|
205 |
// is shape visible
|
206 |
xml.putProperty("isShapeVisible", isShapeVisible);
|
207 |
|
208 |
// x offset
|
209 |
xml.putProperty("xOffset", getOffset().getX());
|
210 |
|
211 |
// y offset
|
212 |
xml.putProperty("yOffset", getOffset().getY());
|
213 |
|
214 |
// rotation
|
215 |
xml.putProperty("rotation", rotation);
|
216 |
return xml;
|
217 |
} |
218 |
|
219 |
public int getSymbolType() { |
220 |
return CHARACTER_MARKER;
|
221 |
} |
222 |
|
223 |
public void drawInsideRectangle(Graphics2D g, |
224 |
AffineTransform scaleInstance, Rectangle r) { |
225 |
g.rotate(getRotation()); |
226 |
// Sirve de algo el scaleInstance???
|
227 |
// g.setFont(font.deriveFont(scaleInstance));
|
228 |
double h = r.getHeight();
|
229 |
Font myFont = font.deriveFont(
|
230 |
(float) (h * FConstant.FONT_HEIGHT_SCALE_FACTOR)).deriveFont(
|
231 |
scaleInstance); |
232 |
g.setFont(myFont); |
233 |
g.setColor(color); |
234 |
r.y = (int) h;
|
235 |
|
236 |
// center character in case the rectangle is not square
|
237 |
int x = r.x;
|
238 |
int y = r.y;
|
239 |
int width = (int) r.getHeight(); |
240 |
int height = (int) r.getWidth(); |
241 |
int diff = (width-height);
|
242 |
if (diff<0) { |
243 |
x -= diff /2;
|
244 |
} else {
|
245 |
y += diff /2;
|
246 |
} |
247 |
|
248 |
// and apply the offset
|
249 |
x = x + (int) offset.getX(); // TODO scale offset |
250 |
y = y + (int) offset.getY(); // TODO scale offset |
251 |
char[] text = new char[] { (char) symbol }; |
252 |
g.drawChars(text, 0, text.length, x, y);
|
253 |
|
254 |
// finally, draw the subsymbols if any
|
255 |
if (subSymbols!=null) { |
256 |
for (int i = 0; i < subSymbols.length; i++) { |
257 |
subSymbols[i].drawInsideRectangle(g, scaleInstance, r); |
258 |
} |
259 |
} |
260 |
} |
261 |
|
262 |
public void setCharacter(int symbol) { |
263 |
this.symbol = symbol;
|
264 |
} |
265 |
|
266 |
public String getClassName() { |
267 |
return this.getClass().getName(); |
268 |
} |
269 |
|
270 |
public void setXMLEntity(XMLEntity xml) { |
271 |
color = StringUtilities.string2Color(xml.getStringProperty("color"));
|
272 |
Point p = new Point(); |
273 |
p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset")); |
274 |
|
275 |
desc = xml.getStringProperty("desc");
|
276 |
font = new Font(xml.getStringProperty("font"), |
277 |
xml.getIntProperty("fontStyle"),
|
278 |
100 /* or whatever*/); |
279 |
isShapeVisible = xml.getBooleanProperty("isShapeVisible");
|
280 |
symbol = xml.getIntProperty("symbolCode");
|
281 |
offset = p; |
282 |
rotation = xml.getDoubleProperty("rotation");
|
283 |
|
284 |
} |
285 |
} |