Statistics
| Revision:

root / branches / F2 / extensions / extJCRS / src / org / gvsig / crs / gui / panels / EPSGpanel.java @ 10789

History | View | Annotate | Download (24.3 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;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Color;
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.KeyEvent;
51
import java.awt.event.KeyListener;
52
import java.sql.ResultSet;
53
import java.sql.SQLException;
54

    
55
import javax.swing.BorderFactory;
56
import javax.swing.ButtonGroup;
57
import javax.swing.JButton;
58
import javax.swing.JLabel;
59
import javax.swing.JOptionPane;
60
import javax.swing.JPanel;
61
import javax.swing.JRadioButton;
62
import javax.swing.JScrollPane;
63
import javax.swing.JTable;
64
import javax.swing.JTextField;
65
import javax.swing.ListSelectionModel;
66
import javax.swing.table.DefaultTableModel;
67
import javax.swing.table.TableColumn;
68

    
69
import org.cresques.cts.IProjection;
70
import org.gvsig.crs.CrsException;
71
import org.gvsig.crs.CrsFactory;
72
import org.gvsig.crs.EpsgConnection;
73
import org.gvsig.crs.ICrs;
74
import org.gvsig.crs.Query;
75
import org.gvsig.crs.ogr.Epsg2wkt;
76
import org.gvsig.crs.ogr.GetCRSepsg;
77

    
78
import com.iver.andami.PluginServices;
79
import com.iver.cit.gvsig.gui.TableSorter;
80

    
81
/**
82
 * Clase perteneciente al panel de elecci?n de CRS del repositorio de la
83
 * EPSG, realiza la b?squeda y filtra los v?lidos.
84
 * 
85
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
86
 * @author Luisa Marina Fern?ndez (luisam.fernandez@uclm.es)
87
 *
88
 */
89
public class EPSGpanel extends JPanel implements KeyListener, ActionListener{
90

    
91
        private static final long serialVersionUID = 1L;
92
        
93
        String cadWKT = "";
94
        String cad_valida = "";
95
        public String key;
96
        public String crs_kind = null;
97
        String[] soported_crs = {"projected","geographic 2D","geographic 3D", "engineering", "vertical", "compound", "geocentric"};
98
        String[] not_soported_crs = {};
99
        
100
        int iteracion = 0;
101
        int transf = 0;
102
        int source_cod = 0;
103
        int method_code = 0;
104
        int datum_code = 0;
105
        int projection_conv_code = 0;
106
        public int epsg_code  = 0;
107
        public int selectedRowTable = -1;
108
        private int codeCRS = -1;
109
        int[] valid_method_code = {9800, 9801, 9802, 9803, 9804, 9805, 9806, 9807, 9808, 9809, 9810, 
110
                        9811, 9812, 9813, 9814, 9815, 9816, 9817, 9602, 9659, 9818, 9819, 9820, 9822, 
111
                        9827};
112
        
113
        
114
        boolean tecla_valida = false;
115
        boolean source_yn = false;
116
        
117

    
118
        private JRadioButton codeRadioButton = null;
119
        private JRadioButton nameRadioButton = null;
120
        private JRadioButton areaRadioButton = null;
121
        private ButtonGroup optGroup;
122
        private JPanel groupRadioButton = null;
123
        
124
        public EpsgConnection connect = null;
125
        public JPanel EPSGpanel = null;
126
        private JLabel lblCriterio;
127
        private JButton infoCrs = null;
128
        private JButton searchButton = null;
129
        private JTextField searchTextField = null;
130
        public JTable jTable = null;
131
        private JScrollPane jScrollPane = null;
132
        public DefaultTableModel dtm = null;        
133
        public TableSorter sorter = null;
134
        
135
        /**
136
         * Construye el Objeto EpsgPanel
137
         *
138
         */
139
        public EPSGpanel(){
140
                initialize();
141
        }
142
        /*
143
         * Define las propiedades y agrega los componentes del EPSGPanel
144
         */
145
        private void initialize(){
146
                this.setLayout(new BorderLayout());
147
                JPanel pNorth=new JPanel();
148
                pNorth.setLayout(new GridLayout(2,1));
149
                pNorth.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
150
                pNorth.add(getGroupRadioButton());
151
                JPanel pInNorth = new JPanel();
152
                pInNorth.setLayout(new FlowLayout(FlowLayout.LEFT,10,1));
153
                pInNorth.add(getSearchButton());
154
                pInNorth.add(getSearchTextField());
155
                pNorth.add(pInNorth);
156
                this.add(pNorth,BorderLayout.NORTH);
157
                this.add(getJScrollPane(), BorderLayout.CENTER);        
158
                JPanel pSouth=new JPanel(new FlowLayout(FlowLayout.RIGHT,10,3));
159
                pSouth.add(getInfoCrs());
160
                this.add(pSouth,BorderLayout.SOUTH);
161
        }
162
        /**
163
         * Establece la conexi?n con la Base de Datos de la Epsg
164
         *
165
         */
166
        public void connection(){
167
                connect = new EpsgConnection();
168
                connect.setConnectionEPSG();
169
        }
170
         /**
171
          * Inicializa el bot?n de opci?n 'c?digo' 
172
          * @return
173
          */   
174
        private JRadioButton getCodeRadioButton() {
175
                if (codeRadioButton == null) {
176
                        codeRadioButton = new JRadioButton();
177
                        codeRadioButton.setText(PluginServices.getText(this,"por_codigo"));//setText("By Code EPSG");
178
                        codeRadioButton.setSelected(true);
179
                        codeRadioButton.addActionListener(this);
180
                }
181
                return codeRadioButton;
182
        }
183
        /**
184
         * Inicializa el bot?n de opci?n 'nombre'
185
         * @return
186
         */ 
187
        private JRadioButton getNameRadioButton() {
188
                if (nameRadioButton == null) {
189
                        nameRadioButton = new JRadioButton();
190
                        nameRadioButton.setText(PluginServices.getText(this,"por_nombre"));
191
                        nameRadioButton.addActionListener(this);
192
                }
193
                return nameRadioButton;
194
        }
195
        /**
196
         * Inicializa el bot?n de opci?n 'Area'
197
         * @return
198
         */
199
        private JRadioButton getAreaRadioButton() {
200
                if (areaRadioButton == null) {
201
                        areaRadioButton = new JRadioButton();
202
                        areaRadioButton.setText(PluginServices.getText(this,"por_area"));
203
                        areaRadioButton.addActionListener(this);
204
                }
205
                return areaRadioButton;
206
        }
207
         /**
208
          * Obtiene el panel con todos los botones de opci?n
209
          * @return
210
          */
211
        private JPanel getGroupRadioButton() {
212
                if (groupRadioButton == null) {
213
                        groupRadioButton = new JPanel();
214
                        groupRadioButton.setLayout(new GridLayout(1,4));
215
                        groupRadioButton.add(getLblCriterio());
216
                        groupRadioButton.add(getCodeRadioButton());
217
                        groupRadioButton.add(getNameRadioButton());
218
                        groupRadioButton.add(getAreaRadioButton());
219
                        //agrupar los botones
220
                        getOptGroup();
221
                }
222
                return groupRadioButton;
223
        }
224
        /**
225
         * Inicializa el laber 'Criterio de b?squeda'
226
         * @return
227
         */
228
        private JLabel getLblCriterio(){
229
                lblCriterio = new JLabel();
230
                lblCriterio.setText(PluginServices.getText(this, "criterio_busqueda")+":");
231
                return lblCriterio;
232
        }
233

    
234
        /**
235
         * M?todo que controla la b?squeda de los CRS siguiendo los criterios
236
         * de b?squeda que le hemos definido. Tambi?n gestiona los casos en que
237
         * no encuentre CRS, o que los par?metros de b?squeda sean err?neos. Si
238
         * encuentra alg?n CRS pero no es soportado por la aplicaci?n
239
         * aparecer? el mensaje de informaci?n correspondiente.
240
         *
241
         */
242
        private void searchButton() {
243
                boolean not_valid = false;
244
                boolean not_numeric = false;
245
                searchTextField.setBackground(Color.white);
246
                
247
                if (searchTextField.getText().equals("")) {
248
                        searchTextField.setBackground(new Color(255,204,204));
249
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,"fill_name"), "Warning...", JOptionPane.WARNING_MESSAGE);
250
                }                
251
                
252
                else {
253
            //Eliminar filas en cada nueva bsqueda
254
                        int numRow = dtm.getRowCount();
255
                        while (numRow != 0) {
256
                                numRow = numRow - 1;
257
                                dtm.removeRow(numRow);
258
                        }
259
                        
260
                        if (codeRadioButton.isSelected() && (searchTextField.getText().length()!=searchTextField.getText().replaceAll("[^0-9]", "").length())){
261
                                not_numeric = true;
262
                        }
263
                        
264
                        //Dependiendo de la opcion se realizada una busqueda
265
                        ResultSet result = null;
266
                        ResultSet result2 = null;
267
                        /*
268
                         * variable que indicara si la busqueda se hace primero en epsg_coordinatereferencesystem
269
                         * o en epsg_area; esto es debido a que HSQLDB no soporta la b?squeda simult?nea en ambas
270
                         * tablas, por lo cual hay que separar la b?squeda
271
                         */
272
                        int bus = 2;                          
273
                        if (codeRadioButton.isSelected() && !not_numeric) {
274
                                bus=0;
275
                                key = searchTextField.getText();
276
                                int code = Integer.parseInt(key);
277
                                String sentence = "SELECT coord_ref_sys_code, coord_ref_sys_name, coord_ref_sys_kind, area_of_use_code, " +
278
                                                                  "source_geogcrs_code, projection_conv_code  " +
279
                                                                  "FROM epsg_coordinatereferencesystem " +                                      
280
                                      "WHERE coord_ref_sys_code = " + code;
281
                                                                
282
                                result = Query.select(sentence,connect.getConnection());        
283
                                
284
                        }
285
                        
286
                        else if (nameRadioButton.isSelected()) {
287
                                bus=0;
288
                                key = searchTextField.getText();
289
                                String key2 = key.substring(0,1);
290
                                String key3 = key.substring(1,key.length());
291
                                key2 = key2.toUpperCase();
292
                                
293
                                String sentence = "SELECT coord_ref_sys_code, coord_ref_sys_name, coord_ref_sys_kind, " +
294
                                                                        "area_of_use_code, source_geogcrs_code, projection_conv_code " +
295
                                                                          "FROM epsg_coordinatereferencesystem " +                                      
296
                                                                          "WHERE (coord_ref_sys_name LIKE '%" + key + "%') OR (coord_ref_sys_name LIKE '%"+ 
297
                                                                          key.toUpperCase() +"%') " +
298
                                                                          "OR (coord_ref_sys_name LIKE '%" + key2+key3 +"%')";
299
                                result = Query.select(sentence,connect.getConnection());
300
                        }
301
                        
302
                        else if (areaRadioButton.isSelected()) {
303
                                bus=1;
304
                                key = searchTextField.getText();
305
                                String key2 = key.substring(0,1);
306
                                String key3 = key.substring(1,key.length());
307
                                key2 = key2.toUpperCase();                                
308
                                
309
                                String sentence = "SELECT area_name, area_of_use, area_code " +
310
                                      "FROM epsg_area " +
311
                                      "WHERE (area_name LIKE '%" + key + "%') OR (area_of_use LIKE '%" + key + "%') "+
312
                                      "OR (area_name LIKE '%" + key.toUpperCase() + "%') OR (area_of_use LIKE '%" + key.toUpperCase() + "%') "+
313
                                      "OR (area_name LIKE '%" + key2+key3 + "%') OR (area_of_use LIKE '%" + key2+key3 + "%') ";
314
                                result = Query.select(sentence,connect.getConnection());                                
315
                        }
316
                        
317
                        if (bus==0){
318
                                try {
319
                                        while(result.next()) {
320
                                                Object[]data = new Object[5];
321
                                                data[0] = String.valueOf(result.getInt("coord_ref_sys_code"));
322
                                                data[1] = result.getString("coord_ref_sys_name");
323
                                                crs_kind = result.getString("coord_ref_sys_kind");
324
                                                data[2] = crs_kind;
325
                                                projection_conv_code = result.getInt("projection_conv_code");
326
                                                
327
                                                int area_of_use_code = Integer.parseInt(result.getString("area_of_use_code"));
328
                                                                                
329
                                                String sentence = "SELECT area_name, area_of_use FROM epsg_area " +
330
                                                                                "WHERE area_code = "+ area_of_use_code ;
331
                                                
332
                                                result2 = Query.select(sentence,connect.getConnection());
333
                                                while(result2.next()){
334
                                                        data[3] = result2.getString("area_name");
335
                                                        data[4] = result2.getString("area_of_use");                                        
336
                                                }
337
                                                if (data[0]!=null && validCRS(projection_conv_code) && valid(crs_kind)){
338
                                                        dtm.addRow(data);
339
                                                }
340
                                        }
341
                                }
342
                                
343
                                catch (SQLException e1) {
344
                                        e1.printStackTrace();
345
                                }
346
                        }
347
                        else if (bus==1){
348
                                try {
349
                                        while(result.next()) {
350
                                                Object[]data = new Object[5];
351
                                                data[3] = result.getString("area_name");
352
                                                data[4] = result.getString("area_of_use");        
353
                                                                                        
354
                                                int area_of_use_code = Integer.parseInt(result.getString("area_code"));
355
                                                                                
356
                                                String sentence = "SELECT coord_ref_sys_code, coord_ref_sys_name, " +
357
                                                                                "coord_ref_sys_kind, source_geogcrs_code, projection_conv_code " +
358
                                                                                "FROM epsg_coordinatereferencesystem " +
359
                                                                                "WHERE area_of_use_code = "+ area_of_use_code ;
360
                                                
361
                                                result2 = Query.select(sentence,connect.getConnection());
362
                                                while(result2.next()){
363
                                                        data[0] = String.valueOf(result2.getInt("coord_ref_sys_code"));
364
                                                        data[1] = result2.getString("coord_ref_sys_name");
365
                                                        data[2] = result2.getString("coord_ref_sys_kind");
366
                                                        crs_kind = (String)data[2];
367
                                                        projection_conv_code = result2.getInt("projection_conv_code");
368
                                                                                
369
                                                }
370
                                                /*
371
                                                 * Buscaremos solo aquellos CRS cuyas proyecciones esten entre las
372
                                                 * 16 soportadas por proj4 para ello creamos un metodo para validar
373
                                                 * si esta entre estas proyecciones
374
                                                 */                                                
375
                                                if (data[0]!=null && validCRS(projection_conv_code) && valid(crs_kind)){
376
                                                        dtm.addRow(data);
377
                                                }        
378
                                                if (notValid(crs_kind)){
379
                                                        not_valid = true;
380
                                                }
381
                                        }
382
                                } 
383
                                
384
                                catch (SQLException e1) {
385
                                        e1.printStackTrace();
386
                                }
387
                        }
388
                        int numr = dtm.getRowCount();                        
389
                        if (not_valid){
390
                                JOptionPane.showMessageDialog(this, PluginServices.getText(this,"crs_not_soported"), "Warning...",
391
                                                JOptionPane.WARNING_MESSAGE);
392
                                not_valid = false;
393
                        }
394
                        else if (not_numeric) {
395
                                JOptionPane.showMessageDialog(EPSGpanel.this, 
396
                                                PluginServices.getText(this,"numeric_format"), 
397
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
398
                                searchTextField.setText("");
399
                        }
400
                        else if (numr == 0){
401
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,"no_results"), "Warning...",
402
                                        JOptionPane.WARNING_MESSAGE);
403
                        }
404
                        else{
405
                                this.getJTable().setRowSelectionInterval(0,0);
406
                        }
407
                }                
408
        }        
409
        
410
        /**
411
         * metodo auxiliar que hara la comprobacion de si el CRS buscado pertenecera
412
         * a los no soportados por proj4
413
         */
414
        private boolean notValid(String kind){                
415
                for (int i = 0; i< not_soported_crs.length; i++){
416
                        if (kind.equals(not_soported_crs[i])){
417
                                return true;
418
                        }
419
                }
420
                return false;
421
        }
422
        
423
        /**
424
         * Metodo que comprueba si el CRS buscado pertenece a algun tipo 
425
         * de los que soporta la aplicacion
426
         */
427
        private boolean valid(String kind){                
428
                for (int i = 0; i< soported_crs.length; i++){
429
                        if (kind.equals(soported_crs[i])){
430
                                return true;
431
                        }
432
                }
433
                return false;
434
        }
435
        
436
        /**
437
         * metodo auxiliar que nos servira para comprobar si la proyeccion del CRS seleccionado
438
         * esta entre las 16 con las que trabaja proj4
439
         */
440
        private boolean validCRS(int projection_conv_code2) {
441
                if (projection_conv_code2 == 0) return true;
442
                String sentence = "SELECT coord_op_method_code " +
443
                                                        "FROM epsg_coordoperation " +
444
                                                        "WHERE coord_op_code = " + projection_conv_code;
445
                ResultSet result = Query.select(sentence,connect.getConnection());
446
                try {
447
                        while (result.next()){
448
                                method_code = result.getInt("coord_op_method_code");
449
                        }
450
                } catch (SQLException e) {
451
                        e.printStackTrace();
452
                }
453
                
454
                for (int i = 0; i< valid_method_code.length; i++){
455
                        if (method_code == valid_method_code[i] ){
456
                                return true;
457
                        }
458
                }                
459
                return false;
460
        }
461

    
462
        /**
463
         * Inicializa el bot?n de b?squeda
464
         * @return
465
         */
466
        private JButton getSearchButton() {
467
                if (searchButton == null) {
468
                        searchButton = new JButton();
469
                        searchButton.setPreferredSize(new Dimension(75,20));
470
                        searchButton.setText(PluginServices.getText(this,"buscar"));
471
                        searchButton.setMnemonic('S');
472
                        searchButton.setToolTipText(PluginServices.getText(this,"buscar_por_criterio_seleccion"));
473
                        searchButton.addActionListener(this);                        
474
                }
475
                return searchButton;
476
        }        
477
        /**
478
         * Inicializa el cuadro de texto en el que se introduce el dato a buscar
479
         * @return
480
         */
481
        private JTextField getSearchTextField() {
482
                if (searchTextField == null) {
483
                        searchTextField = new JTextField();
484
                        searchTextField.setPreferredSize(new Dimension(350,20));
485
                        searchTextField.addKeyListener(this);                        
486
                }
487
                return searchTextField;
488
        }
489
        /**
490
         * Inicializa la tabla que se utiliza para mostrar 
491
         * los resultados de la b?squeda
492
         * @return
493
         */
494
        public JTable getJTable() {
495
                if (jTable == null) {
496
                        String[] columnNames= {PluginServices.getText(this,"codigo"),
497
                                        PluginServices.getText(this,"nombre"),
498
                                        PluginServices.getText(this,"tipo"),
499
                                        PluginServices.getText(this,"area"),
500
                        PluginServices.getText(this,"descripcion")};
501
                        Object[][]data = {};                        
502
                        dtm = new DefaultTableModel(data, columnNames)
503
                         {
504
                                private static final long serialVersionUID = 1L;
505
                                public boolean isCellEditable(int row, int column) {
506
                                        return false;
507
                                }
508
                                /*
509
                                 * metodo necesario para cuando utilizamos tablas ordenadas
510
                                 * ya que sino al ordenar por algun campo no se queda con el orden
511
                                 * actual al seleccionar una fila (non-Javadoc)
512
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
513
                                 */
514
                                public Class getColumnClass(int column)
515
                                {
516
                                        return getValueAt(0, column).getClass();
517
                                }
518
                                };
519
                        sorter = new TableSorter(dtm);                        
520

    
521
                        jTable = new JTable(sorter);
522
                        //jTable.setPreferredSize(new Dimension(800, 100));
523
                        sorter.setTableHeader(jTable.getTableHeader());
524
                        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
525
                        jTable.setCellSelectionEnabled(false);
526
                        jTable.setRowSelectionAllowed(true);
527
                        jTable.setColumnSelectionAllowed(false);
528
                        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
529
                        TableColumn column = null;
530
                        for (int i = 0; i < columnNames.length; i++) {
531
                            column = jTable.getColumnModel().getColumn(i);
532
                            if (i == 0) {
533
                                column.setPreferredWidth(50); //code column is shorter                                     
534
                            }                                    
535
                            else if (i == 2) {
536
                                    column.setPreferredWidth(80);
537
                            }        
538
                            else if (i==4){
539
                                    column.setPreferredWidth(300);
540
                            }
541
                            else {
542
                                column.setPreferredWidth(140);
543
                            }
544
                        }                        
545
                }
546
                
547
                return jTable;
548
                
549
        }
550
        /**
551
         * Establece el c?digo se CRS
552
         * @param code
553
         */
554
        public void setCodeCRS(int code) {
555
                codeCRS = code;
556
        }
557
        /**
558
         * Obtiene el c?digo de CRS
559
         * @return
560
         */
561
        public int getCodeCRS() {
562
                return codeCRS;
563
        }        
564
        /**
565
         * Inicializa el panel que contiene la tabla de resultados
566
         * @return
567
         */
568
        private JScrollPane getJScrollPane() {
569
                if (jScrollPane == null) {
570
                        jScrollPane = new JScrollPane(getJTable(),JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
571
                        jScrollPane.setPreferredSize(new Dimension(500,150));
572
                        jScrollPane.setBorder(
573
                                    BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(3,3,3,3),jScrollPane.getBorder()));
574
                        
575
                        jScrollPane.setViewportView(getJTable());
576
                }
577
                return jScrollPane;
578
        }
579
        /**
580
         * Inicializa el bot?n que muestra la informaci?n de CRS seleccionado
581
         * @return
582
         */
583
        public JButton getInfoCrs() {
584
                if(infoCrs == null) {
585
                        infoCrs = new JButton();
586
                        infoCrs.setPreferredSize(new Dimension(85,20));
587
                        infoCrs.setText(PluginServices.getText(this,"infocrs"));                        
588
                        infoCrs.setMnemonic('I');
589
                        infoCrs.setEnabled(false);
590
                        infoCrs.setToolTipText(PluginServices.getText(this,"more_info"));
591
                        infoCrs.addActionListener(this);
592
                }
593
                return infoCrs;
594
        }
595
        
596
        
597
        public ICrs getProjection() {
598
                try {
599
                        String txt = getWKT();                        
600
                        ICrs crs = new CrsFactory().getCRS(epsg_code, txt); 
601
                        return crs ;
602
                } catch (CrsException e) {
603
                        e.printStackTrace();
604
                }
605
                return null;
606
        }
607
        
608
        /**
609
         * Genera la cadena WKT una vez que escogemos el CRS de la tabla.
610
         * Dicha cadena la generamos con los par?metros cogidos directamente
611
         * del repositorio de la EPSG, y m?s adelante volveremos a tratarla
612
         * para pasarla a una cadena wkt legible por la proj4.
613
         *
614
         */
615
        public void setWKT(){
616
                Epsg2wkt wkt = null;
617
                                                
618
                epsg_code = getCodeCRS();
619
                if (epsg_code != -1){                
620
                        /*
621
                         * ahora que he escogido, recojo toda la informacion que me va a hacer falta
622
                         */
623
                        String sentence = "SELECT source_geogcrs_code, projection_conv_code, " +
624
                                        "coord_ref_sys_kind, datum_code " +
625
                                                        "FROM epsg_coordinatereferencesystem " +
626
                                                        "WHERE coord_ref_sys_code = "+ epsg_code ;
627
                        ResultSet result = Query.select(sentence,connect.getConnection());
628
                        
629
                        try {
630
                                result.next();
631
                                source_cod = result.getInt("source_geogcrs_code");
632
                                projection_conv_code = result.getInt("projection_conv_code");
633
                                crs_kind = result.getString("coord_ref_sys_kind");
634
                                datum_code = result.getInt("datum_code");
635
                        } catch (SQLException e1) {
636
                                e1.printStackTrace();
637
                        }
638
                        
639
                        if (datum_code != 0){
640
                                source_yn = true;
641
                                GetCRSepsg ep = new GetCRSepsg(epsg_code, source_yn, source_cod, projection_conv_code, connect);
642
                                ep.Getepsgdata();
643
                        }
644
                        else if (source_cod != 0){
645
                                source_yn = false;
646
                        }
647
                        else source_yn = true;
648
                        
649
                        GetCRSepsg ep = new GetCRSepsg(epsg_code, source_yn, source_cod, projection_conv_code, connect);
650
                        ep.Getepsgdata();
651
                        
652
                        if (crs_kind.equals("geographic 2D") || crs_kind.equals("geographic 3D")){
653
                                wkt = new Epsg2wkt(ep , "geog");                        
654
                        }
655
                        else if (crs_kind.equals("projected")){
656
                                wkt = new Epsg2wkt(ep, "proj");
657
                        }
658
                        else if (crs_kind.equals("compound")){
659
                                wkt = new Epsg2wkt(ep,"comp");
660
                        }
661
                        else if (crs_kind.equals("geocentric")){
662
                                wkt = new Epsg2wkt(ep,"geoc");
663
                        }
664
                }
665
                else {
666
                        JOptionPane.showMessageDialog(this, 
667
                                        PluginServices.getText(this,"crs_no_selected."), 
668
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
669
                        
670
                }
671
                cadWKT = wkt.getWKT();
672
        }
673
                
674
        public String getWKT(){
675
                return cadWKT;
676
        }
677
        
678
        public void setProjection(IProjection crs) {
679
                //setCrs((ICrs) crs);
680
        }
681
        
682
        /**
683
         * Metodo para cargar en el CRS de la capa el CRS de la vista.
684
         * Se utilizar? a la hora de arrancar la definici?n del CRS de la nueva capa
685
         */        
686
        public void loadViewCRS(int code){
687
                connection();
688
                String sentence = "SELECT coord_ref_sys_code, coord_ref_sys_name, coord_ref_sys_kind, area_of_use_code, " +
689
                                                  "source_geogcrs_code, projection_conv_code  " +
690
                                                  "FROM epsg_coordinatereferencesystem " +                                      
691
                                        "WHERE coord_ref_sys_code = " + code;
692
                ResultSet result = Query.select(sentence,connect.getConnection());
693
                
694
                try {
695
                        result.next();
696
                        Object[]data = new Object[5];
697
                        data[0] = String.valueOf(result.getInt("coord_ref_sys_code"));
698
                        data[1] = result.getString("coord_ref_sys_name");
699
                        crs_kind = result.getString("coord_ref_sys_kind");
700
                        data[2] = crs_kind;
701
                        projection_conv_code = result.getInt("projection_conv_code");
702
                        
703
                        int area_of_use_code = Integer.parseInt(result.getString("area_of_use_code"));
704
                                                        
705
                        sentence = "SELECT area_name, area_of_use FROM epsg_area " +
706
                                                        "WHERE area_code = "+ area_of_use_code ;
707
                        
708
                        ResultSet result2 = Query.select(sentence,connect.getConnection());
709
                        result2.next();
710
                        data[3] = result2.getString("area_name");
711
                        data[4] = result2.getString("area_of_use");                                        
712
                        
713
                        dtm.addRow(data);                                        
714
                }
715
                
716
                catch (SQLException e1) {
717
                        e1.printStackTrace();
718
                }
719
        }
720
        /**
721
         * Controla si se pulsa intro en el cuadro de texto,
722
         * en ese caso se interpreta como si hubiese pulsado el bot?n 'Buscar'
723
         */
724
        public void keyPressed(KeyEvent e) {        
725
                if (e.getSource() == this.getSearchTextField()) {
726
                        if (e.getKeyCode() == 10) {
727
                                searchButton();
728
                        }                        
729
                }                
730
        }
731

    
732
        public void keyReleased(KeyEvent e) {
733
                
734
        }
735

    
736
        public void keyTyped(KeyEvent e) {
737
                
738
        }
739

    
740
        /**
741
         * Maneja los eventos de los botones y los radioButtons del panel
742
         * de la EPSG.
743
         */
744
        public void actionPerformed(ActionEvent e) {
745
                /*Si el objeto que genera el evento es el RadioButton 'Codigo'-->
746
                resetea el texto de b?squeda*/
747
                if (e.getSource() == this.getCodeRadioButton()) {
748
                        searchTextField.setText("");
749
                }
750
                /*Si el objeto que genera el evento es el RadioButton 'Nombre'-->
751
                resetea el texto de b?squeda*/
752
                if (e.getSource() == this.getNameRadioButton()) {
753
                        searchTextField.setText("");
754
                }
755
                /*Si el objeto que genera el evento es el RadioButton 'Area'-->
756
                resetea el texto de b?squeda*/
757
                if (e.getSource() == this.getAreaRadioButton()) {
758
                        searchTextField.setText("");
759
                }
760
                /*Si el objeto que genera el evento es el JButton 'Buscar'-->
761
                Comprueba que no est? vacio*/
762
                if (e.getSource() == this.getSearchButton()) {
763
                        searchTextField.setBackground(Color.white);
764
                        if (searchTextField.getText().equals("")) {
765
                                searchTextField.setBackground(new Color(255,204,204));
766
                                JOptionPane.showMessageDialog(EPSGpanel.this, 
767
                                                PluginServices.getText(this,"fill_name"), 
768
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
769
                        }
770
                        else {
771
                                searchButton();
772
                        }
773
                }
774
                /*Si el objeto que genera el evento es el JButton 'InfoCrs'
775
                se muestra la informaci?n ralicionada con el Crs seleccionado en la tabla*/
776
                if (e.getSource() == this.getInfoCrs()) {
777
                        InfoCRSPanel info = new InfoCRSPanel(getProjection());
778
                        PluginServices.getMDIManager().addWindow(info);
779
                }
780
        }
781
        /**
782
         * Agrupa los botones de opci?n
783
         * @return
784
         */
785
        public ButtonGroup getOptGroup() {
786
                if (optGroup==null){
787
                        optGroup=new ButtonGroup();
788
                        optGroup.add(getCodeRadioButton());
789
                        optGroup.add(getNameRadioButton());
790
                        optGroup.add(getAreaRadioButton());
791
                }
792
                return optGroup;
793
        }
794
}