Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libJCRS / src / org / gvsig / crs / gui / panels / TransformationEpsgPanel.java @ 8657

History | View | Annotate | Download (13.8 KB)

1
package org.gvsig.crs.gui.panels;
2

    
3

    
4
import java.awt.BorderLayout;
5
import java.awt.Component;
6
import java.awt.Dimension;
7
import java.awt.FlowLayout;
8
import java.awt.GridLayout;
9
import java.sql.ResultSet;
10
import java.sql.SQLException;
11
import java.util.ArrayList;
12

    
13
import javax.swing.BorderFactory;
14
import javax.swing.JLabel;
15
import javax.swing.JPanel;
16
import javax.swing.JScrollPane;
17
import javax.swing.JTable;
18
import javax.swing.JTextArea;
19
import javax.swing.ListSelectionModel;
20
import javax.swing.event.ListSelectionEvent;
21
import javax.swing.event.ListSelectionListener;
22
import javax.swing.table.DefaultTableModel;
23
import javax.swing.table.TableColumn;
24

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

    
31
import com.iver.andami.PluginServices;
32
import com.iver.andami.ui.mdiManager.WindowInfo;
33
import com.iver.cit.gvsig.gui.TableSorter;
34

    
35
public class TransformationEpsgPanel extends JPanel implements ListSelectionListener {
36

    
37
        
38
        /**
39
         * 
40
         */
41
        private static final long serialVersionUID = 1L;
42

    
43
        private JPanel panel;
44
        private IProjection firstProj;
45
        
46
        String[] transformations = {"9603", "9606", "9607", "9613", "9615", "9633"};
47
        
48
        private int transformation_code = -1;
49
        private String[] values;
50
        private String params = "+towgs84=";
51
        
52
        private JTable transformationTable;        
53
        private JScrollPane jScrollPane = null;
54
        public boolean inverseTranformation = false;
55
        
56
        public EpsgConnection connect = null;;        
57
        int crs_target = -1;
58
        
59
        public DefaultTableModel dtm = null;
60
        
61
        private int crs_source_code;
62
        private String cadWKT = "";
63
        private ListSelectionModel lsm2 = null;
64
        public int selectedRowTable = -1;
65
        boolean tra = false;
66
        
67
        
68
        private JTextArea info;        
69
        public TableSorter sorter = null;
70
        
71
        public TransformationEpsgPanel(int target) {
72
                crs_target = target;                
73
        }
74
        
75
        public JPanel getJPanel(){
76
                if (panel == null) {
77
                        panel = new JPanel();
78
                        panel.setLayout(new GridLayout(1,2));
79
                        panel.setLayout(new FlowLayout(FlowLayout.LEADING,5,10));
80
                        panel.setPreferredSize(new Dimension(525, 100));
81
                        panel.setBorder(
82
                                    BorderFactory.createCompoundBorder(
83
                                                        BorderFactory.createCompoundBorder(
84
                                                                        BorderFactory.createTitledBorder("Transformacion EPSG"),
85
                                                                        BorderFactory.createEmptyBorder(2,2,2,2)),
86
                                                                        panel.getBorder()));
87
                        //panel.add(getLabel());
88
                        //panel.add(getScrollPanelArea(), BorderLayout.NORTH);
89
                        panel.add(getJScrollPane());
90
                }
91
                return panel;
92
        }
93
        
94
        private Component getInfo() {
95
                if (info == null){
96
                        info = new JTextArea();
97
                        info.setLineWrap(true);
98
                        info.setWrapStyleWord(true);
99
                        info.setPreferredSize(new Dimension(400, 240));
100
                        info.setEditable(false);
101
                        info.append(getWKT());
102
                }
103
                info.setText(getWKT());
104
                return info;
105
        }
106
        
107
        public JTable getJTable() {
108
                if (transformationTable == null) {
109
                        String[] columnNames= {PluginServices.getText(this,"code_transformation"),
110
                                        PluginServices.getText(this,"name_transformation"),
111
                                        PluginServices.getText(this,"type_transformation"),
112
                                        PluginServices.getText(this,"source_crs"),
113
                                        PluginServices.getText(this,"target_crs"),
114
                                        PluginServices.getText(this,"description_area")};
115
                        Object[][]data = {};
116
                        dtm = new DefaultTableModel(data, columnNames)
117
                        {
118
                                public boolean isCellEditable(int row, int column) {
119
                                        return false;
120
                                }
121
                                /*
122
                                 * metodo necesario para cuando utilizamos tablas ordenadas
123
                                 * ya que sino al ordenar por algun campo no se queda con el orden
124
                                 * actual al seleccionar una fila (non-Javadoc)
125
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
126
                                 */
127
                                public Class getColumnClass(int column)
128
                                {
129
                                        return getValueAt(0, column).getClass();
130
                                }
131
                        };
132
                        sorter = new TableSorter(dtm);                        
133

    
134
                        transformationTable = new JTable(sorter);
135
                        sorter.setTableHeader(transformationTable.getTableHeader());        
136
                        transformationTable.setCellSelectionEnabled(false);
137
                        transformationTable.setRowSelectionAllowed(true);
138
                        transformationTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
139
                        transformationTable.setColumnSelectionAllowed(false);
140
                        transformationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
141
                        TableColumn column = null;
142
                        for (int i = 0; i < columnNames.length; i++) {
143
                            column = transformationTable.getColumnModel().getColumn(i);
144
                            if (i == 0 || i == 3 || i == 4) {
145
                                column.setPreferredWidth(40);                                     
146
                            }else if (i == 2) {
147
                                    column.setPreferredWidth(80);
148
                            }
149
                            else {                            
150
                                column.setPreferredWidth(160);
151
                            }
152
                        }
153
                        ListSelectionModel rowSM2 = transformationTable.getSelectionModel();
154
                        rowSM2.addListSelectionListener(this);
155
                }
156
                return transformationTable;
157
        }
158
        
159
        private JScrollPane getJScrollPane() {
160
                if (jScrollPane == null) {
161
                        jScrollPane = new JScrollPane();
162
                        jScrollPane.setPreferredSize(new Dimension(525,200));
163
                        jScrollPane.setBorder(
164
                                    BorderFactory.createCompoundBorder(
165
                                        BorderFactory.createCompoundBorder(
166
                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"transformations")),
167
                                                        BorderFactory.createEmptyBorder(5,5,5,5)),
168
                                                        jScrollPane.getBorder()));
169
                        jScrollPane.setViewportView(getJTable());
170
                }
171
                return jScrollPane;
172
        }
173
                
174
        private void callTransformation(int crsCode){                
175
        int numRow = dtm.getRowCount();
176
                
177
        while (numRow != 0) {
178
                        numRow = numRow - 1;
179
                        dtm.removeRow(numRow);
180
                }
181
        
182
        String sentence = "SELECT source_geogcrs_code " +
183
                                                "FROM epsg_coordinatereferencesystem " +
184
                                                "WHERE coord_ref_sys_code = "+ crsCode ;
185
                ResultSet result = Query.select(sentence,connect.getConnection());
186
                int source = 0;
187
                try {
188
                        result.next();
189
                        source = result.getInt("source_geogcrs_code");
190
                } catch (SQLException e1) {
191
                        e1.printStackTrace();
192
                }
193
                
194
        ResultSet result2 = null;
195
        ResultSet result3 = null;
196
        if (source != 0){
197
                        crsCode = source;                                    
198
        }
199
        
200
        ArrayList codecs = new ArrayList();
201
        codecs.add(String.valueOf(crsCode));                
202
                
203
                for (int j=0; j< codecs.size(); j++){
204
                        sentence = "SELECT coord_op_code, coord_op_name, coord_op_type, source_crs_code, target_crs_code, area_of_use_code, coord_op_method_code " +
205
                                        "FROM epsg_coordoperation " +                        
206
                                        "WHERE source_crs_code = " + codecs.get(j) + "AND target_crs_code = " + crs_target;
207

    
208
            result = Query.select(sentence,connect.getConnection());        
209
            
210
            try {
211
                    while(result.next()) {
212
                            Object[]data = new Object[6];                                                            
213
                            data[0] = String.valueOf(result.getInt("coord_op_code"));
214
                            data[1] = result.getString("coord_op_name");
215
                            data[2] = result.getString("coord_op_type");
216
                            data[3] = String.valueOf(result.getInt("source_crs_code"));
217
                            data[4] = String.valueOf(result.getInt("target_crs_code"));
218
                            /*codecs.add(data[4]);
219
                            codecs = deleteItems(codecs);*/
220
                            
221
                            int aouc = Integer.parseInt(result.getString("area_of_use_code"));
222
                                        
223
                                        sentence = "SELECT area_of_use FROM epsg_area " +
224
                                                                        "WHERE area_code = "+ aouc ;
225
                                        
226
                                        result2 = Query.select(sentence,connect.getConnection());
227
                            while (result2.next())
228
                                    data[5] = result2.getString("area_of_use");
229
                            
230
                            String coord_op_method = result.getString("coord_op_method_code");                                                            
231
                                    
232
                            sentence = "SELECT reverse_op FROM epsg_coordoperationmethod "+
233
                                                    "WHERE coord_op_method_code LIKE " + coord_op_method;
234
                            result3 = Query.select(sentence,connect.getConnection());
235
                            
236
                            while(result3.next()){
237
                                    if (Integer.parseInt(result3.getString("reverse_op")) == 1){
238
                                            for (int i=0; i< transformations.length; i++){
239
                                                    if (coord_op_method.equals(transformations[i])){
240
                                                            dtm.addRow(data);
241
                                                    }
242
                                            }                                            
243
                                    }
244
                            }
245
                    }
246
            }         
247
            catch (SQLException e1) {
248
                    e1.printStackTrace();
249
            }
250
                }
251
        }
252
        
253
        private ArrayList deleteItems(ArrayList codecs) {
254
                ArrayList cod = new ArrayList();
255
                boolean equal = false;
256
                for (int i = 0; i< codecs.size(); i++){
257
                        String c = (String) codecs.get(i);
258
                        for (int j = 0; j<cod.size(); j++){
259
                                if (cod.get(j).equals(c)){
260
                                        equal = true;
261
                                }
262
                        }
263
                        if (!equal){
264
                                cod.add(c);                                
265
                        }
266
                        equal = false;
267
                }
268
                return cod;
269
        }
270
        
271
        private void callInverseTransformation(int crsCode){
272
                //inverseTranformation = true;
273
        
274
        String sentence = "SELECT source_geogcrs_code " +
275
                                                "FROM epsg_coordinatereferencesystem " +
276
                                                "WHERE coord_ref_sys_code = "+ crsCode ;
277
                ResultSet result = Query.select(sentence,connect.getConnection());
278
                int source = 0;
279
                try {
280
                        result.next();
281
                        source = result.getInt("source_geogcrs_code");
282
                } catch (SQLException e1) {
283
                        e1.printStackTrace();
284
                }
285
                
286
        ResultSet result2 = null;
287
        ResultSet result3 = null;
288
        if (source != 0){
289
                        crsCode = source;                                    
290
        }
291
        
292
        ArrayList codecs = new ArrayList();
293
        codecs.add(String.valueOf(crsCode));
294
       
295
                codecs = deleteItems(codecs);
296
                
297
                for (int j=0; j< codecs.size(); j++){
298
                        sentence = "SELECT coord_op_code, coord_op_name, coord_op_type, source_crs_code, target_crs_code, area_of_use_code, coord_op_method_code " +
299
                                        "FROM epsg_coordoperation " +                        
300
                                        "WHERE source_crs_code = " + codecs.get(j) + "AND target_crs_code = " + crs_target;
301

    
302
            result = Query.select(sentence,connect.getConnection());        
303
            
304
            try {
305
                    while(result.next()) {
306
                            Object[]data = new Object[6];                                                            
307
                            data[0] = String.valueOf(result.getInt("coord_op_code"));
308
                            data[1] = result.getString("coord_op_name");
309
                            data[2] = result.getString("coord_op_type");
310
                            data[4] = String.valueOf(result.getInt("source_crs_code"));
311
                            data[3] = String.valueOf(result.getInt("target_crs_code"));
312
                            /*codecs.add(data[4]);
313
                            codecs = deleteItems(codecs);*/
314
                            
315
                            int aouc = Integer.parseInt(result.getString("area_of_use_code"));
316
                                        
317
                                        sentence = "SELECT area_of_use FROM epsg_area " +
318
                                                                        "WHERE area_code = "+ aouc ;
319
                                        
320
                                        result2 = Query.select(sentence,connect.getConnection());
321
                            while (result2.next())
322
                                    data[5] = result2.getString("area_of_use");
323
                            
324
                            String coord_op_method = result.getString("coord_op_method_code");                                                            
325
                                    
326
                            sentence = "SELECT reverse_op FROM epsg_coordoperationmethod "+
327
                                                    "WHERE coord_op_method_code LIKE " + coord_op_method;
328
                            result3 = Query.select(sentence,connect.getConnection());
329
                            
330
                            while(result3.next()){
331
                                    if (Integer.parseInt(result3.getString("reverse_op")) == 1){
332
                                            for (int i=0; i< transformations.length; i++){
333
                                                    if (coord_op_method.equals(transformations[i])){
334
                                                            dtm.addRow(data);
335
                                                    }
336
                                            }
337
                                    }
338
                            }
339
                    }
340
            }         
341
            catch (SQLException e1) {
342
                    e1.printStackTrace();
343
            }
344
                }
345
        }
346
        
347
        public void setValues(String[] val) {
348
                values = val;
349
        }
350
        
351
        public String[] getValues() {
352
                return values;
353
        }
354
        
355
        private void setTrasformation_code(int t_cod) {
356
                transformation_code = t_cod;
357
        }
358
        
359
        public int getTransformation_code() {
360
                return transformation_code;
361
        }
362
                
363
        public ICrs getProjection() {
364
                params += values[0];
365
                for(int i = 1; i < values.length; i++)
366
                        params +=","+values[i];
367
                try {
368
                        ICrs crs = new CrsFactory().getCRS(crs_source_code,
369
                                        cadWKT,params);
370
                        return crs;
371
                } catch (org.gvsig.crs.CrsException e) {
372
                        e.printStackTrace();
373
                }
374
                return null;
375
        }
376
        
377
        public void setProjection(IProjection proj) {
378
                firstProj = proj;
379
        }
380
        
381
        public void setWKT(String cad){
382
                cadWKT = cad;
383
                getInfo();
384
        }
385
        
386
        public String getWKT(){
387
                return cadWKT;
388
        }
389
        
390
        public void setSource(int code){
391
                connect = new EpsgConnection();
392
                connect.setConnectionEPSG();
393
                inverseTranformation = false;
394
                crs_source_code = code;
395
                callTransformation(crs_source_code);
396
                
397
                int new_target = crs_target;
398
                crs_target = code;
399
                crs_source_code = new_target;
400
                callInverseTransformation(crs_source_code);
401
                int real_source = crs_target;
402
                crs_target = new_target;
403
                crs_source_code = real_source;
404
            
405
                int numr = dtm.getRowCount();
406
                if (numr > 0 ){                        
407
                        this.getJTable().setRowSelectionInterval(0,0);
408
                }
409
        }
410
        
411
        public int getSource(){
412
                return crs_source_code;
413
        }
414
        
415
        public WindowInfo getWindowInfo() {
416
                WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
417
                   m_viewinfo.setTitle("Transformation EPSG");
418
                return m_viewinfo;
419
        }
420

    
421
        public void valueChanged(ListSelectionEvent e) {
422
                // TODO Auto-generated method stub
423
                if (e.getSource() == this.getJTable().getSelectionModel()){
424
                        lsm2 = (ListSelectionModel)e.getSource();
425
                if (lsm2.isSelectionEmpty()) {
426
                        selectedRowTable = -1;
427
                        tra = false;
428
                } 
429
                else {
430
                    selectedRowTable = lsm2.getMinSelectionIndex();
431
                    setTrasformation_code(Integer.parseInt((String)sorter.getValueAt(selectedRowTable,0)));
432
                    String sentence = "SELECT source_crs_code " +
433
                                                                "FROM epsg_coordoperation " +                        
434
                                                                "WHERE coord_op_code = " + getTransformation_code() ;
435
                    ResultSet result = Query.select(sentence,connect.getConnection());
436
                    try {
437
                                        result.next();
438
                                        int source = result.getInt("source_crs_code");
439
                                        if (source == getSource()) {
440
                                                inverseTranformation = false;
441
                                        }
442
                                        else inverseTranformation = true;
443
                                } catch (SQLException e1) {                                                        
444
                                        e1.printStackTrace();
445
                                }
446
                }
447
                }
448
        }
449

    
450
}