Statistics
| Revision:

svn-gvsig-desktop / tags / Root_v06 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFramePicture.java @ 4811

History | View | Annotate | Download (11.8 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 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

    
56
import javax.swing.ImageIcon;
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
import org.w3c.dom.Document;
66
import org.w3c.dom.Element;
67
import org.w3c.dom.svg.SVGDocument;
68

    
69
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
import com.iver.cit.gvsig.gui.project.SaveException;
74
import com.iver.utiles.XMLEntity;
75
import com.sun.jimi.core.Jimi;
76

    
77

    
78
/**
79
 * FFrame para introducir una imagen en el Layout o para dibujar sobre el
80
 * graphics un SVG.
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84
public class FFramePicture extends FFrame {
85
        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
        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
        private boolean isSVG = false;
103
        private StaticRenderer renderer = new StaticRenderer();
104
        private Element elt;
105
        private GVTBuilder gvtBuilder = new GVTBuilder();
106
        private GraphicsNode gvtRoot = null;
107

    
108
        /**
109
         * Creates a new FFramePicture object.
110
         */
111
        public FFramePicture() {
112
        }
113

    
114
        /**
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
         * @param imgBase Imagen para acelerar el dibujado.
123
         */
124
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
125
                BufferedImage imgBase) {
126
                Rectangle2D.Double r = getBoundingBox(at);
127
                g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
128
                                r.y + (r.height / 2));
129
                double x = r.getMinX();
130
                double y = r.getMinY();
131
                double w = r.getWidth();
132
                double h = r.getHeight();
133

    
134
                if (intersects(rv, r)) {
135
                        if ((m_image == null) && !isSVG) { //Que no hay una imagen.
136
                                drawEmpty(g);
137
                        } else {
138
                                if ((rv == null) || (getQuality() == PRESENTACION)) {
139
                                        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
                                                try {if(r!=null)
150
                                                        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
                                } else {
160
                                        drawDraft(g);
161
                                }
162
                        }
163
                }
164
                g.rotate(Math.toRadians(-getRotation()),
165
                                r.x + (r.width / 2), r.y + (r.height / 2));
166
        }
167

    
168
        /**
169
         * Dibuja SVG sobre el Graphics que se pasa como par?metro.
170
         *
171
         * @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
         */
176
        private void drawSVG(Graphics2D g, Rectangle2D rect, Rectangle2D rv,
177
                AffineTransform at) {
178
                
179
                if ((rv == null) || rv.contains(rect)) {
180
                        AffineTransform ataux = new AffineTransform();
181

    
182
                        ataux.translate(rect.getX(), rect.getY());
183
                        try{
184
                        ataux.concatenate(ViewBox.getViewTransform(null, elt,
185
                                        (float) rect.getWidth(), (float) rect.getHeight()));
186
                        gvtRoot.setTransform(ataux);
187
                        }catch (Exception e) {
188
                                // TODO: handle exception
189
                        }
190
                        
191
                        
192
                } 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
                
201
                }
202

    
203
                RenderingHints renderingHints = new RenderingHints(defaultRenderingHints);
204
                g.setRenderingHints(renderingHints);
205
        if (gvtRoot != null)
206
            gvtRoot.paint(g);
207
        }
208

    
209
        
210
        /**
211
         * 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

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

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

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

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

    
255
        /**
256
         * Devuelve la ruta del fichero.
257
         *
258
         * @return String
259
         */
260
        public String getPath() {
261
                return m_path;
262
        }
263

    
264
        /**
265
         * Rellena la imagen.
266
         *
267
         * @param image
268
         */
269
        public void setImage(BufferedImage image) {
270
                m_image = image;
271
        }
272

    
273
        /**
274
         * @throws SaveException 
275
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
276
         */
277
        public XMLEntity getXMLEntity() throws SaveException {
278
                XMLEntity xml = new XMLEntity();
279
                try{
280
                xml.putProperty("className", this.getClass().getName());
281
                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
                xml.putProperty("m_rotation",getRotation());
293
                }catch (Exception e) {
294
                        throw new SaveException(e,this.getClass().getName());
295
                }
296
                return xml;
297
        }
298

    
299
        /**
300
         * Devuelve la dimensi?n dela imagen.
301
         *
302
         * @param file Nombre del fichero donde se encuentra la imagen.
303
         *
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
        }
317

    
318
        /**
319
         * Carga el contnido del fichero.
320
         *
321
         * @param file Nombre del fichero a cargar.
322
         *
323
         * @return Imagen
324
         */
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
         * Obtiene el renderer para svg a partir del svg.
361
         *
362
         * @param file Nombre del fichero.
363
         */
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
         * Incorpora los atributos del XMLEntity en el objeto actual.
380
         *
381
         * @param xml XMLEntity
382
         * @param l Referencia al Layout.
383
         */
384
        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
        public void setXMLEntity(XMLEntity xml) {
410
                if (xml.getIntProperty("m_Selected") != 0) {
411
                        this.setSelected(true);
412
                } else {
413
                        this.setSelected(false);
414
                }
415

    
416
                this.m_path = xml.getStringProperty("m_path");
417

    
418
                try {
419
                        load(this.m_path);
420
                } catch (Exception ex) {
421
                        NotificationManager.addError("Excepci?n :", ex);
422
                }
423

    
424
                this.m_quality = xml.getIntProperty("m_quality");
425
                this.m_viewing = xml.getIntProperty("m_viewing");
426
                setRotation(xml.getDoubleProperty("m_rotation"));
427
        }
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
                throws DriverException {
442
                draw(g, at, null, null);
443
        }
444

    
445
        public void initialize() {
446
                // TODO Auto-generated method stub
447
                
448
        }
449

    
450
}