Statistics
| Revision:

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

History | View | Annotate | Download (5.84 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.Image;
51
import java.io.File;
52
import java.io.FileNotFoundException;
53
import java.io.FileOutputStream;
54
import java.io.IOException;
55

    
56
import javax.swing.JFileChooser;
57
import javax.swing.filechooser.FileFilter;
58

    
59
import com.iver.andami.PluginServices;
60
import com.iver.andami.messages.NotificationManager;
61
import com.iver.andami.plugins.Extension;
62
import com.iver.cit.gvsig.gui.View;
63
import com.iver.utiles.GenericFileFilter;
64
import com.sun.jimi.core.Jimi;
65
import com.sun.jimi.core.JimiException;
66
import com.sun.jimi.core.raster.JimiRasterImage;
67

    
68

    
69
/**
70
 * Extensi?n para exportar en algunos formatos raster la vista actual.
71
 *
72
 * @author Fernando Gonz?lez Cort?s
73
 */
74
public class Export extends Extension {
75
        /**
76
         * @see com.iver.andami.plugins.IExtension#isEnabled()
77
         */
78
        public boolean isEnabled() {
79
                View f = (View) PluginServices.getMDIManager().getActiveView();
80

    
81
                if (f == null) {
82
                        return false;
83
                }
84

    
85
                return f.getModel().getMapContext().getLayers().getLayersCount() > 0;
86
        }
87

    
88
        /**
89
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
90
         */
91
        public boolean isVisible() {
92
                com.iver.andami.ui.mdiManager.View f = (com.iver.andami.ui.mdiManager.View) PluginServices.getMDIManager()
93
                                                                                                                                                                                                  .getActiveView();
94

    
95
                if (f == null) {
96
                        return false;
97
                }
98

    
99
                return (f.getClass() == View.class);
100
        }
101

    
102
        /**
103
         * @see com.iver.andami.plugins.IExtension#initialize()
104
         */
105
        public void initialize() {
106
        }
107

    
108
        /**
109
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
110
         */
111
        public void execute(String actionCommand) {
112
                FileFilter pngFilter = new GenericFileFilter("png",
113
                                PluginServices.getText(this, "png"));
114
                String[] s={"jpg","jpeg"};
115
                FileFilter jpgFilter = new GenericFileFilter(s,
116
                                PluginServices.getText(this, "jpg"));
117
                FileFilter bmpFilter = new GenericFileFilter("bmp",
118
                                PluginServices.getText(this, "bmp"));
119
                
120
                JFileChooser jfc = new JFileChooser();
121
                jfc.addChoosableFileFilter(pngFilter);
122
                jfc.addChoosableFileFilter(jpgFilter);
123
                jfc.addChoosableFileFilter(bmpFilter);
124
                jfc.setFileFilter(pngFilter);
125
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
126
                        Image tempImage;
127

    
128
                        tempImage = ((View) PluginServices.getMDIManager().getActiveView()).getImage();
129

    
130
                        File f = jfc.getSelectedFile();
131

    
132
                        try {
133
                                JimiRasterImage jrf = Jimi.createRasterImage(tempImage.getSource());
134
                                String tempName = f.getName().toUpperCase().trim();
135
                                if (jfc.getFileFilter().equals(pngFilter)){
136
                                        System.out.println("pngFilter");
137
                                        if (!(tempName.length()>3) || (!(tempName.substring(tempName.length()-4,tempName.length()).equalsIgnoreCase(".png")))){
138
                                                f=new File(f.getPath()+".png");
139
                                        }
140
                                                FileOutputStream fout = new FileOutputStream(f);
141
                                                Jimi.putImage("image/png", jrf, fout);
142
                                                fout.close();
143
                                } else if (jfc.getFileFilter().equals(bmpFilter)){
144
                                        System.out.println("bmpFilter");
145
                                        if (!(tempName.length()>3) || (!(tempName.substring(tempName.length()-4,tempName.length()).equalsIgnoreCase(".bmp")))){
146
                                                f=new File(f.getPath()+".bmp");
147
                                        }
148
                                                FileOutputStream fout = new FileOutputStream(f);
149
                                                Jimi.putImage("image/bmp", jrf, fout);
150
                                                fout.close();
151
                                }else if (jfc.getFileFilter().equals(jpgFilter)){
152
                                        System.out.println("jpgFilter");
153
                                        if (!(tempName.length()>3) || ((tempName.length()>3 && !(tempName.substring(tempName.length()-4,tempName.length()).equalsIgnoreCase(".jpg")) || (tempName.length()>4 &&tempName.substring(tempName.length()-5,tempName.length()).equalsIgnoreCase(".jpeg"))))){
154
                                                f=new File(f.getPath()+".jpg");
155
                                        }
156
                                                FileOutputStream fout = new FileOutputStream(f);
157
                                                Jimi.putImage("image/jpg", jrf, fout);
158
                                                fout.close();
159
                                        
160
                                }
161
                                /*if (tempName.endsWith(".JPG")) {
162
                                        
163
                                        FileOutputStream fout = new FileOutputStream(f);
164
                                        Jimi.putImage("image/jpg", jrf, fout);
165
                                        fout.close();
166
                                } else if (tempName.endsWith(".PNG")) {
167
                                        FileOutputStream fout = new FileOutputStream(f);
168
                                        Jimi.putImage("image/png", jrf, fout);
169
                                        fout.close();
170
                                } else if (tempName.endsWith(".BMP")) {
171
                                        FileOutputStream fout = new FileOutputStream(f);
172
                                        Jimi.putImage("image/bmp", jrf, fout);
173
                                        fout.close();
174
                                }
175
                                */
176
                        } catch (JimiException e) {
177
                                NotificationManager.addError("Error exportando la imagen", e);
178
                        } catch (FileNotFoundException e) {
179
                                NotificationManager.addError("No se encontr? el fichero", e);
180
                        } catch (IOException e) {
181
                                NotificationManager.addError("Error con el fichero", e);
182
                        }
183
                }
184
        }
185
}