Statistics
| Revision:

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

History | View | Annotate | Download (15.7 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.Iau2wkt;
76

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

    
80
/**
81
 * Clase que genera el panel para la b?squeda de CRS del repositorio
82
 * de la IAU2000
83
 * 
84
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
85
 * @author Luisa Marina Fern?ndez (luisam.fernandez@uclm.es)
86
 *
87
 */
88
public class IAU2000panel extends JPanel implements KeyListener, ActionListener {
89

    
90
        private static final long serialVersionUID = 1L;
91
        public JPanel IAU2000panel = null;
92
        
93
        private JRadioButton codeRadioButton = null;
94
        private JRadioButton nameRadioButton = null;        
95
        private JPanel groupRadioButton = null;
96
        
97
        JLabel jLabel = null;
98
        private JLabel lblCriterio=null;
99
        JTextField crsTextFld = null;        
100
        
101
        String cadWKT = "";
102
        
103
        int transf = 0;        
104
        boolean source_yn = false;
105
        int source_cod = 0;
106
        int method_code = 0;
107
        int datum_code = 0;
108
        int projection_conv_code = 0;
109
        public String crs_kind = null;
110

    
111
        public TableSorter sorter = null;
112
        
113
        public EpsgConnection connect = null;
114
        
115

    
116
        private JButton searchButton = null;
117
        private JTextField searchTextField = null;
118
        public JTable jTable = null;
119
        private JScrollPane jScrollPane = null;
120
        public DefaultTableModel dtm = null;
121
        
122
        public String key;
123
        public int selectedRowTable = -1;        
124
        private int codeCRS = -1;        
125
        
126
        public IAU2000panel() {                
127
                initialize();
128
        }
129
        /*
130
         * Establece las propiedades y los componentes del panel de la IAU2000
131
         */
132
        private void initialize(){
133
                this.setLayout(new BorderLayout());
134
                JPanel pNorth=new JPanel();
135
                JPanel pInNorth=new JPanel();
136
                //Agregar las opciones de b?squeda
137
                pInNorth.setLayout(new FlowLayout(FlowLayout.CENTER,10,1));
138
                pInNorth.add(getSearchButton());
139
                pInNorth.add(getSearchTextField());
140
                
141
                pNorth.setLayout(new GridLayout(0,1));
142
                pNorth.add(getGroupRadioButton());
143
                pNorth.add(pInNorth);
144
                //Agregar estos elementos al panel principal
145
                this.add(pNorth,BorderLayout.NORTH);
146
                this.add(getJScrollPane(),BorderLayout.CENTER);
147
        }
148
        
149
        public void connection(){
150
                connect = new EpsgConnection();
151
                connect.setConnectionIAU2000();
152
                
153
        }
154
        /**
155
         * Inicializa el Radio Button 'C?digo' 
156
         * @return
157
         */
158
        private JRadioButton getCodeRadioButton() {
159
                if (codeRadioButton == null) {
160
                        codeRadioButton = new JRadioButton();
161
                        codeRadioButton.setText(PluginServices.getText(this,"por_codigo"));//setText("By Code EPSG");
162
                        codeRadioButton.setSelected(true);
163
                        codeRadioButton.addActionListener(this);
164
                }
165
                return codeRadioButton;
166
        }
167
        /**
168
         * Inicializa el Radio Button  'Nombre'
169
         * @return
170
         */
171
        private JRadioButton getNameRadioButton() {
172
                if (nameRadioButton == null) {
173
                        nameRadioButton = new JRadioButton();
174
                        nameRadioButton.setText(PluginServices.getText(this,"por_nombre"));
175
                        nameRadioButton.addActionListener(this);
176
                }
177
                return nameRadioButton;
178
        }
179
        /**
180
         * Agrega todos los Radio Buttons a un panel
181
         * @return
182
         */
183
        private JPanel getGroupRadioButton() {
184
                if (groupRadioButton == null) {
185
                        groupRadioButton = new JPanel();
186
                        groupRadioButton.setLayout(new GridLayout(1,0));
187
                        groupRadioButton.setPreferredSize(new Dimension(500,30));
188
                        groupRadioButton.add(getLblCriterio());
189
                        groupRadioButton.add(getCodeRadioButton());
190
                        groupRadioButton.add(getNameRadioButton());        
191
                        //Agrupar los Radio Buttons
192
                        ButtonGroup group=new ButtonGroup();
193
                        group.add(getCodeRadioButton());
194
                        group.add(getNameRadioButton());
195
                }
196
                return groupRadioButton;
197
        }
198
        /**
199
         * Inicializa el Label que nombra el Criterio de B?squeda
200
         * @return
201
         */
202
        private JLabel getLblCriterio(){
203
                if(lblCriterio==null){
204
                        lblCriterio = new JLabel();
205
                        lblCriterio.setPreferredSize(new Dimension(100, 20));
206
                        lblCriterio.setText(PluginServices.getText(this, "criterio_busqueda"));
207
                }
208
                return lblCriterio;
209
        }
210
        
211
        /**
212
         * M?todo que controla la b?squeda de CRS del repositorio de IAU2000.
213
         * Tambi?n maneja los errores en caso de que los par?metros de b?squeda
214
         * sean err?neos, o que no se encuentren resultados.
215
         *
216
         */
217
        private void searchButton() {                
218
                searchTextField.setBackground(Color.white);
219
                boolean not_numeric = false;
220
                
221
                if (searchTextField.getText().equals("")) {
222
                        searchTextField.setBackground(new Color(255,204,204));
223
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,"fill_name"), "Warning...", JOptionPane.WARNING_MESSAGE);
224
                }
225
                
226
                else {
227
            //Eliminar filas en cada nueva bsqueda
228
                        int numRow = dtm.getRowCount();
229
                        while (numRow != 0) {
230
                                numRow = numRow - 1;
231
                                dtm.removeRow(numRow);
232
                        }
233
                        
234
                        if (codeRadioButton.isSelected() && (searchTextField.getText().length()!=searchTextField.getText().replaceAll("[^0-9]", "").length())){
235
                                not_numeric = true;
236
                        }
237
                        
238
                        //Dependiendo de la opcion se realizada una busqueda
239
                        ResultSet result = null;
240
                                                                                          
241
                        if (codeRadioButton.isSelected() && !not_numeric) {
242
                                        
243
                                key = searchTextField.getText();
244
                                int code = Integer.parseInt(key);
245
                                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
246
                                                                  "FROM IAU2000 " +                                      
247
                                      "WHERE iau_code = " + code;
248
                                                                
249
                                result = Query.select(sentence,connect.getConnection());        
250
                                
251
                                
252
                                Object[] data = new Object[4];
253
                                try {
254
                                        while (result.next()){
255
                                                data[0]        = result.getString("iau_code");
256
                                                data[1] = result.getString("iau_wkt");
257
                                                String proj = result.getString("iau_proj");
258
                                                if (!proj.equals("")){
259
                                                        data[1] = proj;
260
                                                        data[2] = PluginServices.getText(this,"si");
261
                                                } 
262
                                                else 
263
                                                {
264
                                                        data[1] = result.getString("iau_geog");
265
                                                        data[2] = PluginServices.getText(this,"no");
266
                                                }
267
                                                
268
                                                data[3] = result.getString("iau_datum");
269
                                                dtm.addRow(data);
270
                                        }
271
                                } catch (SQLException e1) {
272
                                        e1.printStackTrace();
273
                                }
274
                        }
275
                        else if (nameRadioButton.isSelected()) {
276
                                key = searchTextField.getText();
277
                                String key2 = key.substring(0,1);
278
                                String key3 = key.substring(1,key.length());
279
                                key2 = key2.toUpperCase();
280
                                
281
                                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
282
                                        "FROM IAU2000 " +                                      
283
                                        "WHERE (iau_proj LIKE '%" + key + "%') OR (iau_proj LIKE '%"+ 
284
                                        key.toUpperCase() +"%') " +
285
                                        "OR (iau_proj LIKE '%" + key2+key3 +"%') OR " +
286
                                                        "(iau_geog LIKE '%" + key + "%') OR (iau_geog LIKE '%"+ 
287
                                        key.toUpperCase() +"%') " +
288
                                        "OR (iau_geog LIKE '%" + key2+key3 +"%')";
289
                                                                
290
                                result = Query.select(sentence,connect.getConnection());        
291
                                                                
292
                                Object[] data = new Object[4];
293
                                try {
294
                                        while (result.next()){
295
                                                data[0]        = result.getString("iau_code");
296
                                                data[1] = result.getString("iau_wkt");
297
                                                String proj = result.getString("iau_proj");
298
                                                if (!proj.equals("")){
299
                                                        data[1] = proj;
300
                                                        data[2] = PluginServices.getText(this,"si");
301
                                                } 
302
                                                else 
303
                                                {
304
                                                        data[1] = result.getString("iau_geog");
305
                                                        data[2] = PluginServices.getText(this,"no");
306
                                                }
307
                                                
308
                                                data[3] = result.getString("iau_datum");
309
                                                dtm.addRow(data);
310
                                        }
311
                                } catch (SQLException e1) {
312
                                        e1.printStackTrace();
313
                                }
314
                        }
315
                                                
316
                        int numr = dtm.getRowCount();
317
                        if (not_numeric) {
318
                                JOptionPane.showMessageDialog(IAU2000panel.this, 
319
                                                PluginServices.getText(this,"numeric_format"), 
320
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
321
                                searchTextField.setText("");
322
                        }
323
                        else if (numr == 0){
324
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,"no_results"), "Warning...",
325
                                        JOptionPane.WARNING_MESSAGE);
326
                        }
327
                        else{
328
                                this.getJTable().setRowSelectionInterval(0,0);                                
329
                        }
330
                }                
331
        }        
332
                
333
        private JButton getSearchButton() {
334
                if (searchButton == null) {
335
                        searchButton = new JButton();
336
                        searchButton.setPreferredSize(new Dimension(75,20));
337
                        searchButton.setText(PluginServices.getText(this,"buscar"));
338
                        searchButton.setMnemonic('S');
339
                        searchButton.setToolTipText(PluginServices.getText(this,"buscar_por_criterio_seleccion"));
340
                        searchButton.addActionListener(this);                        
341
                }
342
                return searchButton;
343
        }        
344
        
345
        private JTextField getSearchTextField() {
346
                if (searchTextField == null) {
347
                        searchTextField = new JTextField();
348
                        searchTextField.setPreferredSize(new Dimension(300,20));
349
                        searchTextField.addKeyListener(this);
350
                }                
351
                return searchTextField;
352
        }
353
        
354
        public JTable getJTable() {
355
                if (jTable == null) {
356
                        String[] columnNames= {PluginServices.getText(this,"codigo"),
357
                                        PluginServices.getText(this,"nombre"),
358
                                        PluginServices.getText(this,"projected"),
359
                                        PluginServices.getText(this,"datum")};
360
                        Object[][]data = {};                        
361
                        dtm = new DefaultTableModel(data, columnNames)
362
                         {
363
                                private static final long serialVersionUID = 1L;
364
                                public boolean isCellEditable(int row, int column) {
365
                                        return false;
366
                                }
367
                                /*
368
                                 * metodo necesario para cuando utilizamos tablas ordenadas
369
                                 * ya que sino al ordenar por algun campo no se queda con el orden
370
                                 * actual al seleccionar una fila (non-Javadoc)
371
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
372
                                 */
373
                                public Class getColumnClass(int column)
374
                                {
375
                                        return getValueAt(0, column).getClass();
376
                                }
377
                                };
378
                        sorter = new TableSorter(dtm);                        
379

    
380
                        jTable = new JTable(sorter);
381
                        sorter.setTableHeader(jTable.getTableHeader());                        
382
                        jTable.setCellSelectionEnabled(false);
383
                        jTable.setRowSelectionAllowed(true);
384
                        jTable.setColumnSelectionAllowed(false);
385
                        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
386
                        TableColumn column = null;
387
                        for (int i = 0; i < 4; i++) {
388
                            column = jTable.getColumnModel().getColumn(i);
389
                            if (i == 0) {
390
                                column.setPreferredWidth(80); //code column is shorter                                     
391
                            }else if (i ==2) {
392
                                    column.setPreferredWidth(50);
393
                            } else {
394
                                    column.setPreferredWidth(175);
395
                            }
396
                            
397
                        }                        
398
        }
399
                return jTable;
400
                
401
        }
402
        
403
        public void setCodeCRS(int code) {
404
                codeCRS = code;
405
        }
406
        
407
        public int getCodeCRS() {
408
                return codeCRS;
409
        }        
410
        
411
        private JScrollPane getJScrollPane() {
412
                if (jScrollPane == null) {
413
                        jScrollPane = new JScrollPane();
414
                        jScrollPane.setPreferredSize(new Dimension(500,150));
415
                        /*jScrollPane.setBorder(
416
                                    BorderFactory.createCompoundBorder(
417
                                        BorderFactory.createCompoundBorder(
418
                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"IAU2000")),
419
                                                        BorderFactory.createEmptyBorder(5,5,5,5)),
420
                                                        jScrollPane.getBorder()));*/
421
                        jScrollPane.setBorder(
422
                                    BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(3,3,3,3),jScrollPane.getBorder()));
423
                        
424
                        jScrollPane.setViewportView(getJTable());
425
                }
426
                return jScrollPane;
427
        }
428
        
429
        public ICrs getProjection() {
430
                try {
431
                        String txt = getWKT();                        
432
                        ICrs crs = new CrsFactory().getCRS(getCodeCRS(), txt); 
433
                        return crs ;
434
                } catch (CrsException e) {
435
                        e.printStackTrace();
436
                }
437
                return null;
438
        }
439
        
440
        /**
441
         * Consigue la cadena wkt del CRS seleccionado, y genera la cadena que m?s
442
         * tarde volver? a ser tratada para la consecuci?n de una cadena wkt
443
         * legible por la proj4.
444
         *
445
         */
446
        public void setWKT(){
447
                int code = getCodeCRS();
448
                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
449
                                                  "FROM IAU2000 " +                                      
450
                          "WHERE iau_code = " + code;
451
                
452
                
453
                ResultSet result = Query.select(sentence,connect.getConnection());        
454
        
455
                try {
456
                        result.next();                        
457
                        cadWKT = result.getString("iau_wkt");                        
458
                } catch (SQLException e1) {
459
                        e1.printStackTrace();
460
                }                
461
                cadWKT = cadWKT.substring(0, cadWKT.length()-1) + ", AUTHORITY[\"IAU2000\","+ getCodeCRS()+"]]";
462
                if (cadWKT.charAt(0) == 'P'){
463
                        Iau2wkt wk = new Iau2wkt(cadWKT);
464
                        cadWKT = wk.getWkt();
465
                }
466
        }
467
        
468
        public String getWKT(){
469
                return cadWKT;
470
        }
471
        
472
        public void setProjection(IProjection crs) {
473
                //setCrs((ICrs) crs);
474
        }
475
        
476
        /**
477
         * Metodo para cargar en el CRS de la capa el CRS de la vista.
478
         * Se utilizar? a la hora de arrancar la definici?n del CRS de la nueva capa
479
         */
480
        public void loadViewCRS(int code){
481
                connection();                
482
                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
483
                                                  "FROM IAU2000 " +                                      
484
                          "WHERE iau_code = " + code;
485
                                
486
                ResultSet result = Query.select(sentence,connect.getConnection());        
487
                                
488
                Object[] data = new Object[4];
489
                try {
490
                        while (result.next()){
491
                                data[0]        = result.getString("iau_code");
492
                                data[1] = result.getString("iau_wkt");
493
                                String proj = result.getString("iau_proj");
494
                                if (!proj.equals("")){
495
                                        data[1] = proj;
496
                                        data[2] = PluginServices.getText(this,"si");
497
                                } 
498
                                else 
499
                                {
500
                                        data[1] = result.getString("iau_geog");
501
                                        data[2] = PluginServices.getText(this,"no");
502
                                }
503
                                
504
                                data[3] = result.getString("iau_datum");
505
                                dtm.addRow(data);
506
                        }
507
                } catch (SQLException e1) {
508
                        e1.printStackTrace();
509
                }
510
        }
511

    
512
        public void keyPressed(KeyEvent e) {
513
                if (e.getSource() == this.getSearchTextField()){
514
                        if (e.getKeyCode() == 10) {
515
                                searchButton();
516
                        }                        
517
                }                
518
        }
519

    
520
        public void keyReleased(KeyEvent arg0) {
521
        
522
        }
523

    
524
        public void keyTyped(KeyEvent arg0) {
525
        
526
        }
527

    
528
        /**
529
         * Maneja los eventos de los botones y los radioButtons del panel
530
         * del repositorio IAU2000.
531
         */
532
        public void actionPerformed(ActionEvent e) {
533
                if (e.getSource() == this.getSearchButton()){
534
                        searchTextField.setBackground(Color.white);
535
                        if (searchTextField.getText().equals("")) {
536
                                searchTextField.setBackground(new Color(255,204,204));
537
                                JOptionPane.showMessageDialog(IAU2000panel.this, 
538
                                                PluginServices.getText(this,"fill_name"), 
539
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
540
                        }
541
                        else {
542
                                searchButton();
543
                        }
544
                }
545
                if (e.getSource() == this.getCodeRadioButton()) {
546
                        searchTextField.setText("");
547
                }
548
                
549
                if (e.getSource() == this.getNameRadioButton()) {
550
                        searchTextField.setText("");
551
                
552
                }                
553
        }
554

    
555
}