Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFramePicture.java @ 6117

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