Statistics
| Revision:

svn-gvsig-desktop / branches / F2 / extensions / extJCRS / src / org / gvsig / crs / gui / panels / wizard / MainPanel.java @ 11812

History | View | Annotate | Download (16.5 KB)

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

    
3
import java.awt.BorderLayout;
4
import java.awt.CardLayout;
5
import java.awt.FlowLayout;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.sql.ResultSet;
9
import java.sql.SQLException;
10
import java.util.ArrayList;
11

    
12
import javax.swing.JButton;
13
import javax.swing.JOptionPane;
14
import javax.swing.JPanel;
15
import javax.swing.JTabbedPane;
16
import javax.swing.event.ChangeEvent;
17
import javax.swing.event.ChangeListener;
18

    
19
import org.gvsig.crs.CrsException;
20
import org.gvsig.crs.ICrs;
21

    
22
import com.iver.andami.PluginServices;
23
import com.iver.andami.ui.mdiManager.IWindow;
24
import com.iver.andami.ui.mdiManager.WindowInfo;
25

    
26
import es.idr.teledeteccion.connection.EpsgConnection;
27
import es.idr.teledeteccion.connection.Query;
28

    
29

    
30
/**
31
 * 
32
 * @author Luisa Marina Fernndez (luisam.fernandez@uclm.es)
33
 *
34
 */
35
public class MainPanel extends JPanel implements ActionListener, ChangeListener, IWindow {
36
        
37
        private static final long serialVersionUID = 1L;
38
        private JTabbedPane pCard;
39
        private JPanel pSouth;
40
        
41
        private JButton btnCancelar;
42
        private JButton btnSiguiente;
43
        private JButton btnAnterior;
44
        private JButton btnFinalizar;
45
        
46
        private DefCrsUsr pCrsUsr;
47
        private DefinirDatum pDatum;
48
        private DefSistCoordenadas pSistCoord;
49
        private ICrs currentCrs;
50
        private String cadWkt = "";
51
        private int newCrsCode = -1;
52
        private boolean edit = false;
53
        
54
        public MainPanel(ICrs crs) {
55
                super();
56
                setCrs(crs);
57
                this.setLayout(new BorderLayout());
58
                this.add(getPCard(),BorderLayout.CENTER);
59
                this.add(getPSouth(),BorderLayout.SOUTH);
60
        }
61
        /**
62
         * Inicilizar el botn Anterior
63
         * @return
64
         */
65
        public JButton getBtnAnterior() {
66
                if(btnAnterior==null){
67
                        btnAnterior=new JButton();
68
                        btnAnterior.setText(PluginServices.getText(this,"wz_anterior"));
69
                        btnAnterior.addActionListener(this);
70
                        
71
                }
72
                return btnAnterior;
73
        }
74
        /**
75
         * Inicilizar el botn Cancelar
76
         * @return
77
         */
78
        public JButton getBtnCancelar() {
79
                if(btnCancelar==null){
80
                        btnCancelar=new JButton();
81
                        btnCancelar.setText(PluginServices.getText(this,"wz_cancel"));
82
                        btnCancelar.addActionListener(this);
83
                }
84
                return btnCancelar;
85
        }
86
        /**
87
         * Inicilizar el botn Finalizar
88
         * @return
89
         */
90
        public JButton getBtnFinalizar() {
91
                if(btnFinalizar==null){
92
                        btnFinalizar=new JButton();
93
                        btnFinalizar.setText(PluginServices.getText(this,"wz_fin"));
94
                        btnFinalizar.addActionListener(this);
95
                }
96
                return btnFinalizar;
97
        }
98
        /**
99
         * Inicilizar el botn Siguiente
100
         * @return
101
         */
102
        public JButton getBtnSiguiente() {
103
                if(btnSiguiente==null){
104
                        btnSiguiente=new JButton();
105
                        btnSiguiente.setText(PluginServices.getText(this,"wz_siguiente"));
106
                        btnSiguiente.addActionListener(this);
107
                }
108
                return btnSiguiente;
109
        }
110
        /**
111
         * Inicilizar el panel que maneja los subpaneles del asistente
112
         * @return
113
         */
114
        public JTabbedPane getPCard() {
115
                if(pCard==null){
116
                        pCard = new JTabbedPane();                        
117
                        pCard.addTab(PluginServices.getText(this,"crs_usuario"),getPCrsUsr());
118
                        pCard.addTab(PluginServices.getText(this,"Dat_nDatum"),getPDatum());
119
                        pCard.addTab(PluginServices.getText(this,"SistCoor_titmarco"),getPSistCoord());
120
                        //Por defecto se muestra la primera pestaa
121
                        pCard.setSelectedIndex(0);
122
                        pCard.addChangeListener(this);
123
                }
124
                return pCard;
125
        }
126
        /**
127
         * Iniciliza el panel que contiene los botones del asistente
128
         * @return
129
         */
130
        public JPanel getPSouth() {
131
                if(pSouth==null){
132
                        pSouth=new JPanel();
133
                        pSouth.setLayout(new FlowLayout(FlowLayout.RIGHT,5,5));
134
                        pSouth.add(getBtnCancelar());
135
                        pSouth.add(getBtnAnterior());
136
                        pSouth.add(getBtnSiguiente());
137
                        pSouth.add(getBtnFinalizar());
138
                        getBtnFinalizar().setVisible(false);
139
                        getBtnAnterior().setVisible(false);
140
                }
141
                return pSouth;
142
        }
143
        public DefCrsUsr getPCrsUsr() {
144
                if(pCrsUsr==null){
145
                        pCrsUsr=new DefCrsUsr(getCrs());
146
                }
147
                return pCrsUsr;
148
        }
149
        public DefinirDatum getPDatum() {
150
                if(pDatum==null){
151
                        pDatum=new DefinirDatum();
152
                }
153
                return pDatum;
154
        }
155
        public DefSistCoordenadas getPSistCoord() {
156
                if(pSistCoord==null){
157
                        pSistCoord=new DefSistCoordenadas();
158
                }
159
                return pSistCoord;
160
        }
161
        public void actionPerformed(ActionEvent e) {
162
        
163
                if(e.getSource().equals(getBtnAnterior())){
164
                        if (getPSistCoord().isShowing() && isEditing()) {
165
                                getBtnAnterior().setVisible(false);
166
                        }
167
                        if(getPDatum().isShowing()){
168
                                //Se va a mostrar el primer panel del Wizard
169
                                getBtnAnterior().setVisible(false);
170
                                getPCard().setSelectedComponent(getPCrsUsr());
171
                        }
172
                        else if (getPSistCoord().isShowing()) {
173
                                getPCard().setSelectedComponent(getPDatum());
174
                        }
175
                        
176
                        //Establecer los botones del Wizard
177
                        getBtnFinalizar().setVisible(false);
178
                        getBtnSiguiente().setVisible(true);
179
                        
180
                }else if(e.getSource().equals(getBtnSiguiente())){
181
                        if (getPCrsUsr().isShowing() && getPCrsUsr().getRbCrsExistente().isSelected() ){
182
                                ICrs crs = getPCrsUsr().getCrs();
183
                                if (crs != null){
184
                                        fillData(crs);
185
                                }
186
                                else fillData(getCrs());
187
                        }
188
                        else if (getPCrsUsr().isShowing() && getPCrsUsr().getRbNuevoCrs().isSelected() ){
189
                                cleanData();
190
                        }
191
                        if (getPCrsUsr().isShowing())                        
192
                                getPCard().setSelectedComponent(getPDatum());
193
                        else if (getPDatum().isShowing())
194
                                getPCard().setSelectedComponent(getPSistCoord());
195
                        
196
                        getBtnAnterior().setVisible(true);
197
                        //Si aparece el ultimo panel visualizar el botn finalizar
198
                        if (getPDatum().isShowing()){
199
                                getBtnFinalizar().setVisible(true);
200
                                getBtnSiguiente().setVisible(false);
201
                                getBtnAnterior().setVisible(true);
202
                        }
203
                }else if(e.getSource().equals(getBtnFinalizar())){
204
                        //Realizar las acciones de fin del Wizard
205
                        getDataAndUpdate();                        
206
                }else if(e.getSource().equals(getBtnCancelar())){
207
                        //Cerrar el asistente
208
                        PluginServices.getMDIManager().closeWindow(this);
209
                }
210
        }
211
        public WindowInfo getWindowInfo() {
212
                WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
213
                   m_viewinfo.setTitle(PluginServices.getText(this,"wz_titulo"));
214
                   m_viewinfo.setWidth(560);
215
                   //m_viewinfo.setHeight(425);
216
                   m_viewinfo.setHeight(400);
217
                return m_viewinfo;
218
        }
219
        
220
        public ICrs getCrs () {
221
                return currentCrs;
222
        }
223
        
224
        public void setCrs (ICrs crs) {
225
                currentCrs = crs;
226
        }
227
        
228
        
229
        
230
        public void fillData(ICrs crs) {
231
                getPDatum().fillData(crs);
232
                getPSistCoord().fillData(crs);
233
        }
234
        
235
        public void cleanData() {
236
                getPDatum().cleanData();
237
                getPSistCoord().cleanData();
238
        }
239
        
240
        private void getDataAndUpdate() {
241
                EpsgConnection conn = new EpsgConnection();
242
                conn.setConnectionUsr();
243
                String codeCrs = "";
244
                String sentence;
245
                ResultSet result;
246
                
247
                if (getPDatum().getTxtSemMay().getText().equals("")) {
248
                        JOptionPane.showMessageDialog(MainPanel.this, 
249
                                        PluginServices.getText(this,"white_Textbox")+": Semieje Mayor", 
250
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
251
                        return;
252
                }
253
                
254
                if (getPDatum().getTxtLong().getText().equals("")) {
255
                        JOptionPane.showMessageDialog(MainPanel.this, 
256
                                        PluginServices.getText(this,"white_Textbox")+": Longitud", 
257
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
258
                        return;
259
                }
260
                
261
                if (getPDatum().getTxtCodigoCrs().getText().equals("")) {
262
                        JOptionPane.showMessageDialog(MainPanel.this, 
263
                                        PluginServices.getText(this,"white_Textbox")+": Codigo CRS", 
264
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
265
                        return;
266
                }
267
                
268
                String cadenaNumerica = getPDatum().getTxtSemMay().getText().replaceAll("[^0-9.E-]", "");
269
                                                
270
                if (getPDatum().getTxtSemMay().getText().length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
271
                        JOptionPane.showMessageDialog(MainPanel.this, 
272
                                        PluginServices.getText(this,"numeric_format")+": Semieje Mayor", 
273
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
274
                        return;
275
                }                
276
                
277
                cadenaNumerica = getPDatum().getTxtLong().getText().replaceAll("[^0-9.E-]", "");
278
                
279
                if (getPDatum().getTxtLong().getText().length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
280
                
281
                        JOptionPane.showMessageDialog(MainPanel.this, 
282
                                        PluginServices.getText(this,"numeric_format")+": Longitud", 
283
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
284
                        return;
285
                }
286
                
287
                codeCrs = getPDatum().getTxtCodigoCrs().getText().replaceAll("[^0-9]", "");
288
                
289
                if (getPDatum().getTxtCodigoCrs().getText().length() != codeCrs.length()) {
290
                        
291
                        JOptionPane.showMessageDialog(MainPanel.this, 
292
                                        PluginServices.getText(this,"numeric_format")+": Codigo CRS", 
293
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
294
                        return;
295
                }
296
                
297
                double value = Double.valueOf(getPDatum().getTxtSemMay().getText()).doubleValue();
298
                //Comprobar si es numerico, o quedarnos con la parte numerica...
299
                //searchTextField.getText().length()!=searchTextField.getText().replaceAll("[^0-9]", "").length())
300
                String unit = (String)getPDatum().getCbSemMay().getSelectedItem();
301
                double semMay = getPDatum().convert2Meters(unit, value);
302
                value = Double.valueOf(getPDatum().getTxtLong().getText()).doubleValue();
303
                unit = (String)getPDatum().getCbLong().getSelectedItem();
304
                double longitude = getPDatum().convert2Meters(unit, value);
305
                
306
                String[] spheroid = {getPDatum().getTxtElipsoide().getText(),
307
                                                        ""+semMay,getPDatum().getTxtInvF().getText()};
308
                String[] primem = {getPDatum().getTxtMeridian().getText(), ""+longitude};
309
                
310
                String[] authority = {"\"USR\"", codeCrs};
311
                if (!isEditing()){
312
                        sentence = "SELECT usr_code FROM USR WHERE usr_code = " +authority[1];
313
                        result = Query.select(sentence, conn.getConnection());
314
                        try {
315
                                if (result.next()) {
316
                                        JOptionPane.showMessageDialog(MainPanel.this, 
317
                                                        PluginServices.getText(this,"crsRepeat")+": "+authority[1], 
318
                                                        "Warning...", JOptionPane.WARNING_MESSAGE);
319
                                        return;
320
                                }
321
                        } catch (SQLException e1) {
322
                                // TODO Auto-generated catch block
323
                                e1.printStackTrace();
324
                        }                                
325
                }
326
                String datum = getPDatum().getTxtDatum().getText();
327
                
328
                
329
                if (getPSistCoord().getRbGeografico().isSelected()) {
330
                        cadWkt = "GEOGCS[\"" + getPDatum().getTxtNombreCrs().getText() + "\", DATUM[\""+ datum +
331
                        "\", SPHEROID[\""+ spheroid[0] + "\", "+ spheroid[1] + ", "+ spheroid[2] +"]], " +
332
                        "PRIMEM[\""+ primem[0] + "\", "+ primem[1] +"], UNIT[\"Degree\", " + (Math.PI/180) +
333
                        "]]";
334
                        if (!isEditing()){
335
                                sentence = "INSERT INTO USR VALUES("+authority[1]+",'" +
336
                                                        cadWkt +"','','"+getPDatum().getTxtNombreCrs().getText()+"','"+datum+"')";
337
                        } else { 
338
                                sentence = "UPDATE USR SET usr_wkt='" +cadWkt +"'," +
339
                                                "usr_proj='',usr_geog='"+getPDatum().getTxtNombreCrs().getText()+"'," +
340
                                                "usr_datum='"+datum+"' " +
341
                                                "WHERE usr_code = " +authority[1];
342
                        }
343
        
344
                } else {
345
                
346
                        cadWkt = "PROJCS[\""+getPSistCoord().getTxtNombreProy().getText()+
347
                                "\", GEOGCS[\"" + getPDatum().getTxtNombreCrs().getText() + "\", DATUM[\""+ datum +
348
                                "\", SPHEROID[\""+ spheroid[0] + "\", "+ spheroid[1] + ", "+ spheroid[2] +"]], " +
349
                                "PRIMEM[\""+ primem[0] + "\", "+ primem[1] +"], UNIT[\"Degree\", " + (Math.PI/180) +
350
                                "]], PROJECTION[\""+ getPSistCoord().getCbProyeccion().getSelectedItem().toString() + "\"], ";
351
                                
352
                        
353
                        ArrayList maxValues = null;
354
                        ArrayList minValues = null;
355
                        int paramPos = 0;
356
                        try {
357
                                maxValues = getPSistCoord().getProj4().getProj4ProjectionParameterMaxValues(getPSistCoord().getPos());
358
                                minValues = getPSistCoord().getProj4().getProj4ProjectionParameterMinValues(getPSistCoord().getPos());
359
                                
360
                        } catch (CrsException e) {
361
                                e.printStackTrace();
362
                        }
363
                        /*
364
                         * falta la parte de los parámetros... metodo para nombres...
365
                         */                
366
                        for (int i= 0; i< getPSistCoord().getTableParametros().getRowCount();i++){
367
                                if (((String)(getPSistCoord().getTableParametros().getValueAt(i,1))).equals("")) {
368
                                        getPSistCoord().getTableParametros().setValueAt("0",i,1);
369
                                }
370
                                cadenaNumerica = ((String)(getPSistCoord().getTableParametros().getValueAt(i,1))).replaceAll("[^0-9.E-]", "");
371
                                
372
                                if (((String)(getPSistCoord().getTableParametros().getValueAt(i,1))).length() != cadenaNumerica.length() || notANumber(cadenaNumerica)) {
373
                        
374
                                        JOptionPane.showMessageDialog(MainPanel.this, 
375
                                                        PluginServices.getText(this,"numeric_format")+": Parametro "+(String)(getPSistCoord().getTableParametros().getValueAt(i,0)), 
376
                                                        "Warning...", JOptionPane.WARNING_MESSAGE);
377
                                        return;
378
                                }
379
                                String param = (String)getPSistCoord().getTableParametros().getValueAt(i, 0);
380
                                value = Double.parseDouble(cadenaNumerica);
381
                                if (!(param.trim().equals("semi_major") || param.trim().equals("semi_minor"))) {
382
                                        double maxValue = Double.parseDouble((String)maxValues.get(paramPos));
383
                                        double minValue = Double.parseDouble((String) minValues.get(paramPos));
384
                                        if (value > maxValue || value < minValue) {
385
                                                JOptionPane.showMessageDialog(MainPanel.this, 
386
                                                                PluginServices.getText(this,"incorrect_domain")+": Parametro "+(String)(getPSistCoord().getTableParametros().getValueAt(i,0)), 
387
                                                                "Warning...", JOptionPane.WARNING_MESSAGE);
388
                                                return;
389
                                        }                                        
390
                                        paramPos ++;
391
                                }
392
                                                                
393
                                value = 0;
394
                                unit = "";
395
                                /**
396
                                 * Esto está hecho en general, habrá que hacerlo dependiendo del tipo de
397
                                 * unidad que tenga el parametro
398
                                 */
399
                                String type = (String) getPSistCoord().getTableParametros().getValueAt(i, 2);
400
                                if (type.equals("Meters")) {
401
                                        value = Double.parseDouble((String)getPSistCoord().getTableParametros().getValueAt(i, 1));
402
                                        unit = (String)getPSistCoord().getCbUnits().getSelectedItem();
403
                                        value = getPSistCoord().convert2Meters(unit, value);
404
                                }
405
                                else if (type.equals("Degree")) {
406
                                        value = Double.parseDouble((String)getPSistCoord().getTableParametros().getValueAt(i, 1));
407
                                        unit = (String)getPSistCoord().getCbUnits().getSelectedItem();
408
                                        value = getPSistCoord().convert2Degree(unit, value);
409
                                }
410
                                else if (type.equals("Unitless")) {
411
                                        value = Double.parseDouble((String)getPSistCoord().getTableParametros().getValueAt(i, 1));
412
                                        unit = (String)getPSistCoord().getCbUnits().getSelectedItem();
413
                                        value = getPSistCoord().convert2Unitless(unit, value);
414
                                }
415
                                cadWkt += "PARAMETER[\""+getPSistCoord().getTableParametros().getValueAt(i, 0)+"\", " + 
416
                                                        value+ "], ";
417
                        }
418
                        
419
                        cadWkt += "UNIT[\"Meters\", 1.0]]";
420
                        
421
                        if (!isEditing()) {
422
                                sentence = "INSERT INTO USR VALUES("+authority[1]+",'" +
423
                                cadWkt +"','"+getPSistCoord().getTxtNombreProy().getText()+"','"+getPDatum().getTxtNombreCrs().getText()+"','"+datum+"')";
424
                        }
425
                        else { 
426
                                sentence = "UPDATE USR SET usr_wkt='" + cadWkt +"'," +
427
                                                "usr_proj='"+getPSistCoord().getTxtNombreProy().getText()+"'," +
428
                                                "usr_geog='"+getPDatum().getTxtNombreCrs().getText()+"'," +
429
                                                "usr_datum='"+datum+"' " +
430
                                                "WHERE usr_code = " +authority[1];
431
                        }
432
                }                
433
                                
434
                try {
435
                        conn.update(sentence);
436
                        conn.shutdown();
437
                } catch (SQLException e) {
438
                        e.printStackTrace();
439
                }                
440
                setNewCrsCode(Integer.parseInt(authority[1]));
441
                PluginServices.getMDIManager().closeWindow(this);
442
        }
443
        
444
        public void setNewCrsCode(int code) {
445
                newCrsCode = code;
446
        }
447
        
448
        public int getNewCrsCode() {
449
                return newCrsCode;
450
        }
451
        
452
        private boolean notANumber(String cadenaNumerica) {
453
                int puntos = 0;
454
                int signos = 0;
455
                int letras = 0;
456
                for (int i = 0; i< cadenaNumerica.length(); i++) {
457
                        if (cadenaNumerica.charAt(i) == '.')
458
                                puntos++;
459
                        else if (cadenaNumerica.charAt(i) == '-') {
460
                                if (i==0) {
461
                                        signos++;
462
                                }
463
                                else if (i!=0 && cadenaNumerica.charAt(i-1) != 'E') {
464
                                        signos = 2;
465
                                }
466
                        }
467
                        else if (cadenaNumerica.charAt(i) == 'E') {
468
                                if (i== 0) {
469
                                        letras = 2;
470
                                }
471
                                else letras ++;
472
                        }
473
                }
474
                
475
                if ((letras > 1) || (signos > 1) || (puntos > 1))
476
                        return true;
477
                return false;
478
        }
479
        
480
        public void setEditing(boolean edit) {
481
                this.edit = edit;
482
        }
483
        
484
        public boolean isEditing() {
485
                return this.edit;
486
        }
487
        
488
        public void setEditingPanel() {
489
                pCard.setSelectedComponent(getPDatum());
490
                pCard.setEnabledAt(0, false);
491
                fillData(getCrs());
492
                getPDatum().getTxtCodigoCrs().setEnabled(false);
493
                
494
        }
495
        public void stateChanged(ChangeEvent e) {
496
                if (e.getSource() == getPCard()) {
497
                        if (getPCrsUsr().getHasChanged()) {
498
                                if (getPCrsUsr().getRbCrsExistente().isSelected()) {
499
                                        ICrs crs = getPCrsUsr().getCrs();
500
                                        if (crs != null) fillData(crs);                                        
501
                                        else fillData(getCrs());
502
                                        getPCrsUsr().setHasChange(false);
503
                                }
504
                                else if (getPCrsUsr().getRbNuevoCrs().isSelected()) {
505
                                        cleanData();
506
                                        getPCrsUsr().setHasChange(false);
507
                                }
508
                        }
509
                        int i = ((JTabbedPane)e.getSource()).getSelectedIndex();
510
                        if (i==0){
511
                                getBtnAnterior().setVisible(false);
512
                                getBtnSiguiente().setVisible(true);
513
                                getBtnFinalizar().setVisible(false);
514
                        }
515
                        else if (i==1 && !isEditing()) {
516
                                getBtnAnterior().setVisible(true);
517
                                getBtnSiguiente().setVisible(true);
518
                                getBtnFinalizar().setVisible(false);                                
519
                        }
520
                        else if (i==1 && isEditing()) {
521
                                getBtnAnterior().setVisible(false);
522
                                getBtnSiguiente().setVisible(true);
523
                                getBtnFinalizar().setVisible(false);
524
                        }
525
                        else if (i == 2) {                                
526
                                getBtnAnterior().setVisible(true);
527
                                getBtnSiguiente().setVisible(false);
528
                                getBtnFinalizar().setVisible(true);                                
529
                        }
530
                }
531
                
532
        }
533
        
534
}