Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / Export.java @ 28940

History | View | Annotate | Download (10.2 KB)

1
/*
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
/* 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
package com.iver.cit.gvsig;
48

    
49
import java.awt.Component;
50
import java.awt.geom.AffineTransform;
51
import java.awt.image.BufferedImage;
52
import java.io.File;
53
import java.io.IOException;
54
import java.util.Hashtable;
55
import java.util.Iterator;
56

    
57
import javax.swing.JFileChooser;
58
import javax.swing.JOptionPane;
59
import javax.swing.filechooser.FileFilter;
60

    
61
import org.gvsig.raster.RasterLibrary;
62
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
import org.gvsig.raster.dataset.io.RasterDriverException;
67

    
68
import com.iver.andami.PluginServices;
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.andami.plugins.Extension;
71
import com.iver.cit.gvsig.fmap.layers.FLayers;
72
import com.iver.cit.gvsig.project.documents.view.gui.View;
73
import com.sun.jimi.core.Jimi;
74
import com.sun.jimi.core.JimiException;
75

    
76

    
77
/**
78
 * Extensi?n para exportar en algunos formatos raster la vista actual.
79
 *
80
 * @author Fernando Gonz?lez Cort?s
81
 */
82
public class Export extends Extension {
83
        private String lastPath = null;
84
        private Hashtable<String, MyFileFilter> cmsExtensionsSupported = null;
85
        private Hashtable<String, MyFileFilter> jimiExtensionsSupported = null;
86

    
87
        /**
88
         * @see com.iver.andami.plugins.IExtension#isEnabled()
89
         */
90
        public boolean isEnabled() {
91
                View f = (View) PluginServices.getMDIManager().getActiveWindow();
92

    
93
                if (f == null) {
94
                        return false;
95
                }
96

    
97
                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
        }
103

    
104
        /**
105
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
106
         */
107
        public boolean isVisible() {
108
                com.iver.andami.ui.mdiManager.IWindow f = (com.iver.andami.ui.mdiManager.IWindow) PluginServices.getMDIManager().getActiveWindow();
109
                if (f == null) 
110
                        return false;
111

    
112
                return (f instanceof View);
113
        }
114

    
115
        /**
116
         * @see com.iver.andami.plugins.IExtension#initialize()
117
         */
118
        public void initialize() {
119
        }
120

    
121
        /* (non-Javadoc)
122
         * @see com.iver.andami.plugins.Extension#postInitialize()
123
         */
124
        public void postInitialize() {
125
                cmsExtensionsSupported = new Hashtable<String, MyFileFilter>();
126
                jimiExtensionsSupported = new Hashtable<String, MyFileFilter>();
127
                cmsExtensionsSupported.put("jpg", new MyFileFilter("jpg",
128
                                PluginServices.getText(this, "jpg"), "cms"));
129
                jimiExtensionsSupported.put("png",new MyFileFilter("png",
130
                                PluginServices.getText(this, "png"), "jimi"));
131
                jimiExtensionsSupported.put("bmp",new MyFileFilter("bmp",
132
                                PluginServices.getText(this, "bmp"), "jimi"));
133
        }
134

    
135
        public static boolean saveImageCMS(File fileDst,BufferedImage srcImage) throws IOException, NotSupportedExtensionException, RasterDriverException {
136
                RasterLibrary.wakeUp();
137
                RasterizerImage data = new RasterizerImage(srcImage);
138
                GeoRasterWriter writer = null;
139
                writer = GeoRasterWriter.getWriter(
140
                                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
                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
        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

    
175
        /**
176
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
177
         */
178
        public void execute(String actionCommand) {
179
                JFileChooser jfc = new JFileChooser(lastPath);
180

    
181
                jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter());
182

    
183
                Iterator<MyFileFilter> iter = cmsExtensionsSupported.values().iterator();
184
                while (iter.hasNext())
185
                        jfc.addChoosableFileFilter((FileFilter)iter.next());
186

    
187
                iter = jimiExtensionsSupported.values().iterator();
188
                while (iter.hasNext())
189
                        jfc.addChoosableFileFilter((FileFilter)iter.next());
190

    
191
                jfc.setFileFilter((FileFilter)jimiExtensionsSupported.get("png"));
192
                if (jfc.showSaveDialog(
193
                                (Component) PluginServices.getMainFrame()
194
                ) == JFileChooser.APPROVE_OPTION) {
195

    
196
                        BufferedImage tempImage;
197

    
198
                        tempImage = ((View) PluginServices.getMDIManager().getActiveWindow()).getImage();
199

    
200
                        File f = jfc.getSelectedFile();
201

    
202
                        lastPath = f.getParent();
203

    
204
                        MyFileFilter filter = (MyFileFilter)jfc.getFileFilter();
205
                        f = filter.normalizeExtension(f);
206

    
207
                        if (f.exists()) {
208
                                int resp = JOptionPane.showConfirmDialog(
209
                                                (Component) PluginServices.getMainFrame(),
210
                                                PluginServices.getText(this,
211
                                                "fichero_ya_existe_seguro_desea_guardarlo")+
212
                                                "\n"+
213
                                                f.getAbsolutePath(),
214
                                                PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
215
                                if (resp != JOptionPane.YES_OPTION) {
216
                                        return;
217
                                }
218
                        }
219

    
220
                        if (filter.getInfo().equalsIgnoreCase("cms")) {
221

    
222
                                try {
223
                                        saveImageCMS(f, tempImage);
224
                                } catch (IOException e) {
225
                                        NotificationManager.addError("Error exportando la imagen", e);
226
                                } 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
                                }
231
                        } else if (filter.getInfo().equalsIgnoreCase("jimi")) {
232
                                try {
233
                                        saveImageJimi(f, tempImage);
234
                                } catch (Exception e) {
235
                                        NotificationManager.addError("Error exportando la imagen", e);
236
                                }
237

    
238
                        }
239
                }
240
        }
241
}
242

    
243
/**
244
 * 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
 */
248
class RasterizerImage implements IDataWriter {
249
        private BufferedImage source  = null;
250
        private int[]         data    = null;
251
        private int           y       = 0;
252

    
253
        public RasterizerImage(BufferedImage source) {
254
                this.source = source;
255
        }
256

    
257
        /**
258
         * Solo necesita implementar el m?todo RGB porque solo se
259
         * utiliza para exportar la vista.
260
         */
261
        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
                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
                        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
class MyFileFilter extends FileFilter {
297

    
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

    
355
                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

    
369
        public boolean isDirectory(){
370
                return dirs;
371
        }
372

    
373
        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

    
384
        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
                return file;
390
        }
391

    
392
        public String getInfo(){
393
                return this.info;
394
        }
395

    
396
        public void setInfo(String info){
397
                this.info = info;
398
        }
399

    
400
}