Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / rastertools / saveas / SaveAsTocMenuEntry.java @ 31496

History | View | Annotate | Download (11.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.rastertools.saveas;
20

    
21
import java.awt.Component;
22
import java.beans.PropertyChangeEvent;
23
import java.beans.PropertyChangeListener;
24
import java.util.ArrayList;
25

    
26
import javax.swing.Icon;
27
import javax.swing.JOptionPane;
28
import javax.swing.filechooser.FileFilter;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.ui.mdiManager.IWindow;
32
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
33
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
34
import org.gvsig.app.project.documents.view.toc.AbstractTocContextMenuAction;
35
import org.gvsig.app.project.documents.view.toc.ITocItem;
36
import org.gvsig.fmap.mapcontext.layers.FLayer;
37
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
38
import org.gvsig.fmap.raster.layers.ILayerState;
39
import org.gvsig.fmap.raster.layers.IRasterLayerActions;
40
import org.gvsig.gui.beans.propertiespanel.PropertiesComponent;
41
import org.gvsig.gui.beans.swing.JFileChooser;
42
import org.gvsig.raster.RasterProcess;
43
import org.gvsig.raster.buffer.BufferInterpolation;
44
import org.gvsig.raster.buffer.WriterBufferServer;
45
import org.gvsig.raster.dataset.GeoRasterWriter;
46
import org.gvsig.raster.dataset.IRasterDataSource;
47
import org.gvsig.raster.dataset.NotSupportedExtensionException;
48
import org.gvsig.raster.dataset.Params;
49
import org.gvsig.raster.dataset.RasterDriverException;
50
import org.gvsig.raster.gui.IGenericToolBarMenuItem;
51
import org.gvsig.raster.hierarchy.IRasterDataset;
52
import org.gvsig.raster.hierarchy.IRasterGeoOperations;
53
import org.gvsig.raster.hierarchy.IRasterOperations;
54
import org.gvsig.raster.util.ExtendedFileFilter;
55
import org.gvsig.raster.util.RasterToolsUtil;
56
import org.gvsig.raster.util.RasterUtilities;
57
import org.gvsig.raster.util.process.ClippingProcess;
58

    
59

    
60
/**
61
 * <p>
62
 * Entrada del TOC que corresponde con la opci?n "Salvar Como" de raster. Esta se apoya
63
 * en el proceso de recorte de raster para salvar datos.
64
 * </p>
65
 * <p>
66
 * Cuando se abre el dialogo de "Salvar como" se cargan las extensiones soportadas en el
67
 * selector. Cada tipo de datos de la fuente soporta unas extensiones de escritura u otras.
68
 * Por ejemplo, si la capa de entrada  es FLOAT no podremos escribir a JPG2000 porque no
69
 * lo soporta, tendriamos que convertila primero a RGB.
70
 * </p>
71
 * <p>
72
 * Cambiando el tipo de extensi?n en el selector cambian el panel de propiedades asociado.
73
 * El fichero de salidad se salvar? con las propiedades ajustadas.
74
 * </P>
75
 * @version 30/05/2007
76
 * @author Nacho Brodin (nachobrodin@gmail.com)
77
 *
78
 */
79
public class SaveAsTocMenuEntry extends AbstractTocContextMenuAction implements PropertyChangeListener, IGenericToolBarMenuItem {
80
        static private SaveAsTocMenuEntry singleton     = null;
81
        private JFileChooser              chooser       = null;
82
        private PropertiesComponent       panelProperty = null;
83

    
84
        /**
85
         * Nadie puede crear una instancia a esta clase ?nica, hay que usar el
86
         * getSingleton()
87
         */
88
        private SaveAsTocMenuEntry() {}
89

    
90
        /**
91
         * Devuelve un objeto unico a dicha clase
92
         * @return
93
         */
94
        static public SaveAsTocMenuEntry getSingleton() {
95
                if (singleton == null)
96
                        singleton = new SaveAsTocMenuEntry();
97
                return singleton;
98

    
99
        }
100

    
101
        /*
102
         * (non-Javadoc)
103
         * @see com.iver.cit.gvsig.project.documents.contextMenu.AbstractContextMenuAction#getGroup()
104
         */
105
        public String getGroup() {
106
                return "RasterExport";
107
        }
108

    
109
        /*
110
         * (non-Javadoc)
111
         * @see com.iver.cit.gvsig.project.documents.contextMenu.AbstractContextMenuAction#getGroupOrder()
112
         */
113
        public int getGroupOrder() {
114
                return 50;
115
        }
116

    
117
        /*
118
         * (non-Javadoc)
119
         * @see com.iver.cit.gvsig.project.documents.contextMenu.AbstractContextMenuAction#getOrder()
120
         */
121
        public int getOrder() {
122
                return 1;
123
        }
124

    
125
        /*
126
         * (non-Javadoc)
127
         * @see com.iver.cit.gvsig.project.documents.IContextMenuAction#getText()
128
         */
129
        public String getText() {
130
                return PluginServices.getText(this, "saveas");
131
        }
132

    
133
        /*
134
         * (non-Javadoc)
135
         * @see com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction#isEnabled(com.iver.cit.gvsig.project.documents.view.toc.ITocItem, com.iver.cit.gvsig.fmap.layers.FLayer[])
136
         */
137
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
138
                if ((selectedItems == null) || (selectedItems.length != 1))
139
                        return false;
140

    
141
                if (!(selectedItems[0] instanceof ILayerState))
142
                        return false;
143

    
144
                if (!((ILayerState) selectedItems[0]).isOpen())
145
                        return false;
146

    
147
                return true;
148
        }
149

    
150
        /*
151
         * (non-Javadoc)
152
         * @see com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction#isVisible(com.iver.cit.gvsig.project.documents.view.toc.ITocItem, com.iver.cit.gvsig.fmap.layers.FLayer[])
153
         */
154
        public boolean isVisible(ITocItem item, FLayer[] selectedItems) {
155
                if ((selectedItems == null) || (selectedItems.length != 1))
156
                        return false;
157

    
158
                if (!(selectedItems[0] instanceof FLyrRasterSE))
159
                        return false;
160

    
161
                return (((FLyrRasterSE) selectedItems[0]).isActionEnabled(IRasterLayerActions.SAVEAS));
162
        }
163

    
164
        /*
165
         * (non-Javadoc)
166
         * @see com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction#execute(com.iver.cit.gvsig.project.documents.view.toc.ITocItem, com.iver.cit.gvsig.fmap.layers.FLayer[])
167
         */
168
        public void execute(ITocItem item, FLayer[] selectedItems) {
169
                FLayer fLayer = null;
170
                IWindow w = PluginServices.getMDIManager().getActiveWindow();
171

    
172
                if (selectedItems.length != 1)
173
                        return;
174

    
175
                fLayer = selectedItems[0];
176

    
177
                if (!(fLayer instanceof IRasterOperations))
178
                        return;
179

    
180
                chooser = new JFileChooser("SAVE_AS_TOC_MENU_ENTRY",JFileChooser.getLastPath("SAVE_AS_TOC_MENU_ENTRY", null));
181
                chooser.addPropertyChangeListener(this);
182
                chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
183

    
184
                // Cargamos las extensiones en la lista
185
                ArrayList extList = new ArrayList();
186
                try {
187
                        extList = GeoRasterWriter.getExtensionsSupported(((IRasterGeoOperations) fLayer).getDataType()[0], ((IRasterGeoOperations) fLayer).getBandCount(), false);
188
                } catch (RasterDriverException e2) {
189
                        RasterToolsUtil.messageBoxError("error_extensiones_soportadas", chooser, e2);
190
                        return;
191
                }
192

    
193
                int selected_id = extList.size() - 1;
194
                FileFilter selected = null;
195
                for (int i = 0; i < extList.size(); i++) {
196
                        FileFilter filter = new ExtendedFileFilter((String) extList.get(i));
197
                        if (extList.get(i).equals("tif")) {
198
                                selected = filter;
199
                                selected_id = i;
200
                        }
201
                        chooser.addChoosableFileFilter(filter);
202
                }
203
                if (selected != null)
204
                        chooser.setFileFilter(selected);
205

    
206
                // Cargamos el panel de propiedades en el selector
207
                panelProperty = loadPanelProperties((String) extList.get(selected_id));
208
                chooser.setAccessory(panelProperty);
209
                chooser.setAcceptAllFileFilterUsed(false);
210

    
211
                if (w instanceof DefaultViewPanel) {
212
                        if (chooser.showSaveDialog(((AbstractViewPanel) w).getComponent(0)) == JFileChooser.APPROVE_OPTION) {
213
                                // Creaci?n de par?metros
214
                                String tit = PluginServices.getMDIManager().getWindowInfo(w).getTitle();
215
                                WriterBufferServer dataWriter = new WriterBufferServer();
216
                                int[] dValues = new int[] { 0, (int) ((IRasterOperations) fLayer).getPxHeight(), (int) ((IRasterOperations) fLayer).getPxWidth(), 0 };
217
                                IRasterDataSource dataset = (IRasterDataSource)((IRasterDataset) fLayer).getDataSource();
218
                                int[] drawableBands = new int[((IRasterOperations) fLayer).getBandCount()];
219
                                for (int i = 0; i < ((IRasterOperations) fLayer).getBandCount(); i++)
220
                                        drawableBands[i] = i;
221
                                JFileChooser.setLastPath("SAVE_AS_TOC_MENU_ENTRY", chooser.getCurrentDirectory());
222
                                String file =  ((ExtendedFileFilter) chooser.getFileFilter()).getNormalizedFilename(chooser.getSelectedFile());
223
                                
224
                                long bytes = RasterUtilities.getBytesFromRaster((int)((IRasterOperations) fLayer).getPxWidth(),
225
                                                (int) ((IRasterOperations) fLayer).getPxHeight(),
226
                                                ((IRasterGeoOperations) fLayer).getDataType()[0],
227
                                                ((IRasterOperations) fLayer).getBandCount());
228
                                long maxJp2 = 13000 * 12500 * 3;
229
                                if (bytes > (20000*20000*3)){
230
                                        RasterToolsUtil.messageBoxInfo("output_file_too_big", null);
231
                                        return;
232
                                }
233
                                if (file.endsWith(".jp2")){
234
                                        if (bytes > maxJp2){
235
                                                RasterToolsUtil.messageBoxInfo("output_file_too_big_jpeg2000", null);
236
                                                return;
237
                                        }
238
                                }
239
                                
240
                                if (!RasterToolsUtil.canWrite(chooser.getCurrentDirectory().toString(), this))
241
                                        return;
242
                                Params params = null;
243
                                try {
244
                                        params = GeoRasterWriter.getWriter(file).getParams();
245
                                } catch (NotSupportedExtensionException e1) {
246
                                        RasterToolsUtil.messageBoxError("no_driver_escritura", this, e1);
247
                                } catch (RasterDriverException e1) {
248
                                        RasterToolsUtil.messageBoxError("no_driver_escritura", this, e1);
249
                                }
250

    
251
                                // Lanzamiento del proceso de guardado
252
                                RasterProcess clippingProcess = new ClippingProcess();
253
                                clippingProcess.setActions(new SaveAsActions());
254
                                clippingProcess.addParam("viewname", tit);
255
                                clippingProcess.addParam("pixelcoordinates", dValues);
256
                                clippingProcess.addParam("filename", file);
257
                                clippingProcess.addParam("datawriter", dataWriter);
258
                                clippingProcess.addParam("layer", fLayer);
259
                                clippingProcess.addParam("drawablebands", drawableBands);
260
                                clippingProcess.addParam("colorInterpretation", dataset.getColorInterpretation());
261
                                clippingProcess.addParam("onelayerperband", new Boolean(false));
262
                                clippingProcess.addParam("interpolationmethod", new Integer(BufferInterpolation.INTERPOLATION_NearestNeighbour));
263
                                clippingProcess.addParam("affinetransform", dataset.getAffineTransform(0));
264
                                clippingProcess.addParam("resolution", new int[]{(int) ((IRasterOperations) fLayer).getPxWidth(),
265
                                                                                                                                 (int) ((IRasterOperations) fLayer).getPxHeight()});
266

    
267
                                if (params != null)
268
                                        RasterToolsUtil.loadWriterParamsFromPropertiesPanel(panelProperty, params);
269
                                clippingProcess.addParam("driverparams", params);
270
                                clippingProcess.start();
271
                        }
272
                }
273
        }
274

    
275
        /**
276
         * Evento que se produce al cambiar el driver de escritura. Esto
277
         * sustituye el panel properties por el del nuevo driver seleccionado
278
         */
279
        public void propertyChange(PropertyChangeEvent evt) {
280
                if (evt.getNewValue() instanceof ExtendedFileFilter) {
281
                        String ext = ((ExtendedFileFilter) evt.getNewValue()).getExtensions().get(0).toString();
282
                        panelProperty = loadPanelProperties(ext);
283
                        chooser.setAccessory(panelProperty);
284
                        chooser.revalidate();
285
                }
286
        }
287

    
288
        /**
289
         * Obtiene el objeto PropertiesComponent para una extensi?n concreta de fichero
290
         * sobre el que se va a escribir.
291
         * @param file Fichero raster sobre el que se escribir?
292
         * @return PropertiesComponent
293
         */
294
        private PropertiesComponent loadPanelProperties(String file) {
295
                PropertiesComponent panelProperty = new PropertiesComponent();
296
                Params params = null;
297
                try {
298
                        params = GeoRasterWriter.getWriter(file).getParams();
299
                } catch (NotSupportedExtensionException e1) {
300
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "no_driver_escritura"));
301
                        return null;
302
                } catch (RasterDriverException e1) {
303
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "no_driver_escritura"));
304
                        return null;
305
                }
306
                RasterToolsUtil.loadPropertiesFromWriterParams(panelProperty, params, null);
307
                return panelProperty;
308
        }
309

    
310
        /*
311
         * (non-Javadoc)
312
         * @see org.gvsig.rastertools.generictoolbar.IGenericToolBarMenuItem#getIcon()
313
         */
314
        public Icon getIcon() {
315
                return PluginServices.getIconTheme().get("save-icon");
316
        }
317
}