Statistics
| Revision:

svn-gvsig-desktop / branches / F2 / extensions / extJCRS / src / org / gvsig / crs / gui / panels / TransformationNadgridsPanel.java @ 11458

History | View | Annotate | Download (17.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.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.GridLayout;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.io.File;
51
import java.io.FileInputStream;
52
import java.io.FileNotFoundException;
53
import java.io.FileOutputStream;
54
import java.io.IOException;
55
import java.io.InputStream;
56
import java.io.OutputStream;
57
import java.io.RandomAccessFile;
58

    
59
import javax.swing.BorderFactory;
60
import javax.swing.JButton;
61
import javax.swing.JComboBox;
62
import javax.swing.JFileChooser;
63
import javax.swing.JLabel;
64
import javax.swing.JPanel;
65
import javax.swing.JRadioButton;
66
import javax.swing.JTextArea;
67
import javax.swing.border.EmptyBorder;
68
import javax.swing.filechooser.FileFilter;
69

    
70
import org.cresques.cts.IProjection;
71
import org.gvsig.crs.CrsException;
72
import org.gvsig.crs.CrsFactory;
73
import org.gvsig.crs.CrsWkt;
74
import org.gvsig.crs.ICrs;
75
import org.gvsig.crs.persistence.RecentCRSsPersistence;
76

    
77
import au.com.objectix.jgridshift.GridShiftFile;
78
import au.com.objectix.jgridshift.SubGrid;
79

    
80
import com.iver.andami.PluginServices;
81
import com.iver.andami.ui.mdiManager.IWindow;
82
import com.iver.andami.ui.mdiManager.WindowInfo;
83
import com.iver.utiles.XMLEntity;
84

    
85
/**
86
 * Clase para generar el panel de la tranformacin nadgrids y su
87
 * manejo
88
 * 
89
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
90
 * @author Jos Luis Gmez Martnez (jolugomar@gmail.com)
91
 * @author Luisa Marina Fernndez (luisam.fernandez@uclm.es)
92
 *
93
 */
94
public class TransformationNadgridsPanel extends JPanel implements IWindow, ActionListener {
95
        
96
        private static final long serialVersionUID = 1L;
97
        
98
        private JPanel groupRadioButton = null;
99
        private JLabel jLabelChooser = null;
100
        private JRadioButton jRadioButtonSource = null;
101
        private JRadioButton jRadioButtonTarget = null;
102
        
103
        private IProjection firstProj;
104
        private String nadFile = null;
105
        String[] targetAuthority;
106
        String targetAbrev = "";
107
        String sourceAbrev = "";
108
        
109
        private String cadWKT = "";
110
        private String dataPath = "./gvSIG/extensiones/org.gvsig.crs/data/";
111
        private int codeEpsg;
112
        
113
        private JPanel jPanelOpen;
114
        private JPanel jPanelSelectNad;
115
        private JLabel jLabelOpenGsb;
116
        private JFileChooser openFileChooser;
117
        private JButton jButtonOpen;
118
        private TreePanel treePanel = null;
119
        private JComboBox jComboNadFile = null;
120
        private JLabel jLabelSelectNad = null;
121
        
122
        
123
        private JTextArea jTextAreaInfo;
124
        
125
        private PluginServices ps = null;
126
        private XMLEntity xml = null;
127
        
128
        boolean targetNad = false;
129

    
130
        public TransformationNadgridsPanel() {
131
                ps = PluginServices.getPluginServices(RecentCRSsPersistence.pluginClassInstance); 
132
                xml = ps.getPersistentXML();
133
                initialize();
134
        }
135
                
136
        private void initialize(){
137
                
138
                this.setLayout(new BorderLayout());
139
                this.setBorder(BorderFactory.createCompoundBorder(
140
                                BorderFactory.createTitledBorder("+ "+PluginServices.getText(this,"nadgrids")),
141
                                BorderFactory.createEmptyBorder(5,5,5,5)));
142
                JPanel pNorth=new JPanel();
143
                pNorth.setBorder(new EmptyBorder(1,10,1,10));
144
                pNorth.setLayout(new GridLayout(4,1));
145
                
146
                pNorth.add(getJPanelOpen());
147
                //label+combo
148
                pNorth.add(getJPanelSelectNad());
149
                JPanel lbl=new JPanel(new FlowLayout(FlowLayout.LEFT,0,10));
150
                lbl.add(getJLabelChooser());
151
                pNorth.add(lbl);
152
                //dos radio buttons
153
                pNorth.add(getGroupRadioButton());
154
                this.add(pNorth,BorderLayout.NORTH);
155
                this.add(getTreePanel(),BorderLayout.CENTER);
156
                
157
        }
158
        
159
        private JPanel getGroupRadioButton() {
160
                if (groupRadioButton == null) {
161
                        groupRadioButton = new JPanel();
162
                        groupRadioButton.setLayout(new GridLayout(1,0));        
163
                        groupRadioButton.add(getJRadioButtonSource());
164
                        groupRadioButton.add(getJRadioButtonTarget());                        
165
                }
166
                return groupRadioButton;
167
        }
168
        
169
        private JRadioButton getJRadioButtonSource() {
170
                if (jRadioButtonSource == null) {
171
                        jRadioButtonSource = new JRadioButton();                        
172
                        jRadioButtonSource.setSelected(true);
173
                        jRadioButtonSource.addActionListener(this);
174
                }
175
                jRadioButtonSource.setText(PluginServices.getText(this,"aplicar_capa") + " " + "("+getSourceAbrev()+")" );
176
                return jRadioButtonSource;
177
        }
178
        
179
        
180
        private JRadioButton getJRadioButtonTarget() {                
181
                if (jRadioButtonTarget == null) {
182
                        jRadioButtonTarget = new JRadioButton();                        
183
                        jRadioButtonTarget.setSelected(false);
184
                        jRadioButtonTarget.addActionListener(this);
185
                }
186
                jRadioButtonTarget.setText(PluginServices.getText(this,"aplicar_vista") + " " + "("+ getTargetAbrev()+")");
187
                return jRadioButtonTarget;
188
        }        
189
        
190
        private JLabel getJLabelChooser(){
191
                jLabelChooser = new JLabel();
192
                jLabelChooser.setText(PluginServices.getText(this, "seleccion_nadgrids")+":");
193
                return jLabelChooser;
194
        }
195
                
196
        private Component getJTextAreaInfo() {
197
                if (jTextAreaInfo == null){
198
                        jTextAreaInfo = new JTextArea();
199
                        jTextAreaInfo.setLineWrap(true);
200
                        jTextAreaInfo.setWrapStyleWord(true);
201
                        jTextAreaInfo.setPreferredSize(new Dimension(400, 100));
202
                        jTextAreaInfo.setEditable(false);
203
                        jTextAreaInfo.setText(getWKT());
204
                }
205
                jTextAreaInfo.setText(getWKT());
206
                return jTextAreaInfo;
207
        }
208
                
209
        private JPanel getJPanelOpen() {
210
                if(jPanelOpen == null) {
211
                        jPanelOpen = new JPanel();
212
                        jPanelOpen.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
213
                        jPanelOpen.add(getJLabelOpenGsb(), null);
214
                        jPanelOpen.add(getJButtonOpen(), null);                        
215
                }
216
                return jPanelOpen;
217
        }
218
        
219
        private JLabel getJLabelOpenGsb() {
220
                if(jLabelOpenGsb == null) {
221
                        jLabelOpenGsb = new JLabel();
222
                        jLabelOpenGsb.setPreferredSize(new Dimension(130,20));
223
                        jLabelOpenGsb.setText(PluginServices.getText(this,"importar")+" .gsb:");
224
                }
225
                return jLabelOpenGsb;
226
        }
227
        
228
        private JFileChooser getOpenFileChooser(){
229
                if(openFileChooser == null){
230
                        openFileChooser = new JFileChooser();                        
231
                        openFileChooser.setEnabled(false);
232
                        openFileChooser.addChoosableFileFilter(new FiltroNadgrids());
233
                }
234
                return openFileChooser;
235
        }
236

    
237
        private JButton getJButtonOpen() {
238
                if(jButtonOpen == null) {
239
                        jButtonOpen = new JButton();
240
                        jButtonOpen.setText("");
241
                        jButtonOpen.setPreferredSize(new Dimension(20,20));
242
                        jButtonOpen.setText("...");
243
                        jButtonOpen.addActionListener(this);
244
                }
245
                return jButtonOpen;
246
        }
247
        
248
        public ICrs getProjection() {
249
                ICrs crs;
250
                if (jRadioButtonSource.isSelected()){
251
                        try {
252
                                setNad(false);
253
                                //crs = new CrsFactory().getCRS(getSourceAbrev(),        getWKT());
254
                                crs = new CrsFactory().getCRS(getSourceAbrev());
255
                                crs.setParametersTranformation("+nadgrids="+getNadFile());//nadFile);
256
                                return crs;
257
                        } catch (org.gvsig.crs.CrsException e) {
258
                                e.printStackTrace();
259
                        }
260
                        return null;
261
                }
262
                else {        
263
                        setNad(true);
264
                        try {
265
                                //crs = new CrsFactory().getCRS(getSourceAbrev(), getWKT());
266
                                crs = new CrsFactory().getCRS(getSourceAbrev());
267
                                crs.setParametersTranformation("+nadgrids="+getNadFile());//nadFile);
268
                                crs.setParametersTransformationInTarget(true);
269
                                
270
                                return crs;
271
                        } catch (CrsException e) {                                
272
                                e.printStackTrace();
273
                        }
274
                        return null;
275
                }                
276
        }
277
        
278
        public void setProjection(IProjection proj) {
279
                firstProj = proj;
280
        }
281
        
282
        public void setNad(boolean nadg){
283
                targetNad = nadg;                
284
        }
285
        
286
        public boolean getNad(){
287
                return targetNad;                
288
        }
289
        
290
        public void setCode(int cod){
291
                codeEpsg = cod;
292
        }
293
        
294
        public int getCode(){
295
                return codeEpsg;
296
        }
297
        
298
        public void setWKT(String cad){
299
                cadWKT = cad;
300
                CrsWkt parser = new CrsWkt(cad);
301
                //setSourceAbrev(parser.getAuthority()[0], parser.getAuthority()[1]);
302
                //getJTextAreaInfo();
303
                //getJRadioButtonSource();
304
        }
305
        
306
        public String getWKT(){
307
                return cadWKT;
308
        }
309
        
310
        public void setTargetAuthority(String[] authority){
311
                targetAuthority = authority;
312
                setTargetAbrev(targetAuthority[0], targetAuthority[1]);
313
                getJRadioButtonTarget();                
314
        }
315
        
316
        public String[] getTargetAuthority(){
317
                return targetAuthority;
318
        }
319
        
320
        public void setTargetAbrev(String fuente, String codigo){
321
                targetAbrev = fuente + ":" + codigo;
322
        }
323
        
324
        public String getTargetAbrev() {
325
                return targetAbrev;
326
        }
327
        
328
        public void setSourceAbrev(String fuente, String codigo){
329
                sourceAbrev = fuente + ":" + codigo;
330
                getJTextAreaInfo();
331
                getJRadioButtonSource();
332
        }
333
        
334
        public String getSourceAbrev(){
335
                return sourceAbrev;
336
        }
337

    
338
        public WindowInfo getWindowInfo() {
339
                WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
340
                   m_viewinfo.setTitle(PluginServices.getText(this,"nadgrids"));
341
                return m_viewinfo;
342
        }
343

    
344
        public void actionPerformed(ActionEvent e) {
345
                if (e.getSource() == this.getJRadioButtonSource()){
346
                        jRadioButtonSource.setSelected(true);
347
                        jRadioButtonTarget.setSelected(false);
348
                }
349
                
350
                if (e.getSource() == this.getJRadioButtonTarget()){
351
                        jRadioButtonTarget.setSelected(true);
352
                        jRadioButtonSource.setSelected(false);
353
                }
354
                
355
                if (e.getSource() == this.getJButtonOpen()){
356
                        jPanelOpen.add(getOpenFileChooser(), null);
357
                        //openChooser.setCurrentDirectory(f);
358
                        int returnVal = openFileChooser.showOpenDialog(TransformationNadgridsPanel.this);
359
                if (returnVal == JFileChooser.APPROVE_OPTION) {
360
                    File inFile = openFileChooser.getSelectedFile();
361
                    //jTextOpen.setText(file.getAbsolutePath());
362

    
363
                    String fileName = inFile.getName();
364
                    
365
                    //Comprobar que el fichero existe y tiene formato valido
366
                            RandomAccessFile raFile = null;
367
                            try {
368
                                    raFile = new RandomAccessFile(inFile.getAbsolutePath(),"r");
369
                            } catch (FileNotFoundException ex) {
370
                                    ex.printStackTrace();
371
                                    getJComboNadFile().setSelectedIndex(0);
372
                                    getTreePanel().setRoot(PluginServices.getText(this,"nadfile_not_found"));
373
                                    setNadFile(null);//nadFile = null;
374
                                    return;
375
                            }
376
                            GridShiftFile gsf = new GridShiftFile();
377
                            try {
378
                                    gsf.loadGridShiftFile(raFile);
379
                            } catch (IOException ex) {
380
                                    ex.printStackTrace();
381
                                    getJComboNadFile().setSelectedIndex(0);
382
                                    getTreePanel().setRoot(PluginServices.getText(this,"nadfile_bad_format"));
383
                                    setNadFile(null);//nadFile = null;
384
                                    return;
385
                            }catch (java.lang.Exception ex) {
386
                                    ex.printStackTrace();
387
                                    getJComboNadFile().setSelectedIndex(0);
388
                                    getTreePanel().setRoot(PluginServices.getText(this,"nadfile_bad_format"));
389
                                    setNadFile(null);//nadFile = null;
390
                                    return;
391
                            }
392
                    
393
                    //Copiar el fichero seleccionado al directorio data de la extensin:
394
                    
395
                            File outFile = new File(dataPath+fileName);
396
                            InputStream in = null;
397
                            OutputStream out = null;
398
                            try {
399
                                    in = new FileInputStream(inFile);
400
                                    out = new FileOutputStream(outFile);
401
                            } catch (FileNotFoundException ex) {
402
                                    ex.printStackTrace();
403
                            }
404
                            byte[] buf = new byte[1024];
405
                            int len;
406
                            try {
407
                                    while ((len = in.read(buf)) > 0) {
408
                                            out.write(buf, 0, len);
409
                                    }
410
                                    in.close();
411
                                    out.close();
412
                            } catch (IOException ex) {
413
                                    ex.printStackTrace();
414
                            }
415
                            
416
                            //Incluir el fichero importado en el combo y seleccionarlo
417
                            boolean exists = false;
418
                            for (int item=0; item<getJComboNadFile().getItemCount() && !exists;item++)
419
                                    if (getJComboNadFile().getItemAt(item).equals(fileName))
420
                                            exists = true;
421
                            if(!exists)
422
                                    getJComboNadFile().addItem(fileName);
423
                            getJComboNadFile().setSelectedItem(fileName);
424
                            setNadFile(fileName);//nadFile = fileName;
425
                }
426
                }
427
        }
428

    
429
        public TreePanel getTreePanel() {
430
                if (treePanel == null){
431
                        treePanel = new TreePanel(PluginServices.getText(this,"grids_en")+": "+nadFile);
432
                        treePanel.getTree().expandRow(0);
433
                        treePanel.setPanelSize(530,150);
434
                }
435
                return treePanel;
436
        }
437
        
438
        public void initializeTree(){
439
                // leer el fichero nadgrids
440
                RandomAccessFile raFile = null;
441
                try {
442
                        raFile = new RandomAccessFile("./gvSIG/extensiones/org.gvsig.crs/data/"+nadFile,"r");
443
                } catch (FileNotFoundException e) {
444
                        e.printStackTrace();
445
                        getTreePanel().setRoot(PluginServices.getText(this,"nadfile_not_found"));
446
                        setNadFile(null);//nadFile = null;
447
                        return;
448
                }
449
                GridShiftFile gsf = new GridShiftFile();
450
                try {
451
                        gsf.loadGridShiftFile(raFile);
452
                } catch (IOException e) {
453
                        e.printStackTrace();
454
                        getTreePanel().setRoot(PluginServices.getText(this,"nadfile_bad_format"));
455
                        setNadFile(null);//nadFile = null;
456
                        return;
457
                }catch (java.lang.Exception e) {
458
                        e.printStackTrace();
459
                        getTreePanel().setRoot(PluginServices.getText(this,"nadfile_bad_format"));
460
                        setNadFile(null);//nadFile = null;
461
                        return;
462
                }
463
                
464
                SubGrid subGrid[] = gsf.getSubGridTree(); 
465
                
466
                for (int i=0;i<subGrid.length;i++){
467
                        getTreePanel().addClass(subGrid[i].getSubGridName(),i);
468
                        getTreePanel().addEntry(PluginServices.getText(this,"long_min")+": "+String.valueOf(subGrid[i].getMaxLon()/-3600)+"",subGrid[i].getSubGridName(), "");
469
                        getTreePanel().addEntry(PluginServices.getText(this,"lat_min")+": "+String.valueOf(subGrid[i].getMinLat()/3600)+"?",subGrid[i].getSubGridName(), "");
470
                        getTreePanel().addEntry(PluginServices.getText(this,"long_max")+": "+String.valueOf(subGrid[i].getMinLon()/-3600+"?"),subGrid[i].getSubGridName(), "");
471
                        getTreePanel().addEntry(PluginServices.getText(this,"lat_max")+": "+String.valueOf(subGrid[i].getMaxLat()/3600+""),subGrid[i].getSubGridName(), "");
472
                        getTreePanel().addEntry(PluginServices.getText(this,"node_count")+": "+String.valueOf(subGrid[i].getNodeCount()),subGrid[i].getSubGridName(), "");
473
                        getTreePanel().addEntry(PluginServices.getText(this,"detalles")+": "+String.valueOf(subGrid[i].getDetails()),subGrid[i].getSubGridName(), "");
474
                }
475
                getTreePanel().getTree().expandRow(0);
476
                getTreePanel().getList().setText(PluginServices.getText(this,"advertencia_nad"));
477
        }
478

    
479
        public JComboBox getJComboNadFile() {
480
                if (jComboNadFile == null){
481
                        jComboNadFile = new JComboBox();
482
                        jComboNadFile.setPreferredSize(new Dimension(200,25));
483
                        jComboNadFile.setEditable(false);
484
                        jComboNadFile.addItem(PluginServices.getText(this,"seleccionar")+"...");
485
                        //Aadir los al combo los nombres de los .gsb que hay en el directorio data
486
                        File dataDir = new File(dataPath);
487
                        for (int i = 0; i<dataDir.list().length;i++)
488
                                if (dataDir.list()[i].substring(dataDir.list()[i].lastIndexOf('.')+1).equals("gsb")){
489
                                        jComboNadFile.addItem(dataDir.list()[i]);
490
                                }
491
                        if (getNadFile() != null)//nadFile != null)
492
                                jComboNadFile.setSelectedItem(getNadFile());//nadFile);
493
                                                
494
                }
495
                return jComboNadFile;
496
        }
497

    
498
        public JLabel getJLabelSelectNad() {
499
                if (jLabelSelectNad == null){
500
                        jLabelSelectNad = new JLabel(PluginServices.getText(this,"Fichero nadgrids")+":  ");
501
                        jLabelSelectNad.setPreferredSize(new Dimension(130,25));
502
                }
503
                return jLabelSelectNad;
504
        }
505

    
506
        public JPanel getJPanelSelectNad() {
507
                if (jPanelSelectNad == null){
508
                        jPanelSelectNad = new JPanel();
509
                        jPanelSelectNad.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));                
510
                        jPanelSelectNad.add(getJLabelSelectNad());
511
                        jPanelSelectNad.add(getJComboNadFile());
512
                }
513
                return jPanelSelectNad;
514
        }
515
        
516
        public void setNadFile(String nad){
517
                nadFile = nad;
518
        }
519
        
520
        public String getNadFile(){
521
                return nadFile;
522
        }        
523
        
524
        /**
525
         * Guarda el nombre del fichero nadgrids mediante el modelo de persistencia
526
         * de Andami
527
         * @param name Nombre del fichero nadgrids
528
         */
529
        public void saveNadFileName(String name){
530
                ps = PluginServices.getPluginServices(RecentCRSsPersistence.pluginClassInstance); 
531
                xml = ps.getPersistentXML();
532
                
533
                for (int child = 0; child<xml.getChildrenCount();child++)
534
                        if (xml.getChild(child).getPropertyName(0).equals("nadFile"))
535
                                xml.removeChild(child);
536
                
537
                
538
                XMLEntity xmlEnt = new XMLEntity();
539
                xmlEnt.putProperty("nadFile",name);
540
                xml.addChild(xmlEnt);
541
                ps.setPersistentXML(xml);
542
        }
543
        
544
        /**
545
         * Obtiene el nombre del fichero nadgrids guardado mediante el
546
         * modelo de persistencia de Andami.
547
         * 
548
         * @return nombre del fichero nadgrids o null si no ha sido guardado.
549
         */
550
        private String restoreNadFileName(){
551
                String fileName;
552
                for (int child = 0; child<xml.getChildrenCount();child++)
553
                        if (xml.getChild(child).getPropertyName(0).equals("nadFile")){
554
                                fileName = xml.getChild(child).getPropertyValue(0);
555
                                File dataDir = new File(dataPath);
556
                                for (int i = 0; i<dataDir.list().length;i++)
557
                                        if (dataDir.list()[i].equals(fileName)){
558
                                                return fileName;
559
                                        }
560
                        }
561
                
562
                return null;
563
        }
564
        
565
}
566

    
567
class FiltroNadgrids extends FileFilter {
568

    
569
        final static String gsb = "gsb";
570
        public boolean accept(File f) {
571
                if (f.isDirectory()) {
572
           return true;
573
       }
574
       String s = f.getName();
575
       int i = s.lastIndexOf('.');
576

    
577
       if (i > 0 &&  i < s.length() - 1) {
578
           String extension = s.substring(i+1).toLowerCase();
579
           if (gsb.equals(extension)){
580
                   return true;
581
           } else {
582
               return false;
583
           }
584
       }
585
       return false;
586
        }
587

    
588
        public String getDescription() {
589
                 return "Archivos .gsb";
590
        }
591
        
592
}