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