Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.app.jcrs / org.gvsig.projection.app.jcrs.common / src / main / java / org / gvsig / crs / gui / panels / wizard / DefinirDatum.java @ 285

History | View | Annotate | Download (43.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.crs.gui.panels.wizard;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.GridLayout;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.awt.event.FocusEvent;
51
import java.awt.event.FocusListener;
52
import java.awt.event.ItemEvent;
53
import java.awt.event.ItemListener;
54
import java.awt.event.KeyEvent;
55
import java.awt.event.KeyListener;
56
import java.sql.ResultSet;
57
import java.sql.SQLException;
58
import java.util.ArrayList;
59

    
60
import javax.swing.BorderFactory;
61
import javax.swing.ButtonGroup;
62
import javax.swing.JButton;
63
import javax.swing.JLabel;
64
import javax.swing.JList;
65
import javax.swing.JOptionPane;
66
import javax.swing.JPanel;
67
import javax.swing.JRadioButton;
68
import javax.swing.JTextField;
69
import javax.swing.plaf.basic.BasicComboBoxRenderer;
70
import javax.units.ConversionException;
71
import javax.units.Unit;
72

    
73
import org.geotools.referencing.crs.AbstractSingleCRS;
74
import org.geotools.referencing.datum.DefaultEllipsoid;
75
import org.geotools.referencing.datum.DefaultGeodeticDatum;
76
import org.geotools.referencing.datum.DefaultPrimeMeridian;
77
import org.gvsig.andami.PluginServices;
78
import org.gvsig.crs.CrsException;
79
import org.gvsig.crs.CrsFactory;
80
import org.gvsig.crs.ICrs;
81
import org.gvsig.crs.gui.dialog.ImportNewCrsDialog;
82
import org.gvsig.crs.gui.listeners.TransformationCompuestaPanelListener;
83
import org.gvsig.utils.swing.JComboBox;
84
import org.opengis.referencing.crs.CoordinateReferenceSystem;
85

    
86

    
87
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
89

    
90
import es.idr.teledeteccion.connection.EpsgConnection;
91
import es.idr.teledeteccion.connection.Query;
92

    
93
/**
94
 * Panel de definicin del Datum 
95
 * 
96
 * @author Luisa Marina Fernandez Ruiz (luisam.fernandez@uclm.es)
97
 * @author Jose Luis Gomez Martinez (joseluis.gomez@uclm.es)
98
 * 
99
 *
100
 */
101
public class DefinirDatum extends JPanel implements ActionListener, FocusListener, ItemListener, KeyListener{
102
        
103
        public static final Logger logger = LoggerFactory.getLogger(DefinirDatum.class);
104

    
105
        private JTextField txtDatum;
106
        private JTextField txtElipsoide;
107
        private JTextField txtSemMay;
108
        private JTextField txtSemMen;
109
        private JTextField txtInvF;
110
        private JTextField txtMeridian;
111
        private JTextField txtLong;
112
        private JTextField txtNombreCrs;
113
        private JTextField txtCodigoCrs;
114
        
115
        private JLabel lblDatum;
116
        private JLabel lblElipsoide;
117
        private JLabel lblMeridian;
118
        private JLabel lblLong;
119
        private JLabel lblNombreCrs;
120
        private JLabel lblCodigoCrs;
121
        private JLabel lblSemiejeMayor;
122
        private JLabel lblSemiejeMenor;
123
        private JLabel lblInverseFlat;
124
        private JLabel lblDefinir;
125
        
126
        
127
        private JButton btnImportDatum;
128
        private JButton btnImportElipsoide;
129
        private JButton btnImportMeridian;
130
        
131
        private JComboBox cbSemMay;
132
        private JComboBox cbSemMen;
133
        private JComboBox cbInvF;
134
        private JComboBox cbLong;
135
        
136
        private JRadioButton rbA_Inv;
137
        private JRadioButton rbA_B;
138
        private ButtonGroup groupRadioButton;
139
        
140
        private Dimension bigSize;
141
        private Dimension smallSize;
142
        private Dimension dimLabels;
143
//        private ImageIcon imodify;
144
        
145
        private static final long serialVersionUID = 1L;
146
        ICrs crs = null;
147
        
148
        private String sourceUnitLong = null;
149
        private String sourceUnitSemMay = null;
150
        private String sourceUnitSemMen = null;
151
        
152
        ArrayList lengthUnits = null;
153
        ArrayList angularUnits = null;
154
        int divider=10000;
155
        
156
        boolean primera = true;
157
        
158
        /**
159
     * Small tolerance factor for rounding errors.
160
     */
161
    private static final double EPS = 1E-8;
162

    
163
        public DefinirDatum() {
164
                
165
                super();
166
                dimLabels=new Dimension(110,15);
167
                this.setLayout(new BorderLayout());
168
                this.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
169
                bigSize=new Dimension();
170
                bigSize.width=350;
171
                smallSize=new Dimension();
172
                smallSize.width=130;
173
                inicializarNuevo();
174
                
175
        }
176
        private void inicializarNuevo(){
177
                JPanel pMain=new JPanel();
178
                JPanel pDatum=new JPanel();
179
                JPanel pElipsoide=new JPanel();
180
                JPanel pMeridian=new JPanel();
181
                JPanel pNorthis=new JPanel(new FlowLayout(FlowLayout.LEFT,10,5));
182
                pMain.setLayout(new BorderLayout());
183
                
184
                pMain.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this,"Dat_nDatum")));
185
                pElipsoide.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this,"Dat_nElipsoide")));
186
                pMeridian.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this,"Dat_nMeridian")));
187
                
188
                /*Panel que contiene los datos del Datum*/
189
                pDatum.setLayout(new GridLayout(2,1));
190
                JPanel pD1=new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
191
                pD1.add(getLblDatum());
192
                JPanel pD2=new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
193
                pD2.add(getTxtDatum());
194
                pD2.add(getBtnImportDatum());
195
                pDatum.add(pD1);
196
                pDatum.add(pD2);
197
                
198
                /*Panel que contiene los datos del Elipsoide*/
199
                pElipsoide.setLayout(new GridLayout(6,1));
200
                //Crear y agregar los componentes a los subpaneles que componen el panel de definicin del elipsoide
201
                JPanel pE1=new JPanel(new FlowLayout(FlowLayout.LEFT,10,2));
202
                pE1.add(getLblElipsoide());
203
                JPanel pE2=new JPanel(new FlowLayout(FlowLayout.LEFT,10,2));
204
                pE2.add(getTxtElipsoide());
205
                pE2.add(getBtnImportElipsoide());
206
                JPanel pE3=new JPanel(new FlowLayout(FlowLayout.LEFT,10,2));
207
                pE3.add(getLblDefinir());
208
                pE3.add(getRbA_Inv());
209
                pE3.add(getRbA_B());
210
                JPanel pE4=new JPanel(new FlowLayout(FlowLayout.LEFT,10,2));
211
                //Semieje mayor
212
                pE4.add(getLblSemiejeMayor());
213
                pE4.add(getTxtSemMay());
214
                pE4.add(getCbSemMay());
215
                JPanel pE5=new JPanel(new FlowLayout(FlowLayout.LEFT,10,2));
216
                pE5.add(getLblInverseFlat());
217
                pE5.add(getTxtInvF());
218
                JPanel pE6=new JPanel(new FlowLayout(FlowLayout.LEFT,10,2));
219
                //Semieje menor
220
                pE6.add(getLblSemiejeMenor());
221
                pE6.add(getTxtSemMen());
222
                pE6.add(getCbSemMen());
223
                
224
                //Agregar los subpaneles al panel del Elipsoide
225
                pElipsoide.add(pE1);
226
                pElipsoide.add(pE2);
227
                pElipsoide.add(pE3);
228
                pElipsoide.add(pE4);
229
                pElipsoide.add(pE5);
230
                pElipsoide.add(pE6);
231
                //Agrupar los Radio Buttoms
232
                agruparRadioButtons();
233
                
234
                /*Panel que contiene los datos del Meridiano*/
235
                pMeridian.setLayout(new GridLayout(2,1));
236
                JPanel pM1=new JPanel(new FlowLayout(FlowLayout.LEFT,10,3));
237
                pM1.add(getLblMeridian());
238
                pM1.add(getTxtMeridian());
239
                pM1.add(getBtnImportMeridian());
240
                JPanel pM2=new JPanel(new FlowLayout(FlowLayout.LEFT,10,3));
241
                pM2.add(getLblLong());
242
                pM2.add(getTxtLong());
243
                pM2.add(getCbLong());
244
                pMeridian.add(pM1);
245
                pMeridian.add(pM2);
246
                pMain.add(pDatum,BorderLayout.NORTH);
247
                pMain.add(pElipsoide,BorderLayout.CENTER);
248
                pMain.add(pMeridian,BorderLayout.SOUTH);
249
                this.add(pMain,BorderLayout.CENTER);
250
                //Agregar en norte-this el nombre y el cdigo
251
                pNorthis.add(getLblNombreCrs());
252
                pNorthis.add(getTxtNombreCrs());
253
                pNorthis.add(getLblCodigoCrs());
254
                pNorthis.add(getTxtCodigoCrs());
255
                this.add(pNorthis,BorderLayout.NORTH);
256
                
257
        }
258
        /**
259
         * Inicializa el botn Importar del datum
260
         * @return
261
         */
262
        public JButton getBtnImportDatum() {
263
                if(btnImportDatum==null){
264
                        btnImportDatum=new JButton();
265
                        btnImportDatum.setText("...");
266
                        //btnImportDatum.setIcon(imodify);
267
                        btnImportDatum.addActionListener(this);
268
                }
269
                return btnImportDatum;
270
        }
271
        /**
272
         * Inicializa el botn Importar del Elipsoide
273
         * @return
274
         */
275
        public JButton getBtnImportElipsoide() {
276
                if(btnImportElipsoide==null){
277
                        btnImportElipsoide=new JButton();
278
                        btnImportElipsoide.setText("...");
279
                        //btnImportElipsoide.setIcon(imodify);
280
                        btnImportElipsoide.addActionListener(this);
281
                }
282
                return btnImportElipsoide;
283
        }
284
        /**
285
         * Inicializa el botn Importar del Meridiano
286
         * @return
287
         */
288
        public JButton getBtnImportMeridian() {
289
                if(btnImportMeridian==null){
290
                        btnImportMeridian=new JButton();
291
                        btnImportMeridian.setText("...");
292
                        //btnImportMeridian.setIcon(imodify);
293
                        btnImportMeridian.addActionListener(this);
294
                }
295
                return btnImportMeridian;
296
        }
297
        /**
298
         * Inicializa el Combo box con las unidades de Inverse Flat
299
         * @return
300
         */
301
        public JComboBox getCbInvF() {
302
                if (cbInvF==null){
303
                        ArrayList units = obtenerItemsUnidades();
304
                        String[] items = new String[units.size()];
305
                        for (int i=0;i<units.size();i++){
306
                                items[i] = units.get(i).toString();
307
                        }
308
                        cbInvF=new JComboBox(items);
309
                        /*Seleccionar un item por defecto*/
310
                        cbInvF.setSelectedIndex(0);
311
                        cbInvF.addItemListener(this);
312
                }
313
                return cbInvF;
314
        }
315
        /**
316
         * Inicializa el Combo box con las unidades de Longitud
317
         * @return
318
         */
319
        public JComboBox getCbLong() {
320
                if (cbLong==null){
321
                        ArrayList units = obtenerItemsUnidadesAngle();
322
                        String[] items = new String[units.size()];
323
                        String[] tooltips = new String[units.size()];
324
                        for (int i=0;i<units.size();i++){
325
                                items[i] = tooltips[i] = units.get(i).toString();
326
                        }
327
                        cbLong=new JComboBox(items);
328
                        cbLong.setRenderer(new ComboBoxTootipRenderer(tooltips));
329
                        cbLong.setPreferredSize(new Dimension(230, 25));
330
                        /*Seleccionar un item por defecto*/
331
                        cbLong.setSelectedIndex(1);
332
                        cbLong.addItemListener(this);
333
                        sourceUnitLong = getAngularUnit(cbLong.getSelectedIndex());
334
                }
335
                return cbLong;
336
        }
337
        /**
338
         * Inicializa el Combo box con las unidades de Semieje Mayor
339
         * @return
340
         */
341
        public JComboBox getCbSemMay() {
342
                if (cbSemMay==null){
343
                        ArrayList units = obtenerItemsUnidades();
344
                        String[] items = new String[units.size()];
345
                        String[] tooltips = new String[units.size()];
346
                        for (int i=0;i<units.size();i++){
347
                                items[i] = tooltips[i] = units.get(i).toString();
348
                        }
349
                        cbSemMay=new JComboBox(items);
350
                        cbSemMay.setRenderer(new ComboBoxTootipRenderer(tooltips));
351
                        cbSemMay.setPreferredSize(new Dimension(230, 25));
352
                        /*Seleccionar un item por defecto*/
353
                        cbSemMay.setSelectedIndex(0);
354
                        cbSemMay.addItemListener(this);
355
                        sourceUnitSemMay = getLengthUnit(cbSemMay.getSelectedIndex());
356
                }
357
                return cbSemMay;
358
        }
359
        /**
360
         * Inicializa el Combo box con las unidades de Semieje Menor
361
         * @return
362
         */
363
        public JComboBox getCbSemMen() {
364
                if (cbSemMen==null){
365
                        ArrayList units = obtenerItemsUnidades();
366
                        String[] items = new String[units.size()];
367
                        for (int i=0;i<units.size();i++){
368
                                items[i] = units.get(i).toString();
369
                        }
370
                        cbSemMen=new JComboBox(items);
371
                        /*Seleccionar un item por defecto*/
372
                        cbSemMen.setSelectedIndex(0);
373
                        cbSemMen.addItemListener(this);
374
                        sourceUnitSemMen = getLengthUnit(cbSemMen.getSelectedIndex());
375
                }
376
                return cbSemMen;
377
        }
378
        /*
379
         * Accede al la base de datos y obtiene los items de
380
         * los combobox
381
         * @return
382
         */
383
        private ArrayList obtenerItemsUnidades(){ //unidades de longitud...
384
                //TODO: Obtener los items del combo de la base de datos
385
                ArrayList items = new ArrayList();
386
                ArrayList lengthUnits = new ArrayList();
387
                                
388
                String sentence = "SELECT unit_of_meas_name " +
389
                                                  "FROM epsg_unitofmeasure " +        
390
                                                  "WHERE unit_of_meas_type = 'length' " +
391
                                                  "AND uom_code > 9000";
392
                EpsgConnection connect = new EpsgConnection();
393
                connect.setConnectionEPSG();
394
                ResultSet result = Query.select(sentence,connect.getConnection());
395
                int in=0;
396
                try {
397
                        while (result.next()) {
398
                                String item = result.getString("unit_of_meas_name");
399
                                items.add(in,PluginServices.getText(this, item.replaceAll(" ", "_")));
400
                                lengthUnits.add(in, item);
401
                                in++;
402
                        }
403
                } catch (SQLException e) {
404
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
405
                }
406
                setLengthUnit(lengthUnits);
407
                return items;
408
        }
409
        
410
        /*
411
         * Accede al la base de datos y obtiene los items de
412
         * los combobox
413
         * @return
414
         */
415
        private ArrayList obtenerItemsUnidadesAngle(){ //unidades angulares...
416
                //TODO: Obtener los items del combo de la base de datos
417
                ArrayList items = new ArrayList();
418
                ArrayList angularUnit = new ArrayList();
419
                                
420
                String sentence = "SELECT unit_of_meas_name " +
421
                                                  "FROM epsg_unitofmeasure " +                                      
422
                                                  "WHERE unit_of_meas_type = 'angle' " +
423
                                                  "AND factor_b IS NOT NULL " +
424
                                                  "AND factor_c IS NOT NULL " +
425
                                                  "AND uom_code > 9100";                
426
                EpsgConnection connect = new EpsgConnection();
427
                connect.setConnectionEPSG();
428
                ResultSet result = Query.select(sentence,connect.getConnection());
429
                int in=0;
430
                try {
431
                        while (result.next()) {
432
                                String item = result.getString("unit_of_meas_name");
433
                                items.add(in, PluginServices.getText(this, item.replaceAll(" ", "_")));
434
                                angularUnit.add(in, item);
435
                                in++;
436
                        }
437
                } catch (SQLException e) {
438
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
439
                }
440
                setAngularUnit(angularUnit);
441
                return items;
442
        }
443
        /**
444
         * Inicializa el JLabel Datum
445
         * @return
446
         */
447
        public JLabel getLblDatum() {
448
                if (lblDatum==null){
449
                        lblDatum=new JLabel();
450
                        lblDatum.setText(PluginServices.getText(this,"Dat_Datum"));
451
                }
452
                return lblDatum;
453
        }
454
        /**
455
         * Inicializa el JLabel Elipsoide
456
         * @return
457
         */
458
        public JLabel getLblElipsoide() {
459
                if (lblElipsoide==null){
460
                        lblElipsoide=new JLabel();
461
                        lblElipsoide.setText(PluginServices.getText(this,"Dat_Elips"));
462
                        //lblElipsoide.setText("Introduce el Elipsoide");
463
                }
464
                return lblElipsoide;
465
        }
466

    
467
        /**
468
         * Inicializa el JLabel Longitud
469
         * @return
470
         */
471
        public JLabel getLblLong() {
472
                if (lblLong==null){
473
                        lblLong=new JLabel();
474
                        lblLong.setText(PluginServices.getText(this,"Dat_Long"));
475
                        lblLong.setMinimumSize(dimLabels);
476
                        lblLong.setPreferredSize(dimLabels);
477
                }
478
                return lblLong;
479
        }
480
        /**
481
         * Inicializa el JLabel Meridiano
482
         * @return
483
         */
484
        public JLabel getLblMeridian() {
485
                if (lblMeridian==null){
486
                        lblMeridian=new JLabel();
487
                        lblMeridian.setText(PluginServices.getText(this,"Dat_Meridian"));
488
                        lblMeridian.setMinimumSize(dimLabels);
489
                        lblMeridian.setPreferredSize(dimLabels);
490
                }
491
                return lblMeridian;
492
        }
493
        /**
494
         * Inicializa el JLabel del Cdigo de Crs
495
         * @return
496
         */
497
        public JLabel getLblCodigoCrs() {
498
                if (lblCodigoCrs==null){
499
                        lblCodigoCrs=new JLabel(PluginServices.getText(this,"Dat_CodeCrs")+":");
500
                }
501
                return lblCodigoCrs;
502
        }
503
        /**
504
         * Inicializa el JLabel del Nombre del Crs
505
         * @return
506
         */
507
        public JLabel getLblNombreCrs() {
508
                if (lblNombreCrs==null){
509
                        lblNombreCrs=new JLabel(PluginServices.getText(this,"Dat_NombreCrs")+":");
510
                }
511
                return lblNombreCrs;
512
        }
513
        public JLabel getLblInverseFlat() {
514
                if(lblInverseFlat==null){
515
                        lblInverseFlat=new JLabel();
516
                        lblInverseFlat.setText(PluginServices.getText(this,"Dat_InvF")+(" (inv_f)"));
517
                        lblInverseFlat.setMinimumSize(dimLabels);
518
                        lblInverseFlat.setPreferredSize(dimLabels);
519
                }        
520
                return lblInverseFlat;
521
        }
522
        public JLabel getLblSemiejeMayor() {
523
                if(lblSemiejeMayor==null){
524
                        lblSemiejeMayor=new JLabel();
525
                        lblSemiejeMayor.setText(PluginServices.getText(this,"Dat_SemMay")+(" (a)"));
526
                        lblSemiejeMayor.setMinimumSize(dimLabels);
527
                        lblSemiejeMayor.setPreferredSize(dimLabels);
528
                        
529
                }        
530
                return lblSemiejeMayor;
531
        }
532
        public JLabel getLblSemiejeMenor() {
533
                if(lblSemiejeMenor==null){
534
                        lblSemiejeMenor=new JLabel();
535
                        lblSemiejeMenor.setText(PluginServices.getText(this,"Dat_SemMen")+(" (b)"));
536
                        lblSemiejeMenor.setMinimumSize(dimLabels);
537
                        lblSemiejeMenor.setPreferredSize(dimLabels);
538
                }        
539
                return lblSemiejeMenor;
540
        }
541
        public JLabel getLblDefinir() {
542
                if(lblDefinir==null){
543
                        lblDefinir=new JLabel();
544
                        lblDefinir.setText(PluginServices.getText(this,"Dat_DefinirPor"));
545
                        Dimension d=new Dimension(200,15);
546
                        lblDefinir.setMinimumSize(d);
547
                        lblDefinir.setPreferredSize(d);
548
                }
549
                return lblDefinir;
550
        }
551
        
552
        
553
        public JRadioButton getRbA_B() {
554
                if(rbA_B==null){
555
                        rbA_B=new JRadioButton();
556
                        rbA_B.setText("a, b");
557
                        rbA_B.addActionListener(this);
558
                }
559
                return rbA_B;
560
        }
561
        public JRadioButton getRbA_Inv() {
562
                if(rbA_Inv==null){
563
                        rbA_Inv=new JRadioButton();
564
                        rbA_Inv.setText("a, inv_f");
565
                        Dimension d=new Dimension(100,15);
566
                        rbA_Inv.setMinimumSize(d);
567
                        rbA_Inv.setPreferredSize(d);
568
                        rbA_Inv.addActionListener(this);
569
                }
570
                return rbA_Inv;
571
        }
572
        
573
        /*
574
        public JRadioButton getRbInvF() {
575
                if(rbInvF==null){
576
                        rbInvF=new JRadioButton();
577
                        rbInvF.setToolTipText(PluginServices.getText(this,"Dat_InvFToolTipText"));
578
                        rbInvF.setText(PluginServices.getText(this,"Dat_InvF"));
579
                        rbInvF.addActionListener(this);
580
                }
581
                return rbInvF;
582
        }
583
        */
584
        /**
585
         * Inicializa el radio button Semieje Mayor del elipsoide
586
         * @return
587
         */
588
        /*
589
        public JRadioButton getRbSemMay() {
590
                if(rbSemMay==null){
591
                        rbSemMay=new JRadioButton();
592
                        rbSemMay.setToolTipText(PluginServices.getText(this,"Dat_SemMayToolTipText"));
593
                        rbSemMay.setText(PluginServices.getText(this,"Dat_SemMay"));
594
                        rbSemMay.addActionListener(this);
595
                }
596
                return rbSemMay;
597
        }*/
598

    
599
        /**
600
         * Inicializa el radio Button del Semieje Menor del elipsoide
601
         * @return
602
         */
603
        /*
604
        public JRadioButton getRbSemMen() {
605
                if(rbSemMen==null){
606
                        rbSemMen=new JRadioButton();
607
                        rbSemMen.setToolTipText(PluginServices.getText(this,"Dat_SemMenToolTipText"));                        
608
                        rbSemMen.setText(PluginServices.getText(this,"Dat_SemMen"));
609
                        rbSemMen.addActionListener(this);
610
                }
611
                return rbSemMen;
612
        }
613
        */
614

    
615
        /**
616
         * Agrupa los Radio Button de los parmetros del elipsoide
617
         * 
618
         */
619

    
620
        private void agruparRadioButtons() {
621
                if(groupRadioButton==null){
622
                        groupRadioButton=new ButtonGroup();
623
                        //Agrupar los radio Buttons
624
                        groupRadioButton.add(getRbA_B());
625
                        groupRadioButton.add(getRbA_Inv());
626
                        getRbA_Inv().setSelected(true);
627
                        getTxtSemMen().setEditable(false);
628
                        getCbSemMen().setEnabled(false);
629
                        
630
                }
631
        }
632

    
633
        /**
634
         * Inicializa el cuadro de texto que contiene el datum
635
         * @return
636
         */
637
        public JTextField getTxtDatum() {
638
                if (txtDatum==null){
639
                        txtDatum=new JTextField();
640
                        bigSize.height=txtDatum.getPreferredSize().height;
641
                        txtDatum.setPreferredSize(bigSize);
642
                        txtDatum.addActionListener(this);
643
                }
644
                return txtDatum;
645
        }
646
        /**
647
         * Inicializa el cuadro de texto que contiene el elipsoide
648
         * @return
649
         */
650
        public JTextField getTxtElipsoide() {
651
                if (txtElipsoide==null){
652
                        txtElipsoide=new JTextField();
653
                        bigSize.height=txtElipsoide.getPreferredSize().height;
654
                        txtElipsoide.setPreferredSize(bigSize);
655
                        txtElipsoide.addActionListener(this);
656
                }
657
                return txtElipsoide;
658
        }
659
        /**
660
         * Inicializa el cuadro de texto que contiene el Inverse Flat del Elipsoide
661
         * @return
662
         */
663
        public JTextField getTxtInvF() {
664
                if (txtInvF==null){
665
                        txtInvF=new JTextField();
666
                        smallSize.height=txtInvF.getPreferredSize().height;
667
                        txtInvF.setPreferredSize(smallSize);
668
                        txtInvF.setMinimumSize(smallSize);
669
                        txtInvF.addActionListener(this);
670
                        txtInvF.addKeyListener(this);
671
                        txtInvF.addFocusListener(this);
672
                }
673
                return txtInvF;
674
        }
675
        /**
676
         * Inicializa el cuadro de texto que contiene el dato de longitud del
677
         * meridiano
678
         * @return
679
         */
680
        public JTextField getTxtLong() {
681
                if (txtLong==null){
682
                        txtLong=new JTextField();
683
                        smallSize.height=txtLong.getPreferredSize().height;
684
                        txtLong.setPreferredSize(smallSize);
685
                        txtLong.addActionListener(this);
686
                        txtLong.addKeyListener(this);
687
                        txtLong.addFocusListener(this);
688
                }
689
                return txtLong;
690
        }
691
        /**
692
         * Inicializa el cuadro de texto que contiene el meridiano
693
         * @return
694
         */
695
        public JTextField getTxtMeridian() {
696
                if (txtMeridian==null){
697
                        txtMeridian=new JTextField();
698
                        bigSize.height=txtMeridian.getPreferredSize().height;
699
                        Dimension d=new Dimension(280,15);
700
                        d.height=txtMeridian.getPreferredSize().height;
701
                        txtMeridian.setPreferredSize(d);
702
                        txtMeridian.addActionListener(this);
703
                }
704
                return txtMeridian;
705
        }
706
        /**
707
         * Inicializa el cuadro de texto que contiene el Semieje Mayor
708
         * del elipsoide
709
         * @return
710
         */
711
        public JTextField getTxtSemMay() {
712
                if (txtSemMay==null){
713
                        txtSemMay=new JTextField();
714
                        smallSize.height=txtSemMay.getPreferredSize().height;
715
                        txtSemMay.setPreferredSize(smallSize);
716
                        txtSemMay.setMinimumSize(smallSize);
717
                        txtSemMay.addActionListener(this);
718
                        txtSemMay.addKeyListener(this);
719
                        txtSemMay.addFocusListener(this);
720
                }
721
                return txtSemMay;
722
        }
723
        /**
724
         * Inicializa el cuadro de texto que contiene el semieje
725
         * menor del elipsoide
726
         * @return
727
         */
728
        public JTextField getTxtSemMen() {
729
                if (txtSemMen==null){
730
                        txtSemMen=new JTextField();
731
                        smallSize.height=txtSemMen.getPreferredSize().height;
732
                        txtSemMen.setPreferredSize(smallSize);
733
                        txtSemMen.setMinimumSize(smallSize);
734
                        txtSemMen.addActionListener(this);
735
                        txtSemMen.addKeyListener(this);
736
                        txtSemMen.addFocusListener(this);
737
                }
738
                return txtSemMen;
739
        }
740
        /**
741
         * Inicializa el cuadro de texto que contiene el cdigo del Crs definido por el usuario
742
         * @return
743
         */
744
        public JTextField getTxtCodigoCrs() {
745
                if (txtCodigoCrs==null){
746
                        txtCodigoCrs=new JTextField();
747
                        smallSize.height=txtCodigoCrs.getPreferredSize().height;
748
                        txtCodigoCrs.setPreferredSize(smallSize);
749
                        txtCodigoCrs.addActionListener(this);
750
                        txtCodigoCrs.addFocusListener(this);
751
                }
752
                return txtCodigoCrs;
753
        }
754
        /**
755
         * Inicializa el cuadro de texto que contiene el nombre del Crs
756
         * @return
757
         */
758
        public JTextField getTxtNombreCrs() {
759
                if (txtNombreCrs==null){
760
                        txtNombreCrs=new JTextField();
761
                        smallSize.height=txtNombreCrs.getPreferredSize().height;
762
                        txtNombreCrs.setPreferredSize(smallSize);
763
                        txtNombreCrs.addActionListener(this);                        
764
                }
765
                return txtNombreCrs;
766
        }
767
        /*
768
         * Manejador de eventos de los controles
769
         */
770
        public void actionPerformed(ActionEvent e) {
771
        
772
                if(e.getSource().equals(getTxtDatum())){
773
                        System.out.println("txt Datum");
774
                }else if(e.getSource().equals(getTxtElipsoide())){
775
                        System.out.println("txt Elipsoide");
776
                }else if(e.getSource().equals(getTxtInvF())){
777
                        System.out.println("txt Inverse Flat");
778
                }else if(e.getSource().equals(getTxtSemMay())){
779
                        System.out.println("txt Semieje Mayor");
780
                }else if(e.getSource().equals(getTxtSemMen())){
781
                        System.out.println("txt Semieje Menor");
782
                }else if(e.getSource().equals(getTxtLong())){
783
                        System.out.println("txt Longitud");
784
                }else if(e.getSource().equals(getTxtMeridian())){
785
                        System.out.println("txt Meridiano");
786
                }else if(e.getSource().equals(getCbInvF())){
787
                        System.out.println("combo box Inverse Flat");
788
                }else if(e.getSource().equals(getCbLong())){
789
                        System.out.println("combo box Longitud");
790
                }else if(e.getSource().equals(getCbSemMay())){
791
                        System.out.println("combo box Semieje Mayor");
792
                }else if(e.getSource().equals(getCbSemMen())){
793
                        System.out.println("combo box Semieje Menor");
794
                }else if(e.getSource().equals(getRbA_B())){
795
                        getTxtSemMay().setEditable(true);
796
                        getTxtSemMen().setEditable(true);
797
                        getTxtInvF().setEditable(false);
798
                        getCbSemMen().setEnabled(true);
799
                }else if(e.getSource().equals(getRbA_Inv())){
800
                        getTxtSemMay().setEditable(true);
801
                        getTxtSemMen().setEditable(false);
802
                        getTxtInvF().setEditable(true);        
803
                        getCbSemMen().setEnabled(false);
804
                }else if(e.getSource().equals(getBtnImportDatum())){
805
                        System.out.println("Importar Datum");
806
                        ImportNewCrsDialog newCrs = new ImportNewCrsDialog(PluginServices.getText(this, "datum"));
807
                        PluginServices.getMDIManager().addWindow(newCrs);
808
                        if (newCrs.getCode() != -1) {
809
                                setCrs(newCrs.getCode());
810
                                fillDatum(getCrs());
811
                        }
812
                }else if(e.getSource().equals(getBtnImportElipsoide())){
813
                        System.out.println("Importar Elipsoide");
814
                        ImportNewCrsDialog newCrs = new ImportNewCrsDialog(PluginServices.getText(this, "ellips"));
815
                        PluginServices.getMDIManager().addWindow(newCrs);
816
                        if (newCrs.getCode() != -1) {
817
                                setCrs(newCrs.getCode());
818
                                fillEllipsoid(getCrs());
819
                        }
820
                }else if(e.getSource().equals(getBtnImportMeridian())){
821
                        System.out.println("Importar Meridiano");
822
                        ImportNewCrsDialog newCrs = new ImportNewCrsDialog(PluginServices.getText(this, "primem"));
823
                        PluginServices.getMDIManager().addWindow(newCrs);
824
                        if (newCrs.getCode() != -1) {
825
                                setCrs(newCrs.getCode());
826
                                fillPrimeMeridian(getCrs());
827
                        }else if(e.getSource().equals(getRbA_Inv())){
828
                        
829
                        }else if(e.getSource().equals(getRbA_B())){
830
                        
831
                        }
832
                }
833
        }
834
        
835
        public void fillData(ICrs crs){
836
                getTxtNombreCrs().setText(crs.getCrsWkt().getGeogcs());
837
                getTxtCodigoCrs().setText(getMaxCode());
838
                fillDatum(crs);
839
        }
840
        
841
        public void fillData(ICrs crs, boolean editing){
842
                getTxtNombreCrs().setText(crs.getCrsWkt().getGeogcs());
843
                getTxtCodigoCrs().setText(""+crs.getCode());
844
                fillDatum(crs);
845
        }
846
        
847
        public void fillDatum(ICrs crs) {
848
                getTxtDatum().setText(crs.getCrsWkt().getDatumName());
849
                fillEllipsoid(crs);
850
                fillPrimeMeridian(crs);
851
        }
852
        
853
        public void fillData(CoordinateReferenceSystem crsGT) {
854
                AbstractSingleCRS crs = (AbstractSingleCRS)crsGT;
855
                String authority = crs.getName().toString().split(":")[0];
856
                //String code = crs.getName().toString().split(":")[1];
857
                getTxtNombreCrs().setText(authority);
858
                getTxtCodigoCrs().setText(getMaxCode());
859
                DefaultGeodeticDatum d = (DefaultGeodeticDatum) crs.getDatum();
860
                String[] val = d.getName().toString().split(":");
861
                if (val.length<2)
862
                        getTxtDatum().setText(d.getName().toString().split(":")[0]);
863
                else
864
                        getTxtDatum().setText(d.getName().toString().split(":")[1]);
865
                DefaultEllipsoid ellips = (DefaultEllipsoid)d.getEllipsoid();
866
                
867
                Unit u = ellips.getAxisUnit();
868
                double semi_major = convert(ellips.getSemiMajorAxis(), u.toString());
869
                double inv_f = ellips.getInverseFlattening();
870
                val = ellips.getName().toString().split(":");
871
                if (val.length<2)
872
                        getTxtElipsoide().setText(val[0]);
873
                else
874
                        getTxtElipsoide().setText(val[1]);
875
                getTxtSemMay().setText(String.valueOf(semi_major));
876
                getTxtInvF().setText(String.valueOf(inv_f));
877
                getTxtSemMen().setText(String.valueOf(calcularSemMen()));
878
                
879
                DefaultPrimeMeridian primem = (DefaultPrimeMeridian)d.getPrimeMeridian();
880
                
881
                u = primem.getAngularUnit();
882
                double longitude = convert(primem.getGreenwichLongitude(), u.toString());
883
                val = primem.getName().toString().split(":");
884
                if (val.length<2)
885
                        getTxtMeridian().setText(val[0]);
886
                else
887
                        getTxtMeridian().setText(val[1]);
888
                
889
                getTxtLong().setText(""+longitude);                
890
        }
891
        
892
        public void fillEllipsoid(ICrs crs) {
893
                getTxtElipsoide().setText(crs.getCrsWkt().getSpheroid()[0]);
894
                String semMay = crs.getCrsWkt().getSpheroid()[1];
895
                String invF = crs.getCrsWkt().getSpheroid()[2];
896
                getTxtSemMay().setText(semMay);
897
                getTxtInvF().setText(invF);
898
                double semMen = calcularSemMen();
899
                getTxtSemMen().setText(String.valueOf(semMen));                
900
        }
901
        
902
        public void fillPrimeMeridian(ICrs crs) {
903
                getTxtMeridian().setText(crs.getCrsWkt().getPrimen()[0]);
904
                getTxtLong().setText(crs.getCrsWkt().getPrimen()[1]);
905
        }
906
        
907
        
908
        private double calcularSemMen() {
909
                String cadenaNumerica = getTxtSemMay().getText().replaceAll("[^0-9.E]", "");
910
                                
911
                if (getTxtSemMay().getText().length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
912
                        JOptionPane.showMessageDialog(DefinirDatum.this, 
913
                                        PluginServices.getText(this,"numeric_format")+": "+PluginServices.getText(this,"semMay"), 
914
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
915
                        return 0;
916
                }
917
                else {
918
                        if (getTxtInvF().getText().equals("Infinity")) {
919
                                getRbA_B().setSelected(true);
920
                                getTxtSemMay().setEditable(true);
921
                                getTxtSemMen().setEditable(true);
922
                                getTxtInvF().setEditable(false);
923
                                getCbSemMen().setEnabled(true);
924
                                return Double.parseDouble((String)getTxtSemMay().getText());
925
                        }
926
                        cadenaNumerica = getTxtInvF().getText().replaceAll("[^0-9.E-]", "");
927
                        
928
                        if (getTxtInvF().getText().length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
929
                
930
                        JOptionPane.showMessageDialog(DefinirDatum.this, 
931
                                        PluginServices.getText(this,"numeric_format")+": "+PluginServices.getText(this,"inv_f"), 
932
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
933
                        return 0;
934
                        }
935
                }
936
                
937
                double semMay = Double.parseDouble((String)getTxtSemMay().getText());
938
                double invF = Double.parseDouble((String)getTxtInvF().getText());
939
                
940
                if (invF != 0)
941
                        return semMay - (semMay / (invF));
942
                return Double.NEGATIVE_INFINITY;
943
        }
944
        
945
        private double calcularInvF() {
946
                String cadenaNumerica = getTxtSemMay().getText().replaceAll("[^0-9.E]", "");
947
                
948
                if (getTxtSemMay().getText().length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
949
                        JOptionPane.showMessageDialog(DefinirDatum.this, 
950
                                        PluginServices.getText(this,"numeric_format")+": "+PluginServices.getText(this,"semMay"), 
951
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
952
                        return 0;
953
                }
954
                else {
955
                        cadenaNumerica = getTxtSemMen().getText().replaceAll("[^0-9.E-]", "");
956
                        
957
                        if (getTxtSemMen().getText().length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
958
                
959
                        JOptionPane.showMessageDialog(DefinirDatum.this, 
960
                                        PluginServices.getText(this,"numeric_format")+": "+PluginServices.getText(this,"semMen"), 
961
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
962
                        return 0;
963
                        }
964
                }
965
                
966
                double semMay = Double.parseDouble((String)getTxtSemMay().getText());
967
                double semMen = Double.parseDouble((String)getTxtSemMen().getText());
968
                
969
                
970
                if ((semMay - semMen) != 0)
971
                        return (semMay ) / (semMay - semMen);
972
                return Double.POSITIVE_INFINITY;
973
        }
974
        
975
        public void setCrs(int code) {
976
                try {
977
                        crs = new CrsFactory().getCRS("EPSG:"+code);
978
                } catch (CrsException e) {
979
                        logger.error("Can't create EPSG CRS from code "+code+".",e);
980
                }
981
                
982
        }
983
        
984
        public ICrs getCrs() {
985
                return crs;
986
        }
987
        
988
        public void cleanData() {
989
                String max = getMaxCode();
990
                getTxtCodigoCrs().setText(max);
991
                getTxtNombreCrs().setText("");
992
                getTxtDatum().setText("");
993
                getTxtElipsoide().setText("GRS 1980");
994
                getTxtInvF().setText("298.257222101");
995
                getTxtLong().setText("0.0");
996
                getTxtMeridian().setText("Greenwich");
997
                getTxtSemMay().setText("6378137.0");
998
                double semMen = calcularSemMen();
999
                getTxtSemMen().setText(""+semMen);
1000
        }
1001
        
1002
        private String getMaxCode() {
1003
                String sentence = "SELECT usr_code " +
1004
                                                        "FROM USR " +                                      
1005
                                                        "ORDER BY usr_code ASC";
1006
                EpsgConnection connect = new EpsgConnection();
1007
                connect.setConnectionUsr();
1008
                ResultSet result = Query.select(sentence,connect.getConnection());
1009
                int max = 0;
1010
                int lastValue = 0;
1011
                int goodValue = 1;
1012
                boolean firstAccess = true;
1013
                try {
1014
                        while (result.next()) {
1015
                                max = result.getInt("usr_code");
1016
                                if (firstAccess && max != 1) {                                        
1017
                                        return ""+goodValue;
1018
                                }
1019
                                firstAccess = false;
1020
                                if ((max - lastValue) == 1) {
1021
                                        lastValue = max;
1022
                                } else {
1023
                                        goodValue = lastValue+1;
1024
                                        break;
1025
                                }
1026
                        }
1027
                        if (goodValue == 1) {
1028
                                goodValue = max+1;
1029
                        }
1030
                } catch (SQLException e) {
1031
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
1032
                }                
1033
                return ""+goodValue;
1034
        }
1035
        
1036
        public double convert2Meters(String unit, double value) {
1037
                double factor_b = 0;
1038
                double factor_c = 0;
1039
                String sentence = "SELECT factor_b, factor_c " +
1040
                        "FROM epsg_unitofmeasure " +                                      
1041
                        "WHERE unit_of_meas_name = '" +unit + "'";
1042
                EpsgConnection connect = new EpsgConnection();
1043
                connect.setConnectionEPSG();
1044
                ResultSet result = Query.select(sentence,connect.getConnection());
1045
                try {
1046
                        result.next();
1047
                        factor_b = result.getDouble("factor_b");
1048
                        factor_c = result.getDouble("factor_c");                        
1049
                } catch (SQLException e) {
1050
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
1051
                }                
1052
                return ((value*factor_b)/factor_c);
1053
        }
1054
        
1055
        /**
1056
         * Convierte una valor en las unidades indicadas a grados sexagesimales seg?n los factores 
1057
         * de conversi?n obtenidos de la base de datos de la EPSG.
1058
         * 
1059
         * @param unit Unidad de entrada.
1060
         * @param value Valor a convertir
1061
         * @return Valor en grados sexagesimales.
1062
         */
1063
        public double convert2Degree(String unit, double value) {
1064
                //*************************************
1065
                //Se realiza la conversi?n a radianes:
1066
                double factor_b = 0;
1067
                double factor_c = 0;
1068
                String sentence = "SELECT factor_b, factor_c " +
1069
                        "FROM epsg_unitofmeasure " +                                      
1070
                        "WHERE unit_of_meas_name = '" +unit + "'";
1071
                EpsgConnection connect = new EpsgConnection();
1072
                connect.setConnectionEPSG();
1073
                ResultSet result = Query.select(sentence,connect.getConnection());
1074
                try {
1075
                        result.next();
1076
                        factor_b = result.getDouble("factor_b");
1077
                        factor_c = result.getDouble("factor_c");                        
1078
                } catch (SQLException e) {
1079
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
1080
                }        
1081
                if(Math.abs(factor_b/factor_c-Math.PI/180.0)<1.0e-12)
1082
                        return value;
1083
                value = ((value*factor_b)/factor_c);
1084
                //**********************************************
1085
                
1086
                //Conversi? de radianes a grados sexagesimales:
1087
                //return ((value * 180.0) / 200.0);
1088
                return ((value * 180.0) / Math.PI);
1089
        }
1090
        
1091
        public double convert(double value, String measure) throws ConversionException {
1092
                if (measure.equals("D.MS")) {                
1093
                        value *= this.divider;
1094
                int deg,min;
1095
                deg = (int) (value/10000); value -= 10000*deg;
1096
                min = (int) (value/  100); value -=   100*min;
1097
                if (min<=-60 || min>=60) {  // Accepts NaN
1098
                    if (Math.abs(Math.abs(min) - 100) <= EPS) {
1099
                        if (min >= 0) deg++; else deg--;
1100
                        min = 0;
1101
                    } else {
1102
                        throw new ConversionException("Invalid minutes: "+min);
1103
                    }
1104
                }
1105
                if (value<=-60 || value>=60) { // Accepts NaN
1106
                    if (Math.abs(Math.abs(value) - 100) <= EPS) {
1107
                        if (value >= 0) min++; else min--;
1108
                        value = 0;
1109
                    } else {
1110
                        throw new ConversionException("Invalid secondes: "+value);
1111
                    }
1112
                }
1113
                value = ((value/60) + min)/60 + deg;
1114
                return value;
1115
                }
1116
                if (measure.equals("grad") || measure.equals("grade")) 
1117
                        return ((value * 180.0) / 200.0);                        
1118
                if (measure.equals(""+(char)176)) 
1119
                        return value;                
1120
                if (measure.equals("DMS") ) 
1121
                        return value;                
1122
                if (measure.equals("m")) 
1123
                        return value;        
1124
                if (measure.equals("")) 
1125
                        return value;
1126
                
1127
                throw new ConversionException("Conversion no contemplada: "+measure);
1128
    }
1129
        
1130
        public double convertFromDegree(String unitSource, String unitTarget, double value) {
1131
                double factor_b = 0;
1132
                double factor_c = 0;                
1133
                String sentence = "SELECT factor_b, factor_c " +
1134
                        "FROM epsg_unitofmeasure " +                                      
1135
                        "WHERE unit_of_meas_name = '" +unitSource + "'";
1136
                EpsgConnection connect = new EpsgConnection();
1137
                connect.setConnectionEPSG();
1138
                ResultSet result = Query.select(sentence,connect.getConnection());
1139
                try {
1140
                        result.next();
1141
                        factor_b = result.getDouble("factor_b");
1142
                        factor_c = result.getDouble("factor_c");                        
1143
                } catch (SQLException e) {
1144
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
1145
                }                
1146
                value = ((value*factor_b)/factor_c);
1147
                
1148
                sentence = "SELECT factor_b, factor_c " +
1149
                "FROM epsg_unitofmeasure " +                                      
1150
                "WHERE unit_of_meas_name = '" +unitTarget + "'";
1151
                connect = new EpsgConnection();
1152
                connect.setConnectionEPSG();
1153
                result = Query.select(sentence,connect.getConnection());
1154
                try {
1155
                        result.next();
1156
                        factor_b = result.getDouble("factor_b");
1157
                        factor_c = result.getDouble("factor_c");                        
1158
                } catch (SQLException e) {
1159
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
1160
                }                
1161
                return ((value*factor_c)/factor_b);
1162
        }
1163
        
1164
        public void itemStateChanged(ItemEvent e) {
1165
                if (e.getItemSelectable().equals(cbLong)) {
1166
                        String unit = getAngularUnit(((JComboBox)e.getSource()).getSelectedIndex());
1167
                         double value = Double.valueOf(getTxtLong().getText()).doubleValue();
1168
                         value = convertFromDegree(sourceUnitLong, unit, value);
1169
                         sourceUnitLong = unit;
1170
                         String val = String.valueOf(value);
1171
                         getTxtLong().setText(val);
1172
                }
1173
                if (e.getItemSelectable().equals(cbSemMay) ) {
1174
                        String unit = getLengthUnit(((JComboBox)e.getSource()).getSelectedIndex());                         
1175
                        double value = Double.valueOf(getTxtSemMay().getText()).doubleValue();
1176
                         value = convertFromMeters(sourceUnitSemMay, unit, value);
1177
                         sourceUnitSemMay = unit;
1178
                         String val = String.valueOf(value);
1179
                         getTxtSemMay().setText(val);
1180
                }
1181
                if (e.getItemSelectable().equals(cbSemMen) ) {
1182
                        String unit = getLengthUnit(((JComboBox)e.getSource()).getSelectedIndex());
1183
                         double value = Double.valueOf(getTxtSemMen().getText()).doubleValue();
1184
                         value = convertFromMeters(sourceUnitSemMen, unit, value);
1185
                         sourceUnitSemMen = unit;
1186
                         String val = String.valueOf(value);
1187
                         getTxtSemMen().setText(val);
1188
                }                
1189
        }
1190
        
1191
        private boolean notANumber(String cadenaNumerica) {
1192
                int puntos = 0;
1193
                int signos = 0;
1194
                int letras = 0;
1195
                for (int i = 0; i< cadenaNumerica.length(); i++) {
1196
                        if (cadenaNumerica.charAt(i) == '.')
1197
                                puntos++;
1198
                        else if (cadenaNumerica.charAt(i) == '-') {
1199
                                if (i==0) {
1200
                                        signos++;
1201
                                }
1202
                                else if (i!=0 && cadenaNumerica.charAt(i-1) != 'E') {
1203
                                        signos = 2;
1204
                                }
1205
                        }
1206
                        else if (cadenaNumerica.charAt(i) == 'E') {
1207
                                if (i== 0) {
1208
                                        letras = 2;
1209
                                }
1210
                                else letras ++;
1211
                        }
1212
                }
1213
                
1214
                if ((letras > 1) || (signos > 1) || (puntos > 1))
1215
                        return true;
1216
                return false;
1217
        }
1218
        
1219
        public double convertFromMeters(String unitSource, String unitTarget, double value) {
1220
                double factor_b = 0;
1221
                double factor_c = 0;
1222
                String sentence = "SELECT factor_b, factor_c " +
1223
                        "FROM epsg_unitofmeasure " +                                      
1224
                        "WHERE unit_of_meas_name = '" +unitSource.replace("'", "'+char(39)+'") + "'";
1225
                EpsgConnection connect = new EpsgConnection();
1226
                connect.setConnectionEPSG();
1227
                ResultSet result = Query.select(sentence,connect.getConnection());
1228
                try {
1229
                        result.next();
1230
                        factor_b = result.getDouble("factor_b");
1231
                        factor_c = result.getDouble("factor_c");                        
1232
                } catch (SQLException e) {
1233
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
1234
                }                
1235
                value = ((value*factor_b)/factor_c);
1236
                
1237
                sentence = "SELECT factor_b, factor_c " +
1238
                "FROM epsg_unitofmeasure " +                                      
1239
                "WHERE unit_of_meas_name = '" +unitTarget.replace("'", "'+char(39)+'")+ "'";
1240
                connect = new EpsgConnection();
1241
                connect.setConnectionEPSG();
1242
                result = Query.select(sentence,connect.getConnection());
1243
                try {
1244
                        result.next();
1245
                        factor_b = result.getDouble("factor_b");
1246
                        factor_c = result.getDouble("factor_c");                        
1247
                } catch (SQLException e) {
1248
                        logger.error("Can't get values from resulset '"+sentence+"'.",e);
1249
                }                
1250
                return ((value*factor_c)/factor_b);
1251
        }
1252
        
1253
        public void keyPressed(KeyEvent e) {
1254
                primera = true;
1255
                if (e.getSource() == this.getTxtSemMay()) {                        
1256
                        if (e.getKeyCode() == 10) {
1257
                                primera = false;
1258
                                if (getRbA_Inv().isSelected()) {
1259
                                        getTxtSemMen().setText(String.valueOf(calcularSemMen()));
1260
                                }
1261
                                else if (getRbA_B().isSelected()) {
1262
                                        getTxtInvF().setText(String.valueOf(calcularInvF()));                                        
1263
                                }
1264
                        }                        
1265
                }
1266
                if (e.getSource() == this.getTxtSemMen()) {
1267
                        if (e.getKeyCode() == 10) {
1268
                                primera = false;
1269
                                getTxtInvF().setText(String.valueOf(calcularInvF()));                                
1270
                        }                        
1271
                }
1272
                if (e.getSource() == this.getTxtInvF()) {
1273
                        if (e.getKeyCode() == 10) {
1274
                                primera = false;
1275
                                getTxtSemMen().setText(String.valueOf(calcularSemMen()));                                
1276
                        }                        
1277
                }
1278
                if (e.getSource() == this.getTxtLong()) {
1279
                        if (e.getKeyCode() == 10) {
1280
                                primera = false;
1281
                                String cadenaNumerica = getTxtLong().getText().replaceAll("[^0-9.E]", "");
1282
                                
1283
                                if (getTxtLong().getText().length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
1284
                                        JOptionPane.showMessageDialog(DefinirDatum.this, 
1285
                                                        PluginServices.getText(this,"numeric_format")+": "+PluginServices.getText(this,"long"), 
1286
                                                        "Warning...", JOptionPane.WARNING_MESSAGE);
1287
                                        primera = true;
1288
                                        return;
1289
                                }
1290
                        }
1291
                }
1292
                primera = true;
1293
        }
1294
        public void keyReleased(KeyEvent e) {
1295
                // TODO Auto-generated method stub
1296
                
1297
        }
1298
        public void keyTyped(KeyEvent e) {
1299
                // TODO Auto-generated method stub
1300
                
1301
        }
1302
        
1303
        public void setLengthUnit(ArrayList units) {
1304
                this.lengthUnits = units;
1305
        }
1306
        
1307
        public void setAngularUnit(ArrayList units) {
1308
                this.angularUnits = units;
1309
        }
1310
        
1311
        public String getLengthUnit(int indice) {
1312
                return (String)lengthUnits.get(indice);
1313
        }
1314
        
1315
        public String getAngularUnit(int indice) {
1316
                return (String)angularUnits.get(indice);
1317
        }
1318
        public void focusGained(FocusEvent e) {
1319
                // TODO Auto-generated method stub
1320
                
1321
        }
1322
        public void focusLost(FocusEvent e) {
1323
                if (primera) {
1324
                        primera = false;
1325
                        if (e.getSource() == this.getTxtSemMay()) {
1326
                                if (getRbA_Inv().isSelected()) {
1327
                                        getTxtSemMen().setText(String.valueOf(calcularSemMen()));
1328
                                }
1329
                                else if (getRbA_B().isSelected()) {
1330
                                        getTxtInvF().setText(String.valueOf(calcularInvF()));                                        
1331
                                }
1332
                                                        
1333
                        }
1334
                        if (e.getSource() == this.getTxtSemMen()) {
1335
                                getTxtInvF().setText(String.valueOf(calcularInvF()));                                                
1336
                        }
1337
                        if (e.getSource() == this.getTxtInvF()) {
1338
                                getTxtSemMen().setText(String.valueOf(calcularSemMen()));                        
1339
                        }
1340
                        if (e.getSource() == this.getTxtLong()) {
1341
                                String cadenaNumerica = getTxtLong().getText().replaceAll("[^0-9.E]", "");
1342
                                
1343
                                if (getTxtLong().getText().length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
1344
                                        JOptionPane.showMessageDialog(DefinirDatum.this, 
1345
                                                        PluginServices.getText(this,"numeric_format")+": "+PluginServices.getText(this,"long"), 
1346
                                                        "Warning...", JOptionPane.WARNING_MESSAGE);
1347
                                        return;
1348
                                }
1349
                        }
1350
                }
1351
        }
1352
        
1353
        class ComboBoxTootipRenderer extends BasicComboBoxRenderer {
1354
                
1355
                private String[] tooltips = null;
1356
                
1357
            public ComboBoxTootipRenderer(String[] tooltips) {
1358
                    super();
1359
                    this.tooltips = tooltips;
1360
                }
1361

    
1362
                public Component getListCellRendererComponent(JList list, Object value,
1363
                int index, boolean isSelected, boolean cellHasFocus) {
1364
              if (isSelected) {
1365
                setBackground(list.getSelectionBackground());
1366
                setForeground(list.getSelectionForeground());
1367
                if (-1 < index) {
1368
                  list.setToolTipText(tooltips[index]);
1369
                }
1370
              } else {
1371
                setBackground(list.getBackground());
1372
                setForeground(list.getForeground());
1373
              }
1374
              setFont(list.getFont());
1375
              setText((value == null) ? "" : value.toString());
1376
              return this;
1377
            }
1378
          }
1379
                
1380
}