Statistics
| Revision:

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

History | View | Annotate | Download (10 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.JScrollPane;
20
import javax.swing.JTable;
21
import javax.swing.JTextField;
22
import javax.swing.ListSelectionModel;
23
import javax.swing.table.DefaultTableModel;
24
import javax.swing.table.TableColumn;
25

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

    
33
import com.iver.andami.PluginServices;
34
import com.iver.cit.gvsig.gui.TableSorter;
35

    
36
public class IAU2000panel extends JPanel implements KeyListener, ActionListener {
37

    
38
        /**
39
         * 
40
         */
41
        private static final long serialVersionUID = 1L;
42
        public JPanel IAU2000panel = null;
43
        
44
        JLabel jLabel = null;
45
        JTextField crsTextFld = null;        
46
        
47
        String cadWKT = "";
48
        
49
        int transf = 0;        
50
        boolean source_yn = false;
51
        int source_cod = 0;
52
        int method_code = 0;
53
        int datum_code = 0;
54
        int projection_conv_code = 0;
55
        public String crs_kind = null;
56
        
57
        public TableSorter sorter = null;
58
        
59
        public EpsgConnection connect = null;
60
        
61
        private JButton searchButton = null;
62
        private JTextField searchTextField = null;
63
        public JTable jTable = null;
64
        private JScrollPane jScrollPane = null;
65
        public DefaultTableModel dtm = null;
66
        public String key;
67
        public int selectedRowTable = -1;
68
        public int selectedRowTable2 = -1;        
69
        
70
        private int codeCRS = -1;        
71
        
72
        TransformationManualPanel manual = null;
73
        
74
        public IAU2000panel() {                
75
                initialize();
76
        }
77
        
78
        private void initialize(){
79
                this.setLayout(new GridLayout(2,3));
80
                this.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
81
                
82
                this.add(getSearchButton(), null);
83
                this.add(getSearchTextField(), null);
84
                //this.add(getInfoCrs(), null);
85
                this.add(getJScrollPane(), null);        
86
        }
87
        
88
        public void connection(){
89
                connect = new EpsgConnection();
90
                
91
                
92
        }
93
        
94
        private void searchButton() {                
95
                searchTextField.setBackground(Color.white);
96
                
97
                if (searchTextField.getText().equals("")) {
98
                        searchTextField.setBackground(new Color(255,204,204));
99
                        JOptionPane.showMessageDialog(this, "Fill the name of CRS.", "Warning...", JOptionPane.WARNING_MESSAGE);
100
                }
101
                
102
                else {
103
            //Eliminar filas en cada nueva bsqueda
104
                        int numRow = dtm.getRowCount();
105
                        while (numRow != 0) {
106
                                numRow = numRow - 1;
107
                                dtm.removeRow(numRow);
108
                        }
109
                        
110
                        ResultSet result = null;                        
111
                        
112
                        /*
113
                        iau_code
114
                        iau_wkt
115
                        iau_proj
116
                        iau_geog
117
                        iau_datum
118
                         */
119
                        
120
                        key = searchTextField.getText();
121
                        int code = Integer.parseInt(key);
122
                        String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
123
                                                          "FROM IAU2000 " +                                      
124
                              "WHERE iau_code = " + code;
125
                        
126
                        connect.setConnectionIAU2000();        
127
                        result = Query.select(sentence,connect.getConnection());        
128
                        try {
129
                                connect.shutdown();
130
                        } catch (SQLException e) {
131
                                // TODO Auto-generated catch block
132
                                e.printStackTrace();
133
                        }
134
                        
135
                        Object[] data = new Object[4];
136
                        try {
137
                                while (result.next()){
138
                                        data[0]        = result.getString("iau_code");
139
                                        data[1] = result.getString("iau_wkt");
140
                                        String proj = result.getString("iau_proj");
141
                                        if (!proj.equals("")){
142
                                                data[1] = proj;
143
                                                data[2] = PluginServices.getText(this,"S?");
144
                                        } 
145
                                        else 
146
                                        {
147
                                                data[1] = result.getString("iau_geog");
148
                                                data[2] = PluginServices.getText(this,"No");
149
                                        }
150
                                        
151
                                        data[3] = result.getString("iau_datum");
152
                                        dtm.addRow(data);
153
                                }
154
                        } catch (SQLException e1) {
155
                                e1.printStackTrace();
156
                        }
157
                                                
158
                        int numr = dtm.getRowCount();
159
                        
160
                        if (numr == 0 ){
161
                        JOptionPane.showMessageDialog(this, "Not Results found.", "Warning...",
162
                                        JOptionPane.WARNING_MESSAGE);
163
                        }
164
                        else{
165
                                this.getJTable().setRowSelectionInterval(0,0);                                
166
                        }
167
                }                
168
        }        
169
                
170
        private JButton getSearchButton() {
171
                if (searchButton == null) {
172
                        searchButton = new JButton();
173
                        searchButton.setPreferredSize(new Dimension(75,20));
174
                        searchButton.setText(PluginServices.getText(this,"Buscar"));
175
                        searchButton.setMnemonic('S');                        
176
                        searchButton.addActionListener(this);                        
177
                }
178
                return searchButton;
179
        }        
180
        
181
        private JTextField getSearchTextField() {
182
                if (searchTextField == null) {
183
                        searchTextField = new JTextField();
184
                        searchTextField.setPreferredSize(new Dimension(300,20));
185
                        searchTextField.addKeyListener(this);
186
                }                
187
                return searchTextField;
188
        }
189
        
190
        public JTable getJTable() {
191
                if (jTable == null) {
192
                        String[] columnNames= {"Code of CRS","Name", "Projected", "Datum"};
193
                        Object[][]data = {};                        
194
                        dtm = new DefaultTableModel(data, columnNames)
195
                         {
196
                                public boolean isCellEditable(int row, int column) {
197
                                        return false;
198
                                }
199
                                /*
200
                                 * metodo necesario para cuando utilizamos tablas ordenadas
201
                                 * ya que sino al ordenar por algun campo no se queda con el orden
202
                                 * actual al seleccionar una fila (non-Javadoc)
203
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
204
                                 */
205
                                public Class getColumnClass(int column)
206
                                {
207
                                        return getValueAt(0, column).getClass();
208
                                }
209
                                };
210
                        sorter = new TableSorter(dtm);                        
211

    
212
                        jTable = new JTable(sorter);
213
                        sorter.setTableHeader(jTable.getTableHeader());                        
214
                        jTable.setCellSelectionEnabled(false);
215
                        jTable.setRowSelectionAllowed(true);
216
                        jTable.setColumnSelectionAllowed(false);
217
                        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
218
                        TableColumn column = null;
219
                        for (int i = 0; i < 4; i++) {
220
                            column = jTable.getColumnModel().getColumn(i);
221
                            if (i == 0) {
222
                                column.setPreferredWidth(80); //code column is shorter                                     
223
                            }else if (i ==2) {
224
                                    column.setPreferredWidth(50);
225
                            } else {
226
                                    column.setPreferredWidth(175);
227
                            }
228
                            
229
                        }                        
230
        }
231
                return jTable;
232
                
233
        }
234
        
235
        public void setCodeCRS(int code) {
236
                codeCRS = code;
237
        }
238
        
239
        public int getCodeCRS() {
240
                return codeCRS;
241
        }        
242
        
243
        private JScrollPane getJScrollPane() {
244
                if (jScrollPane == null) {
245
                        jScrollPane = new JScrollPane();
246
                        jScrollPane.setPreferredSize(new Dimension(500,150));
247
                        jScrollPane.setBorder(
248
                                    BorderFactory.createCompoundBorder(
249
                                        BorderFactory.createCompoundBorder(
250
                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"IAU2000")),
251
                                                        BorderFactory.createEmptyBorder(5,5,5,5)),
252
                                                        jScrollPane.getBorder()));
253
                        jScrollPane.setViewportView(getJTable());
254
                }
255
                return jScrollPane;
256
        }
257
        
258
        public ICrs getProjection() {
259
                try {
260
                        String txt = getWKT();                        
261
                        ICrs crs = new CrsFactory().getCRS(getCodeCRS(), txt); 
262
                        return crs ;
263
                } catch (CrsException e) {
264
                        e.printStackTrace();
265
                }
266
                return null;
267
        }
268
        
269
        public void setWKT(){
270
                int code = getCodeCRS();
271
                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
272
                                                  "FROM IAU2000 " +                                      
273
                          "WHERE iau_code = " + code;
274
                
275
                connect.setConnectionIAU2000();        
276
                ResultSet result = Query.select(sentence,connect.getConnection());        
277
                try {
278
                        connect.shutdown();
279
                } catch (SQLException e) {
280
                        // TODO Auto-generated catch block
281
                        e.printStackTrace();
282
                }
283
                try {
284
                        result.next();                        
285
                        cadWKT = result.getString("iau_wkt");                        
286
                } catch (SQLException e1) {
287
                        e1.printStackTrace();
288
                }
289
                
290
                cadWKT = cadWKT.substring(0, cadWKT.length()-1) + ", AUTHORITY[\"IAU2000\","+ getCodeCRS()+"]]";
291
        }
292
        
293
        public String getWKT(){
294
                return cadWKT;
295
        }
296
        
297
        public void setProjection(IProjection crs) {
298
                //setCrs((ICrs) crs);
299
        }
300
        
301
        public void loadViewCRS(int code){
302
                connection();                
303
                String sentence = "SELECT iau_code, iau_wkt, iau_proj, iau_geog, iau_datum " +                                                         
304
                                                  "FROM IAU2000 " +                                      
305
                          "WHERE iau_code = " + code;
306
                                
307
                connect.setConnectionIAU2000();        
308
                ResultSet result = Query.select(sentence,connect.getConnection());        
309
                try {
310
                        connect.shutdown();
311
                } catch (SQLException e) {
312
                        // TODO Auto-generated catch block
313
                        e.printStackTrace();
314
                }
315
                
316
                Object[] data = new Object[4];
317
                try {
318
                        while (result.next()){
319
                                data[0]        = result.getString("iau_code");
320
                                data[1] = result.getString("iau_wkt");
321
                                String proj = result.getString("iau_proj");
322
                                if (!proj.equals("")){
323
                                        data[1] = proj;
324
                                        data[2] = PluginServices.getText(this,"S?");
325
                                } 
326
                                else 
327
                                {
328
                                        data[1] = result.getString("iau_geog");
329
                                        data[2] = PluginServices.getText(this,"No");
330
                                }
331
                                
332
                                data[3] = result.getString("iau_datum");
333
                                dtm.addRow(data);
334
                        }
335
                } catch (SQLException e1) {
336
                        e1.printStackTrace();
337
                }
338
        }
339

    
340
        public void keyPressed(KeyEvent e) {
341
                if (e.getSource() == this.getSearchTextField()){
342
                        if (e.getKeyCode() == 10) {
343
                                searchButton();
344
                        }
345
                        
346
                        if ((e.getKeyCode() >= 48 && e.getKeyCode() <= 57) || e.getKeyCode() >= 96 && e.getKeyCode() <= 105 || 
347
                                        (e.getKeyCode() >= 0 && e.getKeyCode()<= 31) || e.getKeyCode() == 127){                                                
348
                        }
349
                        else {
350
                                JOptionPane.showMessageDialog(IAU2000panel.this, 
351
                                                "Only number format.", 
352
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
353
                                String aux = searchTextField.getText();
354
                                if (aux.length() == 1 || aux.length() == 0){
355
                                        searchTextField.setText("");
356
                                }
357
                                else {
358
                                        searchTextField.setText(aux.substring(0, aux.length()-1));
359
                                }
360
                        }
361
                }
362
                
363
        }
364

    
365
        public void keyReleased(KeyEvent arg0) {
366
                // TODO Auto-generated method stub
367
                
368
        }
369

    
370
        public void keyTyped(KeyEvent arg0) {
371
                // TODO Auto-generated method stub
372
                
373
        }
374

    
375
        public void actionPerformed(ActionEvent e) {
376
                // TODO Auto-generated method stub
377
                if (e.getSource() == this.getSearchButton()){
378
                        searchTextField.setBackground(Color.white);
379
                        if (searchTextField.getText().equals("")) {
380
                                searchTextField.setBackground(new Color(255,204,204));
381
                                JOptionPane.showMessageDialog(IAU2000panel.this, 
382
                                                "Fill the key-word of the search.", 
383
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
384
                        }
385
                        else {
386
                                searchButton();
387
                        }
388
                }
389
        }
390

    
391
}