svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameText.java @ 596
History | View | Annotate | Download (9.14 KB)
1 |
/*
|
---|---|
2 |
* Created on 22-jun-2004
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
package com.iver.cit.gvsig.gui.layout.fframes; |
8 |
|
9 |
import java.awt.Color; |
10 |
import java.awt.Font; |
11 |
import java.awt.Graphics2D; |
12 |
import java.awt.font.FontRenderContext; |
13 |
import java.awt.font.TextLayout; |
14 |
import java.awt.geom.AffineTransform; |
15 |
import java.awt.geom.Rectangle2D; |
16 |
import java.awt.image.BufferedImage; |
17 |
import java.util.ArrayList; |
18 |
|
19 |
import com.iver.andami.PluginServices; |
20 |
import com.iver.cit.gvsig.gui.layout.Layout; |
21 |
import com.iver.cit.gvsig.project.castor.Project; |
22 |
import com.iver.utiles.XMLEntity; |
23 |
|
24 |
|
25 |
/**
|
26 |
* FFrame para introducir un texto en el Layout.
|
27 |
*
|
28 |
* @author Vicente Caballero Navarro
|
29 |
*/
|
30 |
public class FFrameText extends FFrame { |
31 |
/** DOCUMENT ME! */
|
32 |
public static final int LEFT = 0; |
33 |
|
34 |
/** DOCUMENT ME! */
|
35 |
public static final int CENTER = 1; |
36 |
|
37 |
/** DOCUMENT ME! */
|
38 |
public static final int RIGTH = 2; |
39 |
private ArrayList m_text = new ArrayList(); |
40 |
private boolean m_isFixed = true; |
41 |
private double m_rotation = 0; |
42 |
private int m_pos = LEFT; |
43 |
|
44 |
//private int m_space=0;
|
45 |
private Font m_f = null; |
46 |
|
47 |
/**
|
48 |
* Crea un nuevo FFrameText.
|
49 |
*/
|
50 |
public FFrameText() {
|
51 |
m_f = new Font("SansSerif", Font.PLAIN, 9); |
52 |
} |
53 |
|
54 |
/**
|
55 |
* DOCUMENT ME!
|
56 |
*
|
57 |
* @param f DOCUMENT ME!
|
58 |
*/
|
59 |
public void setFont(Font f) { |
60 |
m_f = f; |
61 |
} |
62 |
|
63 |
/**
|
64 |
* DOCUMENT ME!
|
65 |
*
|
66 |
* @return DOCUMENT ME!
|
67 |
*/
|
68 |
public Font getFont() { |
69 |
return new Font(m_f.getFontName(), m_f.getStyle(), 9); |
70 |
} |
71 |
|
72 |
/*public void setSpace(int s){
|
73 |
m_space=s;
|
74 |
}
|
75 |
public int getSpace(){
|
76 |
return m_space;
|
77 |
}*/
|
78 |
|
79 |
/**
|
80 |
* Devuelve la posici?n izquierda, centro o derecha del texto.
|
81 |
*
|
82 |
* @return DOCUMENT ME!
|
83 |
*/
|
84 |
public int getPos() { |
85 |
return m_pos;
|
86 |
} |
87 |
|
88 |
/**
|
89 |
* Pone la posici?n izquierda, centro o derecha del texto.
|
90 |
*
|
91 |
* @param p 0=LEFT,1=CENTER,2=RIGTH.
|
92 |
*/
|
93 |
public void setPos(int p) { |
94 |
m_pos = p; |
95 |
} |
96 |
|
97 |
/**
|
98 |
* M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
|
99 |
* la transformada afin que se debe de aplicar y el rect?ngulo que se debe
|
100 |
* de dibujar.
|
101 |
*
|
102 |
* @param g Graphics
|
103 |
* @param at Transformada af?n.
|
104 |
* @param rv rect?ngulo sobre el que hacer un clip.
|
105 |
* @param imgBase DOCUMENT ME!
|
106 |
*/
|
107 |
public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv, |
108 |
BufferedImage imgBase) {
|
109 |
Rectangle2D.Double re = getBoundingBox(at);
|
110 |
int longmax = 1; |
111 |
|
112 |
if (intersects(rv, re)) {
|
113 |
if (m_text.isEmpty()) {
|
114 |
drawEmpty(g); |
115 |
} else {
|
116 |
for (int i = 0; i < m_text.size(); i++) { |
117 |
if (((String) m_text.get(i)).length() > longmax) { |
118 |
longmax = ((String) m_text.get(i)).length();
|
119 |
} |
120 |
} |
121 |
|
122 |
FontRenderContext frc = g.getFontRenderContext();
|
123 |
int scale = ((int) (re.width * 2.2)) / longmax; |
124 |
if (scale>(int)(re.height*10/4/(m_text.size()*1.3))){ |
125 |
scale=(int)((re.height*10/4)/(m_text.size()*1.3)); |
126 |
} |
127 |
int ht = (int) (re.height / m_text.size()); |
128 |
|
129 |
if (m_f != null) { |
130 |
m_f = new Font(m_f.getFontName(), m_f.getStyle(), scale); |
131 |
} else {
|
132 |
m_f = new Font("SansSerif", Font.PLAIN, scale); |
133 |
} |
134 |
|
135 |
for (int i = 0; i < m_text.size(); i++) { |
136 |
if (!((String) m_text.get(i)).equals("")) { |
137 |
TextLayout layout = new TextLayout((String) m_text.get( |
138 |
i), m_f, frc); |
139 |
g.setColor(Color.black);
|
140 |
g.rotate(Math.toRadians(m_rotation),
|
141 |
re.x + (re.width / 2), re.y + (re.height / 2)); |
142 |
|
143 |
int l = ((String) m_text.get(i)).length(); |
144 |
|
145 |
switch (m_pos) {
|
146 |
case (LEFT):
|
147 |
layout.draw(g, (float) re.getX(),
|
148 |
(float) (re.getY() + (ht * (i + 1)))); //- (ht / 2)))); |
149 |
|
150 |
break;
|
151 |
|
152 |
case (CENTER):
|
153 |
|
154 |
float pos1 = (float) ((re.width - |
155 |
((l * scale) / 2.2)) / 2); |
156 |
layout.draw(g, (float) re.getX() + pos1,
|
157 |
(float) (re.getY() + (ht * (i + 1)))); //- (ht / 2)))); |
158 |
|
159 |
break;
|
160 |
|
161 |
case (RIGTH):
|
162 |
|
163 |
float pos2 = (float) ((re.width - |
164 |
((l * scale) / 2.2)));
|
165 |
layout.draw(g, (float) re.getX() + pos2,
|
166 |
(float) (re.getY() + (ht * (i + 1)))); //- (ht / 2)))); |
167 |
|
168 |
break;
|
169 |
} |
170 |
|
171 |
g.rotate(Math.toRadians(-m_rotation),
|
172 |
re.x + (re.width / 2), re.y + (re.height / 2)); |
173 |
} |
174 |
} |
175 |
} |
176 |
} |
177 |
} |
178 |
|
179 |
/**
|
180 |
* Rellenar el texto que se quiere a?adir al Layout.
|
181 |
*
|
182 |
* @param s String a a?adir.
|
183 |
*/
|
184 |
public void addText(String s) { |
185 |
m_text.add(s); |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Devuelve el ArrayList que contiene las l?neas de texto.
|
190 |
*
|
191 |
* @return ArrayList.
|
192 |
*/
|
193 |
public ArrayList getText() { |
194 |
return m_text;
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* Seleccionar si se quiere un tama?o fijo o adecuado a la escala.
|
199 |
*
|
200 |
* @param b true si se quiere tama?o fijo.
|
201 |
*/
|
202 |
public void setSizeFixed(boolean b) { |
203 |
m_isFixed = b; |
204 |
} |
205 |
|
206 |
/**
|
207 |
* DOCUMENT ME!
|
208 |
*
|
209 |
* @return DOCUMENT ME!
|
210 |
*/
|
211 |
public boolean isSizeFixed() { |
212 |
return m_isFixed;
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* Rellenar la rotaci?n para aplicar al texto.
|
217 |
*
|
218 |
* @param rotation rotaci?n que se quiere aplicar.
|
219 |
*/
|
220 |
public void setRotation(double rotation) { |
221 |
m_rotation = rotation; |
222 |
} |
223 |
|
224 |
/**
|
225 |
* DOCUMENT ME!
|
226 |
*
|
227 |
* @return DOCUMENT ME!
|
228 |
*/
|
229 |
public double getRotation() { |
230 |
return m_rotation;
|
231 |
} |
232 |
|
233 |
/**
|
234 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
|
235 |
*/
|
236 |
public XMLEntity getXMLEntity() {
|
237 |
XMLEntity xml = new XMLEntity();
|
238 |
xml.putProperty("nameClass", this.getClass().getName()); |
239 |
xml.putProperty("m_name", m_name);
|
240 |
xml.putProperty("x", getBoundBox().x);
|
241 |
xml.putProperty("y", getBoundBox().y);
|
242 |
xml.putProperty("w", getBoundBox().width);
|
243 |
xml.putProperty("h", getBoundBox().height);
|
244 |
xml.putProperty("m_Selected", m_Selected);
|
245 |
xml.putProperty("type", Layout.RECTANGLETEXT);
|
246 |
|
247 |
String[] s = (String[]) m_text.toArray(new String[0]); |
248 |
xml.putProperty("s", s);
|
249 |
xml.putProperty("m_isFixed", m_isFixed);
|
250 |
|
251 |
xml.putProperty("m_pos", m_pos);
|
252 |
xml.putProperty("m_rotation", m_rotation);
|
253 |
xml.putProperty("fontName", m_f.getFontName());
|
254 |
xml.putProperty("fontStyle", m_f.getStyle());
|
255 |
xml.putProperty("tag",getTag());
|
256 |
|
257 |
return xml;
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
|
262 |
*
|
263 |
* @param xml XMLEntity
|
264 |
* @param p DOCUMENT ME!
|
265 |
*/
|
266 |
|
267 |
/* public static FFrameText createFFrameText(XMLEntity xml) {
|
268 |
FFrameText fframe = new FFrameText();
|
269 |
|
270 |
if (xml.getIntProperty("m_Selected") != 0) {
|
271 |
fframe.setSelected(true);
|
272 |
} else {
|
273 |
fframe.setSelected(false);
|
274 |
}
|
275 |
String[] s = xml.getStringArrayProperty("s");
|
276 |
for (int i = 0; i < s.length; i++) {
|
277 |
fframe.m_text.add(s[i]);
|
278 |
}
|
279 |
fframe.m_isFixed = xml.getBooleanProperty("m_isFixed");
|
280 |
fframe.m_pos = xml.getIntProperty("m_pos");
|
281 |
fframe.m_rotation = xml.getDoubleProperty("m_rotation");
|
282 |
fframe.m_f = new Font(xml.getStringProperty("fontName"),
|
283 |
xml.getIntProperty("fontStyle"), 9);
|
284 |
return fframe;
|
285 |
}
|
286 |
*/
|
287 |
|
288 |
/**
|
289 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
|
290 |
* com.iver.cit.gvsig.project.Project)
|
291 |
*/
|
292 |
public void setXMLEntity(XMLEntity xml, Project p) { |
293 |
if (xml.getIntProperty("m_Selected") != 0) { |
294 |
this.setSelected(true); |
295 |
} else {
|
296 |
this.setSelected(false); |
297 |
} |
298 |
|
299 |
String[] s = xml.getStringArrayProperty("s"); |
300 |
|
301 |
for (int i = 0; i < s.length; i++) { |
302 |
this.m_text.add(s[i]);
|
303 |
} |
304 |
|
305 |
this.m_isFixed = xml.getBooleanProperty("m_isFixed"); |
306 |
this.m_pos = xml.getIntProperty("m_pos"); |
307 |
this.m_rotation = xml.getDoubleProperty("m_rotation"); |
308 |
this.m_f = new Font(xml.getStringProperty("fontName"), |
309 |
xml.getIntProperty("fontStyle"), 9); |
310 |
} |
311 |
|
312 |
/**
|
313 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
|
314 |
*/
|
315 |
public String getNameFFrame() { |
316 |
return PluginServices.getText(this, "texto") + num; |
317 |
} |
318 |
|
319 |
} |