Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extJCRS / src / org / gvsig / crs / gui / panels / TransformationRecentsPanel.java @ 20637

History | View | Annotate | Download (14.7 KB)

1
/* gvSIG. Sistema de Informacin Geogrfica 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 Ibez, 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

    
43
import java.awt.BorderLayout;
44
import java.awt.FlowLayout;
45

    
46
import javax.swing.BorderFactory;
47
import javax.swing.JButton;
48
import javax.swing.JPanel;
49
import javax.swing.JScrollPane;
50
import javax.swing.JTable;
51
import javax.swing.ListSelectionModel;
52
import javax.swing.table.DefaultTableModel;
53
import javax.swing.table.TableColumn;
54

    
55
import org.gvsig.crs.CrsException;
56
import org.gvsig.crs.CrsFactory;
57
import org.gvsig.crs.ICrs;
58
import org.gvsig.crs.ogr.TransEPSG;
59
import org.gvsig.crs.persistence.CompTrData;
60
import org.gvsig.crs.persistence.RecentCRSsPersistence;
61
import org.gvsig.crs.persistence.RecentTrsPersistence;
62
import org.gvsig.crs.persistence.TrData;
63

    
64
import com.iver.andami.PluginServices;
65
import com.iver.cit.gvsig.gui.TableSorter;
66

    
67
import es.idr.teledeteccion.connection.EpsgConnection;
68

    
69
/**
70
 * Panel para la gestin de las transformaciones recientes
71
 * @author Jos Luis Gmez Martnez (jolugomar@gmail.com)
72
 * @author Luisa Marina Fernndez (luisam.fernandez@uclm.es)
73
 *
74
 */
75
public class TransformationRecentsPanel extends JPanel {
76

    
77
        private static final long serialVersionUID = 1L;
78
        int code = 0;
79
        private JTable transformationTable;        
80
        private JScrollPane jScrollPane = null;
81
        public DefaultTableModel dtm = null;
82
        public TableSorter sorter = null;
83
        JButton JButtonInfo = null;
84
        public int selectedRowTable = -1;
85
        private TrData[] trDataArray;
86
        
87
        private String cadWKT = "";
88
        private String params ="";
89
        public EpsgConnection connect = null;
90
        boolean inverseTransformation = false;
91
        int trCode;
92
        boolean targetNad = false;
93
        String sourceAbrev = null;
94

    
95
        public TransformationRecentsPanel() {
96
                initialize();
97
        }
98
        
99
        /**
100
         * Genera el panel contenedor de las transformaciones recientes
101
         * @return
102
         */
103
        private void initialize(){
104
                connect = new EpsgConnection();
105
                connect.setConnectionEPSG();
106
                        //**setLayout(new GridLayout(2,1));
107
                        //**setLayout(new FlowLayout(FlowLayout.LEADING,5,10));
108
                        //**setPreferredSize(new Dimension(525, 100));
109
                setLayout(new BorderLayout());
110
                setBorder(
111
                                    BorderFactory.createCompoundBorder(
112
                                                        BorderFactory.createCompoundBorder(
113
                                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"recents_transformation")),
114
                                                                        BorderFactory.createEmptyBorder(2,2,2,2)),
115
                                                                        getBorder()));
116
                JPanel p=new JPanel();
117
                p.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
118
                p.add(getJButtonInfo());
119
                add(p,BorderLayout.NORTH);
120
                add(getJScrollPane(),BorderLayout.CENTER);        
121
                        //loadRecents();
122
        }
123
        
124
        public JTable getJTable() {
125
                if (transformationTable == null) {
126
                        String[] columnNames= {PluginServices.getText(this,"transformation"),
127
                                        PluginServices.getText(this,"name"),
128
                                        PluginServices.getText(this,"source_crs"),
129
                                        PluginServices.getText(this,"target_crs"),
130
                                        PluginServices.getText(this,"detalles"),};                                        
131
                        Object[][]data = {};
132
                        dtm = new DefaultTableModel(data, columnNames)
133
                        {
134
                                private static final long serialVersionUID = 1L;
135
                                public boolean isCellEditable(int row, int column) {
136
                                        return false;
137
                                }
138
                                /*
139
                                 * metodo necesario para cuando utilizamos tablas ordenadas
140
                                 * ya que sino al ordenar por algun campo no se queda con el orden
141
                                 * actual al seleccionar una fila
142
                                 * @see javax.swing.table.TableModel#getColumnClass(int)
143
                                 */
144
                                public Class getColumnClass(int column)
145
                                {
146
                                        return getValueAt(0, column).getClass();
147
                                }
148
                        };
149
                        sorter = new TableSorter(dtm);                        
150

    
151
                        transformationTable = new JTable(sorter);
152
                        sorter.setTableHeader(transformationTable.getTableHeader());        
153
                        transformationTable.setCellSelectionEnabled(false);
154
                        transformationTable.setRowSelectionAllowed(true);
155
                        transformationTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
156
                        transformationTable.setColumnSelectionAllowed(false);
157
                        transformationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
158
                        TableColumn column = null;
159
                        for (int i = 0; i < columnNames.length; i++) {
160
                            column = transformationTable.getColumnModel().getColumn(i);
161
                            if (i == 0) {
162
                                    column.setPreferredWidth(80);
163
                            }
164
                            else if(i == 4) {
165
                                    column.setPreferredWidth(200);
166
                            }
167
                            else {                            
168
                                column.setPreferredWidth(100);
169
                            }
170
                        }                        
171
                }
172
                return transformationTable;
173
        }
174
        
175
        private JScrollPane getJScrollPane() {
176
                if (jScrollPane == null) {
177
                        jScrollPane = new JScrollPane();
178
                        //jScrollPane.setPreferredSize(new Dimension(525,200));
179
                        jScrollPane.setBorder(
180
                                    BorderFactory.createCompoundBorder(
181
                                        BorderFactory.createCompoundBorder(
182
                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"transformations")),
183
                                                        BorderFactory.createEmptyBorder(12,5,70,5)),
184
                                                        jScrollPane.getBorder()));
185
                        jScrollPane.setViewportView(getJTable());
186
                }
187
                return jScrollPane;
188
        }
189
        
190
        public JButton getJButtonInfo() {
191
                if(JButtonInfo == null) {
192
                        JButtonInfo = new JButton();
193
                        //Poner: JButtonInfo.setPreferredSize(new Dimension(100,20));                        
194
                        JButtonInfo.setText(PluginServices.getText(this,"info_transformations"));                        
195
                        JButtonInfo.setMnemonic('I');
196
                        JButtonInfo.setEnabled(false);                        
197
                }
198
                return JButtonInfo;
199
        }
200
        
201
        /**
202
         * Consigue el crs elegido de las transformaciones
203
         * recientes cuando el se utiliza transformacin EPSG o manual
204
         * @return
205
         */
206
        public ICrs getProjection() {
207
                ICrs crs = null;
208
                try {
209
                        String[] sourceAuthority = getSourceAbrev().split(":");
210
                        //crs = new CrsFactory().getCRS(getCode(), getWKT(),getParams());
211
                        crs = new CrsFactory().getCRS(sourceAuthority[0]+":"+getCode());
212
                        crs.setTransformationParams(getParams(),null);
213
                } catch (CrsException e) {
214
                        e.printStackTrace();
215
                }
216
                return crs;
217
        }
218
        
219
        /**
220
         * Consigue el crs elegido de las transformaciones recientes compuestas
221
         * cuando se utiliza una transformacion compuesta
222
         * @param info
223
         * @return
224
         */
225
        public ICrs getProjectionComplex(String details) {
226
                ICrs crs = null;
227
                RecentTrsPersistence trPersistence = new RecentTrsPersistence();
228
                TrData crsTrDataArray[] = trPersistence.getArrayOfTrData();
229
                CompTrData comp = null;
230
                String sourceTransformation = null;
231
                String targetTransformation = null;
232
                for (int iRow = crsTrDataArray.length-1; iRow >= 0; iRow--) {
233
                        if (details.equals(crsTrDataArray[iRow].getDetails())) {
234
                                if (crsTrDataArray[iRow] instanceof CompTrData) {
235
                                        comp = (CompTrData) crsTrDataArray[iRow];
236
                                        break;
237
                                }
238
                        }
239
                }
240
                
241
                if (comp.getFirstTr().getAuthority().equals(PluginServices.getText(this, "EPSG"))) {
242
                        TransEPSG epsgParams = new TransEPSG(comp.getFirstTr().getCode(),connect, getInverseTransformation());
243
                        setParamsEPGS(epsgParams.getParamValue());
244
                        sourceTransformation = getParams();
245
                } else if (comp.getFirstTr().getAuthority().equals(PluginServices.getText(this, "USR"))) {
246
                        //mirar si funciona y lleva los [  ]
247
                        setParamsManual(comp.getFirstTr().getDetails());
248
                        sourceTransformation = getParams();
249
                } else if (comp.getFirstTr().getAuthority().equals(PluginServices.getText(this, "NADGR"))) {
250
                        String info = comp.getFirstTr().getDetails();
251
                        String[] partes = info.split("\\(");
252
                        String nadFile = partes[0];                
253
                        sourceTransformation = "+nadgrids="+nadFile;                        
254
                }
255
                
256
                if (comp.getSecondTr().getAuthority().equals(PluginServices.getText(this, "EPSG"))) {
257
                        TransEPSG epsgParams = new TransEPSG(comp.getSecondTr().getCode(),connect, getInverseTransformation());
258
                        setParamsEPGS(epsgParams.getParamValue());
259
                        targetTransformation = getParams();
260
                } else if (comp.getSecondTr().getAuthority().equals(PluginServices.getText(this, "USR"))) {
261
                        //mirar si funciona y lleva los [  ]
262
                        setParamsManual(comp.getSecondTr().getDetails());
263
                        targetTransformation = getParams();
264
                } else if (comp.getSecondTr().getAuthority().equals(PluginServices.getText(this, "NADGR"))) {
265
                        String info = comp.getSecondTr().getDetails();
266
                        String[] partes = info.split("\\(");
267
                        String nadFile = partes[0];                
268
                        targetTransformation = "+nadgrids="+nadFile;                        
269
                }
270
                
271
                try {
272
                        crs = new CrsFactory().getCRS(getSourceAbrev());
273
                        crs.setTransformationParams(sourceTransformation,targetTransformation);//nadFile);
274
                        return crs;
275
                } catch (org.gvsig.crs.CrsException e) {
276
                        e.printStackTrace();
277
                }
278
                                
279
                return crs;
280
        }
281
        
282
        /**
283
         * Consigue el crs elegido de las transformaciones
284
         * recientes cuando se utiliza transformacin de rejillas
285
         * @param info
286
         * @return
287
         */
288
        public ICrs getProjectionNad(String info) {
289
                String[] partes = info.split("\\(");
290
                String nadFile = partes[0];                
291
                int codigoNad = Integer.parseInt((partes[1].substring(0,partes[1].length()-1)).split(":")[1]);
292
                ICrs crs;
293
                String[] sourceAuthority = getSourceAbrev().split(":");
294
                
295
                if (getCode() == codigoNad){
296
                        try {
297
                                setNad(false);
298
                                //Siempre EPSG porque solo permitimos transformaciones
299
                                //en el caso en que source y target sean de la EPSG
300
                                crs = new CrsFactory().getCRS(sourceAuthority[0]+":"+getCode());
301
                                crs.setTransformationParams("+nadgrids="+nadFile,null);//nadFile);
302
                                return crs;
303
                        } catch (org.gvsig.crs.CrsException e) {
304
                                e.printStackTrace();
305
                        }
306
                        return null;
307
                }
308
                else {        
309
                        setNad(true);
310
                        try {
311
                                crs = new CrsFactory().getCRS(sourceAuthority[0]+":"+getCode());
312
                                crs.setTransformationParams(null,"+nadgrids="+nadFile);//nadFile);
313
                                
314
                                return crs;
315
                        } catch (CrsException e) {                                
316
                                e.printStackTrace();
317
                        }
318
                        return null;
319
                }        
320
        }
321
        
322
        /**
323
         * 
324
         * @param nadg Define si el fichero rejillas se calcula en el crs fuente o destino
325
         */
326
        public void setNad(boolean nadg){
327
                targetNad = nadg;                
328
        }
329
        
330
        /**
331
         * 
332
         * @return Devuelve si el fichero de rejillas se calcula en el crs fuente o destino
333
         */
334
        public boolean getNad(){
335
                return targetNad;                
336
        }
337
        
338
        /**
339
         * 
340
         * @param cod Cdigo del CRS elegido
341
         */
342
        public void setCode(int cod){
343
                code = cod;
344
        }
345
        
346
        /**
347
         * 
348
         * @return Devuelve el cdigo del CRS elegido
349
         */
350
        public int getCode(){
351
                return code;
352
        }
353
        
354
        /**
355
         * 
356
         * @param cad Cadena wkt del crs fuente
357
         */
358
        public void setWKT(String cad){
359
                cadWKT = cad;                
360
        }        
361
        
362
        /**
363
         * 
364
         * @return Devuelve la cadena wkt del crs fuente
365
         */
366
        public String getWKT(){
367
                return cadWKT;
368
        }
369
        
370
        /**
371
         * 
372
         * @param param Hacemos la cadena con los parmetros de la transformacin manual
373
         */
374
        public void setParamsManual(String param){
375
                params = "+towgs84="+param.substring(1,param.length()-1)+" ";
376
        }
377
        
378
        /**
379
         * 
380
         * @param values Hacemos la cadena con los parmetros de la transformacin EPSG
381
         */
382
        public void setParamsEPGS(String[] values){
383
                params = "+towgs84="+values[0];
384
                for(int i = 1; i < values.length; i++)
385
                        params +=","+values[i];
386
                params += " ";
387
        }
388
        
389
        /**
390
         * 
391
         * @param nadfile
392
         */
393
        public void setParamsNads(String nadfile){
394
                
395
        }
396
        
397
        /**
398
         * 
399
         * @return Deuelve una cadena con los parmetros de la transformacin
400
         */
401
        public String getParams(){
402
                return params;
403
        }
404
        
405
        /**
406
         * 
407
         * @param inverse Parmetro que define si la transformacin es directa o inversa
408
         */
409
        public void setInverseTransformation(boolean inverse){
410
                inverseTransformation = inverse;
411
        }
412
        
413
        /**
414
         * 
415
         * @return Devuelve si es una transformacin directa o inversa
416
         */
417
        public boolean getInverseTransformation(){
418
                return inverseTransformation;
419
        }
420
        
421
        /**
422
         * 
423
         * @param code Cdigo de la transformacin EPGS elegida
424
         */
425
        public void setTrCode(int code){
426
                trCode = code;
427
        }
428
        
429
        /**
430
         * 
431
         * @return Devuelve el cdigo de la transformacin
432
         */
433
        public int getTrCode(){
434
                return trCode;
435
        }
436
        
437
        /**
438
         * Carga en la tabla los CRSs leidos del sistema de persistencia.
439
         */
440
        public void loadRecents(String source, String target){
441
                setSourceAbrev(source);
442
                //                Eliminar filas en cada nueva bsqueda
443
                int numRow = dtm.getRowCount();
444
                while (numRow != 0) {
445
                        numRow = numRow - 1;
446
                        dtm.removeRow(numRow);
447
                }
448
                RecentTrsPersistence persistence = new RecentTrsPersistence();
449
                trDataArray = persistence.getArrayOfTrData();
450
                
451

    
452
                for (int iRow = trDataArray.length-1;iRow>=0;iRow--){
453
                        String crsSource = ((String)trDataArray[iRow].getCrsSource());
454
                        String crsTarget = ((String)trDataArray[iRow].getCrsTarget());
455
                        if(source.equals(crsSource) && target.equals(crsTarget)){
456
                                Object row[] ={trDataArray[iRow].getAuthority()+":"+trDataArray[iRow].getCode(),trDataArray[iRow].getName(),trDataArray[iRow].getCrsSource(),
457
                                        trDataArray[iRow].getCrsTarget(),trDataArray[iRow].getDetails()};
458
                                dtm.addRow(row);
459
                        }
460
                }
461
                
462
                /*
463
                /*Seleccionar el primer registro.
464
                 */
465
                int numr = dtm.getRowCount();
466
                if (numr != 0 )
467
                        this.getJTable().setRowSelectionInterval(0,0);
468
        }
469
        
470
        /**
471
         * Carga en la tabla los CRSs leidos del sistema de persistencia, para
472
         * las transformaciones compuestas
473
         */
474
        public void loadRecentsCompuesta(String source){
475
                
476
                //                Eliminar filas en cada nueva bsqueda
477
                int numRow = dtm.getRowCount();
478
                while (numRow != 0) {
479
                        numRow = numRow - 1;
480
                        dtm.removeRow(numRow);
481
                }
482
                RecentTrsPersistence persistence = new RecentTrsPersistence();
483
                trDataArray = persistence.getArrayOfTrData();
484
                
485

    
486
                for (int iRow = trDataArray.length-1;iRow>=0;iRow--){
487
                        if (trDataArray[iRow] instanceof CompTrData) {
488
                                continue;
489
                        } else {
490
                                String crsSource = ((String)trDataArray[iRow].getCrsSource());
491
                                String crsTarget = ((String)trDataArray[iRow].getCrsTarget());
492
                                if(source.equals(crsSource) || source.equals(crsTarget)){
493
                                        Object row[] ={trDataArray[iRow].getAuthority()+":"+trDataArray[iRow].getCode(),trDataArray[iRow].getName(),trDataArray[iRow].getCrsSource(),
494
                                                trDataArray[iRow].getCrsTarget(),trDataArray[iRow].getDetails()};
495
                                        dtm.addRow(row);
496
                                }
497
                        }
498
                }
499
                
500
                /*
501
                /*Seleccionar el primer registro.
502
                 */
503
                int numr = dtm.getRowCount();
504
                if (numr != 0 )
505
                        this.getJTable().setRowSelectionInterval(0,0);
506
        }
507
        
508
        public void setSourceAbrev(String source) {
509
                sourceAbrev = source;
510
        }
511
        
512
        public String getSourceAbrev() {
513
                return sourceAbrev;
514
        }
515

    
516
}