Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_1_RELEASE / extensions / extRasterTools / src / org / gvsig / rasterTools / saveRaster / ui / main / SaveRasterDefaultPanel.java @ 9531

History | View | Annotate | Download (9.91 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.gvsig.rasterTools.saveRaster.ui.main;
25

    
26
import java.awt.Component;
27
import java.awt.Container;
28
import java.awt.Dimension;
29
import java.awt.event.ActionEvent;
30
import java.awt.geom.Rectangle2D;
31
import java.io.File;
32

    
33
import javax.swing.JOptionPane;
34
import javax.swing.JPanel;
35

    
36
import org.cresques.cts.IProjection;
37
import org.cresques.io.GeoRasterWriter;
38
import org.cresques.ui.DefaultDialogPanel;
39
import org.gvsig.rasterTools.saveRaster.ui.listener.DataInputListener;
40
import org.gvsig.rasterTools.saveRaster.ui.properties.ProgressSaveRasterDialog;
41
import org.gvsig.rasterTools.saveRaster.ui.properties.SaveRasterPropsDialog;
42
import org.gvsig.rasterTools.saveRaster.util.DriverProperties;
43

    
44
import com.iver.andami.PluginServices;
45
import com.iver.andami.ui.mdiManager.IWindow;
46
import com.iver.andami.ui.mdiManager.WindowInfo;
47
import com.iver.cit.gvsig.fmap.MapControl;
48
import com.iver.cit.gvsig.fmap.layers.FLayers;
49

    
50

    
51
/**
52
 * Panel que contiene los botones de Aceptar, Cancelar y Aplicar heredando de
53
 * DefaultDialogPanel. Dentro de este estar? el panel con los controles de
54
 * salvar a raster.
55
 * @author Nacho Brodin (brodin_ign@gva.es)
56
 */
57
public class SaveRasterDefaultPanel extends DefaultDialogPanel implements IWindow {
58
    final private static long                         serialVersionUID = -3370601314380922368L;
59
    private DataInputListener                        listener =  null;
60
    private GeoRasterWriter                                writer=null;
61
        private IProjection                                 currentProjection = null;
62
        private SaveRasterPropsDialog                 frameSaveRasterProps = null;
63
        private int                                                 compresion = 1;
64
        private int                                                 sizeBlock = GeoRasterWriter.blockSizeDefault;//64;
65
        public DriverProperties                                driverProps = null;
66
        private FLayers                                                 layers = null;
67
        private MapControl                                        mapCtrl = null;
68

    
69
        /**
70
         * Constructor de la ventana de dialogo para gvSIG.
71
         */
72
        public SaveRasterDefaultPanel(FLayers layers, MapControl mapCtrl) {
73
                super(false);
74
                initialize();
75
                driverProps = new DriverProperties();
76
                this.layers = layers;
77
                this.mapCtrl = mapCtrl;
78
                init();
79
        }
80
        
81
        /**
82
         * Constructor generico para poder visualizar la ventana desde
83
         * una funci?n main.
84
         */
85
    public SaveRasterDefaultPanel() {
86
        super(false);
87
                initialize();
88
        init();
89
    }
90

    
91
    
92
        /**
93
     * Inicializaci?n del panel.
94
     * @return void
95
     */
96
    public void init() {
97
            super.initialize();
98
            //this.setSize(410, 380);
99
        this.setSize(new java.awt.Dimension(410,390));
100
        this.pButton.setPreferredSize(new java.awt.Dimension(390, 25));
101
            this.getListener();
102
            
103
        //Ocultamos el bot?n de aplicar
104
        this.setLocation(new java.awt.Point(0,0));
105
        this.getApplyButton().setVisible(false);
106
        this.getAcceptButton().setEnabled(false);
107
        
108
        setName("saveRaster");
109
                
110
                 //this.getAcceptButton().setEnabled(false);
111
                 this.getAcceptButton().addActionListener(new java.awt.event.ActionListener() {
112
            public void actionPerformed(java.awt.event.ActionEvent evt) {
113
                    if(acceptButtonActionPerformed(evt))
114
                        closeJDialog();
115
            }
116
        });
117
                 this.getCancelButton().addActionListener(new java.awt.event.ActionListener() {
118
            public void actionPerformed(java.awt.event.ActionEvent evt) {
119
                closeJDialog();
120
            }
121
        });
122
                 this.getApplyButton().addActionListener(new java.awt.event.ActionListener() {
123
            public void actionPerformed(java.awt.event.ActionEvent evt) {
124
                    acceptButtonActionPerformed(evt);
125
             }
126
        });
127
                
128
                //Captura de eventos para el bot?n de propiedades
129
                
130
                ((SaveRasterControlsPanel)this.getContentPanel()).getBProperties().addActionListener(new java.awt.event.ActionListener() {
131
            public void actionPerformed(java.awt.event.ActionEvent evt) {
132
                    propsButtonActionPerformed(evt);
133
             }
134
        });
135
                
136
    }
137

    
138
    /**
139
     * Cierra esta ventana a trav?s de andami 
140
     */
141
    public void closeJDialog() {
142
                this.hide();
143
                for(int i=0;i<layers.getLayersCount();i++){
144
                        layers.getLayer(i).getMapContext().invalidate();
145
                }
146
                PluginServices.getMDIManager().closeWindow(SaveRasterDefaultPanel.this);
147
        }
148
    
149
    /**
150
     * Funci?n a ejecutar cuando se pulsa el bot?n de propiedades.
151
     * @param e ActionEvent
152
     */
153
    private void propsButtonActionPerformed(ActionEvent e){
154
            String name = this.getListener().getFileName();
155
                                
156
                //Si no se ha seleccionado ning?n fichero salimos
157
                if(name == null || name.equals(""))
158
                        return;
159
        
160
                GeoRasterWriter writer = GeoRasterWriter.getWriter(name);
161
                String ext = name.toLowerCase().substring(name.lastIndexOf(".") + 1);
162
                if(driverProps.getProperties(ext)!=null)
163
                        writer.setProps(driverProps.getProperties(ext));
164
                
165
                frameSaveRasterProps = new SaveRasterPropsDialog(writer);
166
                frameSaveRasterProps.setParentDialog(this);
167
                frameSaveRasterProps.show();
168
        }
169
        
170
        /**
171
         * 
172
         * @param e
173
         */
174
    private boolean acceptButtonActionPerformed(ActionEvent e) {
175
            SaveRasterControlsPanel dialog = (SaveRasterControlsPanel)this.getContentPanel();
176
                        
177
                String fName = this.getListener().getFileName();
178
                Dimension dimension = new Dimension(        this.getListener().getWidthInPixels(), 
179
                                                                                        this.getListener().getHeightInPixels() );
180
                
181
                //Comprobamos que el en la ruta de destino tengamos permisos de escritura
182
                /*File f = new File(fName);
183
                if(!f.exists()){
184
                        if(!checkFileExists(fName))
185
                                return false;
186
                }
187
                f = new File(fName.substring(0, fName.lastIndexOf(File.separator)));
188
                if(f.exists() && f.isDirectory() && !f.canWrite()){
189
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
190
                                        PluginServices.getText(this, "error_file_not_writable"));
191
                        return false;
192
                }*/
193
                
194
                double lrX = Double.parseDouble(dialog.getTInfDerX().getText());
195
                double lrY = Double.parseDouble(dialog.getTInfDerY().getText());
196
                double ulX = Double.parseDouble(dialog.getTSupIzqX().getText());
197
                double ulY = Double.parseDouble(dialog.getTSupIzqY().getText());
198
                double width = 0;
199
                if(ulX>lrX)width = (double)(ulX-lrX);
200
                        else width = (double)(lrX-ulX);
201
                double height = 0;
202
                if(ulY>lrY)height = (double)(ulY-lrY);
203
                        else height = (double)(lrY-ulY);
204
                Rectangle2D ext = null;
205
                
206
                if(ulY<lrY)
207
                        ext = new Rectangle2D.Double(ulX, ulY, width, height);
208
                else
209
                        ext = new Rectangle2D.Double(ulX, lrY, width, height);
210
                
211
                ProgressSaveRasterDialog progressBar = 
212
                        new ProgressSaveRasterDialog(mapCtrl, this.layers, this.currentProjection,
213
                                                                                dimension, ext, this);
214
                
215
                progressBar.setProps(driverProps);
216
                progressBar.setWriter(writer);
217
                progressBar.setFName(fName);
218
                progressBar.setBlockSize(sizeBlock);
219
                progressBar.setCompresion(compresion);
220
                progressBar.showWindow();
221
                                
222
                for(int i=0;i<layers.getLayersCount();i++){
223
                        layers.getLayer(i).getMapContext().invalidate();
224
                }
225
                return true;
226
    }
227
    
228
    /**
229
         * Comprueba si existe el fichero pasado por par?metro existe solicitando confirmaci?n
230
         * de sobreescritura. Si el usuario acepta devolver? true y si no acepta devuelve false.
231
         * Si se acepta la sobreescritura el fichero es borrado. 
232
         * @param file Cadena con el nombre del fichero a comprobar su existencia.
233
         * @return Devuelve true si el fichero existe y va a ser sobreescrito
234
         */
235
        private boolean checkFileExists(String file){
236
                File f = new File(file);
237
                if(f.exists()){
238
                        String string1 = PluginServices.getText(this, "yes");
239
                        String string2 = PluginServices.getText(this, "no");
240
                        Object[] options = {string1, string2};
241
                        int n = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
242
                                        PluginServices.getText(this, "error_file_exists"),
243
                                        PluginServices.getText(this, "confirmacion"),
244
                                        JOptionPane.YES_NO_OPTION,
245
                                        JOptionPane.QUESTION_MESSAGE,
246
                                        null,     
247
                                        options,  
248
                                        string1); 
249
                        if (n == JOptionPane.YES_OPTION){ 
250
                                f.delete();
251
                                return true;
252
                        }else
253
                                return false;
254
                }
255
                return true;
256
        }
257
        
258
    /**
259
     * Obtiene el panel con los controles de Salvar a Raster.
260
     * @return
261
     */
262
    public JPanel getContentPanel() {
263
        if (contentPane == null) {
264
                contentPane = new SaveRasterControlsPanel();
265
            contentPane.setPreferredSize(new java.awt.Dimension(379, 317));
266
        }
267
        return contentPane;
268
    }
269

    
270
        public DataInputListener getListener() {
271
                if(listener == null){
272
                        listener = new DataInputListener((SaveRasterControlsPanel)this.getContentPanel());
273
                        listener.setDialogPanel(this);
274
                }
275
                return listener;
276
        }
277
        
278
        /**
279
         * This method initializes jContentPane        
280
         *         
281
         * @return javax.swing.JPanel        
282
         */    
283
        public Container getContentPane() {
284
                return this;
285
        }
286
        
287
        /**
288
         * 
289
         * @param prj
290
         */
291
        
292
        public void setProjection(IProjection prj) {
293
                this.currentProjection = prj;
294
        }
295
        
296
        /**
297
         * Asigna la lista de capas
298
         * @param layers
299
         */
300
        public void setLayerList(FLayers layers){
301
                this.layers = layers;
302
        }
303
        
304
        /**
305
         * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
306
         */
307
        public WindowInfo getWindowInfo() {
308
                WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
309
                    m_viewinfo.setTitle(PluginServices.getText(this, "salvar_raster"));
310
                return m_viewinfo;
311
        }
312

    
313
        /**
314
         * Asigna el tama?o de bloque 
315
         * @param sizeBlock
316
         */
317
        public void setSizeBlock(int sizeBlock) {
318
                this.sizeBlock = sizeBlock;
319
        }
320
}