Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / ui / raster / SaveSetupPanel.java @ 2849

History | View | Annotate | Download (12.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
import java.awt.Dimension;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.awt.event.FocusEvent;
30
import java.awt.event.FocusListener;
31
import java.awt.event.KeyEvent;
32
import java.awt.event.KeyListener;
33
import java.awt.event.MouseEvent;
34
import java.awt.event.MouseListener;
35

    
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
/**
44
 * Panel encargado de manejar los eventos del los controles de Salvar a Raste
45
 * @author Nacho Brodin (brodin_ign@gva.es)
46
 */
47
public class SaveSetupPanel extends JPanel implements ActionListener,
48
                                                      MouseListener,
49
                                                      FocusListener,
50
                                                      KeyListener {
51
    final private static long serialVersionUID = -3370601314380922368L;
52
    private DataInputSaveRaster saveRaster = null;
53
    private SaveRasterDialogPanel dialogPanel = null;
54
    private String fName = null;
55

    
56
    /**
57
     * This method initializes
58
     *
59
     */
60
    public SaveSetupPanel() {
61
        super();
62
        initialize();
63
    }
64

    
65
    /**
66
     * This method initializes this
67
     *
68
     * @return void
69
     */
70
    void initialize() {
71
        this.setPreferredSize(new Dimension(350, 210)); //AnchoxAlto  panel interior
72
        this.setBounds(0, 0, 300, 250);
73
        this.setLayout(null);
74
        this.add(getSaveParameters(), null);
75

    
76
        //A?adimos gesti?n de eventos        
77
        saveRaster.getJComboBox().addActionListener(this);
78
        saveRaster.getTescala().addActionListener(this);
79
        saveRaster.getTescala().addMouseListener(this);
80
        saveRaster.getTescala().addFocusListener(this);
81
        saveRaster.getTinf_derX().addActionListener(this);
82
        saveRaster.getTinf_derY().addActionListener(this);
83
        saveRaster.getTsup_izqX().addActionListener(this);
84
        saveRaster.getTsup_izqY().addActionListener(this);
85
        saveRaster.getTinf_derX().addMouseListener(this);
86
        saveRaster.getTinf_derY().addMouseListener(this);
87
        saveRaster.getTsup_izqX().addMouseListener(this);
88
        saveRaster.getTsup_izqY().addMouseListener(this);
89
        saveRaster.getTinf_derX().addFocusListener(this);
90
        saveRaster.getTinf_derY().addFocusListener(this);
91
        saveRaster.getTsup_izqX().addFocusListener(this);
92
        saveRaster.getTsup_izqY().addFocusListener(this);
93
        saveRaster.getBseleccion().addActionListener(this);
94
        saveRaster.getBPropiedades().addActionListener(this);
95
        saveRaster.getTescala().addKeyListener(this);
96
        saveRaster.getJComboBox().addKeyListener(this);
97
    }
98

    
99
    /**
100
     * Asigna un valor al panel padre
101
     * @param dialogPanel
102
     */
103
    public void setDialogPanel(SaveRasterDialogPanel dialogPanel) {
104
        this.dialogPanel = dialogPanel;
105
    }
106

    
107
    /**
108
     * Obtiene el Panel de Controles interior
109
     * @return
110
     */
111
    public DataInputSaveRaster getSaveParameters() {
112
        if (saveRaster == null) {
113
            saveRaster = new DataInputSaveRaster();
114

    
115
            //saveRaster.setBounds(9, 9, 328, 75);
116
        }
117

    
118
        return saveRaster;
119
    }
120

    
121
    /**
122
     * Calculo del tama?o en pixels de la imagen a partir de las coordenadas del
123
     * mundo real, la escala y los puntos por pulgada
124
     */
125
    private void calcSizeImage() {
126
        double lrX = Double.parseDouble(saveRaster.getTinf_derX().getText());
127
        double lrY = Double.parseDouble(saveRaster.getTinf_derY().getText());
128
        double ulX = Double.parseDouble(saveRaster.getTsup_izqX().getText());
129
        double ulY = Double.parseDouble(saveRaster.getTsup_izqY().getText());
130
        double wc_anchomts = 0;
131

    
132
        if (ulX > lrX) {
133
            wc_anchomts = ulX - lrX;
134
        } else {
135
            wc_anchomts = lrX - ulX;
136
        }
137

    
138
        double wc_altomts = 0;
139

    
140
        if (ulY > lrY) {
141
            wc_altomts = ulY - lrY;
142
        } else {
143
            wc_altomts = lrY - ulY;
144
        }
145

    
146
        //No aseguramos de que la escala sea un entero. Si no lo es ponemos un 0
147
        try {
148
            Integer.parseInt(saveRaster.getTescala().getText());
149
        } catch (NumberFormatException e) {
150
            saveRaster.getTescala().setText("0");
151
        }
152

    
153
        if ((wc_anchomts <= 0) || (wc_altomts <= 0) ||
154
                (Integer.parseInt(saveRaster.getTescala().getText()) == 0)) {
155
            saveRaster.getTancho().setText("0");
156
            saveRaster.getTalto().setText("0");
157

    
158
            return;
159
        }
160

    
161
        //Calculo del tama?o de la imagen definitiva en pulgadas
162
        double anchopulgadas = (wc_anchomts / Double.parseDouble(saveRaster.getTescala()
163
                                                                           .getText())) * 39.73007874D;
164
        double altopulgadas = (wc_altomts / Double.parseDouble(saveRaster.getTescala()
165
                                                                         .getText())) * 39.73007874D;
166

    
167
        //Ancho en pixeles = ppp * sizepulgadas
168
        int anchopixels = (int) (Integer.parseInt(saveRaster.getJComboBox()
169
                                                            .getSelectedItem()
170
                                                            .toString()) * anchopulgadas);
171
        int altopixels = (int) (Integer.parseInt(saveRaster.getJComboBox()
172
                                                           .getSelectedItem()
173
                                                           .toString()) * altopulgadas);
174

    
175
        //int anchopixels =(int) (Toolkit.getDefaultToolkit().getScreenResolution() * anchopulgadas);
176
        //int altopixels = (int) (Toolkit.getDefaultToolkit().getScreenResolution() * altopulgadas);
177
        saveRaster.getTancho().setText(String.valueOf(anchopixels));
178
        saveRaster.getTalto().setText(String.valueOf(altopixels));
179
    }
180

    
181
    /**
182
     * Controla cuando se cumplen todos los requisitos en el formulario para
183
     * poder activar el bot?n de aceptar.
184
     */
185
    private void enableButtons() {
186
        if ((saveRaster.getTescala() == null) ||
187
                saveRaster.getTescala().getText().equals("") ||
188
                saveRaster.getTescala().getText().equals("0") ||
189
                (this.fName == null) || this.fName.equals("")) {
190
            if (dialogPanel != null) {
191
                dialogPanel.getAcceptButton().setEnabled(false);
192
            }
193

    
194
            return;
195
        }
196

    
197
        if (saveRaster.getTancho().getText().equals("0") ||
198
                saveRaster.getTalto().getText().equals("0")) {
199
            if (dialogPanel != null) {
200
                dialogPanel.getAcceptButton().setEnabled(false);
201
            }
202

    
203
            return;
204
        }
205

    
206
        if (dialogPanel != null) {
207
            dialogPanel.getAcceptButton().setEnabled(true);
208
        }
209
    }
210

    
211
    /**
212
     * Gestiona los eventos del cambio de escalla de la imagen y lla apertura del
213
     * dialogo de selecci?n del nombre del fichero.
214
     */
215
    public void actionPerformed(ActionEvent e) {
216
        if (e.getSource().equals(saveRaster.getJComboBox()) ||
217
                e.getSource().equals(saveRaster.getTescala()) ||
218
                e.getSource().equals(saveRaster.getTinf_derX()) ||
219
                e.getSource().equals(saveRaster.getTinf_derY()) ||
220
                e.getSource().equals(saveRaster.getTsup_izqX()) ||
221
                e.getSource().equals(saveRaster.getTsup_izqY())) {
222
            this.calcSizeImage();
223
            this.enableButtons();
224
        }
225

    
226
        //Selector de Fichero sobre el cual se va a salvar a raster
227
        if (e.getSource().equals(saveRaster.getBseleccion())) {
228
            JFileChooser chooser = new JFileChooser("./");
229
            chooser.setDialogTitle("Seleccionar el fichero");
230

    
231
            String os = System.getProperties().getProperty("os.version");
232

    
233
            if (os.startsWith("2.4")) {
234
                chooser.addChoosableFileFilter(new EcwFilter(chooser));
235
            }
236

    
237
            chooser.addChoosableFileFilter(new TifFilter(chooser));
238
            
239
            int returnVal = chooser.showOpenDialog(this);
240

    
241
            if (returnVal == JFileChooser.APPROVE_OPTION) {
242
                this.fName = chooser.getSelectedFile().toString();
243

    
244
                FileFilter filter = chooser.getFileFilter();
245

    
246
                if ((fName != null) && !fName.equals("")) {
247
                    if (this.fName.endsWith(".ecw") && os.startsWith("2.4")) {
248
                        saveRaster.getBPropiedades().setText("Propiedades Ecw");
249
                    } else if (this.fName.endsWith(".tif")) {
250
                        saveRaster.getBPropiedades().setText("Propiedades GeoTiff");
251
                    } else if (filter.getDescription().equals(".ecw") &&
252
                                   os.startsWith("2.4")) {
253
                        saveRaster.getBPropiedades().setText("Propiedades Ecw");
254
                    } else if (filter.getDescription().equals(".tif")) {
255
                        saveRaster.getBPropiedades().setText("Propiedades GeoTiff");
256
                    }
257
                }
258

    
259
                if (!fName.endsWith(".ecw") && 
260
                        !fName.endsWith(".tif") ) {
261
                    fName = new String(chooser.getSelectedFile().toString() +
262
                                       filter.getDescription());
263
                }
264

    
265
                enableButtons();
266
            }
267
        }
268
    }
269

    
270
    /**
271
     * Devuelve el nombre del fichero seleccionado
272
     * @return Nombre del fichero seleccionado
273
     */
274
    public String getFileName() {
275
        return fName;
276
    }
277

    
278
    /**
279
     * Unused
280
     */
281
    public void mouseExited(MouseEvent e) {
282
    }
283

    
284
    /**
285
     * Unused. Only for compilant purposes
286
     */
287
    public void mouseReleased(MouseEvent e) {
288
    }
289

    
290
    /**
291
     * Unused. Only for compilant purposes
292
     */
293
    public void mouseEntered(MouseEvent e) {
294
    }
295

    
296
    public void mouseClicked(MouseEvent e) {
297
        this.calcSizeImage();
298
        enableButtons();
299
    }
300

    
301
    /**
302
     * Unused. Only for compilant purposes
303
     */
304
    public void mousePressed(MouseEvent e) {
305
    }
306

    
307
    /**
308
     * Unused. Only for compilant purposes
309
     */
310
    public void focusGained(FocusEvent e) {
311
    }
312

    
313
    /**
314
     * Recalcula el tama?o de la imagen con los datos existentes y
315
     * activa o desactiva los botones.
316
     */
317
    public void focusLost(FocusEvent e) {
318
        this.calcSizeImage();
319
        enableButtons();
320
    }
321

    
322
    /**
323
     * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
324
     */
325
    public void keyTyped(KeyEvent e) {
326
        enableButtons();
327
    }
328

    
329
    /**
330
     * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
331
     */
332
    public void keyPressed(KeyEvent e) {
333
        enableButtons();
334
    }
335

    
336
    /**
337
     * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
338
     */
339
    public void keyReleased(KeyEvent e) {
340
        enableButtons();
341
    }
342
}
343

    
344

    
345
/**
346
 * @author Nacho Brodin <brodin_ign@gva.es>
347
 * Filtro Ecw para el selector de formatos de escritura
348
 */
349
class EcwFilter extends javax.swing.filechooser.FileFilter {
350
    private JFileChooser chooser = null;
351

    
352
    public EcwFilter(JFileChooser ch) {
353
        this.chooser = ch;
354
    }
355

    
356
    public boolean accept(File f) {
357
        return f.isDirectory() || f.getName().toLowerCase().endsWith(".ecw");
358
    }
359

    
360
    public String getDescription() {
361
        return ".ecw";
362
    }
363
}
364

    
365

    
366
/**
367
 * @author Nacho Brodin <brodin_ign@gva.es>
368
 * Filtro GTiff para el selector de formatos de escritura
369
 */
370
class TifFilter extends javax.swing.filechooser.FileFilter {
371
    private JFileChooser chooser = null;
372

    
373
    public TifFilter(JFileChooser ch) {
374
        this.chooser = ch;
375
    }
376

    
377
    public boolean accept(File f) {
378
        return f.isDirectory() || f.getName().toLowerCase().endsWith(".tif");
379
    }
380

    
381
    public String getDescription() {
382
        return ".tif";
383
    }
384
}
385