Statistics
| Revision:

root / branches / gvSIG_03_SLD / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFramePicture.java @ 2074

History | View | Annotate | Download (8.98 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.Graphics;
48
import java.awt.Graphics2D;
49
import java.awt.Image;
50
import java.awt.geom.AffineTransform;
51
import java.awt.geom.Rectangle2D;
52
import java.awt.image.BufferedImage;
53
import java.io.File;
54
import java.net.MalformedURLException;
55

    
56
import javax.swing.ImageIcon;
57

    
58
import org.apache.batik.transcoder.TranscoderException;
59
import org.apache.batik.transcoder.TranscoderOutput;
60

    
61
import com.iver.andami.PluginServices;
62
import com.iver.andami.messages.NotificationManager;
63
import com.iver.cit.gvsig.fmap.DriverException;
64
import com.iver.cit.gvsig.gui.layout.Layout;
65
import com.iver.utiles.XMLEntity;
66
import com.sun.jimi.core.Jimi;
67

    
68

    
69
/**
70
 * FFrame para introducir una imagen en el Layout.
71
 *
72
 * @author Vicente Caballero Navarro
73
 */
74
public class FFramePicture extends FFrame {
75
        private static final int PRESENTACION = 0;
76
        private static final int BORRADOR = 1;
77
        private static final int ACTIVO = 1;
78
        private static final int SIEMPRE = 0;
79
        private BufferedImage m_image = null;
80
        private int m_quality = PRESENTACION;
81
        private int m_viewing = ACTIVO;
82
        private String m_path = null;
83
        private Graphics svgGraphics=null;
84
        private boolean isSVG=false;
85
        /**
86
         * Creates a new FFramePicture object.
87
         */
88
        public FFramePicture() {
89
        }
90

    
91
        /**
92
         * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
93
         * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
94
         * de dibujar.
95
         *
96
         * @param g Graphics
97
         * @param at Transformada afin.
98
         * @param rv rect?ngulo sobre el que hacer un clip.
99
         * @param imgBase Imagen para acelerar el dibujado.
100
         */
101
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
102
                BufferedImage imgBase) {
103
                Rectangle2D.Double r = getBoundingBox(at);
104
                double x = r.getMinX();
105
                double y = r.getMinY();
106
                double w = r.getWidth();
107
                double h = r.getHeight();
108

    
109
                if (intersects(rv, r)) {
110
                        if (m_image == null) { //Que no hay una imagen.
111
                                drawEmpty(g);
112
                        } else {
113
                                if ((rv == null) || (getQuality() == PRESENTACION)) {
114
                                        ///if (!isSVG){
115
                                        double scalex = (double) w / m_image.getWidth(null);
116
                                        double scaley = (double) h / m_image.getHeight(null);
117
                                        AffineTransform xform = AffineTransform.getScaleInstance(scalex,
118
                                                        scaley);
119
                                        AffineTransform xpos = AffineTransform.getTranslateInstance(x,
120
                                                        y);
121
                                        xpos.concatenate(xform);
122
                                        g.drawRenderedImage(m_image, xpos);
123
                                        ///}else if (isSVG){
124
                                        ///        drawSVG(g,at,r);
125
                                        ///}
126
                                } else {
127
                                        drawDraft(g);
128
                                }
129
                        }
130
                }
131
        }
132

    
133
        /*private void drawSVG(Graphics2D g,AffineTransform at,Rectangle2D rect) {
134
                JSVGCanvas svgCanvas = new JSVGCanvas();
135
                try {
136
                        svgCanvas.setURI(new File(m_path).toURL().toString());
137
                } catch (MalformedURLException e) {
138
                        e.printStackTrace();
139
                }
140
                svgCanvas.paint(g); no funciona
141
        }
142
*/
143
        /**
144
         * Rellena la calidad seg?n el entero que se pasa como par?metro.
145
         *
146
         * @param q entero que representa el tipo de calidad elegido.
147
         */
148
        public void setQuality(int q) {
149
                m_quality = q;
150
        }
151

    
152
        /**
153
         * Devuelve la calidad que est? seleccionada.
154
         *
155
         * @return entero que representa la calidad seleccionada.
156
         */
157
        public int getQuality() {
158
                return m_quality;
159
        }
160

    
161
        /**
162
         * Devuelve un entero que representa la forma en que se actualiza la vista.
163
         *
164
         * @return forma que se actualiza la vista.
165
         */
166
        public int getViewing() {
167
                return m_viewing;
168
        }
169

    
170
        /**
171
         * Rellena la forma de actualizar la vista.
172
         *
173
         * @param v entero que representa la forma de actualizar la vista.
174
         */
175
        public void setViewing(int v) {
176
                m_viewing = v;
177
        }
178

    
179
        /**
180
         * Rellena el nombre de la imagen.
181
         *
182
         * @param path nombre de la imagen.
183
         */
184
        public void setPath(String path) {
185
                m_path = path;
186
        }
187

    
188
        /**
189
         * Devuelve la ruta del fichero.
190
         *
191
         * @return String
192
         */
193
        public String getPath() {
194
                return m_path;
195
        }
196

    
197
        /**
198
         * Rellena la imagen.
199
         *
200
         * @param image
201
         */
202
        public void setImage(BufferedImage image) {
203
                m_image = image;
204
        }
205

    
206
        /**
207
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
208
         */
209
        public XMLEntity getXMLEntity() {
210
                XMLEntity xml = new XMLEntity();
211
                xml.putProperty("className",this.getClass().getName());
212
                xml.putProperty("m_name", m_name);
213
                xml.putProperty("x", getBoundBox().x);
214
                xml.putProperty("y", getBoundBox().y);
215
                xml.putProperty("w", getBoundBox().width);
216
                xml.putProperty("h", getBoundBox().height);
217
                xml.putProperty("m_Selected", m_Selected);
218
                xml.putProperty("type", Layout.RECTANGLEPICTURE);
219
                xml.putProperty("m_path", m_path);
220
                xml.putProperty("m_quality", m_quality);
221
                xml.putProperty("m_viewing", m_viewing);
222
                xml.putProperty("tag", getTag());
223

    
224
                return xml;
225
        }
226
        public Image load(String file)
227
        {
228
            ImageIcon tmpIcon = null;
229
            String iString = file.toLowerCase();
230
        if (iString.endsWith("jpg") || iString.endsWith("jpeg") || iString.endsWith("gif")) {
231
            tmpIcon = new ImageIcon(Jimi.getImage(file ,Jimi.VIRTUAL_MEMORY));//((File)main.allImages.elementAt(x)).getAbsolutePath());
232
        } else if (iString.endsWith("png") || iString.endsWith("tif") || iString.endsWith("ico") ||  iString.endsWith("xpm") ||  iString.endsWith("bmp")) {
233
            tmpIcon = new ImageIcon(Jimi.getImage(file ,Jimi.VIRTUAL_MEMORY)); //new ImageIcon(f.getPath());
234
        } else if (iString.endsWith("svg")){
235
                        try {
236
                                tmpIcon = new ImageIcon(getImageFromSVG(file));
237
                        } catch (MalformedURLException e) {
238
                                e.printStackTrace();
239
                        } catch (TranscoderException e) {
240
                                e.printStackTrace();
241
                        }
242
        }
243
        Image image = tmpIcon.getImage();
244
                setPath(file);
245
                BufferedImage bi = new BufferedImage((int) image.getWidth(null),
246
                        (int) image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
247
                Graphics2D biContext = bi.createGraphics();
248
                biContext.drawImage(image, 0, 0, null);
249
                
250
                setImage(bi);
251
                return image; 
252
        }
253

    
254
        
255
        private Image getImageFromSVG(String file) throws TranscoderException, MalformedURLException {
256
                   /* Rasterizer r = new Rasterizer();
257
                        TranscodingHints hints = new TranscodingHints();
258

259
                r.setTranscodingHints((Map)hints);
260
                        TranscoderInput input = new TranscoderInput(new File(file).toURL().toString());
261
                        r.transcode(input, null);
262
                return m_image;
263
*/
264
        /*        isSVG=true;
265
                JSVGCanvas svgCanvas = new JSVGCanvas();
266
                svgCanvas.setURI(new File(file).toURL().toString());
267
                svgGraphics=m_image.getGraphics();
268
                svgGraphics=svgCanvas.getGraphics();
269
                return m_image;
270
                */
271
                
272
                isSVG=true;
273
                SVGRasterizer r = new SVGRasterizer(new File(file).toURL());
274
        r.setBackgroundColor(java.awt.Color.white);
275
        return r.createBufferedImage();
276
        
277
                }
278

    
279
        /**
280
         * Incorpora los atributos del XMLEntity en el objeto actual.
281
         *
282
         * @param xml XMLEntity
283
         * @param l Referencia al Layout.
284
         */
285
        public void setXMLEntity(XMLEntity xml, Layout l) {
286
                if (xml.getIntProperty("m_Selected") != 0) {
287
                        this.setSelected(true);
288
                } else {
289
                        this.setSelected(false);
290
                }
291

    
292
                this.m_path = xml.getStringProperty("m_path");
293

    
294
                try {
295
                        load(this.m_path);
296
                } catch (Exception ex) {
297
                        NotificationManager.addError("Excepci?n :", ex);
298
                }
299

    
300
                this.m_quality = xml.getIntProperty("m_quality");
301
                this.m_viewing = xml.getIntProperty("m_viewing");
302
        }
303

    
304
        /**
305
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
306
         */
307
        public String getNameFFrame() {
308
                return PluginServices.getText(this, "imagen") + num;
309
        }
310

    
311
        /**
312
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
313
         *                 java.awt.geom.AffineTransform)
314
         */
315
        public void print(Graphics2D g, AffineTransform at)
316
                throws DriverException {
317
                draw(g, at, null, null);
318
        }
319
        private class Rasterizer extends org.apache.batik.transcoder.image.ImageTranscoder {
320

    
321
        public BufferedImage createImage(int w, int h) {
322
            return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
323
        }
324

    
325
        public void writeImage(BufferedImage img, TranscoderOutput output)
326
            throws TranscoderException {
327
            m_image = img;
328
        }
329
    }
330

    
331
}