svn-gvsig-desktop / tags / gvSIGv0_6_1RELEASE / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFramePicture.java @ 5222
History | View | Annotate | Download (11.8 KB)
1 | 312 | fernando | /*
|
---|---|---|---|
2 | * Created on 20-feb-2004
|
||
3 | *
|
||
4 | */
|
||
5 | 1103 | fjp | /* 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 | 312 | fernando | package com.iver.cit.gvsig.gui.layout.fframes; |
46 | |||
47 | 2429 | caballero | import java.awt.Dimension; |
48 | import java.awt.Graphics2D; |
||
49 | import java.awt.Image; |
||
50 | import java.awt.RenderingHints; |
||
51 | import java.awt.geom.AffineTransform; |
||
52 | import java.awt.geom.Rectangle2D; |
||
53 | import java.awt.image.BufferedImage; |
||
54 | import java.io.File; |
||
55 | 1073 | vcaballero | |
56 | 2429 | caballero | import javax.swing.ImageIcon; |
57 | 1073 | vcaballero | |
58 | 2209 | vcaballero | import org.apache.batik.bridge.BridgeContext; |
59 | import org.apache.batik.bridge.DocumentLoader; |
||
60 | import org.apache.batik.bridge.GVTBuilder; |
||
61 | import org.apache.batik.bridge.UserAgentAdapter; |
||
62 | import org.apache.batik.bridge.ViewBox; |
||
63 | import org.apache.batik.gvt.GraphicsNode; |
||
64 | import org.apache.batik.gvt.renderer.StaticRenderer; |
||
65 | import org.w3c.dom.Document; |
||
66 | import org.w3c.dom.Element; |
||
67 | import org.w3c.dom.svg.SVGDocument; |
||
68 | |||
69 | 2429 | caballero | import com.iver.andami.PluginServices; |
70 | import com.iver.andami.messages.NotificationManager; |
||
71 | import com.iver.cit.gvsig.fmap.DriverException; |
||
72 | import com.iver.cit.gvsig.gui.layout.Layout; |
||
73 | 3183 | caballero | import com.iver.cit.gvsig.gui.project.SaveException; |
74 | 2429 | caballero | import com.iver.utiles.XMLEntity; |
75 | import com.sun.jimi.core.Jimi; |
||
76 | 312 | fernando | |
77 | 2209 | vcaballero | |
78 | 312 | fernando | /**
|
79 | 2209 | vcaballero | * FFrame para introducir una imagen en el Layout o para dibujar sobre el
|
80 | * graphics un SVG.
|
||
81 | 312 | fernando | *
|
82 | * @author Vicente Caballero Navarro
|
||
83 | */
|
||
84 | public class FFramePicture extends FFrame { |
||
85 | 2209 | vcaballero | protected static RenderingHints defaultRenderingHints; |
86 | |||
87 | static {
|
||
88 | defaultRenderingHints = new RenderingHints(null); |
||
89 | defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
|
||
90 | RenderingHints.VALUE_ANTIALIAS_ON);
|
||
91 | |||
92 | defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
|
||
93 | RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||
94 | } |
||
95 | |||
96 | 649 | vcaballero | private static final int PRESENTACION = 0; |
97 | private static final int ACTIVO = 1; |
||
98 | private BufferedImage m_image = null; |
||
99 | private int m_quality = PRESENTACION; |
||
100 | private int m_viewing = ACTIVO; |
||
101 | private String m_path = null; |
||
102 | 2209 | vcaballero | private boolean isSVG = false; |
103 | private StaticRenderer renderer = new StaticRenderer(); |
||
104 | private Element elt; |
||
105 | private GVTBuilder gvtBuilder = new GVTBuilder(); |
||
106 | 2915 | caballero | private GraphicsNode gvtRoot = null; |
107 | 398 | vcaballero | |
108 | 649 | vcaballero | /**
|
109 | * Creates a new FFramePicture object.
|
||
110 | */
|
||
111 | public FFramePicture() {
|
||
112 | } |
||
113 | 312 | fernando | |
114 | 649 | vcaballero | /**
|
115 | * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
|
||
116 | * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
|
||
117 | * de dibujar.
|
||
118 | *
|
||
119 | * @param g Graphics
|
||
120 | * @param at Transformada afin.
|
||
121 | * @param rv rect?ngulo sobre el que hacer un clip.
|
||
122 | 1073 | vcaballero | * @param imgBase Imagen para acelerar el dibujado.
|
123 | 649 | vcaballero | */
|
124 | public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv, |
||
125 | BufferedImage imgBase) {
|
||
126 | Rectangle2D.Double r = getBoundingBox(at);
|
||
127 | 2304 | vcaballero | g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), |
128 | r.y + (r.height / 2));
|
||
129 | 649 | vcaballero | double x = r.getMinX();
|
130 | double y = r.getMinY();
|
||
131 | double w = r.getWidth();
|
||
132 | double h = r.getHeight();
|
||
133 | 312 | fernando | |
134 | 649 | vcaballero | if (intersects(rv, r)) {
|
135 | 2209 | vcaballero | if ((m_image == null) && !isSVG) { //Que no hay una imagen. |
136 | 649 | vcaballero | drawEmpty(g); |
137 | } else {
|
||
138 | if ((rv == null) || (getQuality() == PRESENTACION)) { |
||
139 | 2209 | vcaballero | if (!isSVG) {
|
140 | double scalex = (double) w / m_image.getWidth(null); |
||
141 | double scaley = (double) h / m_image.getHeight(null); |
||
142 | AffineTransform xform = AffineTransform.getScaleInstance(scalex, |
||
143 | scaley); |
||
144 | AffineTransform xpos = AffineTransform.getTranslateInstance(x, |
||
145 | y); |
||
146 | xpos.concatenate(xform); |
||
147 | g.drawRenderedImage(m_image, xpos); |
||
148 | } else if (isSVG) { |
||
149 | 2915 | caballero | try {if(r!=null) |
150 | 2209 | vcaballero | drawSVG(g, r, rv, at); |
151 | } catch (OutOfMemoryError e) { |
||
152 | System.out.println("Dibujando SVG = " + e); |
||
153 | } catch (IllegalArgumentException e) { |
||
154 | System.out.println("Dibujando SVG = " + e); |
||
155 | } |
||
156 | |||
157 | System.gc();
|
||
158 | } |
||
159 | 649 | vcaballero | } else {
|
160 | drawDraft(g); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | 2304 | vcaballero | g.rotate(Math.toRadians(-getRotation()),
|
165 | r.x + (r.width / 2), r.y + (r.height / 2)); |
||
166 | 649 | vcaballero | } |
167 | 312 | fernando | |
168 | 649 | vcaballero | /**
|
169 | 3135 | caballero | * Dibuja SVG sobre el Graphics que se pasa como par?metro.
|
170 | 2209 | vcaballero | *
|
171 | 3135 | caballero | * @param g Graphics
|
172 | * @param rect rect?ngulo que ocupa.
|
||
173 | * @param rv Rect?ngulo que forma la parte visible del Layout.
|
||
174 | * @param at Matriz de transformaci?n.
|
||
175 | 2209 | vcaballero | */
|
176 | private void drawSVG(Graphics2D g, Rectangle2D rect, Rectangle2D rv, |
||
177 | AffineTransform at) {
|
||
178 | 2429 | caballero | |
179 | 2209 | vcaballero | if ((rv == null) || rv.contains(rect)) { |
180 | AffineTransform ataux = new AffineTransform(); |
||
181 | |||
182 | ataux.translate(rect.getX(), rect.getY()); |
||
183 | 2915 | caballero | try{
|
184 | 2209 | vcaballero | ataux.concatenate(ViewBox.getViewTransform(null, elt,
|
185 | (float) rect.getWidth(), (float) rect.getHeight())); |
||
186 | gvtRoot.setTransform(ataux); |
||
187 | 2915 | caballero | }catch (Exception e) { |
188 | // TODO: handle exception
|
||
189 | } |
||
190 | 2429 | caballero | |
191 | 2915 | caballero | |
192 | 2209 | vcaballero | } else {
|
193 | AffineTransform ataux = new AffineTransform(); |
||
194 | |||
195 | ataux.translate(rect.getX(), rect.getY()); |
||
196 | ataux.concatenate(ViewBox.getViewTransform(null, elt,
|
||
197 | (float) rect.getWidth(), (float) rect.getHeight())); |
||
198 | |||
199 | gvtRoot.setTransform(ataux); |
||
200 | 2429 | caballero | |
201 | 2209 | vcaballero | } |
202 | |||
203 | RenderingHints renderingHints = new RenderingHints(defaultRenderingHints); |
||
204 | g.setRenderingHints(renderingHints); |
||
205 | 2984 | fjp | if (gvtRoot != null) |
206 | gvtRoot.paint(g); |
||
207 | 2209 | vcaballero | } |
208 | |||
209 | 2429 | caballero | |
210 | 2209 | vcaballero | /**
|
211 | 649 | vcaballero | * Rellena la calidad seg?n el entero que se pasa como par?metro.
|
212 | *
|
||
213 | * @param q entero que representa el tipo de calidad elegido.
|
||
214 | */
|
||
215 | public void setQuality(int q) { |
||
216 | m_quality = q; |
||
217 | } |
||
218 | 312 | fernando | |
219 | 649 | vcaballero | /**
|
220 | * Devuelve la calidad que est? seleccionada.
|
||
221 | *
|
||
222 | * @return entero que representa la calidad seleccionada.
|
||
223 | */
|
||
224 | public int getQuality() { |
||
225 | return m_quality;
|
||
226 | } |
||
227 | 312 | fernando | |
228 | 649 | vcaballero | /**
|
229 | * Devuelve un entero que representa la forma en que se actualiza la vista.
|
||
230 | *
|
||
231 | * @return forma que se actualiza la vista.
|
||
232 | */
|
||
233 | public int getViewing() { |
||
234 | return m_viewing;
|
||
235 | } |
||
236 | 312 | fernando | |
237 | 649 | vcaballero | /**
|
238 | * Rellena la forma de actualizar la vista.
|
||
239 | *
|
||
240 | * @param v entero que representa la forma de actualizar la vista.
|
||
241 | */
|
||
242 | public void setViewing(int v) { |
||
243 | m_viewing = v; |
||
244 | } |
||
245 | 312 | fernando | |
246 | 649 | vcaballero | /**
|
247 | * Rellena el nombre de la imagen.
|
||
248 | *
|
||
249 | * @param path nombre de la imagen.
|
||
250 | */
|
||
251 | public void setPath(String path) { |
||
252 | m_path = path; |
||
253 | } |
||
254 | 312 | fernando | |
255 | 649 | vcaballero | /**
|
256 | * Devuelve la ruta del fichero.
|
||
257 | *
|
||
258 | * @return String
|
||
259 | */
|
||
260 | public String getPath() { |
||
261 | return m_path;
|
||
262 | } |
||
263 | 312 | fernando | |
264 | 649 | vcaballero | /**
|
265 | * Rellena la imagen.
|
||
266 | *
|
||
267 | * @param image
|
||
268 | */
|
||
269 | public void setImage(BufferedImage image) { |
||
270 | m_image = image; |
||
271 | } |
||
272 | 312 | fernando | |
273 | 649 | vcaballero | /**
|
274 | 3183 | caballero | * @throws SaveException
|
275 | 649 | vcaballero | * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
|
276 | */
|
||
277 | 3183 | caballero | public XMLEntity getXMLEntity() throws SaveException { |
278 | 649 | vcaballero | XMLEntity xml = new XMLEntity();
|
279 | 3183 | caballero | try{
|
280 | 2209 | vcaballero | xml.putProperty("className", this.getClass().getName()); |
281 | 649 | vcaballero | xml.putProperty("m_name", m_name);
|
282 | xml.putProperty("x", getBoundBox().x);
|
||
283 | xml.putProperty("y", getBoundBox().y);
|
||
284 | xml.putProperty("w", getBoundBox().width);
|
||
285 | xml.putProperty("h", getBoundBox().height);
|
||
286 | xml.putProperty("m_Selected", m_Selected);
|
||
287 | xml.putProperty("type", Layout.RECTANGLEPICTURE);
|
||
288 | xml.putProperty("m_path", m_path);
|
||
289 | xml.putProperty("m_quality", m_quality);
|
||
290 | xml.putProperty("m_viewing", m_viewing);
|
||
291 | xml.putProperty("tag", getTag());
|
||
292 | 2304 | vcaballero | xml.putProperty("m_rotation",getRotation());
|
293 | 3183 | caballero | }catch (Exception e) { |
294 | throw new SaveException(e,this.getClass().getName()); |
||
295 | } |
||
296 | 649 | vcaballero | return xml;
|
297 | } |
||
298 | 2209 | vcaballero | |
299 | /**
|
||
300 | 3135 | caballero | * Devuelve la dimensi?n dela imagen.
|
301 | 2209 | vcaballero | *
|
302 | 3135 | caballero | * @param file Nombre del fichero donde se encuentra la imagen.
|
303 | 2209 | vcaballero | *
|
304 | * @return DOCUMENT ME!
|
||
305 | */
|
||
306 | public Dimension getBound(String file) { |
||
307 | Image img = load(file);
|
||
308 | |||
309 | if (isSVG) {
|
||
310 | return new Dimension(100, 100); |
||
311 | } |
||
312 | if (img==null){ |
||
313 | return new Dimension((int)getBoundingBox(null).getWidth(),(int)getBoundingBox(null).getHeight()); |
||
314 | } |
||
315 | return new Dimension(img.getWidth(null), img.getHeight(null)); |
||
316 | 1305 | fjp | } |
317 | 312 | fernando | |
318 | 649 | vcaballero | /**
|
319 | 3135 | caballero | * Carga el contnido del fichero.
|
320 | 2209 | vcaballero | *
|
321 | 3135 | caballero | * @param file Nombre del fichero a cargar.
|
322 | 2209 | vcaballero | *
|
323 | 3135 | caballero | * @return Imagen
|
324 | 2209 | vcaballero | */
|
325 | public Image load(String file) { |
||
326 | ImageIcon tmpIcon = null; |
||
327 | if (file==null)return null; |
||
328 | String iString = file.toLowerCase();
|
||
329 | |||
330 | if (iString.endsWith("jpg") || iString.endsWith("jpeg") || |
||
331 | iString.endsWith("gif")) {
|
||
332 | tmpIcon = new ImageIcon(Jimi.getImage(file, Jimi.VIRTUAL_MEMORY)); //((File)main.allImages.elementAt(x)).getAbsolutePath()); |
||
333 | } else if (iString.endsWith("png") || iString.endsWith("tif") || |
||
334 | iString.endsWith("ico") || iString.endsWith("xpm") || |
||
335 | iString.endsWith("bmp")) {
|
||
336 | tmpIcon = new ImageIcon(Jimi.getImage(file, Jimi.VIRTUAL_MEMORY)); //new ImageIcon(f.getPath()); |
||
337 | } else if (iString.endsWith("svg")) { |
||
338 | isSVG = true;
|
||
339 | obtainStaticRenderer(new File(file)); |
||
340 | } |
||
341 | |||
342 | if (!isSVG && tmpIcon!=null) { |
||
343 | Image image = tmpIcon.getImage();
|
||
344 | setPath(file); |
||
345 | |||
346 | BufferedImage bi = new BufferedImage((int) image.getWidth(null), |
||
347 | (int) image.getHeight(null), BufferedImage.TYPE_INT_ARGB); |
||
348 | Graphics2D biContext = bi.createGraphics();
|
||
349 | biContext.drawImage(image, 0, 0, null); |
||
350 | |||
351 | setImage(bi); |
||
352 | |||
353 | return image;
|
||
354 | } |
||
355 | |||
356 | return null; |
||
357 | } |
||
358 | |||
359 | /**
|
||
360 | 3135 | caballero | * Obtiene el renderer para svg a partir del svg.
|
361 | 2209 | vcaballero | *
|
362 | 3135 | caballero | * @param file Nombre del fichero.
|
363 | 2209 | vcaballero | */
|
364 | private void obtainStaticRenderer(File file) { |
||
365 | try {
|
||
366 | UserAgentAdapter userAgent = new UserAgentAdapter();
|
||
367 | DocumentLoader loader = new DocumentLoader(userAgent);
|
||
368 | BridgeContext ctx = new BridgeContext(userAgent, loader);
|
||
369 | Document svgDoc = loader.loadDocument(file.toURI().toString());
|
||
370 | gvtRoot = gvtBuilder.build(ctx, svgDoc); |
||
371 | renderer.setTree(gvtRoot); |
||
372 | elt = ((SVGDocument) svgDoc).getRootElement(); |
||
373 | } catch (Exception ex) { |
||
374 | ex.printStackTrace(); |
||
375 | } |
||
376 | } |
||
377 | |||
378 | /**
|
||
379 | 1073 | vcaballero | * Incorpora los atributos del XMLEntity en el objeto actual.
|
380 | 649 | vcaballero | *
|
381 | * @param xml XMLEntity
|
||
382 | 1073 | vcaballero | * @param l Referencia al Layout.
|
383 | 649 | vcaballero | */
|
384 | 2183 | fernando | public void setXMLEntity03(XMLEntity xml, Layout l) { |
385 | if (xml.getIntProperty("m_Selected") != 0) { |
||
386 | this.setSelected(true); |
||
387 | } else {
|
||
388 | this.setSelected(false); |
||
389 | } |
||
390 | |||
391 | this.m_path = xml.getStringProperty("m_path"); |
||
392 | |||
393 | try {
|
||
394 | load(this.m_path);
|
||
395 | } catch (Exception ex) { |
||
396 | NotificationManager.addError("Excepci?n :", ex);
|
||
397 | } |
||
398 | |||
399 | this.m_quality = xml.getIntProperty("m_quality"); |
||
400 | this.m_viewing = xml.getIntProperty("m_viewing"); |
||
401 | } |
||
402 | |||
403 | /**
|
||
404 | * Incorpora los atributos del XMLEntity en el objeto actual.
|
||
405 | *
|
||
406 | * @param xml XMLEntity
|
||
407 | * @param l Referencia al Layout.
|
||
408 | */
|
||
409 | 3135 | caballero | public void setXMLEntity(XMLEntity xml) { |
410 | 649 | vcaballero | if (xml.getIntProperty("m_Selected") != 0) { |
411 | this.setSelected(true); |
||
412 | } else {
|
||
413 | this.setSelected(false); |
||
414 | } |
||
415 | 312 | fernando | |
416 | 649 | vcaballero | this.m_path = xml.getStringProperty("m_path"); |
417 | 312 | fernando | |
418 | 649 | vcaballero | try {
|
419 | 1305 | fjp | load(this.m_path);
|
420 | 649 | vcaballero | } catch (Exception ex) { |
421 | NotificationManager.addError("Excepci?n :", ex);
|
||
422 | } |
||
423 | 312 | fernando | |
424 | 649 | vcaballero | this.m_quality = xml.getIntProperty("m_quality"); |
425 | this.m_viewing = xml.getIntProperty("m_viewing"); |
||
426 | 2304 | vcaballero | setRotation(xml.getDoubleProperty("m_rotation"));
|
427 | 649 | vcaballero | } |
428 | |||
429 | /**
|
||
430 | * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
|
||
431 | */
|
||
432 | public String getNameFFrame() { |
||
433 | return PluginServices.getText(this, "imagen") + num; |
||
434 | } |
||
435 | |||
436 | /**
|
||
437 | * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
|
||
438 | * java.awt.geom.AffineTransform)
|
||
439 | */
|
||
440 | public void print(Graphics2D g, AffineTransform at) |
||
441 | 654 | fernando | throws DriverException {
|
442 | 649 | vcaballero | draw(g, at, null, null); |
443 | } |
||
444 | 2209 | vcaballero | |
445 | 4209 | jaume | public void initialize() { |
446 | // TODO Auto-generated method stub
|
||
447 | |||
448 | } |
||
449 | |||
450 | 312 | fernando | } |