Statistics
| Revision:

root / branches / F2 / extensions / extJCRS / src / org / gvsig / crs / gui / panels / TransformationNadgridsPanel.java @ 10788

History | View | Annotate | Download (17.5 KB)

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

    
41
package org.gvsig.crs.gui.panels;
42

    
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 tranformaci?n nadgrids y su
87
 * manejo
88
 * 
89
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
90
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
91
 * @author Luisa Marina Fern?ndez (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(getCode(),
254
                                                getWKT());
255
                                crs.setNadGrid("+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(getCode(),
266
                                                getWKT());
267
                                crs.setNadGrid("+nadgrids="+getNadFile());//nadFile);
268
                                crs.setNadInTarget(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
        }
331
        
332
        public String getSourceAbrev(){
333
                return sourceAbrev;
334
        }
335

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

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

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

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

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

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

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

    
565
class FiltroNadgrids extends FileFilter {
566

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

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

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