root / trunk / extensions / extRasterTools / src / org / gvsig / rasterTools / saveRaster / ui / listener / DataInputListener.java @ 11038
History | View | Annotate | Download (24.8 KB)
1 | 4547 | nacho | /*
|
---|---|---|---|
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.listener; |
||
25 | |||
26 | import java.awt.event.ActionEvent; |
||
27 | import java.awt.event.ActionListener; |
||
28 | import java.awt.event.FocusEvent; |
||
29 | import java.awt.event.FocusListener; |
||
30 | import java.awt.event.KeyEvent; |
||
31 | import java.awt.event.KeyListener; |
||
32 | import java.awt.event.MouseEvent; |
||
33 | import java.awt.event.MouseListener; |
||
34 | import java.io.File; |
||
35 | |||
36 | import javax.swing.JFileChooser; |
||
37 | import javax.swing.JTextField; |
||
38 | import javax.swing.filechooser.FileFilter; |
||
39 | |||
40 | import org.cresques.io.GeoRasterWriter; |
||
41 | import org.gvsig.rasterTools.saveRaster.ui.main.SaveRasterControlsPanel; |
||
42 | import org.gvsig.rasterTools.saveRaster.ui.main.SaveRasterDefaultPanel; |
||
43 | import org.gvsig.rasterTools.saveRaster.util.MathUtils; |
||
44 | |||
45 | import com.iver.andami.PluginServices; |
||
46 | |||
47 | |||
48 | /**
|
||
49 | * Panel encargado de manejar los eventos del los controles de Salvar a Raster
|
||
50 | * @author Nacho Brodin (brodin_ign@gva.es)
|
||
51 | */
|
||
52 | public class DataInputListener implements ActionListener, |
||
53 | MouseListener,
|
||
54 | FocusListener,
|
||
55 | KeyListener {
|
||
56 | final private static long serialVersionUID = -3370601314380922368L; |
||
57 | private SaveRasterControlsPanel controlPanel = null; |
||
58 | private SaveRasterDefaultPanel dialog = null; |
||
59 | private String fName = null; |
||
60 | private int widthInPixels = 0; |
||
61 | private int heightInPixels = 0; |
||
62 | private double mtsPerPixel = 0D; |
||
63 | private Object obj = null; |
||
64 | private double widthMts = 0D, heightMts = 0D; |
||
65 | |||
66 | /**
|
||
67 | * This method initializes
|
||
68 | *
|
||
69 | */
|
||
70 | public DataInputListener(SaveRasterControlsPanel controlPanel) {
|
||
71 | super();
|
||
72 | this.controlPanel = controlPanel;
|
||
73 | initialize(); |
||
74 | } |
||
75 | |||
76 | /**
|
||
77 | * This method initializes this
|
||
78 | *
|
||
79 | * @return void
|
||
80 | */
|
||
81 | void initialize() {
|
||
82 | |||
83 | //A?adimos gesti?n de eventos
|
||
84 | controlPanel.getTScale().addActionListener(this);
|
||
85 | controlPanel.getTScale().addMouseListener(this);
|
||
86 | controlPanel.getTScale().addFocusListener(this);
|
||
87 | controlPanel.getTScale().addKeyListener(this);
|
||
88 | |||
89 | controlPanel.getTMtsPixel().addActionListener(this);
|
||
90 | controlPanel.getTMtsPixel().addMouseListener(this);
|
||
91 | controlPanel.getTMtsPixel().addFocusListener(this);
|
||
92 | controlPanel.getTMtsPixel().addKeyListener(this);
|
||
93 | |||
94 | controlPanel.getTWidth().addActionListener(this);
|
||
95 | controlPanel.getTWidth().addMouseListener(this);
|
||
96 | controlPanel.getTWidth().addFocusListener(this);
|
||
97 | controlPanel.getTWidth().addKeyListener(this);
|
||
98 | controlPanel.getTHeight().addActionListener(this);
|
||
99 | controlPanel.getTHeight().addMouseListener(this);
|
||
100 | controlPanel.getTHeight().addFocusListener(this);
|
||
101 | controlPanel.getTHeight().addKeyListener(this);
|
||
102 | |||
103 | controlPanel.getCbMeasureType().addActionListener(this);
|
||
104 | |||
105 | controlPanel.getTInfDerX().addActionListener(this);
|
||
106 | controlPanel.getTInfDerY().addActionListener(this);
|
||
107 | controlPanel.getTSupIzqX().addActionListener(this);
|
||
108 | controlPanel.getTSupIzqY().addActionListener(this);
|
||
109 | controlPanel.getTInfDerX().addMouseListener(this);
|
||
110 | controlPanel.getTInfDerY().addMouseListener(this);
|
||
111 | controlPanel.getTSupIzqX().addMouseListener(this);
|
||
112 | controlPanel.getTSupIzqY().addMouseListener(this);
|
||
113 | controlPanel.getTInfDerX().addFocusListener(this);
|
||
114 | controlPanel.getTInfDerY().addFocusListener(this);
|
||
115 | controlPanel.getTSupIzqX().addFocusListener(this);
|
||
116 | controlPanel.getTSupIzqY().addFocusListener(this);
|
||
117 | |||
118 | controlPanel.getBSelect().addActionListener(this);
|
||
119 | controlPanel.getBProperties().addActionListener(this);
|
||
120 | |||
121 | controlPanel.getCbResolution().addActionListener(this);
|
||
122 | controlPanel.getCbResolution().addKeyListener(this);
|
||
123 | } |
||
124 | |||
125 | /**
|
||
126 | * Asigna un valor al panel padre
|
||
127 | * @param dialogPanel
|
||
128 | */
|
||
129 | public void setDialogPanel(SaveRasterDefaultPanel dialog) { |
||
130 | this.dialog = dialog;
|
||
131 | } |
||
132 | |||
133 | /**
|
||
134 | * Obtiene el Panel de Controles interior
|
||
135 | * @return
|
||
136 | */
|
||
137 | public SaveRasterControlsPanel getSaveParameters() {
|
||
138 | if (controlPanel == null) { |
||
139 | controlPanel = new SaveRasterControlsPanel();
|
||
140 | //saveRaster.setBounds(9, 9, 328, 75);
|
||
141 | } |
||
142 | return controlPanel;
|
||
143 | } |
||
144 | |||
145 | /**
|
||
146 | * A partir del valor de tama?o de imagen de salida, comprueba el tipo de
|
||
147 | * dato seleccionado por el usuario (Pixels, mms, cms, mts, pulgadas) y devuelve
|
||
148 | * el valor en pixeles.
|
||
149 | * @param value Cadena introducida por el usuario
|
||
150 | */
|
||
151 | private int getCorrectMeasure(String value){ |
||
152 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Pixels")) |
||
153 | return Integer.parseInt(value); |
||
154 | |||
155 | int ppp = Integer.parseInt((String)controlPanel.getCbResolution().getSelectedItem()); |
||
156 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Mts")) |
||
157 | return MathUtils.convertMtsToPixels(Double.parseDouble(value), ppp); |
||
158 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Cms")) |
||
159 | return MathUtils.convertCmsToPixels(Double.parseDouble(value), ppp); |
||
160 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Mms")) |
||
161 | return MathUtils.convertMmsToPixels(Double.parseDouble(value), ppp); |
||
162 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Inches")) |
||
163 | return MathUtils.convertInchesToPixels(Double.parseDouble(value), ppp); |
||
164 | |||
165 | return 0; |
||
166 | } |
||
167 | |||
168 | /**
|
||
169 | * Asigna al JTextField pasado como par?metro el valor en pixels, cms, mms ,mtrs, pulgadas dependiendo
|
||
170 | * de la selecci?n del combo
|
||
171 | * @param pixel Valor en pixels
|
||
172 | * @param field Campo donde se escribe el valor
|
||
173 | */
|
||
174 | private void setCorrectMeasure(int pixel, JTextField field){ |
||
175 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Pixels")) |
||
176 | field.setText(String.valueOf(pixel));
|
||
177 | |||
178 | int ppp = Integer.parseInt((String)controlPanel.getCbResolution().getSelectedItem()); |
||
179 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Mts")) |
||
180 | field.setText(String.valueOf(MathUtils.tailDecimals(MathUtils.convertPixelsToMts(pixel, ppp),5))); |
||
181 | |||
182 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Cms")) |
||
183 | field.setText(String.valueOf(MathUtils.tailDecimals(MathUtils.convertPixelsToCms(pixel, ppp),5))); |
||
184 | |||
185 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Mms")) |
||
186 | field.setText(String.valueOf(MathUtils.tailDecimals(MathUtils.convertPixelsToMms(pixel, ppp),5))); |
||
187 | |||
188 | if(controlPanel.getCbMeasureType().getSelectedItem().equals("Inches")) |
||
189 | field.setText(String.valueOf(MathUtils.tailDecimals(MathUtils.convertPixelsToInches(pixel, ppp),5))); |
||
190 | |||
191 | } |
||
192 | |||
193 | /**
|
||
194 | * Calculo del tama?o en pixels de la imagen a partir de las coordenadas del
|
||
195 | * mundo real, la escala y los puntos por pulgada
|
||
196 | */
|
||
197 | private void recalcParams() { |
||
198 | validateFields(); |
||
199 | double[] size = null; |
||
200 | try{
|
||
201 | size = calcSizeInMts(); |
||
202 | } catch (NumberFormatException e) { |
||
203 | return;
|
||
204 | } |
||
205 | widthMts = size[0];
|
||
206 | heightMts = size[1];
|
||
207 | |||
208 | int resolution = Integer.parseInt((String)controlPanel.getCbResolution().getSelectedItem()); |
||
209 | |||
210 | //Al variar la resoluci?n independientemente del m?todo seleccionado recalculamos el ancho y el alto
|
||
211 | if (obj.equals(controlPanel.getCbResolution())) {
|
||
212 | String escala = controlPanel.getTScale().getText();
|
||
213 | 6271 | maquerol | try{
|
214 | if(controlPanel.getProjection().isProjected())
|
||
215 | 4547 | nacho | calcSizeMtsPixel(Integer.parseInt(escala), resolution);
|
216 | 6271 | maquerol | } |
217 | catch(NumberFormatException exc){ |
||
218 | calcSizeMtsPixel(0, resolution);
|
||
219 | } |
||
220 | 4547 | nacho | return;
|
221 | } |
||
222 | |||
223 | //M?todo por escala seleccionado
|
||
224 | if( controlPanel.getRbScale().isSelected() &&
|
||
225 | !controlPanel.getTScale().getText().equals("")){
|
||
226 | int scale = Integer.parseInt(controlPanel.getTScale().getText()); |
||
227 | if(controlPanel.getProjection().isProjected())
|
||
228 | calcSizeMtsPixel(scale, resolution); |
||
229 | else
|
||
230 | calcSizeMtsPixelGeodesicas(scale, resolution); |
||
231 | } |
||
232 | |||
233 | //M?todo por tama?o seleccionado
|
||
234 | if(controlPanel.getRbSize().isSelected()){
|
||
235 | double rel = (widthMts / heightMts);
|
||
236 | |||
237 | if( obj != null && obj.equals(controlPanel.getTWidth()) && |
||
238 | !controlPanel.getTWidth().getText().equals("")){
|
||
239 | this.widthInPixels = this.getCorrectMeasure(controlPanel.getTWidth().getText()); |
||
240 | this.heightInPixels = (int)Math.floor(this.widthInPixels / rel); |
||
241 | if(controlPanel.getProjection().isProjected())
|
||
242 | calcScaleMtsPixel(this.widthInPixels, this.heightInPixels, resolution); |
||
243 | } |
||
244 | if( obj != null && obj.equals(controlPanel.getTHeight()) && |
||
245 | !controlPanel.getTHeight().getText().equals("")){
|
||
246 | this.heightInPixels = this.getCorrectMeasure(controlPanel.getTHeight().getText()); |
||
247 | this.widthInPixels = (int)Math.ceil(this.heightInPixels * rel); |
||
248 | if(controlPanel.getProjection().isProjected())
|
||
249 | calcScaleMtsPixel(this.widthInPixels, this.heightInPixels, resolution); |
||
250 | } |
||
251 | if( obj != null && |
||
252 | obj.equals(controlPanel.getCbMeasureType())){ |
||
253 | if(controlPanel.getProjection().isProjected())
|
||
254 | calcScaleMtsPixel(this.widthInPixels, this.heightInPixels, resolution); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | //M?todo metros por pixel seleccionado
|
||
259 | if( controlPanel.getRbMtsPixel().isSelected() &&
|
||
260 | !controlPanel.getTMtsPixel().getText().equals("")){
|
||
261 | double mtsPixel = Double.parseDouble(controlPanel.getTMtsPixel().getText()); |
||
262 | if(controlPanel.getProjection().isProjected())
|
||
263 | calcSizeScale(mtsPixel, resolution); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /**
|
||
268 | * Comprueba si un campo de texto tiene el tipo de dato entero o double y si no lo
|
||
269 | * tiene lo borra ya que su contenido es invalido para operar con el
|
||
270 | * @param field Campo de texto a validar
|
||
271 | * @param isInt true si el valor a validar es entero y false si es double
|
||
272 | */
|
||
273 | private void validateTextField(JTextField field, boolean isInt){ |
||
274 | try {
|
||
275 | if(isInt)
|
||
276 | Integer.parseInt(field.getText());
|
||
277 | else
|
||
278 | Double.parseDouble(field.getText());
|
||
279 | } catch (NumberFormatException e) { |
||
280 | field.setText("");
|
||
281 | } |
||
282 | } |
||
283 | |||
284 | /**
|
||
285 | * Valida los campos de texto
|
||
286 | */
|
||
287 | private void validateFields(){ |
||
288 | |||
289 | //Validamos la escala si se ha introducido algo
|
||
290 | if(!controlPanel.getTScale().getText().equals("")) |
||
291 | validateTextField(controlPanel.getTScale(), true);
|
||
292 | |||
293 | //Validamos los mts por pixel si se ha introducido algo
|
||
294 | if(!controlPanel.getTMtsPixel().getText().equals("")) |
||
295 | validateTextField(controlPanel.getTMtsPixel(), false);
|
||
296 | |||
297 | //Validamos el ancho si se ha introducido algo
|
||
298 | if(!controlPanel.getTWidth().getText().equals("")){ |
||
299 | if(((String)controlPanel.getCbMeasureType().getSelectedItem()).equals("Pixels")) |
||
300 | validateTextField(controlPanel.getTWidth(), true);
|
||
301 | else
|
||
302 | validateTextField(controlPanel.getTWidth(), false);
|
||
303 | } |
||
304 | |||
305 | //Validamos el alto si se ha introducido algo
|
||
306 | if(!controlPanel.getTHeight().getText().equals("")){ |
||
307 | if(((String)controlPanel.getCbMeasureType().getSelectedItem()).equals("Pixels")) |
||
308 | validateTextField(controlPanel.getTHeight(), true);
|
||
309 | else
|
||
310 | validateTextField(controlPanel.getTHeight(), false);
|
||
311 | } |
||
312 | } |
||
313 | |||
314 | /**
|
||
315 | * Calcula el tama?o en mtrs a partir de las coordenadas.
|
||
316 | * TODO: Esto solo es valido para UTM. El ancho y alto debe obtenerse desde la vista
|
||
317 | * @return
|
||
318 | * @throws NumberFormatException
|
||
319 | */
|
||
320 | private double[] calcSizeInMts()throws NumberFormatException{ |
||
321 | double[] size = new double[2]; |
||
322 | |||
323 | double lrX = Double.parseDouble(controlPanel.getTInfDerX().getText()); |
||
324 | double lrY = Double.parseDouble(controlPanel.getTInfDerY().getText()); |
||
325 | double ulX = Double.parseDouble(controlPanel.getTSupIzqX().getText()); |
||
326 | double ulY = Double.parseDouble(controlPanel.getTSupIzqY().getText()); |
||
327 | |||
328 | if (ulX > lrX) {
|
||
329 | size[0] = ulX - lrX;
|
||
330 | } else {
|
||
331 | size[0] = lrX - ulX;
|
||
332 | } |
||
333 | |||
334 | if (ulY > lrY) {
|
||
335 | size[1] = ulY - lrY;
|
||
336 | } else {
|
||
337 | size[1] = lrY - ulY;
|
||
338 | } |
||
339 | return size;
|
||
340 | } |
||
341 | |||
342 | /**
|
||
343 | * A partir de la escala y la resoluci?n calcula el tama?o en pixels y los metros por pixel
|
||
344 | * si la imagen est? en coordenadas geograficas.
|
||
345 | * @param scale Escala
|
||
346 | * @param resolution Resoluci?n
|
||
347 | */
|
||
348 | 4557 | nacho | private void calcSizeMtsPixelGeodesicas(int scaleIntro, int resolution){ |
349 | //TODO: Calculos para imagenes en coordenadas geograficas
|
||
350 | |||
351 | 4547 | nacho | /*double lrX = Double.parseDouble(controlPanel.getTInfDerX().getText());
|
352 | double ulX = Double.parseDouble(controlPanel.getTSupIzqX().getText());
|
||
353 | |||
354 | //Nos aseguramos de que la escala sea un entero. Si no lo es ponemos un 0
|
||
355 | try {
|
||
356 | Integer.parseInt(controlPanel.getTScale().getText());
|
||
357 | } catch (NumberFormatException e) {
|
||
358 | controlPanel.getTScale().setText("0");
|
||
359 | }
|
||
360 | |||
361 | double scale = controlPanel.getProjection().getScale(ulX, lrX,
|
||
362 | controlPanel.getWidthInPixelsGeodesicas(),
|
||
363 | Integer.parseInt((String)controlPanel.getCbResolution().getSelectedItem()));
|
||
364 | |||
365 | this.widthInPixels = (int)((scaleIntro * controlPanel.getWidthInPixelsGeodesicas()) / scale);
|
||
366 | this.heightInPixels = (int)((scaleIntro * controlPanel.getHeightInPixelsGeodesicas()) / scale);
|
||
367 | |||
368 | mtsPerPixel = MathUtils.tailDecimals(((double)(widthMts / this.widthInPixels)), 5);
|
||
369 | |||
370 | controlPanel.getTMtsPixel().setText(String.valueOf(mtsPerPixel));
|
||
371 | this.setCorrectMeasure(this.widthInPixels, controlPanel.getTWidth());
|
||
372 | this.setCorrectMeasure(this.heightInPixels, controlPanel.getTHeight());*/
|
||
373 | } |
||
374 | |||
375 | /**
|
||
376 | * A partir de la escala y la resoluci?n calcula el tama?o en pixels y los metros por pixel
|
||
377 | * @param scale Escala
|
||
378 | * @param resolution Resoluci?n
|
||
379 | */
|
||
380 | private void calcSizeMtsPixel(int scale, int resolution){ |
||
381 | 6271 | maquerol | try{
|
382 | if ((widthMts <= 0) || (heightMts <= 0) || |
||
383 | (Integer.parseInt(controlPanel.getTScale().getText()) == 0)){ |
||
384 | controlPanel.getTWidth().setText("");
|
||
385 | controlPanel.getTHeight().setText("");
|
||
386 | return;
|
||
387 | } |
||
388 | }catch(NumberFormatException exc){ |
||
389 | System.err.println("Fomato no num?rico"); |
||
390 | controlPanel.getTWidth().setText("");
|
||
391 | 4547 | nacho | controlPanel.getTHeight().setText("");
|
392 | return;
|
||
393 | 6271 | maquerol | } |
394 | 4547 | nacho | |
395 | //Calculo del tama?o de la imagen definitiva en pulgadas
|
||
396 | double widthInches = (widthMts / Double.parseDouble(controlPanel.getTScale().getText())) * MathUtils.INCHESMTR; |
||
397 | double heightInches = (heightMts / Double.parseDouble(controlPanel.getTScale().getText())) * MathUtils.INCHESMTR; |
||
398 | |||
399 | //Ancho en pixeles = ppp * sizepulgadas
|
||
400 | this.widthInPixels = (int) (Integer.parseInt(controlPanel.getCbResolution().getSelectedItem().toString()) * widthInches); |
||
401 | this.heightInPixels = (int) (Integer.parseInt(controlPanel.getCbResolution().getSelectedItem().toString()) * heightInches); |
||
402 | |||
403 | mtsPerPixel = (double)(widthMts / this.widthInPixels); |
||
404 | //double mtsPixelH = wc_altomts/altoPixels;
|
||
405 | |||
406 | //recortamos a 5 decimales
|
||
407 | mtsPerPixel = MathUtils.tailDecimals(mtsPerPixel, 5);
|
||
408 | |||
409 | controlPanel.getTMtsPixel().setText(String.valueOf(mtsPerPixel));
|
||
410 | this.setCorrectMeasure(this.widthInPixels, controlPanel.getTWidth()); |
||
411 | this.setCorrectMeasure(this.heightInPixels, controlPanel.getTHeight()); |
||
412 | |||
413 | //int anchopixels =(int) (Toolkit.getDefaultToolkit().getScreenResolution() * anchopulgadas);
|
||
414 | //int altopixels = (int) (Toolkit.getDefaultToolkit().getScreenResolution() * altopulgadas);
|
||
415 | } |
||
416 | |||
417 | /**
|
||
418 | * A partir de los metros por pixel y la resoluci?n calcula el tama?o en pixels y la escala
|
||
419 | * @param mtsPixel Metros por pixel
|
||
420 | * @param resolution Resoluci?n
|
||
421 | */
|
||
422 | private void calcSizeScale(double mtsPixel, int resolution){ |
||
423 | //N?mero de p?xeles de ancho y alto
|
||
424 | this.widthInPixels = (int)(widthMts / mtsPixel); |
||
425 | this.heightInPixels = (int)(heightMts / mtsPixel); |
||
426 | |||
427 | //Obtenemos los mts/pixel reales ya que el n?mero de pixeles es entero deben redondearse
|
||
428 | mtsPerPixel = (double)(widthMts / widthInPixels);
|
||
429 | |||
430 | //recortamos a 5 decimales
|
||
431 | mtsPerPixel = MathUtils.tailDecimals(mtsPerPixel, 5);
|
||
432 | |||
433 | //Obtenemos el ancho y alto en pulgadas
|
||
434 | double widthInches = (double)(widthInPixels / Integer.parseInt(controlPanel.getCbResolution().getSelectedItem().toString())); |
||
435 | double heightInches = (double)(heightInPixels / Integer.parseInt(controlPanel.getCbResolution().getSelectedItem().toString())); |
||
436 | |||
437 | //Calculo de la escala
|
||
438 | int scale = (int)((widthMts * MathUtils.INCHESMTR) / widthInches); |
||
439 | |||
440 | controlPanel.getTScale().setText(String.valueOf(scale));
|
||
441 | controlPanel.getTMtsPixel().setText(String.valueOf(mtsPerPixel));
|
||
442 | this.setCorrectMeasure(widthInPixels, controlPanel.getTWidth());
|
||
443 | this.setCorrectMeasure(heightInPixels, controlPanel.getTHeight());
|
||
444 | } |
||
445 | |||
446 | /**
|
||
447 | * A partir del tama?o en pixels de la imagen y la resoluci?n calcula los metros por pixel y la escala
|
||
448 | * @param widthPixels Ancho de la imagen en pixels
|
||
449 | * @param heightPixels Alto de la imagen en pixels
|
||
450 | * @param resolution Resoluci?n
|
||
451 | */
|
||
452 | private void calcScaleMtsPixel(int widthPixels, int heightPixels, int resolution){ |
||
453 | this.widthInPixels = widthPixels;
|
||
454 | this.heightInPixels = heightPixels;
|
||
455 | |||
456 | //Obtenemos los mts/pixel reales ya que el n?mero de pixeles es entero deben redondearse
|
||
457 | mtsPerPixel = (double)(widthMts / widthPixels);
|
||
458 | |||
459 | //recortamos a 5 decimales
|
||
460 | mtsPerPixel = MathUtils.tailDecimals(mtsPerPixel, 5);
|
||
461 | |||
462 | //Obtenemos el ancho y alto en pulgadas
|
||
463 | double widthInches = (double)(widthPixels / Integer.parseInt(controlPanel.getCbResolution().getSelectedItem().toString())); |
||
464 | double heightInches = (double)(heightPixels / Integer.parseInt(controlPanel.getCbResolution().getSelectedItem().toString())); |
||
465 | |||
466 | //Calculo de la escala
|
||
467 | int scale = (int)((widthMts * MathUtils.INCHESMTR) / widthInches); |
||
468 | |||
469 | controlPanel.getTScale().setText(String.valueOf(scale));
|
||
470 | controlPanel.getTMtsPixel().setText(String.valueOf(mtsPerPixel));
|
||
471 | this.setCorrectMeasure(widthPixels, controlPanel.getTWidth());
|
||
472 | this.setCorrectMeasure(heightPixels, controlPanel.getTHeight());
|
||
473 | } |
||
474 | |||
475 | /**
|
||
476 | * Controla cuando se cumplen todos los requisitos en el formulario para
|
||
477 | * poder activar el bot?n de aceptar.
|
||
478 | */
|
||
479 | private void enableButtons() { |
||
480 | if( controlPanel.getTWidth().getText().equals("0") || |
||
481 | controlPanel.getTHeight().getText().equals("0") ||
|
||
482 | controlPanel.getTWidth().getText().equals("") ||
|
||
483 | controlPanel.getTHeight().getText().equals("") ||
|
||
484 | this.fName == null || this.fName.equals("")){ |
||
485 | if (dialog != null) |
||
486 | dialog.getAcceptButton().setEnabled(false);
|
||
487 | return;
|
||
488 | } |
||
489 | |||
490 | if (dialog != null) |
||
491 | dialog.getAcceptButton().setEnabled(true);
|
||
492 | } |
||
493 | |||
494 | /**
|
||
495 | * Gestiona los eventos del cambio de escala de la imagen y lla apertura del
|
||
496 | * dialogo de selecci?n del nombre del fichero.
|
||
497 | */
|
||
498 | public void actionPerformed(ActionEvent e) { |
||
499 | obj = e.getSource(); |
||
500 | |||
501 | //Selector de Fichero sobre el cual se va a salvar a raster
|
||
502 | if (obj.equals(controlPanel.getBSelect())) {
|
||
503 | JFileChooser chooser = new JFileChooser("./"); |
||
504 | chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero")); |
||
505 | |||
506 | //A?adimos las extensiones que hayan sido registradas en el driver
|
||
507 | String[] extList = GeoRasterWriter.getDriversExtensions(); |
||
508 | for(int i=0;i<extList.length;i++) |
||
509 | chooser.addChoosableFileFilter(new WriterFilter(chooser, extList[i]));
|
||
510 | |||
511 | int returnVal = chooser.showOpenDialog(controlPanel);
|
||
512 | |||
513 | if (returnVal == JFileChooser.APPROVE_OPTION) { |
||
514 | this.fName = chooser.getSelectedFile().toString();
|
||
515 | String ext = ((WriterFilter)chooser.getFileFilter()).getDescription();
|
||
516 | |||
517 | FileFilter filter = chooser.getFileFilter();
|
||
518 | ext = ext.toLowerCase().substring(ext.lastIndexOf(".") + 1, ext.length()); |
||
519 | |||
520 | if ((fName != null) && !fName.equals("")) { |
||
521 | 11038 | jmvivo | if(!fName.endsWith("." + ext)) |
522 | 4547 | nacho | fName = fName + "." + ext;
|
523 | |||
524 | controlPanel.getBProperties().setText(PluginServices.getText(this, "props")+" "+GeoRasterWriter.getDriverType(ext)); |
||
525 | } |
||
526 | controlPanel.getBProperties().setEnabled(true);
|
||
527 | controlPanel.getLFileName().setText(fName.substring(fName.lastIndexOf(File.separator) + 1, fName.length())); |
||
528 | |||
529 | enableButtons(); |
||
530 | } |
||
531 | } |
||
532 | |||
533 | //Al variar las unidades recalculamos el ancho y el alto
|
||
534 | if (e.getSource().equals(controlPanel.getCbMeasureType())) {
|
||
535 | this.setCorrectMeasure(this.widthInPixels, controlPanel.getTWidth()); |
||
536 | this.setCorrectMeasure(this.heightInPixels, controlPanel.getTHeight()); |
||
537 | } |
||
538 | |||
539 | if (!obj.equals(controlPanel.getBSelect()))
|
||
540 | this.recalcParams();
|
||
541 | |||
542 | this.enableButtons();
|
||
543 | |||
544 | } |
||
545 | |||
546 | /**
|
||
547 | * Devuelve el nombre del fichero seleccionado
|
||
548 | * @return Nombre del fichero seleccionado
|
||
549 | */
|
||
550 | public String getFileName() { |
||
551 | return fName;
|
||
552 | } |
||
553 | |||
554 | /**
|
||
555 | * Unused
|
||
556 | */
|
||
557 | public void mouseExited(MouseEvent e) { |
||
558 | } |
||
559 | |||
560 | /**
|
||
561 | * Unused. Only for compilant purposes
|
||
562 | */
|
||
563 | public void mouseReleased(MouseEvent e) { |
||
564 | } |
||
565 | |||
566 | /**
|
||
567 | * Unused. Only for compilant purposes
|
||
568 | */
|
||
569 | public void mouseEntered(MouseEvent e) { |
||
570 | } |
||
571 | |||
572 | public void mouseClicked(MouseEvent e) { |
||
573 | if( (obj.equals(controlPanel.getTHeight()) && e.getSource().equals(controlPanel.getTWidth())) ||
|
||
574 | (obj.equals(controlPanel.getTWidth()) && e.getSource().equals(controlPanel.getTHeight())) ) |
||
575 | return;
|
||
576 | obj = e.getSource(); |
||
577 | this.recalcParams();
|
||
578 | enableButtons(); |
||
579 | } |
||
580 | |||
581 | /**
|
||
582 | * Unused. Only for compilant purposes
|
||
583 | */
|
||
584 | public void mousePressed(MouseEvent e) { |
||
585 | } |
||
586 | |||
587 | /**
|
||
588 | * Unused. Only for compilant purposes
|
||
589 | */
|
||
590 | public void focusGained(FocusEvent e) { |
||
591 | } |
||
592 | |||
593 | /**
|
||
594 | * Recalcula el tama?o de la imagen con los datos existentes y
|
||
595 | * activa o desactiva los botones.
|
||
596 | */
|
||
597 | public void focusLost(FocusEvent e) { |
||
598 | obj = e.getSource(); |
||
599 | this.recalcParams();
|
||
600 | enableButtons(); |
||
601 | } |
||
602 | |||
603 | /**
|
||
604 | * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
|
||
605 | */
|
||
606 | public void keyTyped(KeyEvent e) { |
||
607 | enableButtons(); |
||
608 | } |
||
609 | |||
610 | /**
|
||
611 | * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
|
||
612 | */
|
||
613 | public void keyPressed(KeyEvent e) { |
||
614 | enableButtons(); |
||
615 | } |
||
616 | |||
617 | /**
|
||
618 | * Activa o Desactiva los botones a trav?s de la funci?n que valida los campos
|
||
619 | */
|
||
620 | public void keyReleased(KeyEvent e) { |
||
621 | enableButtons(); |
||
622 | } |
||
623 | |||
624 | /**
|
||
625 | * Obtiene la altura en pixels de la imagen de salida
|
||
626 | * @return entero con la altura en pixels
|
||
627 | */
|
||
628 | public int getHeightInPixels() { |
||
629 | return heightInPixels;
|
||
630 | } |
||
631 | |||
632 | /**
|
||
633 | * Obtiene la anchura en pixels de la imagen de salida
|
||
634 | * @return entero con la anchura en pixels
|
||
635 | */
|
||
636 | public int getWidthInPixels() { |
||
637 | return widthInPixels;
|
||
638 | } |
||
639 | } |
||
640 | |||
641 | /**
|
||
642 | * @author Nacho Brodin <brodin_ign@gva.es>
|
||
643 | * Filtro para el selector de formatos de escritura
|
||
644 | */
|
||
645 | class WriterFilter extends javax.swing.filechooser.FileFilter { |
||
646 | private JFileChooser chooser = null; |
||
647 | private String filter; |
||
648 | |||
649 | public WriterFilter(JFileChooser ch, String fil) { |
||
650 | this.chooser = ch;
|
||
651 | this.filter = fil;
|
||
652 | } |
||
653 | |||
654 | public boolean accept(File f) { |
||
655 | return f.isDirectory() || f.getName().toLowerCase().endsWith("."+filter); |
||
656 | } |
||
657 | |||
658 | public String getDescription() { |
||
659 | return "."+filter; |
||
660 | } |
||
661 | } |