Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / ui / raster / SaveSetupPanel.java @ 2312

History | View | Annotate | Download (10.5 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.cresques.ui.raster;
25

    
26

    
27
import java.awt.Dimension;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.awt.event.FocusEvent;
31
import java.awt.event.FocusListener;
32
import java.awt.event.KeyEvent;
33
import java.awt.event.KeyListener;
34
import java.awt.event.MouseEvent;
35
import java.awt.event.MouseListener;
36
import java.io.File;
37

    
38
import javax.swing.JFileChooser;
39
import javax.swing.JPanel;
40
import javax.swing.filechooser.FileFilter;
41

    
42
/**
43
 * Panel encargado de manejar los eventos del los controles de Salvar a Raste
44
 * @author Nacho Brodin <brodin_ign@gva.es>
45
 */
46
public class SaveSetupPanel extends JPanel implements ActionListener, MouseListener, FocusListener, KeyListener{
47
        private DataInputSaveRaster saveRaster = null;
48
        private SaveRasterDialogPanel dialogPanel = null;
49
        private String fName = null; 
50

    
51
        /**
52
         * This method initializes 
53
         * 
54
         */
55
        public SaveSetupPanel() {
56
                super();
57
                initialize();
58
        }
59
        /**
60
         * This method initializes this
61
         * 
62
         * @return void
63
         */
64
        void initialize() {
65
        this.setPreferredSize(new Dimension(350,210)); //AnchoxAlto  panel interior
66
        this.setBounds(0, 0, 300, 250);
67
        this.setLayout(null);
68
        this.add(getSaveParameters(), null);
69
        
70
        //A?adimos gesti?n de eventos        
71
        
72
        saveRaster.getJComboBox().addActionListener(this);
73
        saveRaster.getTescala().addActionListener(this);
74
        saveRaster.getTescala().addMouseListener(this);
75
        saveRaster.getTescala().addFocusListener(this);
76
        saveRaster.getTinf_derX().addActionListener(this);
77
        saveRaster.getTinf_derY().addActionListener(this);
78
        saveRaster.getTsup_izqX().addActionListener(this);
79
        saveRaster.getTsup_izqY().addActionListener(this);
80
        saveRaster.getTinf_derX().addMouseListener(this);
81
        saveRaster.getTinf_derY().addMouseListener(this);
82
        saveRaster.getTsup_izqX().addMouseListener(this);
83
        saveRaster.getTsup_izqY().addMouseListener(this);
84
        saveRaster.getTinf_derX().addFocusListener(this);
85
        saveRaster.getTinf_derY().addFocusListener(this);
86
        saveRaster.getTsup_izqX().addFocusListener(this);
87
        saveRaster.getTsup_izqY().addFocusListener(this);
88
        saveRaster.getBseleccion().addActionListener(this);
89
        saveRaster.getBPropiedades().addActionListener(this);
90
        saveRaster.getTescala().addKeyListener(this);
91
        saveRaster.getJComboBox().addKeyListener(this);
92
        }
93

    
94
        /**
95
         * Asigna un valor al panel padre
96
         * @param dialogPanel
97
         */
98
        
99
        public void setDialogPanel(SaveRasterDialogPanel dialogPanel){
100
                this.dialogPanel = dialogPanel;
101
        }
102
        
103
        /**
104
         * Obtiene el Panel de Controles interior
105
         * @return
106
         */
107
        
108
        public DataInputSaveRaster getSaveParameters() {
109
                if (saveRaster == null) {
110
                        saveRaster = new DataInputSaveRaster();
111
                        //saveRaster.setBounds(9, 9, 328, 75);
112

    
113
                }
114
                return saveRaster;
115
        }
116
        
117
        /**
118
         * Calculo del tama?o en pixels de la imagen a partir de las coordenadas del 
119
         * mundo real, la escala y los puntos por pulgada
120
         */
121
        private void calcSizeImage(){
122
                double lrX = Double.parseDouble(saveRaster.getTinf_derX().getText());
123
                double lrY = Double.parseDouble(saveRaster.getTinf_derY().getText());
124
                double ulX = Double.parseDouble(saveRaster.getTsup_izqX().getText());
125
                double ulY = Double.parseDouble(saveRaster.getTsup_izqY().getText());
126
                double wc_anchomts = 0;
127
                if(ulX>lrX)wc_anchomts = ulX-lrX;
128
                        else wc_anchomts = lrX-ulX;
129
                double wc_altomts = 0;
130
                if(ulY>lrY)wc_altomts = ulY-lrY;
131
                        else wc_altomts = lrY-ulY;
132
                
133
                //No aseguramos de que la escala sea un entero. Si no lo es ponemos un 0
134
                
135
                try{
136
                        Integer.parseInt(saveRaster.getTescala().getText());
137
                }catch(NumberFormatException e){
138
                        saveRaster.getTescala().setText("0");
139
                }
140
                
141
                if(wc_anchomts<=0 || wc_altomts<=0 ||  Integer.parseInt(saveRaster.getTescala().getText())==0 ){
142
                        saveRaster.getTancho().setText("0");
143
                        saveRaster.getTalto().setText("0");
144
                        return;
145
                }
146
                
147
                //Calculo del tama?o de la imagen definitiva en pulgadas
148
                
149
                double anchopulgadas = (wc_anchomts / Double.parseDouble(saveRaster.getTescala().getText())) * 39.73007874;
150
                double altopulgadas = (wc_altomts / Double.parseDouble (saveRaster.getTescala().getText())) * 39.73007874;
151
                
152
                //Ancho en pixeles = ppp * sizepulgadas
153
                
154
                int anchopixels =(int) (Integer.parseInt(saveRaster.getJComboBox().getSelectedItem().toString() ) * anchopulgadas);
155
                int altopixels = (int) (Integer.parseInt(saveRaster.getJComboBox().getSelectedItem().toString() ) * altopulgadas);
156
                           
157
                //int anchopixels =(int) (Toolkit.getDefaultToolkit().getScreenResolution() * anchopulgadas);
158
                //int altopixels = (int) (Toolkit.getDefaultToolkit().getScreenResolution() * altopulgadas);
159
                
160
                saveRaster.getTancho().setText(String.valueOf(anchopixels));
161
                saveRaster.getTalto().setText(String.valueOf(altopixels));
162
                
163
        }
164
        
165
        /**
166
         * Controla cuando se cumplen todos los requisitos en el formulario para 
167
         * poder activar el bot?n de aceptar.
168
         */
169
        private void enableButtons(){
170
                
171
                if(        saveRaster.getTescala()== null || 
172
                        saveRaster.getTescala().getText().equals("") ||
173
                        saveRaster.getTescala().getText().equals("0") ||
174
                        this.fName == null ||
175
                    this.fName.equals("")){
176
                                if(dialogPanel!=null)
177
                                        dialogPanel.getAcceptButton().setEnabled(false);
178
                                return;
179
                }
180
        
181
                
182
                if( saveRaster.getTancho().getText().equals("0") ||
183
                        saveRaster.getTalto().getText().equals("0")){
184
                        if(dialogPanel!=null)
185
                                dialogPanel.getAcceptButton().setEnabled(false);
186
                        return;
187
                }
188
                
189
                if(dialogPanel!=null)
190
                        dialogPanel.getAcceptButton().setEnabled(true);
191
        }
192
        
193
                
194
        /**
195
         * Gestiona los eventos del cambio de escalla de la imagen y lla apertura del
196
         * dialogo de selecci?n del nombre del fichero.
197
         */
198
        public void actionPerformed(ActionEvent e){
199
                
200
                if(                e.getSource().equals(saveRaster.getJComboBox()) ||
201
                                e.getSource().equals(saveRaster.getTescala()) ||
202
                                e.getSource().equals(saveRaster.getTinf_derX()) || 
203
                                e.getSource().equals(saveRaster.getTinf_derY()) ||
204
                                e.getSource().equals(saveRaster.getTsup_izqX()) ||
205
                                e.getSource().equals(saveRaster.getTsup_izqY()) ){
206
                        this.calcSizeImage();
207
                        this.enableButtons();
208
                }
209
                        
210
                //Selector de Fichero sobre el cual se va a salvar a raster
211
                
212
                if(        e.getSource().equals(saveRaster.getBseleccion())){
213
                         JFileChooser chooser = new JFileChooser("./");
214
                         chooser.setDialogTitle("Seleccionar el fichero");
215
                         String os = System.getProperties().getProperty("os.version");
216
                         if(os.startsWith("2.4"))
217
                                 chooser.addChoosableFileFilter(new EcwFilter(chooser));
218
                         chooser.addChoosableFileFilter(new TifFilter(chooser));
219
                         int returnVal = chooser.showOpenDialog(this);
220
                         if(returnVal == JFileChooser.APPROVE_OPTION){
221
                                 this.fName = chooser.getSelectedFile().toString();
222
        
223
                                 FileFilter filter = chooser.getFileFilter();
224
                                 if(fName!=null && !fName.equals("")){
225
                                         if(this.fName.endsWith(".ecw") && os.startsWith("2.4"))
226
                                                 saveRaster.getBPropiedades().setText("Propiedades Ecw");
227
                                         else if(this.fName.endsWith(".tif"))
228
                                                 saveRaster.getBPropiedades().setText("Propiedades GeoTiff");
229
                                         else if(filter.getDescription().equals(".ecw") && os.startsWith("2.4"))
230
                                                 saveRaster.getBPropiedades().setText("Propiedades Ecw");
231
                                         else if(filter.getDescription().equals(".tif"))
232
                                                 saveRaster.getBPropiedades().setText("Propiedades GeoTiff");
233
                                 }
234
                                 if(        !fName.endsWith(".ecw") &&
235
                                         !fName.endsWith(".tif"))
236
                                         fName = new String(chooser.getSelectedFile().toString()+filter.getDescription());
237
                
238
                                 enableButtons();
239
                         }
240
            }
241
        }
242
        
243
        
244
        /**
245
         * Devuelve el nombre del fichero seleccionado
246
         * @return Nombre del fichero seleccionado
247
         */
248
        public String getFileName(){
249
                return fName;
250
        }
251
        
252
        
253
        /**
254
         * Unused
255
         */
256
        public void mouseExited(MouseEvent e){}
257
        
258
        /**
259
         * Unused. Only for compilant purposes
260
         */
261
        public void mouseReleased(MouseEvent e){}        
262
        
263
        /**
264
         * Unused. Only for compilant purposes
265
         */
266
        public void mouseEntered(MouseEvent e){}
267
        
268
        public void mouseClicked(MouseEvent e){
269
                this.calcSizeImage();
270
                enableButtons();
271
        }
272
        
273
        /**
274
         * Unused. Only for compilant purposes
275
         */
276
        public void mousePressed(MouseEvent e){}
277
        
278
        /**
279
         * Unused. Only for compilant purposes
280
         */
281
        public void focusGained(FocusEvent e){}
282
        
283
        /**
284
         * Recalcula el tama?o de la imagen con los datos existentes y
285
         * activa o desactiva los botones.
286
         */
287
        public void focusLost(FocusEvent e){
288
                this.calcSizeImage();
289
                enableButtons();
290
        }
291
        
292
        /**
293
         * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
294
         */
295
        public void keyTyped(KeyEvent e){enableButtons();}
296
        
297
        /**
298
         * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
299
         */
300
        public void keyPressed(KeyEvent e){enableButtons();}
301
        
302
        /**
303
         * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
304
         */
305
        public void keyReleased(KeyEvent e){enableButtons();}
306
 }  
307

    
308
/**
309
 * @author Nacho Brodin <brodin_ign@gva.es>
310
 * Filtro Ecw para el selector de formatos de escritura
311
 */
312
class EcwFilter extends javax.swing.filechooser.FileFilter {
313
        
314
        private JFileChooser chooser = null;
315
        
316
        public EcwFilter(JFileChooser ch){
317
                this.chooser = ch;
318
        }
319
        
320
    public boolean accept(File f) {
321

    
322
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".ecw");
323
    }
324
    
325
    public String getDescription() {
326
            return ".ecw";
327
    }
328
    
329
}
330

    
331
/**
332
 * @author Nacho Brodin <brodin_ign@gva.es>
333
 * Filtro GTiff para el selector de formatos de escritura
334
 */
335
class TifFilter extends javax.swing.filechooser.FileFilter {
336
        
337
        private JFileChooser chooser = null;
338
        
339
        public TifFilter(JFileChooser ch){
340
                this.chooser = ch;
341
        }
342
        
343
    public boolean accept(File f) {
344
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".tif");
345
    }
346
    
347
    public String getDescription() {
348
            return ".tif";
349
    }
350
    
351
}