Statistics
| Revision:

root / branches / F2 / extensions / extJCRS / src / org / gvsig / crs / gui / panels / ESRIpanel.java @ 10790

History | View | Annotate | Download (13.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.crs.gui.panels;
42
import java.awt.Color;
43
import java.awt.Dimension;
44
import java.awt.FlowLayout;
45
import java.awt.GridLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.awt.event.KeyEvent;
49
import java.awt.event.KeyListener;
50
import java.sql.ResultSet;
51
import java.sql.SQLException;
52

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

    
66
import org.cresques.cts.IProjection;
67
import org.gvsig.crs.CrsException;
68
import org.gvsig.crs.CrsFactory;
69
import org.gvsig.crs.EpsgConnection;
70
import org.gvsig.crs.ICrs;
71
import org.gvsig.crs.Query;
72

    
73
import com.iver.andami.PluginServices;
74
import com.iver.cit.gvsig.gui.TableSorter;
75

    
76

    
77
/**
78
 * Clase que genera el panel para el repositorio ESRI
79
 * 
80
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
81
 *
82
 */
83
public class ESRIpanel extends JPanel implements ActionListener, KeyListener {
84
        
85
        /**
86
         * 
87
         */
88
        private static final long serialVersionUID = 1L;        
89
        
90

    
91
        private JRadioButton codeRadioButton = null;
92
        private JRadioButton nameRadioButton = null;        
93
        private JPanel groupRadioButton = null;
94
        public TableSorter sorter = null;
95
        public JTable jTable = null;
96
        private JScrollPane jScrollPane = null;
97
        private JButton searchButton = null;
98
        private JTextField searchTextField = null;
99
        public DefaultTableModel dtm = null;
100
        private int codeCRS = -1;
101
        public String key;
102
        String cadWKT;
103
        public int selectedRowTable = -1;        
104
        
105
        public EpsgConnection connect = null;
106
        
107
        public ESRIpanel() {
108
                initialize();                
109
        }
110
        
111
        private void initialize(){
112
                this.setLayout(new GridLayout(2,3));
113
                this.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
114
                this.add(getGroupRadioButton(), null);
115
                this.add(getSearchButton(), null);
116
                this.add(getSearchTextField(), null);                
117
                this.add(getJScrollPane(), null);        
118
        }
119
        
120
        public void connection(){
121
                connect = new EpsgConnection();
122
//                connect.setConnectionEsri();                                
123
        }
124
        
125
        private JRadioButton getCodeRadioButton() {
126
                if (codeRadioButton == null) {
127
                        codeRadioButton = new JRadioButton();
128
                        codeRadioButton.setText(PluginServices.getText(this,"por_codigo"));//setText("By Code EPSG");
129
                        codeRadioButton.setSelected(true);
130
                        codeRadioButton.addActionListener(this);
131
                }
132
                return codeRadioButton;
133
        }
134
                  
135
        private JRadioButton getNameRadioButton() {
136
                if (nameRadioButton == null) {
137
                        nameRadioButton = new JRadioButton();
138
                        nameRadioButton.setText(PluginServices.getText(this,"por_nombre"));
139
                        nameRadioButton.addActionListener(this);
140
                }
141
                return nameRadioButton;
142
        }
143
                 
144
        private JPanel getGroupRadioButton() {
145
                if (groupRadioButton == null) {
146
                        groupRadioButton = new JPanel();
147
                        groupRadioButton.setLayout(new GridLayout(1,0));
148
                        groupRadioButton.setPreferredSize(new Dimension(500,30));
149
                        groupRadioButton.add(getLabel());
150
                        groupRadioButton.add(getCodeRadioButton());
151
                        groupRadioButton.add(getNameRadioButton());                        
152
                }
153
                return groupRadioButton;
154
        }
155
        
156
        private JLabel getLabel(){
157
                JLabel criterio = new JLabel();
158
                criterio.setPreferredSize(new Dimension(100, 20));
159
                criterio.setText(PluginServices.getText(this, "criterio_busqueda"));
160
                return criterio;
161
        }
162
        
163
        private JButton getSearchButton() {
164
                if (searchButton == null) {
165
                        searchButton = new JButton();
166
                        searchButton.setPreferredSize(new Dimension(75,20));
167
                        searchButton.setText(PluginServices.getText(this,"buscar"));
168
                        searchButton.setMnemonic('S');                        
169
                        searchButton.addActionListener(this);                        
170
                }
171
                return searchButton;
172
        }        
173
        
174
        private JTextField getSearchTextField() {
175
                if (searchTextField == null) {
176
                        searchTextField = new JTextField();
177
                        searchTextField.setPreferredSize(new Dimension(300,20));
178
                        searchTextField.addKeyListener(this);
179
                }                
180
                return searchTextField;
181
        }
182
        
183
        public JTable getJTable() {
184
                if (jTable == null) {
185
                        String[] columnNames= {PluginServices.getText(this,"codigo"),
186
                                        PluginServices.getText(this,"nombre"),
187
                                        PluginServices.getText(this,"projected"),
188
                                        PluginServices.getText(this,"datum")};
189
                        Object[][]data = {};                        
190
                        dtm = new DefaultTableModel(data, columnNames)
191
                         {
192
                                private static final long serialVersionUID = 1L;
193
                                public boolean isCellEditable(int row, int column) {
194
                                        return false;
195
                                }
196
                                /*
197
                                 * metodo necesario para cuando utilizamos tablas ordenadas
198
                                 * ya que sino al ordenar por algun campo no se queda con el orden
199
                                 * actual al seleccionar una fila (non-Javadoc)
200
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
201
                                 */
202
                                public Class getColumnClass(int column)
203
                                {
204
                                        return getValueAt(0, column).getClass();
205
                                }
206
                                };
207
                        sorter = new TableSorter(dtm);                        
208

    
209
                        jTable = new JTable(sorter);
210
                        sorter.setTableHeader(jTable.getTableHeader());                        
211
                        jTable.setCellSelectionEnabled(false);
212
                        jTable.setRowSelectionAllowed(true);
213
                        jTable.setColumnSelectionAllowed(false);
214
                        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
215
                        TableColumn column = null;
216
                        for (int i = 0; i < 4; i++) {
217
                            column = jTable.getColumnModel().getColumn(i);
218
                            if (i == 0) {
219
                                column.setPreferredWidth(80); //code column is shorter                                     
220
                            }else if (i ==2) {
221
                                    column.setPreferredWidth(50);
222
                            } else {
223
                                    column.setPreferredWidth(175);
224
                            }
225
                            
226
                        }                        
227
        }
228
                return jTable;
229
                
230
        }
231
        
232
        public void setCodeCRS(int code) {
233
                codeCRS = code;
234
        }
235
        
236
        public int getCodeCRS() {
237
                return codeCRS;
238
        }        
239
        
240
        private JScrollPane getJScrollPane() {
241
                if (jScrollPane == null) {
242
                        jScrollPane = new JScrollPane();
243
                        jScrollPane.setPreferredSize(new Dimension(500,150));
244
                        jScrollPane.setBorder(
245
                                    BorderFactory.createCompoundBorder(
246
                                        BorderFactory.createCompoundBorder(
247
                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"ESRI")),
248
                                                        BorderFactory.createEmptyBorder(5,5,5,5)),
249
                                                        jScrollPane.getBorder()));
250
                        jScrollPane.setViewportView(getJTable());
251
                }
252
                return jScrollPane;
253
        }
254
        
255
        public void keyPressed(KeyEvent e) {
256
                if (e.getSource() == this.getSearchTextField()){
257
                        if (e.getKeyCode() == 10) {
258
                                searchButton();
259
                        }                        
260
                }                
261
        }
262
        
263
        /**
264
         * M?todo que controla la b?squeda de CRS del repositorio de ESRI.
265
         * Tambi?n maneja los errores en caso de que los par?metros de b?squeda
266
         * sean err?neos, o que no se encuentren resultados.
267
         *
268
         */
269
        private void searchButton() {                
270
                searchTextField.setBackground(Color.white);
271
                boolean not_numeric = false;
272
                
273
                if (searchTextField.getText().equals("")) {
274
                        searchTextField.setBackground(new Color(255,204,204));
275
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,"fill_name"), "Warning...", JOptionPane.WARNING_MESSAGE);
276
                }
277
                
278
                else {
279
            //Eliminar filas en cada nueva bsqueda
280
                        int numRow = dtm.getRowCount();
281
                        while (numRow != 0) {
282
                                numRow = numRow - 1;
283
                                dtm.removeRow(numRow);
284
                        }
285
                        
286
                        if (codeRadioButton.isSelected() && (searchTextField.getText().length()!=searchTextField.getText().replaceAll("[^0-9]", "").length())){
287
                                not_numeric = true;
288
                        }
289
                        
290
                        //Dependiendo de la opcion se realizada una busqueda
291
                        ResultSet result = null;
292
                                                                                          
293
                        if (codeRadioButton.isSelected() && !not_numeric) {
294
                                        
295
                                key = searchTextField.getText();
296
                                int code = Integer.parseInt(key);
297
                                String sentence = "SELECT esri_code, esri_wkt, esri_proj, esri_geog, esri_datum " +                                                         
298
                                                                  "FROM ESRI " +                                      
299
                                      "WHERE esri_code = " + code;
300
                                                                
301
                                result = Query.select(sentence,connect.getConnection());        
302
                                
303
                                
304
                                Object[] data = new Object[4];
305
                                try {
306
                                        while (result.next()){
307
                                                data[0]        = result.getString("esri_code");
308
                                                data[1] = result.getString("esri_wkt");
309
                                                String proj = result.getString("esri_proj");
310
                                                if (!proj.equals("")){
311
                                                        data[1] = proj;
312
                                                        data[2] = PluginServices.getText(this,"si");
313
                                                } 
314
                                                else 
315
                                                {
316
                                                        data[1] = result.getString("esri_geog");
317
                                                        data[2] = PluginServices.getText(this,"no");
318
                                                }
319
                                                
320
                                                data[3] = result.getString("esri_datum");
321
                                                dtm.addRow(data);
322
                                        }
323
                                } catch (SQLException e1) {
324
                                        e1.printStackTrace();
325
                                }
326
                        }
327
                        else if (nameRadioButton.isSelected()) {
328
                                key = searchTextField.getText();
329
                                String key2 = key.substring(0,1);
330
                                String key3 = key.substring(1,key.length());
331
                                key2 = key2.toUpperCase();
332
                                
333
                                String sentence = "SELECT esri_code, esri_wkt, esri_proj, esri_geog, esri_datum " +                                                         
334
                                        "FROM ESRI " +                                      
335
                                        "WHERE (esri_proj LIKE '%" + key + "%') OR (esri_proj LIKE '%"+ 
336
                                        key.toUpperCase() +"%') " +
337
                                        "OR (esri_proj LIKE '%" + key2+key3 +"%') OR " +
338
                                                        "(esri_geog LIKE '%" + key + "%') OR (esri_geog LIKE '%"+ 
339
                                        key.toUpperCase() +"%') " +
340
                                        "OR (esri_geog LIKE '%" + key2+key3 +"%')";
341
                                                                
342
                                result = Query.select(sentence,connect.getConnection());        
343
                                                                
344
                                Object[] data = new Object[4];
345
                                try {
346
                                        while (result.next()){
347
                                                data[0]        = result.getString("esri_code");
348
                                                data[1] = result.getString("esri_wkt");
349
                                                String proj = result.getString("esri_proj");
350
                                                if (!proj.equals("")){
351
                                                        data[1] = proj;
352
                                                        data[2] = PluginServices.getText(this,"si");
353
                                                } 
354
                                                else 
355
                                                {
356
                                                        data[1] = result.getString("esri_geog");
357
                                                        data[2] = PluginServices.getText(this,"no");
358
                                                }
359
                                                
360
                                                data[3] = result.getString("esri_datum");
361
                                                dtm.addRow(data);
362
                                        }
363
                                } catch (SQLException e1) {
364
                                        e1.printStackTrace();
365
                                }
366
                        }
367
                                                
368
                        int numr = dtm.getRowCount();
369
                        if (not_numeric) {
370
                                JOptionPane.showMessageDialog(ESRIpanel.this, 
371
                                                PluginServices.getText(this,"numeric_format"), 
372
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
373
                                searchTextField.setText("");
374
                        }
375
                        else if (numr == 0){
376
                        JOptionPane.showMessageDialog(this, PluginServices.getText(this,"no_results"), "Warning...",
377
                                        JOptionPane.WARNING_MESSAGE);
378
                        }
379
                        else{
380
                                this.getJTable().setRowSelectionInterval(0,0);                                
381
                        }
382
                }                
383
        }        
384

    
385
        public void keyReleased(KeyEvent arg0) {
386
                
387
        }
388

    
389
        public void keyTyped(KeyEvent arg0) {
390
        
391
        }
392

    
393
        /**
394
         * Maneja los eventos de los botones y los radioButtons del panel
395
         * del repositorio ESRI.
396
         */
397
        public void actionPerformed(ActionEvent e) {
398
                if (e.getSource() == this.getSearchButton()){
399
                        searchTextField.setBackground(Color.white);
400
                        if (searchTextField.getText().equals("")) {
401
                                searchTextField.setBackground(new Color(255,204,204));
402
                                JOptionPane.showMessageDialog(ESRIpanel.this, 
403
                                                PluginServices.getText(this,"fill_name"), 
404
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
405
                        }
406
                        else {
407
                                searchButton();
408
                        }
409
                }
410
                if (e.getSource() == this.getCodeRadioButton()) {
411
                        searchTextField.setText("");
412
                        codeRadioButton.setSelected(true);
413
                        nameRadioButton.setSelected(false);
414
                }
415
                
416
                if (e.getSource() == this.getNameRadioButton()) {
417
                        searchTextField.setText("");
418
                        nameRadioButton.setSelected(true);
419
                        codeRadioButton.setSelected(false);
420
                }                
421
        }        
422
        
423
        public ICrs getProjection() {
424
                try {
425
                        String txt = getWKT();                        
426
                        ICrs crs = new CrsFactory().getCRS(getCodeCRS(), txt); 
427
                        return crs ;
428
                } catch (CrsException e) {
429
                        e.printStackTrace();
430
                }
431
                return null;
432
        }
433
        
434
        /**
435
         * Consigue la cadena wkt del CRS seleccionado, y genera la cadena que m?s
436
         * tarde volver? a ser tratada para la consecuci?n de una cadena wkt
437
         * legible por la proj4.
438
         *
439
         */
440
        public void setWKT(){
441
                int code = getCodeCRS();
442
                String sentence = "SELECT esri_wkt " +                                                         
443
                                                  "FROM ESRI " +                                      
444
                          "WHERE esri_code = " + code;
445
                
446
                
447
                ResultSet result = Query.select(sentence,connect.getConnection());        
448
        
449
                try {
450
                        result.next();                        
451
                        cadWKT = result.getString("esri_wkt");                        
452
                } catch (SQLException e1) {
453
                        e1.printStackTrace();
454
                }                
455
                cadWKT = cadWKT.substring(0, cadWKT.length()-1) + ", AUTHORITY[\"ESRI\","+ getCodeCRS()+"]]";
456
                
457
        }
458
        
459
        public String getWKT(){
460
                return cadWKT;
461
        }
462
        
463
        public void setProjection(IProjection crs) {
464
                //setCrs((ICrs) crs);
465
        }
466
        
467
        
468
        
469
        
470
}