Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / saveas / SaveAsTocMenuEntry.java @ 19568

History | View | Annotate | Download (11.3 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.JFileChooser;
28
import javax.swing.JOptionPane;
29

    
30
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
31
import org.gvsig.fmap.raster.layers.ILayerState;
32
import org.gvsig.fmap.raster.layers.IRasterLayerActions;
33
import org.gvsig.gui.beans.propertiespanel.PropertiesComponent;
34
import org.gvsig.raster.RasterProcess;
35
import org.gvsig.raster.buffer.BufferInterpolation;
36
import org.gvsig.raster.buffer.WriterBufferServer;
37
import org.gvsig.raster.dataset.GeoRasterWriter;
38
import org.gvsig.raster.dataset.IRasterDataSource;
39
import org.gvsig.raster.dataset.NotSupportedExtensionException;
40
import org.gvsig.raster.dataset.Params;
41
import org.gvsig.raster.dataset.io.RasterDriverException;
42
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
43
import org.gvsig.raster.gui.IGenericToolBarMenuItem;
44
import org.gvsig.raster.hierarchy.IRasterDataset;
45
import org.gvsig.raster.hierarchy.IRasterGeoOperations;
46
import org.gvsig.raster.hierarchy.IRasterOperations;
47
import org.gvsig.raster.util.ExtendedFileFilter;
48
import org.gvsig.raster.util.RasterToolsUtil;
49
import org.gvsig.raster.util.process.ClippingProcess;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.andami.ui.mdiManager.IWindow;
53
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
54
import com.iver.cit.gvsig.fmap.layers.FLayer;
55
import com.iver.cit.gvsig.project.documents.view.gui.View;
56
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
57
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
58

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

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

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

    
98
        }
99

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

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

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

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

    
132
        /*
133
         * (non-Javadoc)
134
         * @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[])
135
         */
136
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
137
                if ((selectedItems == null) || (selectedItems.length != 1))
138
                        return false;
139

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

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

    
146
                return true;
147
        }
148

    
149
        /*
150
         * (non-Javadoc)
151
         * @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[])
152
         */
153
        public boolean isVisible(ITocItem item, FLayer[] selectedItems) {
154
                if ((selectedItems == null) || (selectedItems.length != 1))
155
                        return false;
156

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

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

    
163
        /*
164
         * (non-Javadoc)
165
         * @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[])
166
         */
167
        public void execute(ITocItem item, FLayer[] selectedItems) {
168
                FLayer fLayer = null;
169
                IWindow w = PluginServices.getMDIManager().getActiveWindow();
170

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

    
174
                fLayer = selectedItems[0];
175

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

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

    
183
                // Cargamos las extensiones en la lista
184
                ArrayList extList = new ArrayList();
185
                try {
186
                        extList = GeoRasterWriter.getExtensionsSupported(((IRasterGeoOperations) fLayer).getDataType()[0], ((IRasterGeoOperations) fLayer).getBandCount());
187
                } catch (RasterDriverException e2) {
188
                        RasterToolsUtil.messageBoxError("error_extensiones_soportadas", chooser, e2);
189
                        return;
190
                }
191
                for (int i = 0; i < extList.size(); i++)
192
                        chooser.addChoosableFileFilter(new ExtendedFileFilter((String) extList.get(i)));
193

    
194
                // Cargamos el panel de propiedades en el selector
195
                panelProperty = loadPanelProperties((String) extList.get(extList.size() - 1));
196
                chooser.setAccessory(panelProperty);
197
                chooser.setAcceptAllFileFilterUsed(false);
198

    
199
                if (w instanceof View) {
200
                        if (chooser.showSaveDialog(((View) w).getComponent(0)) == JFileChooser.APPROVE_OPTION) {
201
                                // Creaci?n de par?metros
202
                                String tit = PluginServices.getMDIManager().getWindowInfo(w).getTitle();
203
                                WriterBufferServer dataWriter = new WriterBufferServer();
204
                                int[] dValues = new int[] { 0, (int) ((IRasterOperations) fLayer).getPxHeight(), (int) ((IRasterOperations) fLayer).getPxWidth(), 0 };
205
                                IRasterDataSource dataset = (IRasterDataSource)((IRasterDataset) fLayer).getDataSource();
206
                                int[] drawableBands = new int[((IRasterOperations) fLayer).getBandCount()];
207
                                for (int i = 0; i < ((IRasterOperations) fLayer).getBandCount(); i++)
208
                                        drawableBands[i] = i;
209
                                FileOpenWizard.setLastPath(chooser.getCurrentDirectory().toString());
210
                                String file =  ((ExtendedFileFilter) chooser.getFileFilter()).getNormalizedFilename(chooser.getSelectedFile());
211
                                if (!RasterToolsUtil.canWrite(chooser.getCurrentDirectory().toString(), this))
212
                                        return;
213
                                Params params = null;
214
                                try {
215
                                        params = GeoRasterWriter.getWriter(file).getParams();
216
                                } catch (NotSupportedExtensionException e1) {
217
                                        RasterToolsUtil.messageBoxError("no_driver_escritura", this, e1);
218
                                } catch (RasterDriverException e1) {
219
                                        RasterToolsUtil.messageBoxError("no_driver_escritura", this, e1);
220
                                }
221

    
222
                                DatasetColorInterpretation colorInterp = dataset.getColorInterpretation(0);
223
                                for (int datasetNumber = 1; datasetNumber < ((IRasterDataset) fLayer).getDataSource().getDatasetCount(); datasetNumber++) 
224
                                         colorInterp.addColorInterpretation(dataset.getColorInterpretation(datasetNumber));                                        
225
                                
226
                                
227
                                // Lanzamiento del proceso de guardado
228
                                RasterProcess clippingProcess = new ClippingProcess();
229
                                clippingProcess.setActions(new SaveAsActions());
230
                                clippingProcess.addParam("viewname", tit);
231
                                clippingProcess.addParam("pixelcoordinates", dValues);
232
                                clippingProcess.addParam("filename", file);
233
                                clippingProcess.addParam("datawriter", dataWriter);
234
                                clippingProcess.addParam("layer", fLayer);
235
                                clippingProcess.addParam("drawablebands", drawableBands);
236
                                clippingProcess.addParam("colorInterpretation", colorInterp);
237
                                clippingProcess.addParam("onelayerperband", new Boolean(false));
238
                                clippingProcess.addParam("interpolationmethod", new Integer(BufferInterpolation.INTERPOLATION_NearestNeighbour));
239
                                clippingProcess.addParam("affinetransform", dataset.getAffineTransform());
240
                                clippingProcess.addParam("resolution", new int[]{(int) ((IRasterOperations) fLayer).getPxWidth(),
241
                                                                                                                                 (int) ((IRasterOperations) fLayer).getPxHeight()});
242
                                                                
243
                                if (params != null)
244
                                        RasterToolsUtil.loadWriterParamsFromPropertiesPanel(panelProperty, params);
245
                                clippingProcess.addParam("driverparams", params);
246
                                clippingProcess.start();
247
                        }
248
                }
249
        }
250

    
251
        /**
252
         * Evento que se produce al cambiar el driver de escritura. Esto
253
         * sustituye el panel properties por el del nuevo driver seleccionado
254
         */
255
        public void propertyChange(PropertyChangeEvent evt) {
256
                if (evt.getNewValue() instanceof ExtendedFileFilter) {
257
                        String ext = ((ExtendedFileFilter) evt.getNewValue()).getExtensions().get(0).toString();
258
                        panelProperty = loadPanelProperties(ext);
259
                        chooser.setAccessory(panelProperty);
260
                        chooser.revalidate();
261
                }
262
        }
263

    
264
        /**
265
         * Obtiene el objeto PropertiesComponent para una extensi?n concreta de fichero
266
         * sobre el que se va a escribir.
267
         * @param file Fichero raster sobre el que se escribir?
268
         * @return PropertiesComponent
269
         */
270
        private PropertiesComponent loadPanelProperties(String file) {
271
                PropertiesComponent panelProperty = new PropertiesComponent();
272
                Params params = null;
273
                try {
274
                        params = GeoRasterWriter.getWriter(file).getParams();
275
                } catch (NotSupportedExtensionException e1) {
276
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "no_driver_escritura"));
277
                        return null;
278
                } catch (RasterDriverException e1) {
279
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "no_driver_escritura"));
280
                        return null;
281
                }
282
                RasterToolsUtil.loadPropertiesFromWriterParams(panelProperty, params, null);
283
                return panelProperty;
284
        }
285
        
286
        /*
287
         * (non-Javadoc)
288
         * @see org.gvsig.rastertools.generictoolbar.IGenericToolBarMenuItem#getIcon()
289
         */
290
        public Icon getIcon() {
291
                return PluginServices.getIconTheme().get("save-icon");
292
        }
293
}