Statistics
| Revision:

root / trunk / libraries / libJCRS / src / org / gvsig / crs / ui / CRSSelectionPanel.java @ 6855

History | View | Annotate | Download (17.1 KB)

1
package org.gvsig.crs.ui;
2

    
3
import java.awt.Color;
4
import java.awt.Dimension;
5
import java.awt.FlowLayout;
6
import java.awt.GridLayout;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.KeyEvent;
9
import java.awt.event.KeyListener;
10
import java.sql.ResultSet;
11
import java.sql.SQLException;
12

    
13
import javax.swing.BorderFactory;
14
import javax.swing.JButton;
15
import javax.swing.JLabel;
16
import javax.swing.JOptionPane;
17
import javax.swing.JPanel;
18
import javax.swing.JRadioButton;
19
import javax.swing.JScrollPane;
20
import javax.swing.JTable;
21
import javax.swing.JTextField;
22
import javax.swing.ListSelectionModel;
23
import javax.swing.event.ListSelectionEvent;
24
import javax.swing.event.ListSelectionListener;
25
import javax.swing.table.DefaultTableModel;
26
import javax.swing.table.TableColumn;
27

    
28
import org.cresques.cts.IProjection;
29
import org.gvsig.crs.CrsException;
30
import org.gvsig.crs.CrsFactory;
31
import org.gvsig.crs.ICrs;
32
import org.gvsig.crs.Query;
33
import org.gvsig.crs.ogr.Epsg2wkt;
34
import org.gvsig.crs.ogr.GetCRSepsg;
35

    
36
import com.iver.andami.PluginServices;
37
import com.iver.andami.ui.mdiManager.View;
38
import com.iver.andami.ui.mdiManager.ViewInfo;
39
import com.iver.cit.gvsig.gui.panels.ProjChooserPanel;
40

    
41
public class CRSSelectionPanel extends JPanel {
42
        /**
43
         * 
44
         */
45
        private static final long serialVersionUID = 1L;
46
        JLabel jLabel = null;
47
        JTextField crsTextFld = null;        
48
        
49
        ProjChooserPanel pcp;
50
        
51
        int transf = 0;
52
        int epsg_code = 0;
53
        boolean source_yn = false;
54
        int source_cod = 0;
55
        int method_code = 0;
56
        int projection_conv_code = 0;
57
        int[] valid_method_code = {9800, 9801, 9802, 9803, 9804, 9805, 9806, 9807, 9808, 9809, 9810, 
58
                        9811, 9812, 9813, 9814, 9815, 9816, 9817};
59
        boolean select = false;
60
        
61
        private JRadioButton codeRadioButton = null;
62
        private JRadioButton nameRadioButton = null;
63
        private JRadioButton areaRadioButton = null;
64
        private JPanel groupRadioButton = null;
65
        
66
        private JButton infoCrs = null;
67
        private JButton searchButton = null;
68
        private JTextField searchTextField = null;
69
        private JTable jTable = null;
70
        private JScrollPane jScrollPane = null;
71
        public DefaultTableModel dtm = null;
72
        public DefaultTableModel dtm2 = null;
73
        public String key;
74
        public int selectedRowTable = -1;
75
        public int selectedRowTable2 = -1;        
76
        
77
        private int codeCRS = -1;        
78
        
79
        public CRSSelectionPanel() {                
80
                super();        
81
                pcp = new ProjChooserPanel();
82
                initialize();
83
        }
84
        
85
        private void initialize() {        
86
                getJPanel();                
87
        }
88
        
89
        private void getJPanel() {                        
90
                        this.add(getSearchButton(), null);
91
                        this.add(getSearchTextField(), null);
92
                        this.add(getInfoCrs(), null);
93
                        this.add(getGroupRadioButton(), null);
94
                        this.add(getJScrollPane(), null);                                                        
95
        }
96
        
97
        private void codeRadioButton_actionPerformed(ActionEvent e) {
98
                searchTextField.setText("");
99
                nameRadioButton.setSelected(false);
100
                areaRadioButton.setSelected(false);
101
        }
102
            
103
        private JRadioButton getCodeRadioButton() {
104
                if (codeRadioButton == null) {
105
                        codeRadioButton = new JRadioButton();
106
                        codeRadioButton.setText("By Code EPSG");
107
                        codeRadioButton.setSelected(true);
108
                        codeRadioButton.addActionListener(new java.awt.event.ActionListener() { 
109
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
110
                                        codeRadioButton_actionPerformed(e);
111
                                }
112
                        });
113
                }
114
                return codeRadioButton;
115
        }
116
        
117
        private void nameRadioButton_actionPerformed(ActionEvent e) {
118
                searchTextField.setText("");
119
                codeRadioButton.setSelected(false);
120
                areaRadioButton.setSelected(false);
121
        }
122
          
123
        private JRadioButton getNameRadioButton() {
124
                if (nameRadioButton == null) {
125
                        nameRadioButton = new JRadioButton();
126
                        nameRadioButton.setText("By CRS Name");
127
                        nameRadioButton.addActionListener(new java.awt.event.ActionListener() { 
128
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
129
                                        nameRadioButton_actionPerformed(e);
130
                                }
131
                        });
132
                }
133
                return nameRadioButton;
134
        }
135
        
136
        private void areaRadioButton_actionPerformed(ActionEvent e) {
137
                searchTextField.setText("");
138
                codeRadioButton.setSelected(false);
139
                nameRadioButton.setSelected(false);
140
        }
141
  
142
        private JRadioButton getAreaRadioButton() {
143
                if (areaRadioButton == null) {
144
                        areaRadioButton = new JRadioButton();
145
                        areaRadioButton.setText("By Area of Use");
146
                        areaRadioButton.addActionListener(new java.awt.event.ActionListener() { 
147
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
148
                                        areaRadioButton_actionPerformed(e);
149
                                }
150
                        });
151
                }
152
                return areaRadioButton;
153
        }
154
         
155
        private JPanel getGroupRadioButton() {
156
                if (groupRadioButton == null) {
157
                        groupRadioButton = new JPanel();
158
                        groupRadioButton.setLayout(new GridLayout(1,0));
159
                        groupRadioButton.setPreferredSize(new Dimension(400,50));
160
                        groupRadioButton.add(getCodeRadioButton());
161
                        groupRadioButton.add(getNameRadioButton());
162
                        groupRadioButton.add(getAreaRadioButton());
163
                }
164
                return groupRadioButton;
165
        }
166

    
167
        private void searchButton_actionPerformed(ActionEvent e) {
168
                searchTextField.setBackground(Color.white);
169
                
170
                if (searchTextField.getText().equals("")) {
171
                        searchTextField.setBackground(new Color(255,204,204));
172
                        JOptionPane.showMessageDialog(this, "Fill the name of CRS.", "Warning...", JOptionPane.WARNING_MESSAGE);
173
                }
174
                
175
                else {
176
            //Eliminar filas en cada nueva bsqueda
177
                        int numRow = dtm.getRowCount();
178
                        while (numRow != 0) {
179
                                numRow = numRow - 1;
180
                                dtm.removeRow(numRow);
181
                        }
182
                        
183
                        //Dependiendo de la opcion se realizada una busqueda
184
                        ResultSet result = null;
185
                        ResultSet result2 = null;
186
                        /*
187
                         * variable que indicara si la busqueda se hace primero en epsg_coordinatereferencesystem
188
                         * o en epsg_area; esto es debido a que HSQLDB no soporta la b?squeda simult?nea en ambas
189
                         * tablas, por lo cual hay que separar la b?squeda
190
                         */
191
                        int bus = 2;                          
192
                        if (codeRadioButton.isSelected()) {
193
                                bus=0;
194
                                key = searchTextField.getText();
195
                                int code = Integer.parseInt(key);
196
                                String sentence = "SELECT coord_ref_sys_code, coord_ref_sys_name, coord_ref_sys_kind, area_of_use_code, " +
197
                                                                  "source_geogcrs_code, projection_conv_code  " +
198
                                                                  "FROM epsg_coordinatereferencesystem " +                                      
199
                                      "WHERE coord_ref_sys_code = " + code;
200
                                result = Query.select(sentence,pcp.connect.getConnection());        
201
                        }
202
                        
203
                        else if (nameRadioButton.isSelected()) {
204
                                bus=0;
205
                                key = searchTextField.getText();
206
                                String key2 = key.substring(0,1);
207
                                String key3 = key.substring(1,key.length());
208
                                key2 = key2.toUpperCase();
209
                                
210
                                String sentence = "SELECT coord_ref_sys_code, coord_ref_sys_name, coord_ref_sys_kind, " +
211
                                                                        "area_of_use_code, source_geogcrs_code, projection_conv_code " +
212
                                                                          "FROM epsg_coordinatereferencesystem " +                                      
213
                                                                          "WHERE (coord_ref_sys_name LIKE '%" + key + "%') OR (coord_ref_sys_name LIKE '%"+ 
214
                                                                          key.toUpperCase() +"%') " +
215
                                                                          "OR (coord_ref_sys_name LIKE '%" + key2+key3 +"%')";
216
                                result = Query.select(sentence,pcp.connect.getConnection());
217
                        }
218
                        
219
                        else {
220
                                bus=1;
221
                                key = searchTextField.getText();
222
                                String key2 = key.substring(0,1);
223
                                String key3 = key.substring(1,key.length());
224
                                key2 = key2.toUpperCase();                                
225
                                
226
                                String sentence = "SELECT area_name, area_of_use, area_code " +
227
                                      "FROM epsg_area " +
228
                                      "WHERE (area_name LIKE '%" + key + "%') OR (area_of_use LIKE '%" + key + "%') "+
229
                                      "OR (area_name LIKE '%" + key.toUpperCase() + "%') OR (area_of_use LIKE '%" + key.toUpperCase() + "%') "+
230
                                      "OR (area_name LIKE '%" + key2+key3 + "%') OR (area_of_use LIKE '%" + key2+key3 + "%') ";
231
                                result = Query.select(sentence,pcp.connect.getConnection());                                
232
                        }
233
                        String crs_kind = null;
234
                        if (bus==0){
235
                                try {
236
                                        while(result.next()) {
237
                                                Object[]data = new Object[5];
238
                                                data[0] = String.valueOf(result.getInt("coord_ref_sys_code"));
239
                                                data[1] = result.getString("coord_ref_sys_name");
240
                                                crs_kind = result.getString("coord_ref_sys_kind");
241
                                                data[2] = crs_kind;
242
                                                projection_conv_code = result.getInt("projection_conv_code");
243
                                                
244
                                                int area_of_use_code = Integer.parseInt(result.getString("area_of_use_code"));
245
                                                                                
246
                                                String sentence = "SELECT area_name, area_of_use FROM epsg_area " +
247
                                                                                "WHERE area_code = "+ area_of_use_code ;
248
                                                
249
                                                result2 = Query.select(sentence,pcp.connect.getConnection());
250
                                                while(result2.next()){
251
                                                        data[3] = result2.getString("area_name");
252
                                                        data[4] = result2.getString("area_of_use");                                        
253
                                                }
254
                                                if (data[0]!=null && validCRS(projection_conv_code) && (crs_kind.equals("projected")
255
                                                                || crs_kind.equals("geographic 2D"))){
256
                                                        dtm.addRow(data);
257
                                                }
258
                                        }
259
                                } 
260
                                
261
                                catch (SQLException e1) {
262
                                        e1.printStackTrace();
263
                                }
264
                        }
265
                        else if (bus==1){
266
                                try {
267
                                        while(result.next()) {
268
                                                Object[]data = new Object[5];
269
                                                data[3] = result.getString("area_name");
270
                                                data[4] = result.getString("area_of_use");        
271
                                                                                        
272
                                                int area_of_use_code = Integer.parseInt(result.getString("area_code"));
273
                                                                                
274
                                                String sentence = "SELECT coord_ref_sys_code, coord_ref_sys_name, " +
275
                                                                                "coord_ref_sys_kind, source_geogcrs_code, projection_conv_code " +
276
                                                                                "FROM epsg_coordinatereferencesystem " +
277
                                                                                "WHERE area_of_use_code = "+ area_of_use_code ;
278
                                                
279
                                                result2 = Query.select(sentence,pcp.connect.getConnection());
280
                                                while(result2.next()){
281
                                                        data[0] = String.valueOf(result2.getInt("coord_ref_sys_code"));
282
                                                        data[1] = result2.getString("coord_ref_sys_name");
283
                                                        data[2] = result2.getString("coord_ref_sys_kind");
284
                                                        crs_kind = (String)data[2];
285
                                                        projection_conv_code = result2.getInt("projection_conv_code");
286
                                                                                
287
                                                }
288
                                                /*
289
                                                 * Buscaremos solo aquellos CRS cuyas proyecciones esten entre las
290
                                                 * 16 soportadas por proj4 para ello creamos un metodo para validar
291
                                                 * si esta entre estas proyecciones
292
                                                 */                                                
293
                                                if (data[0]!=null && validCRS(projection_conv_code) && (crs_kind.equals("projected")
294
                                                                || crs_kind.equals("geographic 2D"))){
295
                                                        dtm.addRow(data);
296
                                                }                                                
297
                                        }
298
                                } 
299
                                
300
                                catch (SQLException e1) {
301
                                        e1.printStackTrace();
302
                                }
303
                        }
304
                        int numr = dtm.getRowCount();
305
                        if (numr == 0 ){
306
                        JOptionPane.showMessageDialog(this, "Not results find.", "Warning...",
307
                                        JOptionPane.WARNING_MESSAGE);
308
                        }                        
309
                }
310
        }
311
        
312
        /*
313
         * metodo auxiliar que nos servira para comprobar si la proyeccion del CRS seleccionado
314
         * esta entre las 16 con las que trabaja proj4
315
         */
316
        private boolean validCRS(int projection_conv_code2) {
317
                if (projection_conv_code2 == 0) return true;
318
                String sentence = "SELECT coord_op_method_code " +
319
                                                        "FROM epsg_coordoperation " +
320
                                                        "WHERE coord_op_code = " + projection_conv_code;
321
                ResultSet result = Query.select(sentence,pcp.connect.getConnection());
322
                try {
323
                        while (result.next()){
324
                                method_code = result.getInt("coord_op_method_code");
325
                        }
326
                } catch (SQLException e) {
327
                        e.printStackTrace();
328
                }
329
                
330
                for (int i = 0; i< valid_method_code.length; i++){
331
                        if (method_code == valid_method_code[i] ){
332
                                return true;
333
                        }
334
                }                
335
                return false;
336
        }
337

    
338
        private JButton getSearchButton() {
339
                if (searchButton == null) {
340
                        searchButton = new JButton();
341
                        searchButton.setPreferredSize(new Dimension(75,20));
342
                        searchButton.setText("Search");
343
                        searchButton.setMnemonic('S');
344
                        searchButton.setToolTipText("Search of a CRS by selected criterion");
345
                        searchButton.addActionListener(new java.awt.event.ActionListener() { 
346
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
347
                                        searchTextField.setBackground(Color.white);
348
                                        if (searchTextField.getText().equals("")) {
349
                                                searchTextField.setBackground(new Color(255,204,204));
350
                                                JOptionPane.showMessageDialog(CRSSelectionPanel.this, 
351
                                                                "Fill the key-word of the search.", 
352
                                                                "Warning...", JOptionPane.WARNING_MESSAGE);
353
                                        }
354
                                        else {
355
                                                searchButton_actionPerformed(e);
356
                                        }
357
                                }
358
                        });                        
359
                }
360
                return searchButton;
361
        }        
362
        
363
        private JTextField getSearchTextField() {
364
                if (searchTextField == null) {
365
                        searchTextField = new JTextField();
366
                        searchTextField.setPreferredSize(new Dimension(300,20));
367
                }
368
                searchTextField.addKeyListener(new KeyListener() {
369
                        public void keyPressed(KeyEvent e) {                                
370
                                if (e.getKeyCode() == 10) {
371
                                        searchButton_actionPerformed(null);
372
                                }
373
                                if (codeRadioButton.isSelected()){
374
                                        if ((e.getKeyCode() >= 48 && e.getKeyCode() <= 57) || e.getKeyCode() >= 96 && e.getKeyCode() <= 105 || 
375
                                                        (e.getKeyCode() >= 0 && e.getKeyCode()<= 31) || e.getKeyCode() == 127){                                                
376
                                        }
377
                                        else {
378
                                                JOptionPane.showMessageDialog(CRSSelectionPanel.this, 
379
                                                                "Only number format.", 
380
                                                                "Warning...", JOptionPane.WARNING_MESSAGE);
381
                                                String aux = searchTextField.getText();
382
                                                if (aux.length() == 1){
383
                                                        searchTextField.setText("");
384
                                                }
385
                                                else {
386
                                                        searchTextField.setText(aux.substring(0, aux.length()-1));
387
                                                }
388
                                        }
389
                                }
390
                        }
391

    
392
                        public void keyReleased(KeyEvent arg0) {                                
393
                        }
394

    
395
                        public void keyTyped(KeyEvent arg0) {
396
                        }
397
                });
398
                return searchTextField;
399
        }
400
        
401
        private JTable getJTable() {
402
                if (jTable == null) {
403
                        String[] columnNames= {"Code of CRS","Name of CRS","Type of CRS","Name of Area","Description of Area"};
404
                        Object[][]data = {};                        
405
                        dtm = new DefaultTableModel(data, columnNames);
406
                        jTable = new JTable(dtm);
407
                        jTable.setCellSelectionEnabled(false);
408
                        jTable.setRowSelectionAllowed(true);
409
                        jTable.setColumnSelectionAllowed(false);
410
                        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
411
                        TableColumn column = null;
412
                        for (int i = 0; i < 5; i++) {
413
                            column = jTable.getColumnModel().getColumn(i);
414
                            if (i == 0) {
415
                                column.setPreferredWidth(40); //code column is shorter                                     
416
                            }else if (i == 2) {
417
                                    column.setPreferredWidth(80);
418
                            }
419
                            else {                            
420
                                column.setPreferredWidth(160);
421
                            }
422
                        }
423
                        ListSelectionModel rowSM = jTable.getSelectionModel();
424
                        rowSM.addListSelectionListener(new ListSelectionListener() {
425
                            public void valueChanged(ListSelectionEvent e) {
426
                                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
427
                                if (lsm.isSelectionEmpty()) {
428
                                        selectedRowTable = -1;
429
                                        setCodeCRS(-1);
430
                                } 
431
                                else {
432
                                    selectedRowTable = lsm.getMinSelectionIndex();
433

    
434
                                    setCodeCRS(Integer.parseInt((String)dtm.getValueAt(selectedRowTable,0)));                                 
435
                                }
436
                                infoCrs.setEnabled(true);
437
                            }
438
                });
439
        }
440
                return jTable;
441
                
442
        }
443
        
444
        private void setCodeCRS(int code) {
445
                codeCRS = code;
446
        }
447
        
448
        public int getCodeCRS() {
449
                return codeCRS;
450
        }        
451
        
452
        private JScrollPane getJScrollPane() {
453
                if (jScrollPane == null) {
454
                        jScrollPane = new JScrollPane();
455
                        jScrollPane.setPreferredSize(new Dimension(600,250));
456
                        jScrollPane.setBorder(
457
                                    BorderFactory.createCompoundBorder(
458
                                        BorderFactory.createCompoundBorder(
459
                                                        BorderFactory.createTitledBorder("Coordinate Reference Systems"),
460
                                                        BorderFactory.createEmptyBorder(5,5,5,5)),
461
                                                        jScrollPane.getBorder()));
462
                        jScrollPane.setViewportView(getJTable());
463
                }
464
                return jScrollPane;
465
        }
466
        
467
        private JButton getInfoCrs() {
468
                if(infoCrs == null) {
469
                        infoCrs = new JButton();
470
                        infoCrs.setText("InfoCrs");
471
                        infoCrs.setPreferredSize(new Dimension(75,20));
472
                        infoCrs.setMnemonic('I');
473
                        infoCrs.setEnabled(false);
474
                        infoCrs.setToolTipText("Mostrar la informacion del Crs selecionado");
475
                        infoCrs.addActionListener(new java.awt.event.ActionListener() { 
476
                                public void actionPerformed(java.awt.event.ActionEvent e) {    
477
                                        infoCrs_actionPerformed(e);                
478
                                }        
479
                        });
480
                }
481
                return infoCrs;
482
        }
483
        
484
        private void infoCrs_actionPerformed(ActionEvent e) {
485
                InfoCrs info = new InfoCrs(getCrs());
486
                info.setSize(new Dimension(550,350));
487
                info.setLayout(new GridLayout(0,1));
488
                info.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
489
                PluginServices.getMDIManager().addView(info);
490
        }
491
        
492
        public ICrs getProjection() {
493
                return getCrs();
494
        }
495
        public ICrs getCrs() {
496
                Epsg2wkt wkt = null;
497
                
498
                epsg_code = getCodeCRS();
499
                if (epsg_code != -1){                
500
                        /*
501
                         * ahora que he escogido, recojo toda la informacion que me va a hacer falta
502
                         */
503
                        String sentence = "SELECT source_geogcrs_code, projection_conv_code " +
504
                                                        "FROM epsg_coordinatereferencesystem " +
505
                                                        "WHERE coord_ref_sys_code = "+ epsg_code ;
506
                        ResultSet result = Query.select(sentence,pcp.connect.getConnection());
507
                        
508
                        try {
509
                                result.next();
510
                                source_cod = result.getInt("source_geogcrs_code");
511
                                projection_conv_code = result.getInt("projection_conv_code");
512
                        } catch (SQLException e1) {
513
                                e1.printStackTrace();
514
                        }
515
                        
516
                        if (source_cod != 0){
517
                                source_yn = false;
518
                        }
519
                        else source_yn = true;
520
                        
521
                        GetCRSepsg ep = new GetCRSepsg(epsg_code, source_yn, source_cod, projection_conv_code, pcp);
522
                        ep.Getepsgdata();
523
                        
524
                        if (source_yn){
525
                                wkt = new Epsg2wkt(ep ,0);                        
526
                        }
527
                        else {
528
                                wkt = new Epsg2wkt(ep);
529
                        }                                
530
                }
531
                else {
532
                        JOptionPane.showMessageDialog(this, 
533
                                        "No CRS selected.", 
534
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
535
                        return null;
536
                }        
537
                try {
538
                        String txt = wkt.getWKT();
539
                        ICrs crs = new CrsFactory().getCRS(epsg_code, txt); 
540
                        return crs ;
541
                } catch (CrsException e) {
542
                        e.printStackTrace();
543
                }
544
                return null;
545
        }
546
        
547
        public void setProjection(IProjection crs) {
548
                //setCrs((ICrs) crs);
549
        }
550
}