Statistics
| Revision:

root / trunk / libraries / libJCRS / src / org / gvsig / crs / gui / panels / IAU2000panel.java @ 8404

History | View | Annotate | Download (13.5 KB)

1
package org.gvsig.crs.gui.panels;
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.ActionListener;
9
import java.awt.event.KeyEvent;
10
import java.awt.event.KeyListener;
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13

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

    
27
import org.cresques.cts.IProjection;
28
import org.gvsig.crs.CrsException;
29
import org.gvsig.crs.CrsFactory;
30
import org.gvsig.crs.EpsgConnection;
31
import org.gvsig.crs.ICrs;
32
import org.gvsig.crs.Query;
33
import org.gvsig.crs.ogr.iau2wkt;
34

    
35
import com.iver.andami.PluginServices;
36
import com.iver.cit.gvsig.gui.TableSorter;
37

    
38
public class IAU2000panel extends JPanel implements KeyListener, ActionListener {
39

    
40
        /**
41
         * 
42
         */
43
        private static final long serialVersionUID = 1L;
44
        public JPanel IAU2000panel = null;
45
        
46
        private JRadioButton codeRadioButton = null;
47
        private JRadioButton nameRadioButton = null;        
48
        private JPanel groupRadioButton = null;
49
        
50
        JLabel jLabel = null;
51
        JTextField crsTextFld = null;        
52
        
53
        String cadWKT = "";
54
        
55
        int transf = 0;        
56
        boolean source_yn = false;
57
        int source_cod = 0;
58
        int method_code = 0;
59
        int datum_code = 0;
60
        int projection_conv_code = 0;
61
        public String crs_kind = null;
62
        
63
        public TableSorter sorter = null;
64
        
65
        public EpsgConnection connect = null;
66
        
67
        private JButton searchButton = null;
68
        private JTextField searchTextField = null;
69
        public JTable jTable = null;
70
        private JScrollPane jScrollPane = null;
71
        public DefaultTableModel dtm = null;
72
        public String key;
73
        public int selectedRowTable = -1;        
74
        
75
        private int codeCRS = -1;        
76
        
77
        public IAU2000panel() {                
78
                initialize();
79
        }
80
        
81
        private void initialize(){
82
                this.setLayout(new GridLayout(2,3));
83
                this.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
84
                this.add(getGroupRadioButton(), null);
85
                this.add(getSearchButton(), null);
86
                this.add(getSearchTextField(), null);                
87
                this.add(getJScrollPane(), null);        
88
        }
89
        
90
        public void connection(){
91
                connect = new EpsgConnection();
92
                
93
                
94
        }
95
        
96
        private JRadioButton getCodeRadioButton() {
97
                if (codeRadioButton == null) {
98
                        codeRadioButton = new JRadioButton();
99
                        codeRadioButton.setText(PluginServices.getText(this,"por_codigo"));//setText("By Code EPSG");
100
                        codeRadioButton.setSelected(true);
101
                        codeRadioButton.addActionListener(this);
102
                }
103
                return codeRadioButton;
104
        }
105
                  
106
        private JRadioButton getNameRadioButton() {
107
                if (nameRadioButton == null) {
108
                        nameRadioButton = new JRadioButton();
109
                        nameRadioButton.setText(PluginServices.getText(this,"por_nombre"));
110
                        nameRadioButton.addActionListener(this);
111
                }
112
                return nameRadioButton;
113
        }
114
                 
115
        private JPanel getGroupRadioButton() {
116
                if (groupRadioButton == null) {
117
                        groupRadioButton = new JPanel();
118
                        groupRadioButton.setLayout(new GridLayout(1,0));
119
                        groupRadioButton.setPreferredSize(new Dimension(500,30));
120
                        groupRadioButton.add(getLabel());
121
                        groupRadioButton.add(getCodeRadioButton());
122
                        groupRadioButton.add(getNameRadioButton());                        
123
                }
124
                return groupRadioButton;
125
        }
126
        
127
        private JLabel getLabel(){
128
                JLabel criterio = new JLabel();
129
                criterio.setPreferredSize(new Dimension(100, 20));
130
                criterio.setText(PluginServices.getText(this, "criterio_busqueda"));
131
                return criterio;
132
        }
133
        
134
        private void searchButton() {                
135
                searchTextField.setBackground(Color.white);
136
                
137
                if (searchTextField.getText().equals("")) {
138
                        searchTextField.setBackground(new Color(255,204,204));
139
                        JOptionPane.showMessageDialog(this, "Fill the name of CRS.", "Warning...", JOptionPane.WARNING_MESSAGE);
140
                }
141
                
142
                else {
143
            //Eliminar filas en cada nueva bsqueda
144
                        int numRow = dtm.getRowCount();
145
                        while (numRow != 0) {
146
                                numRow = numRow - 1;
147
                                dtm.removeRow(numRow);
148
                        }
149
                        
150
                        //Dependiendo de la opcion se realizada una busqueda
151
                        ResultSet result = null;
152
                                                                                          
153
                        if (codeRadioButton.isSelected()) {
154
                                        
155
                                key = searchTextField.getText();
156
                                int code = Integer.parseInt(key);
157
                                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
158
                                                                  "FROM IAU2000 " +                                      
159
                                      "WHERE iau_code = " + code;
160
                                
161
                                connect.setConnectionIAU2000();        
162
                                result = Query.select(sentence,connect.getConnection());        
163
                                try {
164
                                        connect.shutdown();
165
                                } catch (SQLException e) {
166
                                        // TODO Auto-generated catch block
167
                                        e.printStackTrace();
168
                                }
169
                                
170
                                Object[] data = new Object[4];
171
                                try {
172
                                        while (result.next()){
173
                                                data[0]        = result.getString("iau_code");
174
                                                data[1] = result.getString("iau_wkt");
175
                                                String proj = result.getString("iau_proj");
176
                                                if (!proj.equals("")){
177
                                                        data[1] = proj;
178
                                                        data[2] = PluginServices.getText(this,"si");
179
                                                } 
180
                                                else 
181
                                                {
182
                                                        data[1] = result.getString("iau_geog");
183
                                                        data[2] = PluginServices.getText(this,"no");
184
                                                }
185
                                                
186
                                                data[3] = result.getString("iau_datum");
187
                                                dtm.addRow(data);
188
                                        }
189
                                } catch (SQLException e1) {
190
                                        e1.printStackTrace();
191
                                }
192
                        }
193
                        else if (nameRadioButton.isSelected()) {
194
                                key = searchTextField.getText();
195
                                String key2 = key.substring(0,1);
196
                                String key3 = key.substring(1,key.length());
197
                                key2 = key2.toUpperCase();
198
                                
199
                                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
200
                                        "FROM IAU2000 " +                                      
201
                                        "WHERE (iau_proj LIKE '%" + key + "%') OR (iau_proj LIKE '%"+ 
202
                                        key.toUpperCase() +"%') " +
203
                                        "OR (iau_proj LIKE '%" + key2+key3 +"%') OR " +
204
                                                        "(iau_geog LIKE '%" + key + "%') OR (iau_geog LIKE '%"+ 
205
                                        key.toUpperCase() +"%') " +
206
                                        "OR (iau_geog LIKE '%" + key2+key3 +"%')";
207
                                
208
                                connect.setConnectionIAU2000();        
209
                                result = Query.select(sentence,connect.getConnection());        
210
                                try {
211
                                        connect.shutdown();
212
                                } catch (SQLException e) {
213
                                        // TODO Auto-generated catch block
214
                                        e.printStackTrace();
215
                                }
216
                                
217
                                Object[] data = new Object[4];
218
                                try {
219
                                        while (result.next()){
220
                                                data[0]        = result.getString("iau_code");
221
                                                data[1] = result.getString("iau_wkt");
222
                                                String proj = result.getString("iau_proj");
223
                                                if (!proj.equals("")){
224
                                                        data[1] = proj;
225
                                                        data[2] = PluginServices.getText(this,"si");
226
                                                } 
227
                                                else 
228
                                                {
229
                                                        data[1] = result.getString("iau_geog");
230
                                                        data[2] = PluginServices.getText(this,"no");
231
                                                }
232
                                                
233
                                                data[3] = result.getString("iau_datum");
234
                                                dtm.addRow(data);
235
                                        }
236
                                } catch (SQLException e1) {
237
                                        e1.printStackTrace();
238
                                }
239
                        }
240
                                                
241
                        int numr = dtm.getRowCount();
242
                        
243
                        if (numr == 0 ){
244
                        JOptionPane.showMessageDialog(this, "Not Results found.", "Warning...",
245
                                        JOptionPane.WARNING_MESSAGE);
246
                        }
247
                        else{
248
                                this.getJTable().setRowSelectionInterval(0,0);                                
249
                        }
250
                }                
251
        }        
252
                
253
        private JButton getSearchButton() {
254
                if (searchButton == null) {
255
                        searchButton = new JButton();
256
                        searchButton.setPreferredSize(new Dimension(75,20));
257
                        searchButton.setText(PluginServices.getText(this,"buscar"));
258
                        searchButton.setMnemonic('S');                        
259
                        searchButton.addActionListener(this);                        
260
                }
261
                return searchButton;
262
        }        
263
        
264
        private JTextField getSearchTextField() {
265
                if (searchTextField == null) {
266
                        searchTextField = new JTextField();
267
                        searchTextField.setPreferredSize(new Dimension(300,20));
268
                        searchTextField.addKeyListener(this);
269
                }                
270
                return searchTextField;
271
        }
272
        
273
        public JTable getJTable() {
274
                if (jTable == null) {
275
                        String[] columnNames= {PluginServices.getText(this,"codigo"),
276
                                        PluginServices.getText(this,"nombre"),
277
                                        PluginServices.getText(this,"projected"),
278
                                        PluginServices.getText(this,"datum")};
279
                        Object[][]data = {};                        
280
                        dtm = new DefaultTableModel(data, columnNames)
281
                         {
282
                                public boolean isCellEditable(int row, int column) {
283
                                        return false;
284
                                }
285
                                /*
286
                                 * metodo necesario para cuando utilizamos tablas ordenadas
287
                                 * ya que sino al ordenar por algun campo no se queda con el orden
288
                                 * actual al seleccionar una fila (non-Javadoc)
289
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
290
                                 */
291
                                public Class getColumnClass(int column)
292
                                {
293
                                        return getValueAt(0, column).getClass();
294
                                }
295
                                };
296
                        sorter = new TableSorter(dtm);                        
297

    
298
                        jTable = new JTable(sorter);
299
                        sorter.setTableHeader(jTable.getTableHeader());                        
300
                        jTable.setCellSelectionEnabled(false);
301
                        jTable.setRowSelectionAllowed(true);
302
                        jTable.setColumnSelectionAllowed(false);
303
                        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
304
                        TableColumn column = null;
305
                        for (int i = 0; i < 4; i++) {
306
                            column = jTable.getColumnModel().getColumn(i);
307
                            if (i == 0) {
308
                                column.setPreferredWidth(80); //code column is shorter                                     
309
                            }else if (i ==2) {
310
                                    column.setPreferredWidth(50);
311
                            } else {
312
                                    column.setPreferredWidth(175);
313
                            }
314
                            
315
                        }                        
316
        }
317
                return jTable;
318
                
319
        }
320
        
321
        public void setCodeCRS(int code) {
322
                codeCRS = code;
323
        }
324
        
325
        public int getCodeCRS() {
326
                return codeCRS;
327
        }        
328
        
329
        private JScrollPane getJScrollPane() {
330
                if (jScrollPane == null) {
331
                        jScrollPane = new JScrollPane();
332
                        jScrollPane.setPreferredSize(new Dimension(500,150));
333
                        jScrollPane.setBorder(
334
                                    BorderFactory.createCompoundBorder(
335
                                        BorderFactory.createCompoundBorder(
336
                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"IAU2000")),
337
                                                        BorderFactory.createEmptyBorder(5,5,5,5)),
338
                                                        jScrollPane.getBorder()));
339
                        jScrollPane.setViewportView(getJTable());
340
                }
341
                return jScrollPane;
342
        }
343
        
344
        public ICrs getProjection() {
345
                try {
346
                        String txt = getWKT();                        
347
                        ICrs crs = new CrsFactory().getCRS(getCodeCRS(), txt); 
348
                        return crs ;
349
                } catch (CrsException e) {
350
                        e.printStackTrace();
351
                }
352
                return null;
353
        }
354
        
355
        public void setWKT(){
356
                int code = getCodeCRS();
357
                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
358
                                                  "FROM IAU2000 " +                                      
359
                          "WHERE iau_code = " + code;
360
                
361
                connect.setConnectionIAU2000();        
362
                ResultSet result = Query.select(sentence,connect.getConnection());        
363
                try {
364
                        connect.shutdown();
365
                } catch (SQLException e) {
366
                        // TODO Auto-generated catch block
367
                        e.printStackTrace();
368
                }
369
                try {
370
                        result.next();                        
371
                        cadWKT = result.getString("iau_wkt");                        
372
                } catch (SQLException e1) {
373
                        e1.printStackTrace();
374
                }                
375
                cadWKT = cadWKT.substring(0, cadWKT.length()-1) + ", AUTHORITY[\"IAU2000\","+ getCodeCRS()+"]]";
376
                if (cadWKT.charAt(0) == 'P'){
377
                        iau2wkt wk = new iau2wkt(cadWKT);
378
                        cadWKT = wk.getWkt();
379
                }
380
        }
381
        
382
        public String getWKT(){
383
                return cadWKT;
384
        }
385
        
386
        public void setProjection(IProjection crs) {
387
                //setCrs((ICrs) crs);
388
        }
389
        
390
        public void loadViewCRS(int code){
391
                connection();                
392
                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
393
                                                  "FROM IAU2000 " +                                      
394
                          "WHERE iau_code = " + code;
395
                                
396
                connect.setConnectionIAU2000();        
397
                ResultSet result = Query.select(sentence,connect.getConnection());        
398
                try {
399
                        connect.shutdown();
400
                } catch (SQLException e) {
401
                        // TODO Auto-generated catch block
402
                        e.printStackTrace();
403
                }
404
                
405
                Object[] data = new Object[4];
406
                try {
407
                        while (result.next()){
408
                                data[0]        = result.getString("iau_code");
409
                                data[1] = result.getString("iau_wkt");
410
                                String proj = result.getString("iau_proj");
411
                                if (!proj.equals("")){
412
                                        data[1] = proj;
413
                                        data[2] = PluginServices.getText(this,"si");
414
                                } 
415
                                else 
416
                                {
417
                                        data[1] = result.getString("iau_geog");
418
                                        data[2] = PluginServices.getText(this,"no");
419
                                }
420
                                
421
                                data[3] = result.getString("iau_datum");
422
                                dtm.addRow(data);
423
                        }
424
                } catch (SQLException e1) {
425
                        e1.printStackTrace();
426
                }
427
        }
428

    
429
        public void keyPressed(KeyEvent e) {
430
                if (e.getSource() == this.getSearchTextField()){
431
                        if (e.getKeyCode() == 10) {
432
                                searchButton();
433
                        }
434
                        if (codeRadioButton.isSelected()) {
435
                                                                
436
                                if ((e.getKeyCode() >= 48 && e.getKeyCode() <= 57) || e.getKeyCode() >= 96 && e.getKeyCode() <= 105 || 
437
                                                (e.getKeyCode() >= 0 && e.getKeyCode()<= 31) || e.getKeyCode() == 127){                                                
438
                                }
439
                                else {
440
                                        JOptionPane.showMessageDialog(IAU2000panel.this, 
441
                                                        "Only number format.", 
442
                                                        "Warning...", JOptionPane.WARNING_MESSAGE);
443
                                        String aux = searchTextField.getText();
444
                                        if (aux.length() == 1 || aux.length() == 0){
445
                                                searchTextField.setText("");
446
                                        }
447
                                        else {
448
                                                searchTextField.setText(aux.substring(0, aux.length()-1));
449
                                        }
450
                                }
451
                        }
452
                }
453
                
454
        }
455

    
456
        public void keyReleased(KeyEvent arg0) {
457
                // TODO Auto-generated method stub
458
                
459
        }
460

    
461
        public void keyTyped(KeyEvent arg0) {
462
                // TODO Auto-generated method stub
463
                
464
        }
465

    
466
        public void actionPerformed(ActionEvent e) {
467
                // TODO Auto-generated method stub
468
                if (e.getSource() == this.getSearchButton()){
469
                        searchTextField.setBackground(Color.white);
470
                        if (searchTextField.getText().equals("")) {
471
                                searchTextField.setBackground(new Color(255,204,204));
472
                                JOptionPane.showMessageDialog(IAU2000panel.this, 
473
                                                "Fill the key-word of the search.", 
474
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
475
                        }
476
                        else {
477
                                searchButton();
478
                        }
479
                }
480
                if (e.getSource() == this.getCodeRadioButton()) {
481
                        searchTextField.setText("");
482
                        codeRadioButton.setSelected(true);
483
                        nameRadioButton.setSelected(false);
484
                }
485
                
486
                if (e.getSource() == this.getNameRadioButton()) {
487
                        searchTextField.setText("");
488
                        nameRadioButton.setSelected(true);
489
                        codeRadioButton.setSelected(false);
490
                }                
491
        }
492

    
493
}