Statistics
| Revision:

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

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.addActionListener(this);                        
340
                }
341
                return searchButton;
342
        }        
343
        
344
        private JTextField getSearchTextField() {
345
                if (searchTextField == null) {
346
                        searchTextField = new JTextField();
347
                        searchTextField.setPreferredSize(new Dimension(300,20));
348
                        searchTextField.addKeyListener(this);
349
                }                
350
                return searchTextField;
351
        }
352
        
353
        public JTable getJTable() {
354
                if (jTable == null) {
355
                        String[] columnNames= {PluginServices.getText(this,"codigo"),
356
                                        PluginServices.getText(this,"nombre"),
357
                                        PluginServices.getText(this,"projected"),
358
                                        PluginServices.getText(this,"datum")};
359
                        Object[][]data = {};                        
360
                        dtm = new DefaultTableModel(data, columnNames)
361
                         {
362
                                private static final long serialVersionUID = 1L;
363
                                public boolean isCellEditable(int row, int column) {
364
                                        return false;
365
                                }
366
                                /*
367
                                 * metodo necesario para cuando utilizamos tablas ordenadas
368
                                 * ya que sino al ordenar por algun campo no se queda con el orden
369
                                 * actual al seleccionar una fila (non-Javadoc)
370
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
371
                                 */
372
                                public Class getColumnClass(int column)
373
                                {
374
                                        return getValueAt(0, column).getClass();
375
                                }
376
                                };
377
                        sorter = new TableSorter(dtm);                        
378

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

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

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

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

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

    
554
}