Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Export.java @ 27352

History | View | Annotate | Download (10.2 KB)

1 312 fernando
/*
2
 * Created on 17-feb-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7 1103 fjp
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47 312 fernando
package com.iver.cit.gvsig;
48
49 596 fernando
import java.awt.Component;
50 15967 jmvivo
import java.awt.geom.AffineTransform;
51 11918 jmvivo
import java.awt.image.BufferedImage;
52 596 fernando
import java.io.File;
53 15967 jmvivo
import java.io.IOException;
54 11918 jmvivo
import java.util.Hashtable;
55
import java.util.Iterator;
56 596 fernando
57
import javax.swing.JFileChooser;
58 11918 jmvivo
import javax.swing.JOptionPane;
59 596 fernando
import javax.swing.filechooser.FileFilter;
60
61 17382 jmvivo
import org.gvsig.raster.RasterLibrary;
62 15967 jmvivo
import org.gvsig.raster.dataset.GeoRasterWriter;
63
import org.gvsig.raster.dataset.IBuffer;
64
import org.gvsig.raster.dataset.IDataWriter;
65
import org.gvsig.raster.dataset.NotSupportedExtensionException;
66 16693 vcaballero
import org.gvsig.raster.dataset.io.RasterDriverException;
67 15967 jmvivo
68 5005 jorpiell
import com.iver.andami.PluginServices;
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.andami.plugins.Extension;
71 6778 jmvivo
import com.iver.cit.gvsig.fmap.layers.FLayers;
72 7304 caballero
import com.iver.cit.gvsig.project.documents.view.gui.View;
73 5005 jorpiell
import com.sun.jimi.core.Jimi;
74
import com.sun.jimi.core.JimiException;
75 596 fernando
76 5005 jorpiell
77 312 fernando
/**
78 1219 vcaballero
 * Extensi?n para exportar en algunos formatos raster la vista actual.
79 312 fernando
 *
80
 * @author Fernando Gonz?lez Cort?s
81
 */
82 5005 jorpiell
public class Export extends Extension {
83 7385 jaume
        private String lastPath = null;
84 26946 vcaballero
        private Hashtable<String, MyFileFilter> cmsExtensionsSupported = null;
85
        private Hashtable<String, MyFileFilter> jimiExtensionsSupported = null;
86 7385 jaume
87 596 fernando
        /**
88 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
89 596 fernando
         */
90
        public boolean isEnabled() {
91 6880 cesar
                View f = (View) PluginServices.getMDIManager().getActiveWindow();
92 312 fernando
93 596 fernando
                if (f == null) {
94
                        return false;
95
                }
96 312 fernando
97 6778 jmvivo
                FLayers layers = f.getModel().getMapContext().getLayers();
98
                for (int i=0;i< layers.getLayersCount();i++) {
99
                        return layers.getLayer(i).isAvailable();
100
                }
101
                return false;
102 596 fernando
        }
103 312 fernando
104 596 fernando
        /**
105 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
106 596 fernando
         */
107
        public boolean isVisible() {
108 26946 vcaballero
                com.iver.andami.ui.mdiManager.IWindow f = (com.iver.andami.ui.mdiManager.IWindow) PluginServices.getMDIManager().getActiveWindow();
109
                if (f == null)
110 596 fernando
                        return false;
111 312 fernando
112 5900 jorpiell
                return (f instanceof View);
113 596 fernando
        }
114 312 fernando
115 596 fernando
        /**
116 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#initialize()
117 596 fernando
         */
118 5005 jorpiell
        public void initialize() {
119 596 fernando
        }
120 312 fernando
121 11918 jmvivo
        /* (non-Javadoc)
122
         * @see com.iver.andami.plugins.Extension#postInitialize()
123
         */
124
        public void postInitialize() {
125 26946 vcaballero
                cmsExtensionsSupported = new Hashtable<String, MyFileFilter>();
126
                jimiExtensionsSupported = new Hashtable<String, MyFileFilter>();
127 12234 jmvivo
                cmsExtensionsSupported.put("jpg", new MyFileFilter("jpg",
128
                                PluginServices.getText(this, "jpg"), "cms"));
129 11918 jmvivo
                jimiExtensionsSupported.put("png",new MyFileFilter("png",
130
                                PluginServices.getText(this, "png"), "jimi"));
131
                jimiExtensionsSupported.put("bmp",new MyFileFilter("bmp",
132 15967 jmvivo
                                PluginServices.getText(this, "bmp"), "jimi"));
133 11918 jmvivo
        }
134
135 26946 vcaballero
        public static boolean saveImageCMS(File fileDst,BufferedImage srcImage) throws IOException, NotSupportedExtensionException, RasterDriverException {
136 17382 jmvivo
                RasterLibrary.wakeUp();
137 15967 jmvivo
                RasterizerImage data = new RasterizerImage(srcImage);
138 26946 vcaballero
                GeoRasterWriter writer = null;
139 15967 jmvivo
                writer = GeoRasterWriter.getWriter(
140 26946 vcaballero
                                data,
141
                                fileDst.getAbsolutePath(),
142
                                3,
143
                                new AffineTransform(),
144
                                srcImage.getWidth(),
145
                                srcImage.getHeight(),
146
                                IBuffer.TYPE_IMAGE,
147
                                GeoRasterWriter.getWriter(fileDst.getAbsolutePath()).getParams(),
148
                                null,
149
                                false
150
                );
151 15967 jmvivo
                if (writer == null){
152
                        PluginServices.getLogger().error("No supported Format: " + fileDst.getAbsolutePath());
153
                        return false;
154
                }
155
                try {
156
                        writer.dataWrite();
157
                } catch (InterruptedException e) {
158
                }
159
                writer.writeClose();
160
                return true;
161
        }
162
163
164
165 11918 jmvivo
        public static boolean saveImageJimi(File fileDst,BufferedImage srcImage) throws Exception{
166
                try {
167
                        Jimi.putImage(srcImage, fileDst.getAbsolutePath());
168
                } catch (JimiException e) {
169
                        throw new Exception(fileDst.getAbsolutePath(),e);
170
                }
171
                return true;
172
173
        }
174 15967 jmvivo
175 596 fernando
        /**
176 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
177 596 fernando
         */
178
        public void execute(String actionCommand) {
179 7385 jaume
                JFileChooser jfc = new JFileChooser(lastPath);
180 15967 jmvivo
181 11918 jmvivo
                jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter());
182 15967 jmvivo
183 26946 vcaballero
                Iterator<MyFileFilter> iter = cmsExtensionsSupported.values().iterator();
184
                while (iter.hasNext())
185 11918 jmvivo
                        jfc.addChoosableFileFilter((FileFilter)iter.next());
186 312 fernando
187 11918 jmvivo
                iter = jimiExtensionsSupported.values().iterator();
188 26946 vcaballero
                while (iter.hasNext())
189 11918 jmvivo
                        jfc.addChoosableFileFilter((FileFilter)iter.next());
190
191
                jfc.setFileFilter((FileFilter)jimiExtensionsSupported.get("png"));
192
                if (jfc.showSaveDialog(
193
                                (Component) PluginServices.getMainFrame()
194 26946 vcaballero
                ) == JFileChooser.APPROVE_OPTION) {
195 15967 jmvivo
196 11918 jmvivo
                        BufferedImage tempImage;
197
198 6880 cesar
                        tempImage = ((View) PluginServices.getMDIManager().getActiveWindow()).getImage();
199 312 fernando
200 596 fernando
                        File f = jfc.getSelectedFile();
201 15967 jmvivo
202 11918 jmvivo
                        lastPath = f.getParent();
203 15967 jmvivo
204 11918 jmvivo
                        MyFileFilter filter = (MyFileFilter)jfc.getFileFilter();
205
                        f = filter.normalizeExtension(f);
206 15967 jmvivo
207 26946 vcaballero
                        if (f.exists()) {
208 11918 jmvivo
                                int resp = JOptionPane.showConfirmDialog(
209
                                                (Component) PluginServices.getMainFrame(),
210
                                                PluginServices.getText(this,
211 26946 vcaballero
                                                "fichero_ya_existe_seguro_desea_guardarlo")+
212
                                                "\n"+
213
                                                f.getAbsolutePath(),
214 11918 jmvivo
                                                PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
215
                                if (resp != JOptionPane.YES_OPTION) {
216
                                        return;
217 1319 vcaballero
                                }
218 11918 jmvivo
                        }
219 15967 jmvivo
220 26946 vcaballero
                        if (filter.getInfo().equalsIgnoreCase("cms")) {
221 15967 jmvivo
222 11918 jmvivo
                                try {
223
                                        saveImageCMS(f, tempImage);
224
                                } catch (IOException e) {
225
                                        NotificationManager.addError("Error exportando la imagen", e);
226 15967 jmvivo
                                } catch (NotSupportedExtensionException e) {
227
                                        NotificationManager.addError("Error exportando la imagen: formato no soportado", e);
228
                                } catch (RasterDriverException e) {
229
                                        NotificationManager.addError("Error exportando la imagen", e);
230 596 fernando
                                }
231 26946 vcaballero
                        } else if (filter.getInfo().equalsIgnoreCase("jimi")) {
232 11918 jmvivo
                                try {
233
                                        saveImageJimi(f, tempImage);
234
                                } catch (Exception e) {
235
                                        NotificationManager.addError("Error exportando la imagen", e);
236
                                }
237 15967 jmvivo
238 596 fernando
                        }
239
                }
240
        }
241 312 fernando
}
242 11918 jmvivo
243 13075 bsanchez
/**
244 26946 vcaballero
 * Servidor de datos desde un BufferedImage. Cada petici?n es de un tama?o
245
 * de bloque indicado por el escritor en los par?metros de la llamada por lo
246
 * que en la siguiente petici?n habr? que escribir el bloque siguiente.
247 13075 bsanchez
 */
248 26946 vcaballero
class RasterizerImage implements IDataWriter {
249
        private BufferedImage source  = null;
250
        private int[]         data    = null;
251
        private int           y       = 0;
252 11918 jmvivo
253 15967 jmvivo
        public RasterizerImage(BufferedImage source) {
254
                this.source = source;
255
        }
256
257 26946 vcaballero
        /**
258
         * Solo necesita implementar el m?todo RGB porque solo se
259
         * utiliza para exportar la vista.
260
         */
261 15967 jmvivo
        public int[] readARGBData(int sX, int sY, int nBand){
262
                return readData( sX, sY, nBand);
263
        }
264
265
        public int[] readData(int sizeX, int sizeY, int nBand) {
266 26946 vcaballero
                if(nBand == 0) { //Con nBand==0 se devuelven las 3 bandas
267
                        this.data = this.source.getRGB(0, y, sizeX, sizeY, this.data, 0, sizeX);
268
                        y += sizeY;
269 15967 jmvivo
                        return this.data;
270
                }
271
                return null;
272
        }
273
274
        public byte[][] readByteData(int sizeX, int sizeY) {
275
                return null;
276
        }
277
278
        public double[][] readDoubleData(int sizeX, int sizeY) {
279
                return null;
280
        }
281
282
        public float[][] readFloatData(int sizeX, int sizeY) {
283
                return null;
284
        }
285
286
        public int[][] readIntData(int sizeX, int sizeY) {
287
                return null;
288
        }
289
290
        public short[][] readShortData(int sizeX, int sizeY) {
291
                return null;
292
        }
293
294
}
295
296 26946 vcaballero
class MyFileFilter extends FileFilter {
297 11918 jmvivo
298
        private String[] extensiones=new String[1];
299
        private String description;
300
        private boolean dirs = true;
301
        private String info= null;
302
303
        public MyFileFilter(String[] ext, String desc) {
304
                extensiones = ext;
305
                description = desc;
306
        }
307
308
        public MyFileFilter(String[] ext, String desc,String info) {
309
                extensiones = ext;
310
                description = desc;
311
                this.info = info;
312
        }
313
314
        public MyFileFilter(String ext, String desc) {
315
                extensiones[0] = ext;
316
                description = desc;
317
        }
318
319
        public MyFileFilter(String ext, String desc,String info) {
320
                extensiones[0] = ext;
321
                description = desc;
322
                this.info = info;
323
        }
324
325
        public MyFileFilter(String ext, String desc, boolean dirs) {
326
                extensiones[0] = ext;
327
                description = desc;
328
                this.dirs = dirs;
329
        }
330
331
        public MyFileFilter(String ext, String desc, boolean dirs,String info) {
332
                extensiones[0] = ext;
333
                description = desc;
334
                this.dirs = dirs;
335
                this.info = info;
336
        }
337
338
        public boolean accept(File f) {
339
                if (f.isDirectory()) {
340
                        if (dirs) {
341
                                return true;
342
                        } else {
343
                                return false;
344
                        }
345
                }
346
                for (int i=0;i<extensiones.length;i++){
347
                        if (extensiones[i].equals("")){
348
                                continue;
349
                        }
350
                        if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])){
351
                                return true;
352
                        }
353
                }
354 15967 jmvivo
355 11918 jmvivo
                return false;
356
        }
357
358
        /**
359
         * @see javax.swing.filechooser.FileFilter#getDescription()
360
         */
361
        public String getDescription() {
362
                return description;
363
        }
364
365
        public String[] getExtensions() {
366
                return extensiones;
367
        }
368 15967 jmvivo
369 11918 jmvivo
        public boolean isDirectory(){
370
                return dirs;
371
        }
372 15967 jmvivo
373 11918 jmvivo
        private String getExtensionOfAFile(File file){
374
                String name;
375
                int dotPos;
376
                name = file.getName();
377
                dotPos = name.lastIndexOf(".");
378
                if (dotPos < 1){
379
                        return "";
380
                }
381
                return name.substring(dotPos+1);
382
        }
383 15967 jmvivo
384 11918 jmvivo
        public File normalizeExtension(File file){
385
                String ext = getExtensionOfAFile(file);
386
                if (ext.equals("") || !(this.accept(file))){
387
                        return new File(file.getAbsolutePath() + "." + extensiones[0]);
388
                }
389 15967 jmvivo
                return file;
390 11918 jmvivo
        }
391 15967 jmvivo
392 11918 jmvivo
        public String getInfo(){
393
                return this.info;
394
        }
395 15967 jmvivo
396 11918 jmvivo
        public void setInfo(String info){
397
                this.info = info;
398
        }
399
400
}