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