Statistics
| Revision:

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

History | View | Annotate | Download (9.76 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.Dimension;
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.cresques.geo.ViewPortData;
62
import org.cresques.io.GeoRasterWriter;
63
import org.cresques.io.IDataWriter;
64
import org.cresques.px.Extent;
65

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

    
75

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

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

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

    
96
                FLayers layers = f.getModel().getMapContext().getLayers();
97
                for (int i=0;i< layers.getLayersCount();i++) {
98
                        return layers.getLayer(i).isAvailable();
99
                }
100
                return false;
101
        }
102

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

    
110
                if (f == null) {
111
                        return false;
112
                }
113

    
114
                return (f instanceof View);
115
        }
116

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

    
123
        /* (non-Javadoc)
124
         * @see com.iver.andami.plugins.Extension#postInitialize()
125
         */
126
        public void postInitialize() {
127
                cmsExtensionsSupported = new Hashtable(); 
128
                jimiExtensionsSupported = new Hashtable();
129
                
130
//                String[] ext;
131
//                ext = GeoRasterWriter.getDriversExtensions(); 
132
//                int i;
133
//                for (i=0;i< ext.length;i++){
134
//                        cmsExtensionsSupported.put(ext[i], new MyFileFilter(ext[i],
135
//                                        PluginServices.getText(this, ext[i]), "cms"));
136
//                }
137
                
138
                cmsExtensionsSupported.put("jpg", new MyFileFilter("jpg",
139
                                PluginServices.getText(this, "jpg"), "cms"));
140
                
141
//                ext = Jimi.getEncoderTypes();
142
//                for (i=0;i<ext.length;i++){
143
//                        if (!cmsExtensionsSupported.contains( ext[i])) {
144
//                                jimiExtensionSupported.put(ext[i], new GenericFileFilter(ext[i],
145
//                                                PluginServices.getText(this, ext[i])));
146
//                        }
147
//                }
148
                
149
                jimiExtensionsSupported.put("png",new MyFileFilter("png",
150
                                PluginServices.getText(this, "png"), "jimi"));
151
                jimiExtensionsSupported.put("bmp",new MyFileFilter("bmp",
152
                                PluginServices.getText(this, "bmp"), "jimi"));                
153
//                jimiExtensionsSupported.put("tga",new MyFileFilter("tga",
154
//                                PluginServices.getText(this, "tga"), "jimi"));
155

    
156

    
157
                
158
                
159
                
160
        }
161

    
162
        public static boolean saveImageCMS(File fileDst,BufferedImage srcImage) throws IOException{
163
                RasterizerImage data = new RasterizerImage(srcImage);
164
                Extent ex = new Extent(0,0,srcImage.getWidth(),srcImage.getHeight());
165
                ViewPortData vpData = new ViewPortData(Project.getDefaultProjection(), ex, new Dimension(srcImage.getWidth(),srcImage.getHeight()));
166
                GeoRasterWriter writer = GeoRasterWriter.getWriter(data,fileDst.getAbsolutePath(),GeoRasterWriter.blockSizeDefault,3,vpData,10,srcImage.getWidth(),srcImage.getHeight());
167
                //writer.setWriteTfw(false); OJO !!!
168
                if (writer == null){
169
                        PluginServices.getLogger().error("No supported Format: " + fileDst.getAbsolutePath());
170
                        return false;
171
                }
172
                writer.dataWrite();
173
                writer.writeClose(); 
174
                return true;
175
        }
176
        
177
        
178
        
179
        public static boolean saveImageJimi(File fileDst,BufferedImage srcImage) throws Exception{
180
                try {
181

    
182
                        Jimi.putImage(srcImage, fileDst.getAbsolutePath());
183
                        
184
                } catch (JimiException e) {
185
                        throw new Exception(fileDst.getAbsolutePath(),e);
186
                }
187
                return true;
188

    
189
        }
190
        
191
        /**
192
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
193
         */
194
        public void execute(String actionCommand) {
195
                JFileChooser jfc = new JFileChooser(lastPath);
196
                
197
                jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter());
198
                
199
                Iterator iter;
200
                
201
                iter = cmsExtensionsSupported.values().iterator();
202
                while (iter.hasNext()){
203
                        jfc.addChoosableFileFilter((FileFilter)iter.next());
204
                }
205

    
206
                iter = jimiExtensionsSupported.values().iterator();
207
                while (iter.hasNext()){
208
                        jfc.addChoosableFileFilter((FileFilter)iter.next());
209
                }
210

    
211
                jfc.setFileFilter((FileFilter)jimiExtensionsSupported.get("png"));
212
                if (jfc.showSaveDialog(
213
                                (Component) PluginServices.getMainFrame()
214
                                ) == JFileChooser.APPROVE_OPTION) {
215
                        
216
                        BufferedImage tempImage;
217

    
218
                        tempImage = ((View) PluginServices.getMDIManager().getActiveWindow()).getImage();
219

    
220
                        File f = jfc.getSelectedFile();
221
                        
222
                        lastPath = f.getParent();
223
                        
224
                        MyFileFilter filter = (MyFileFilter)jfc.getFileFilter();
225
                        f = filter.normalizeExtension(f);
226
                        
227
                        
228
                        if (f.exists()){
229
                                int resp = JOptionPane.showConfirmDialog(
230
                                                (Component) PluginServices.getMainFrame(),
231
                                                PluginServices.getText(this,
232
                                                                "fichero_ya_existe_seguro_desea_guardarlo")+
233
                                                                "\n"+ 
234
                                                                f.getAbsolutePath(),
235
                                                PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
236
                                if (resp != JOptionPane.YES_OPTION) {
237
                                        return;
238
                                }
239

    
240
                        }
241
                        
242
                        if (filter.getInfo().equalsIgnoreCase("cms")){
243
                                
244
                                try {
245
                                        saveImageCMS(f, tempImage);
246
                                } catch (IOException e) {
247
                                        NotificationManager.addError("Error exportando la imagen", e);
248
                                }
249
                        } else if (filter.getInfo().equalsIgnoreCase("jimi")) {
250
                                try {
251
                                        saveImageJimi(f, tempImage);
252
                                } catch (Exception e) {
253
                                        NotificationManager.addError("Error exportando la imagen", e);
254
                                }
255
                                
256
                        }
257

    
258
                        
259
                }
260
        }
261
}
262

    
263
class RasterizerImage implements IDataWriter{
264
        private BufferedImage source;
265
        private int[] data = null;
266
        
267
        public RasterizerImage(BufferedImage source) {
268
                this.source = source;
269
        }
270

    
271
        public int[] readARGBData(int sX, int sY, int nBand){
272
                return readData( sX, sY, nBand);
273
        }
274
        
275
        public int[] readData(int sizeX, int sizeY, int nBand) {
276
                if(nBand == 0){ //Con nBand==0 se devuelven las 3 bandas
277
                        if (this.data == null){                                        
278
                                this.data = this.source.getRGB(0, 0, sizeX, sizeY, this.data, 0, sizeX);
279
                        }
280
                        return this.data;
281
                }
282
                return null;
283
        }
284
        
285
}
286

    
287
class MyFileFilter extends FileFilter{
288

    
289
        private String[] extensiones=new String[1];
290
        private String description;
291
        private boolean dirs = true;
292
        private String info= null;
293

    
294
        public MyFileFilter(String[] ext, String desc) {
295
                extensiones = ext;
296
                description = desc;
297
        }
298

    
299
        public MyFileFilter(String[] ext, String desc,String info) {
300
                extensiones = ext;
301
                description = desc;
302
                this.info = info;
303
        }
304

    
305
        public MyFileFilter(String ext, String desc) {
306
                extensiones[0] = ext;
307
                description = desc;
308
        }
309

    
310
        public MyFileFilter(String ext, String desc,String info) {
311
                extensiones[0] = ext;
312
                description = desc;
313
                this.info = info;
314
        }
315

    
316
        public MyFileFilter(String ext, String desc, boolean dirs) {
317
                extensiones[0] = ext;
318
                description = desc;
319
                this.dirs = dirs;
320
        }
321

    
322
        public MyFileFilter(String ext, String desc, boolean dirs,String info) {
323
                extensiones[0] = ext;
324
                description = desc;
325
                this.dirs = dirs;
326
                this.info = info;
327
        }
328

    
329
        public boolean accept(File f) {
330
                if (f.isDirectory()) {
331
                        if (dirs) {
332
                                return true;
333
                        } else {
334
                                return false;
335
                        }
336
                }
337
                for (int i=0;i<extensiones.length;i++){
338
                        if (extensiones[i].equals("")){
339
                                continue;
340
                        }
341
                        if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])){
342
                                return true;
343
                        }
344
                }
345
                
346
                return false;
347
        }
348

    
349
        /**
350
         * @see javax.swing.filechooser.FileFilter#getDescription()
351
         */
352
        public String getDescription() {
353
                return description;
354
        }
355

    
356
        public String[] getExtensions() {
357
                return extensiones;
358
        }
359
        
360
        public boolean isDirectory(){
361
                return dirs;
362
        }
363
        
364
        private String getExtensionOfAFile(File file){
365
                String name;
366
                int dotPos;
367
                name = file.getName();
368
                dotPos = name.lastIndexOf(".");
369
                if (dotPos < 1){
370
                        return "";
371
                }
372
                return name.substring(dotPos+1);
373
        }
374
        
375
        public File normalizeExtension(File file){
376
                String ext = getExtensionOfAFile(file);
377
                if (ext.equals("") || !(this.accept(file))){
378
                        return new File(file.getAbsolutePath() + "." + extensiones[0]);
379
                }
380
                return file;                
381
        }
382
        
383
        public String getInfo(){
384
                return this.info;
385
        }
386
        
387
        public void setInfo(String info){
388
                this.info = info;
389
        }
390

    
391
}