svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameText.java @ 1551
History | View | Annotate | Download (8.73 KB)
1 |
/*
|
---|---|
2 |
* Created on 22-jun-2004
|
3 |
*
|
4 |
*/
|
5 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
6 |
*
|
7 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
8 |
*
|
9 |
* This program is free software; you can redistribute it and/or
|
10 |
* modify it under the terms of the GNU General Public License
|
11 |
* as published by the Free Software Foundation; either version 2
|
12 |
* of the License, or (at your option) any later version.
|
13 |
*
|
14 |
* This program is distributed in the hope that it will be useful,
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
* GNU General Public License for more details.
|
18 |
*
|
19 |
* You should have received a copy of the GNU General Public License
|
20 |
* along with this program; if not, write to the Free Software
|
21 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
22 |
*
|
23 |
* For more information, contact:
|
24 |
*
|
25 |
* Generalitat Valenciana
|
26 |
* Conselleria d'Infraestructures i Transport
|
27 |
* Av. Blasco Ib??ez, 50
|
28 |
* 46010 VALENCIA
|
29 |
* SPAIN
|
30 |
*
|
31 |
* +34 963862235
|
32 |
* gvsig@gva.es
|
33 |
* www.gvsig.gva.es
|
34 |
*
|
35 |
* or
|
36 |
*
|
37 |
* IVER T.I. S.A
|
38 |
* Salamanca 50
|
39 |
* 46005 Valencia
|
40 |
* Spain
|
41 |
*
|
42 |
* +34 963163400
|
43 |
* dac@iver.es
|
44 |
*/
|
45 |
package com.iver.cit.gvsig.gui.layout.fframes; |
46 |
|
47 |
import com.iver.andami.PluginServices; |
48 |
|
49 |
import com.iver.cit.gvsig.gui.layout.Layout; |
50 |
|
51 |
import com.iver.utiles.StringUtilities; |
52 |
import com.iver.utiles.XMLEntity; |
53 |
|
54 |
import java.awt.Color; |
55 |
import java.awt.Font; |
56 |
import java.awt.Graphics2D; |
57 |
import java.awt.font.FontRenderContext; |
58 |
import java.awt.font.TextLayout; |
59 |
import java.awt.geom.AffineTransform; |
60 |
import java.awt.geom.Rectangle2D; |
61 |
import java.awt.image.BufferedImage; |
62 |
|
63 |
import java.util.ArrayList; |
64 |
|
65 |
|
66 |
/**
|
67 |
* FFrame para introducir un texto en el Layout.
|
68 |
*
|
69 |
* @author Vicente Caballero Navarro
|
70 |
*/
|
71 |
public class FFrameText extends FFrame { |
72 |
/** Localizaci?n del texto. */
|
73 |
public static final int LEFT = 0; |
74 |
public static final int CENTER = 1; |
75 |
public static final int RIGTH = 2; |
76 |
private ArrayList m_text = new ArrayList(); |
77 |
private boolean m_isFixed = true; |
78 |
private double m_rotation = 0; |
79 |
private int m_pos = LEFT; |
80 |
private Color textColor = Color.BLACK; |
81 |
|
82 |
//private int m_space=0;
|
83 |
private Font m_f = null; |
84 |
|
85 |
/**
|
86 |
* Crea un nuevo FFrameText.
|
87 |
*/
|
88 |
public FFrameText() {
|
89 |
m_f = new Font("SansSerif", Font.PLAIN, 9); |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Inserta la fuente del texto.
|
94 |
*
|
95 |
* @param f Fuente del texto.
|
96 |
*/
|
97 |
public void setFont(Font f) { |
98 |
m_f = f; |
99 |
} |
100 |
|
101 |
/**
|
102 |
* Devuelve el color del texto del FFrameText.
|
103 |
*
|
104 |
* @return Color del texto.
|
105 |
*/
|
106 |
public Color getTextColor() { |
107 |
return textColor;
|
108 |
} |
109 |
|
110 |
/**
|
111 |
* Inserta el color del texto a escribir.
|
112 |
*
|
113 |
* @param color Color del texto.
|
114 |
*/
|
115 |
public void setTextColor(Color color) { |
116 |
textColor = color; |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Devuelve la fuente del texto.
|
121 |
*
|
122 |
* @return Fuente del texto.
|
123 |
*/
|
124 |
public Font getFont() { |
125 |
return new Font(m_f.getFontName(), m_f.getStyle(), 9); |
126 |
} |
127 |
|
128 |
/*public void setSpace(int s){
|
129 |
m_space=s;
|
130 |
}
|
131 |
public int getSpace(){
|
132 |
return m_space;
|
133 |
}*/
|
134 |
|
135 |
/**
|
136 |
* Devuelve la posici?n izquierda, centro o derecha del texto.
|
137 |
*
|
138 |
* @return Posici?n del texto.
|
139 |
*/
|
140 |
public int getPos() { |
141 |
return m_pos;
|
142 |
} |
143 |
|
144 |
/**
|
145 |
* Pone la posici?n izquierda, centro o derecha del texto.
|
146 |
*
|
147 |
* @param p 0=LEFT,1=CENTER,2=RIGTH.
|
148 |
*/
|
149 |
public void setPos(int p) { |
150 |
m_pos = p; |
151 |
} |
152 |
|
153 |
/**
|
154 |
* M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
|
155 |
* la transformada afin que se debe de aplicar y el rect?ngulo que se debe
|
156 |
* de dibujar.
|
157 |
*
|
158 |
* @param g Graphics
|
159 |
* @param at Transformada af?n.
|
160 |
* @param rv rect?ngulo sobre el que hacer un clip.
|
161 |
* @param imgBase Imagen para acelerar el dibujado.
|
162 |
*/
|
163 |
public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv, |
164 |
BufferedImage imgBase) {
|
165 |
Rectangle2D.Double re = getBoundingBox(at);
|
166 |
int longmax = 1; |
167 |
|
168 |
if (intersects(rv, re)) {
|
169 |
if (m_text.isEmpty()) {
|
170 |
drawEmpty(g); |
171 |
} else {
|
172 |
for (int i = 0; i < m_text.size(); i++) { |
173 |
if (((String) m_text.get(i)).length() > longmax) { |
174 |
longmax = ((String) m_text.get(i)).length();
|
175 |
} |
176 |
} |
177 |
|
178 |
FontRenderContext frc = g.getFontRenderContext();
|
179 |
int scale = ((int) (re.width * 2.2)) / longmax; |
180 |
|
181 |
if (scale > (int) ((re.height * 10) / 4 / (m_text.size() * 1.3))) { |
182 |
scale = (int) (((re.height * 10) / 4) / (m_text.size() * 1.3)); |
183 |
} |
184 |
|
185 |
int ht = (int) (re.height / m_text.size()); |
186 |
|
187 |
if (m_f != null) { |
188 |
m_f = new Font(m_f.getFontName(), m_f.getStyle(), scale); |
189 |
} else {
|
190 |
m_f = new Font("SansSerif", Font.PLAIN, scale); |
191 |
} |
192 |
|
193 |
for (int i = 0; i < m_text.size(); i++) { |
194 |
if (!((String) m_text.get(i)).equals("")) { |
195 |
TextLayout layout = new TextLayout((String) m_text.get( |
196 |
i), m_f, frc); |
197 |
g.setColor(textColor); |
198 |
g.rotate(Math.toRadians(m_rotation),
|
199 |
re.x + (re.width / 2), re.y + (re.height / 2)); |
200 |
|
201 |
int l = ((String) m_text.get(i)).length(); |
202 |
|
203 |
switch (m_pos) {
|
204 |
case (LEFT):
|
205 |
layout.draw(g, (float) re.getX(),
|
206 |
(float) (re.getY() + (ht * (i + 1)))); //- (ht / 2)))); |
207 |
|
208 |
break;
|
209 |
|
210 |
case (CENTER):
|
211 |
|
212 |
float pos1 = (float) ((re.width - |
213 |
((l * scale) / 2.2)) / 2); |
214 |
layout.draw(g, (float) re.getX() + pos1,
|
215 |
(float) (re.getY() + (ht * (i + 1)))); //- (ht / 2)))); |
216 |
|
217 |
break;
|
218 |
|
219 |
case (RIGTH):
|
220 |
|
221 |
float pos2 = (float) ((re.width - |
222 |
((l * scale) / 2.2)));
|
223 |
layout.draw(g, (float) re.getX() + pos2,
|
224 |
(float) (re.getY() + (ht * (i + 1)))); //- (ht / 2)))); |
225 |
|
226 |
break;
|
227 |
} |
228 |
|
229 |
g.rotate(Math.toRadians(-m_rotation),
|
230 |
re.x + (re.width / 2), re.y + (re.height / 2)); |
231 |
} |
232 |
} |
233 |
} |
234 |
} |
235 |
} |
236 |
|
237 |
/**
|
238 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
|
239 |
* java.awt.geom.AffineTransform)
|
240 |
*/
|
241 |
public void print(Graphics2D g, AffineTransform at) { |
242 |
draw(g, at, null, null); |
243 |
} |
244 |
|
245 |
/**
|
246 |
* Rellenar el texto que se quiere a?adir al Layout.
|
247 |
*
|
248 |
* @param s String a a?adir.
|
249 |
*/
|
250 |
public void addText(String s) { |
251 |
m_text.add(s); |
252 |
} |
253 |
|
254 |
/**
|
255 |
* Devuelve el ArrayList que contiene las l?neas de texto.
|
256 |
*
|
257 |
* @return ArrayList.
|
258 |
*/
|
259 |
public ArrayList getText() { |
260 |
return m_text;
|
261 |
} |
262 |
|
263 |
/**
|
264 |
* Seleccionar si se quiere un tama?o fijo o adecuado a la escala.
|
265 |
*
|
266 |
* @param b true si se quiere tama?o fijo.
|
267 |
*/
|
268 |
public void setSizeFixed(boolean b) { |
269 |
m_isFixed = b; |
270 |
} |
271 |
|
272 |
/**
|
273 |
* Devuelve si est? fijado el tama?o.
|
274 |
*
|
275 |
* @return True si est? fijado el tama?o.
|
276 |
*/
|
277 |
public boolean isSizeFixed() { |
278 |
return m_isFixed;
|
279 |
} |
280 |
|
281 |
/**
|
282 |
* Rellenar la rotaci?n para aplicar al texto.
|
283 |
*
|
284 |
* @param rotation rotaci?n que se quiere aplicar.
|
285 |
*/
|
286 |
public void setRotation(double rotation) { |
287 |
m_rotation = rotation; |
288 |
} |
289 |
|
290 |
/**
|
291 |
* Devuelve la rotaci?n del texto.
|
292 |
*
|
293 |
* @return Rotaci?n del texto.
|
294 |
*/
|
295 |
public double getRotation() { |
296 |
return m_rotation;
|
297 |
} |
298 |
|
299 |
/**
|
300 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
|
301 |
*/
|
302 |
public XMLEntity getXMLEntity() {
|
303 |
XMLEntity xml = new XMLEntity();
|
304 |
xml.putProperty("className", this.getClass().getName()); |
305 |
xml.putProperty("m_name", m_name);
|
306 |
xml.putProperty("x", getBoundBox().x);
|
307 |
xml.putProperty("y", getBoundBox().y);
|
308 |
xml.putProperty("w", getBoundBox().width);
|
309 |
xml.putProperty("h", getBoundBox().height);
|
310 |
xml.putProperty("m_Selected", m_Selected);
|
311 |
xml.putProperty("type", Layout.RECTANGLETEXT);
|
312 |
|
313 |
String[] s = (String[]) m_text.toArray(new String[0]); |
314 |
xml.putProperty("s", s);
|
315 |
xml.putProperty("m_isFixed", m_isFixed);
|
316 |
|
317 |
xml.putProperty("m_pos", m_pos);
|
318 |
xml.putProperty("m_rotation", m_rotation);
|
319 |
xml.putProperty("fontName", m_f.getFontName());
|
320 |
xml.putProperty("fontStyle", m_f.getStyle());
|
321 |
xml.putProperty("tag", getTag());
|
322 |
xml.putProperty("textColor",StringUtilities.color2String(textColor));
|
323 |
|
324 |
return xml;
|
325 |
} |
326 |
|
327 |
/**
|
328 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
|
329 |
* com.iver.cit.gvsig.project.Project)
|
330 |
*/
|
331 |
public void setXMLEntity(XMLEntity xml, Layout l) { |
332 |
if (xml.getIntProperty("m_Selected") != 0) { |
333 |
this.setSelected(true); |
334 |
} else {
|
335 |
this.setSelected(false); |
336 |
} |
337 |
|
338 |
String[] s = xml.getStringArrayProperty("s"); |
339 |
|
340 |
for (int i = 0; i < s.length; i++) { |
341 |
this.m_text.add(s[i]);
|
342 |
} |
343 |
|
344 |
this.m_isFixed = xml.getBooleanProperty("m_isFixed"); |
345 |
this.m_pos = xml.getIntProperty("m_pos"); |
346 |
this.m_rotation = xml.getDoubleProperty("m_rotation"); |
347 |
this.m_f = new Font(xml.getStringProperty("fontName"), |
348 |
xml.getIntProperty("fontStyle"), 9); |
349 |
if (xml.contains("textColor")){//TODO Incluido en la versi?n 0.3 en posteriores versiones no hace falta esta comprobaci?n. |
350 |
this.textColor = StringUtilities.string2Color(xml.getStringProperty("textColor")); |
351 |
} |
352 |
} |
353 |
|
354 |
/**
|
355 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
|
356 |
*/
|
357 |
public String getNameFFrame() { |
358 |
return PluginServices.getText(this, "texto") + num; |
359 |
} |
360 |
} |