Statistics
| Revision:

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

History | View | Annotate | Download (11.6 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

    
11
import javax.swing.JButton;
12
import javax.swing.JOptionPane;
13
import javax.swing.JPanel;
14

    
15
import org.gvsig.crs.ICrs;
16
import org.gvsig.crs.gui.panels.EPSGpanel;
17

    
18
import com.iver.andami.PluginServices;
19
import com.iver.andami.ui.mdiManager.IWindow;
20
import com.iver.andami.ui.mdiManager.WindowInfo;
21

    
22
import es.idr.teledeteccion.connection.EpsgConnection;
23
import es.idr.teledeteccion.connection.Query;
24

    
25

    
26
/**
27
 * 
28
 * @author Luisa Marina Fernndez (luisam.fernandez@uclm.es)
29
 *
30
 */
31
public class MainPanel extends JPanel implements ActionListener,IWindow {
32
        
33
        private static final long serialVersionUID = 1L;
34
        private JPanel pCard;
35
        private JPanel pSouth;
36
        
37
        private JButton btnCancelar;
38
        private JButton btnSiguiente;
39
        private JButton btnAnterior;
40
        private JButton btnFinalizar;
41
        
42
        private DefCrsUsr pCrsUsr;
43
        private DefinirDatum pDatum;
44
        private DefSistCoordenadas pSistCoord;
45
        
46
         final static String CRSUSUARIOPANEL = "CrsUsuario";
47
         final static String DEFINIRDATUMPANEL = "DefinirDatum"; 
48
         final static String SISTEMACOORDENADASPANEL = "SistemaCoordenadas";
49
         
50
        private ICrs currentCrs;
51
        private String cadWkt = "";
52
        private int newCrsCode = -1;
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 JPanel getPCard() {
115
                if(pCard==null){
116
                        pCard=new JPanel();
117
                        pCard.setLayout(new CardLayout());
118
                        
119
                        pCard.add(CRSUSUARIOPANEL,getPCrsUsr());
120
                        pCard.add(DEFINIRDATUMPANEL,getPDatum());
121
                        pCard.add(SISTEMACOORDENADASPANEL,getPSistCoord());
122
                        
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
                CardLayout cl = (CardLayout)(pCard.getLayout());  
164
                
165
                if(e.getSource().equals(getBtnAnterior())){
166
                        
167
                        if(getPDatum().isShowing()){
168
                                //Se va a mostrar el primer panel del Wizard
169
                                getBtnAnterior().setVisible(false);
170
                        }
171
                        //Mostrar el panel anterior
172
                        cl.previous(pCard);
173
                        //Establecer los botones del Wizard
174
                        getBtnFinalizar().setVisible(false);
175
                        getBtnSiguiente().setVisible(true);
176
                        
177
                }else if(e.getSource().equals(getBtnSiguiente())){
178
                        if (getPCrsUsr().isShowing() && getPCrsUsr().getRbCrsExistente().isSelected() ){
179
                                ICrs crs = getPCrsUsr().getCrs();
180
                                if (crs != null){
181
                                        fillData(crs);
182
                                }
183
                                else fillData(getCrs());
184
                        }
185
                        else if (getPCrsUsr().isShowing() && getPCrsUsr().getRbNuevoCrs().isSelected() ){
186
                                cleanData();
187
                        }
188
                        cl.next(pCard);
189
                        getBtnAnterior().setVisible(true);
190
                        //Si aparece el ultimo panel visualizar el botn finalizar
191
                        if (getPSistCoord().isShowing()){
192
                                getBtnFinalizar().setVisible(true);
193
                                getBtnSiguiente().setVisible(false);
194
                                getBtnAnterior().setVisible(true);
195
                        }
196
                                
197
                                
198
                }else if(e.getSource().equals(getBtnFinalizar())){
199
                        //Realizar las acciones de fin del Wizard
200
                        getDataAndUpdate();                        
201
                }else if(e.getSource().equals(getBtnCancelar())){
202
                        //Cerrar el asistente
203
                        PluginServices.getMDIManager().closeWindow(this);
204
                }
205
        }
206
        public WindowInfo getWindowInfo() {
207
                WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
208
                   m_viewinfo.setTitle(PluginServices.getText(this,"wz_titulo"));
209
                   m_viewinfo.setWidth(444);
210
                   //m_viewinfo.setHeight(425);
211
                   m_viewinfo.setHeight(390);
212
                return m_viewinfo;
213
        }
214
        
215
        public ICrs getCrs () {
216
                return currentCrs;
217
        }
218
        
219
        public void setCrs (ICrs crs) {
220
                currentCrs = crs;
221
        }
222
        
223
        
224
        
225
        private void fillData(ICrs crs) {
226
                getPDatum().fillData(crs);
227
                getPSistCoord().fillData(crs);
228
        }
229
        
230
        private void cleanData() {
231
                getPDatum().cleanData();
232
                getPSistCoord().cleanData();
233
        }
234
        
235
        private void getDataAndUpdate() {
236
                EpsgConnection conn = new EpsgConnection();
237
                conn.setConnectionUsr();
238
                String cadenaNumerica = getPDatum().getTxtSemMay().getText().replaceAll("[^0-9.]", "");
239
                int puntos = 0;
240
                int signos = 0;
241
                for (int i = 0; i< cadenaNumerica.length(); i++) {
242
                        if (cadenaNumerica.charAt(i) == '.')
243
                                puntos++;
244
                }
245
                if (getPDatum().getTxtSemMay().getText().length() != cadenaNumerica.length() || (puntos > 1)) {
246
                        JOptionPane.showMessageDialog(MainPanel.this, 
247
                                        PluginServices.getText(this,"numeric_format")+": Semieje Mayor", 
248
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
249
                        return;
250
                }                
251
                else {
252
                        cadenaNumerica = getPDatum().getTxtLong().getText().replaceAll("[^0-9.]", "");
253
                        puntos = 0;
254
                        for (int i = 0; i< cadenaNumerica.length(); i++) {
255
                                if (cadenaNumerica.charAt(i) == '.')
256
                                        puntos++;
257
                        }
258
                        if (getPDatum().getTxtLong().getText().length() != cadenaNumerica.length() || (puntos > 1)) {
259
                
260
                        JOptionPane.showMessageDialog(MainPanel.this, 
261
                                        PluginServices.getText(this,"numeric_format")+": Longitud", 
262
                                        "Warning...", JOptionPane.WARNING_MESSAGE);
263
                        return;
264
                        }
265
                }
266
                double value = Double.valueOf(getPDatum().getTxtSemMay().getText()).doubleValue();
267
                //Comprobar si es numerico, o quedarnos con la parte numerica...
268
                //searchTextField.getText().length()!=searchTextField.getText().replaceAll("[^0-9]", "").length())
269
                String unit = (String)getPDatum().getCbSemMay().getSelectedItem();
270
                double semMay = getPDatum().convert2Meters(unit, value);
271
                value = Double.valueOf(getPDatum().getTxtLong().getText()).doubleValue();
272
                unit = (String)getPDatum().getCbLong().getSelectedItem();
273
                double longitude = getPDatum().convert2Meters(unit, value);
274
                
275
                String[] spheroid = {getPDatum().getTxtElipsoide().getText(),
276
                                                        ""+semMay,getPDatum().getTxtInvF().getText()};
277
                String[] primem = {getPDatum().getTxtMeridian().getText(), ""+longitude};
278
                
279
                String[] authority = {"\"USR\"", "2001"};
280
                String sentence = "SELECT usr_code FROM USR WHERE usr_code = " +authority[1];
281
                ResultSet result = Query.select(sentence, conn.getConnection());
282
                try {
283
                        if (result.next()) {
284
                                JOptionPane.showMessageDialog(MainPanel.this, 
285
                                                PluginServices.getText(this,"crsRepeat")+": "+authority[1], 
286
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
287
                                return;
288
                        }
289
                } catch (SQLException e1) {
290
                        // TODO Auto-generated catch block
291
                        e1.printStackTrace();
292
                }
293
                String datum = getPDatum().getTxtDatum().getText();
294
                
295
                
296
                if (getPSistCoord().getRbGeografico().isSelected()) {
297
                        cadWkt = "GEOGCS[\"" + "Nombre crs usuario" + "\", DATUM[\""+ datum +
298
                        "\", SPHEROID[\""+ spheroid[0] + "\", "+ spheroid[1] + ", "+ spheroid[2] +"]], " +
299
                        "PRIMEM[\""+ primem[0] + "\", "+ primem[1] +"], UNIT[\"Degree\", " + (Math.PI/180) +
300
                        "]]";
301
                        
302
                        sentence = "INSERT INTO USR VALUES("+authority[1]+",'" +
303
                        cadWkt +"','','Nombre crs usuario','"+datum+"')";
304
        
305
                } else {
306
                
307
                        cadWkt = "PROJCS[\""+ "crs proyectado usuario"/*A cambiar cuando arreglemos paneles*/+
308
                                "\", GEOGCS[\"" + "Nombre crs usuario" + "\", DATUM[\""+ datum +
309
                                "\", SPHEROID[\""+ spheroid[0] + "\", "+ spheroid[1] + ", "+ spheroid[2] +"]], " +
310
                                "PRIMEM[\""+ primem[0] + "\", "+ primem[1] +"], UNIT[\"Degree\", " + (Math.PI/180) +
311
                                "]], PROJECTION[\""+ getPSistCoord().getCbProyeccion().getSelectedItem().toString() + "\"], ";
312
                                
313
                        /*
314
                         * falta la parte de los parámetros... metodo para nombres...
315
                         */                
316
                        for (int i= 0; i< getPSistCoord().getTableParametros().getRowCount();i++){
317
                                cadenaNumerica = ((String)(getPSistCoord().getTableParametros().getValueAt(i,1))).replaceAll("[^0-9.-]", "");
318
                                puntos = 0;
319
                                signos = 0;
320
                                for (int j = 0; j< cadenaNumerica.length(); j++) {
321
                                        if (cadenaNumerica.charAt(j) == '.')
322
                                                puntos++;
323
                                        else if (cadenaNumerica.charAt(j) == '-')
324
                                                signos++;
325
                                }
326
                                if (((String)(getPSistCoord().getTableParametros().getValueAt(i,1))).length() != cadenaNumerica.length() || (puntos > 1) || (signos >1)) {
327
                        
328
                                JOptionPane.showMessageDialog(MainPanel.this, 
329
                                                PluginServices.getText(this,"numeric_format")+": Parametro "+(String)(getPSistCoord().getTableParametros().getValueAt(i,0)), 
330
                                                "Warning...", JOptionPane.WARNING_MESSAGE);
331
                                return;
332
                                }
333
                                
334
                                value = 0;
335
                                unit = "";
336
                                /**
337
                                 * Esto está hecho en general, habrá que hacerlo dependiendo del tipo de
338
                                 * unidad que tenga el parametro
339
                                 */
340
                                String type = (String) getPSistCoord().getTableParametros().getValueAt(i, 2);
341
                                if (type.equals("Meters")) {
342
                                        value = Double.parseDouble((String)getPSistCoord().getTableParametros().getValueAt(i, 1));
343
                                        unit = (String)getPSistCoord().getCbUnits().getSelectedItem();
344
                                        value = getPSistCoord().convert2Meters(unit, value);
345
                                }
346
                                else if (type.equals("Degree")) {
347
                                        value = Double.parseDouble((String)getPSistCoord().getTableParametros().getValueAt(i, 1));
348
                                        unit = (String)getPSistCoord().getCbUnits().getSelectedItem();
349
                                        value = getPSistCoord().convert2Degree(unit, value);
350
                                }
351
                                else if (type.equals("Unitless")) {
352
                                        value = Double.parseDouble((String)getPSistCoord().getTableParametros().getValueAt(i, 1));
353
                                        unit = (String)getPSistCoord().getCbUnits().getSelectedItem();
354
                                        value = getPSistCoord().convert2Unitless(unit, value);
355
                                }
356
                                cadWkt += "PARAMETER[\""+getPSistCoord().getTableParametros().getValueAt(i, 0)+"\", " + 
357
                                                        value+ "], ";
358
                        }
359
                        
360
                        cadWkt += "UNIT[\"Meters\", 1.0]]";
361
                        
362
                        
363
                        sentence = "INSERT INTO USR VALUES("+authority[1]+",'" +
364
                        cadWkt +"','crs proyectado usuario','Nombre crs usuario','"+datum+"')";
365
        
366
                }                
367
                                
368
                try {
369
                        conn.update(sentence);
370
                        conn.shutdown();
371
                } catch (SQLException e) {
372
                        // TODO Auto-generated catch block
373
                        e.printStackTrace();
374
                }                
375
                setNewCrsCode(Integer.parseInt(authority[1]));
376
                PluginServices.getMDIManager().closeWindow(this);
377
        }
378
        
379
        public void setNewCrsCode(int code) {
380
                newCrsCode = code;
381
        }
382
        
383
        public int getNewCrsCode() {
384
                return newCrsCode;
385
        }
386
        
387
}