Statistics
| Revision:

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

History | View | Annotate | Download (13.3 KB)

1
/*
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.gui.layout.fframes;
46

    
47
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
import com.iver.cit.gvsig.gui.project.SaveException;
53

    
54
import com.iver.utiles.XMLEntity;
55

    
56
import com.sun.jimi.core.Jimi;
57

    
58
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

    
66
import org.w3c.dom.Document;
67
import org.w3c.dom.Element;
68
import org.w3c.dom.svg.SVGDocument;
69

    
70
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

    
78
import java.io.File;
79

    
80
import javax.swing.ImageIcon;
81

    
82

    
83
/**
84
 * FFrame para introducir una imagen en el Layout o para dibujar sobre el
85
 * graphics un SVG.
86
 *
87
 * @author Vicente Caballero Navarro
88
 */
89
public class FFramePicture extends FFrame {
90
    protected static RenderingHints defaultRenderingHints;
91

    
92
    static {
93
        defaultRenderingHints = new RenderingHints(null);
94
        defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
95
            RenderingHints.VALUE_ANTIALIAS_ON);
96

    
97
        defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
98
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
99
    }
100

    
101
    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

    
113
    /**
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
        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

    
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)) {
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
                        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);
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

    
165
                        System.gc();
166
                    }
167
                } else {
168
                    drawDraft(g);
169
                }
170
            }
171
        }
172

    
173
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
174
            r.y + (r.height / 2));
175
    }
176

    
177
    /**
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
     */
184
    private void drawSVG(Graphics2D g, Rectangle2D rect, Rectangle2D rv) {
185
        if ((rv == null) || rv.contains(rect)) {
186
            AffineTransform ataux = new AffineTransform();
187

    
188
            ataux.translate(rect.getX(), rect.getY());
189

    
190
            try {
191
                ataux.concatenate(ViewBox.getViewTransform(null, elt,
192
                        (float) rect.getWidth(), (float) rect.getHeight()));
193
                gvtRoot.setTransform(ataux);
194
            } catch (Exception e) {
195
                // TODO: handle exception
196
            }
197
        } else {
198
            AffineTransform ataux = new AffineTransform();
199

    
200
            ataux.translate(rect.getX(), rect.getY());
201
            ataux.concatenate(ViewBox.getViewTransform(null, elt,
202
                    (float) rect.getWidth(), (float) rect.getHeight()));
203

    
204
            gvtRoot.setTransform(ataux);
205
        }
206

    
207
        RenderingHints renderingHints = new RenderingHints(defaultRenderingHints);
208
        g.setRenderingHints(renderingHints);
209

    
210
        if (gvtRoot != null) {
211
            gvtRoot.paint(g);
212
        }
213
    }
214

    
215
    /**
216
     * Rellena la calidad seg?n el entero que se pasa como par?metro.
217
     *
218
     * @param q entero que representa el tipo de calidad elegido.
219
     */
220
    public void setQuality(int q) {
221
        m_quality = q;
222
    }
223

    
224
    /**
225
     * Devuelve la calidad que est? seleccionada.
226
     *
227
     * @return entero que representa la calidad seleccionada.
228
     */
229
    public int getQuality() {
230
        return m_quality;
231
    }
232

    
233
    /**
234
     * Devuelve un entero que representa la forma en que se actualiza la vista.
235
     *
236
     * @return forma que se actualiza la vista.
237
     */
238
    public int getViewing() {
239
        return m_viewing;
240
    }
241

    
242
    /**
243
     * Rellena la forma de actualizar la vista.
244
     *
245
     * @param v entero que representa la forma de actualizar la vista.
246
     */
247
    public void setViewing(int v) {
248
        m_viewing = v;
249
    }
250

    
251
    /**
252
     * Rellena el nombre de la imagen.
253
     *
254
     * @param path nombre de la imagen.
255
     */
256
    public void setPath(String path) {
257
        m_path = path;
258
    }
259

    
260
    /**
261
     * Devuelve la ruta del fichero.
262
     *
263
     * @return String
264
     */
265
    public String getPath() {
266
        return m_path;
267
    }
268

    
269
    /**
270
     * Rellena la imagen.
271
     *
272
     * @param image
273
     */
274
    public void setImage(BufferedImage image) {
275
        m_image = image;
276
    }
277

    
278
    /**
279
     * DOCUMENT ME!
280
     *
281
     * @return DOCUMENT ME!
282
     *
283
     * @throws SaveException
284
     *
285
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
286
     */
287
    public XMLEntity getXMLEntity() throws SaveException {
288
        XMLEntity xml = super.getXMLEntity();
289

    
290
        try {
291
            xml.putProperty("type", Layout.RECTANGLEPICTURE);
292
            xml.putProperty("m_path", m_path);
293
            xml.putProperty("m_quality", m_quality);
294
            xml.putProperty("m_viewing", m_viewing);
295
        } catch (Exception e) {
296
            throw new SaveException(e, this.getClass().getName());
297
        }
298

    
299
        return xml;
300
    }
301

    
302
    /**
303
     * Devuelve la dimensi?n dela imagen.
304
     *
305
     * @param file Nombre del fichero donde se encuentra la imagen.
306
     *
307
     * @return DOCUMENT ME!
308
     */
309
    public Dimension getBound(String file) {
310
        Image img = load(file);
311

    
312
        if (isSVG) {
313
            return new Dimension(100, 100);
314
        }
315

    
316
        if (img == null) {
317
            return new Dimension((int) getBoundingBox(null).getWidth(),
318
                (int) getBoundingBox(null).getHeight());
319
        }
320

    
321
        return new Dimension(img.getWidth(null), img.getHeight(null));
322
    }
323

    
324
    /**
325
     * Carga el contnido del fichero.
326
     *
327
     * @param file Nombre del fichero a cargar.
328
     *
329
     * @return Imagen
330
     */
331
    public Image load(String file) {
332
        ImageIcon tmpIcon = null;
333

    
334
        if (file == null) {
335
            return null;
336
        }
337

    
338
        String iString = file.toLowerCase();
339

    
340
        if (iString.endsWith("jpg") || iString.endsWith("jpeg") ||
341
                iString.endsWith("gif")) {
342
            tmpIcon = new ImageIcon(Jimi.getImage(file, Jimi.VIRTUAL_MEMORY)); //((File)main.allImages.elementAt(x)).getAbsolutePath());
343
        } else if (iString.endsWith("png") || iString.endsWith("tif") ||
344
                iString.endsWith("ico") || iString.endsWith("xpm") ||
345
                iString.endsWith("bmp")) {
346
            tmpIcon = new ImageIcon(Jimi.getImage(file, Jimi.VIRTUAL_MEMORY)); //new ImageIcon(f.getPath());
347
        } else if (iString.endsWith("svg")) {
348
            isSVG = true;
349
            obtainStaticRenderer(new File(file));
350
        }
351

    
352
        if (!isSVG && (tmpIcon != null)) {
353
            Image image = tmpIcon.getImage();
354
            setPath(file);
355

    
356
            BufferedImage bi = new BufferedImage(image.getWidth(null),
357
                    image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
358
            Graphics2D biContext = bi.createGraphics();
359
            biContext.drawImage(image, 0, 0, null);
360

    
361
            setImage(bi);
362

    
363
            return image;
364
        }
365

    
366
        return null;
367
    }
368

    
369
    /**
370
     * Obtiene el renderer para svg a partir del svg.
371
     *
372
     * @param file Nombre del fichero.
373
     */
374
    private void obtainStaticRenderer(File file) {
375
        try {
376
            UserAgentAdapter userAgent = new UserAgentAdapter();
377
            DocumentLoader loader = new DocumentLoader(userAgent);
378
            BridgeContext ctx = new BridgeContext(userAgent, loader);
379
            Document svgDoc = loader.loadDocument(file.toURI().toString());
380
            gvtRoot = gvtBuilder.build(ctx, svgDoc);
381
            renderer.setTree(gvtRoot);
382
            elt = ((SVGDocument) svgDoc).getRootElement();
383
        } catch (Exception ex) {
384
            ex.printStackTrace();
385
        }
386
    }
387

    
388
    /**
389
     * Incorpora los atributos del XMLEntity en el objeto actual.
390
     *
391
     * @param xml XMLEntity
392
     * @param l Referencia al Layout.
393
     */
394
    public void setXMLEntity03(XMLEntity xml, Layout l) {
395
        if (xml.getIntProperty("m_Selected") != 0) {
396
            this.setSelected(true);
397
        } else {
398
            this.setSelected(false);
399
        }
400

    
401
        this.m_path = xml.getStringProperty("m_path");
402

    
403
        try {
404
            load(this.m_path);
405
        } catch (Exception ex) {
406
            NotificationManager.addError("Excepci?n :", ex);
407
        }
408

    
409
        this.m_quality = xml.getIntProperty("m_quality");
410
        this.m_viewing = xml.getIntProperty("m_viewing");
411
    }
412

    
413
    /**
414
     * Incorpora los atributos del XMLEntity en el objeto actual.
415
     *
416
     * @param xml XMLEntity
417
     */
418
    public void setXMLEntity(XMLEntity xml) {
419
        if (xml.getIntProperty("m_Selected") != 0) {
420
            this.setSelected(true);
421
        } else {
422
            this.setSelected(false);
423
        }
424

    
425
        this.m_path = xml.getStringProperty("m_path");
426

    
427
        try {
428
            load(this.m_path);
429
        } catch (Exception ex) {
430
            NotificationManager.addError("Excepci?n :", ex);
431
        }
432

    
433
        this.m_quality = xml.getIntProperty("m_quality");
434
        this.m_viewing = xml.getIntProperty("m_viewing");
435
        setRotation(xml.getDoubleProperty("m_rotation"));
436
    }
437

    
438
    /**
439
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
440
     */
441
    public String getNameFFrame() {
442
        return PluginServices.getText(this, "imagen")+ num;
443
    }
444

    
445
    /**
446
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
447
     *      java.awt.geom.AffineTransform)
448
     */
449
    public void print(Graphics2D g, AffineTransform at)
450
        throws DriverException {
451
        draw(g, at, null, null);
452
    }
453

    
454
        public void initialize() {
455
                // TODO Auto-generated method stub
456

    
457
        }
458
}