Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extOracleSpatial / src / org / gvsig / oraclespatial / gui / OracleConnectionChooserPanel.java @ 29455

History | View | Annotate | Download (12 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop 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
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package org.gvsig.oraclespatial.gui;
44

    
45
import java.awt.Component;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.awt.event.KeyEvent;
49
import java.awt.event.KeyListener;
50
import java.util.ArrayList;
51
import java.util.Iterator;
52
import java.util.List;
53

    
54
import javax.swing.ImageIcon;
55
import javax.swing.JButton;
56
import javax.swing.JComboBox;
57
import javax.swing.JOptionPane;
58
import javax.swing.JPanel;
59

    
60
import org.gvsig.fmap.dal.DALLocator;
61
import org.gvsig.fmap.dal.DataManager;
62
import org.gvsig.fmap.dal.DataServerExplorer;
63
import org.gvsig.fmap.dal.DataServerExplorerParameters;
64
import org.gvsig.fmap.dal.exception.DataException;
65
import org.gvsig.fmap.dal.exception.InitializeException;
66
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
67
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
68
import org.gvsig.fmap.dal.store.oraclespatial.OracleSpatialResource;
69
import org.gvsig.fmap.dal.store.oraclespatial.OracleSpatialResourceParameters;
70
import org.gvsig.fmap.dal.store.oraclespatial.OracleSpatialServerExplorerParameters;
71
import org.slf4j.Logger;
72
import org.slf4j.LoggerFactory;
73

    
74
import com.iver.andami.PluginServices;
75
import com.iver.andami.messages.NotificationManager;
76
import com.iver.andami.ui.mdiManager.IWindow;
77
import com.iver.andami.ui.mdiManager.WindowInfo;
78
import com.iver.utiles.swing.JPasswordDlg;
79
import com.prodevelop.cit.gvsig.vectorialdb.wizard.VectorialDBConnectionParamsDialog;
80

    
81
/**
82
 * This dialog lets the user choose an available connection.
83
 * 
84
 * @author jldominguez
85
 * 
86
 */
87
public class OracleConnectionChooserPanel extends JPanel implements IWindow,
88
                ActionListener, KeyListener {
89

    
90
        private static Logger logger = LoggerFactory
91
                        .getLogger(OracleConnectionChooserPanel.class.getName());
92

    
93
        private DataManager manager = DALLocator.getDataManager();
94

    
95
        private JComboBox availableConnectionsComboBox = null;
96
        private JButton okButton = null;
97
        private JButton cancelButton = null;
98
        private JButton jdbcButton = null;
99

    
100
        private boolean okPressed = false;
101
        WindowInfo winfo = null;
102

    
103
        /**
104
         * This method initializes
105
         * 
106
         */
107
        public OracleConnectionChooserPanel() {
108
                super();
109
                initialize();
110
        }
111

    
112
        private void reloadCombo() {
113

    
114
                getAvailableConnectionsComboBox().removeAllItems();
115
                getAvailableConnectionsComboBox().addItem("");
116

    
117
                List<String> explorers = manager.getExplorerProviders();
118

    
119
                Iterator<String> iter = explorers.iterator();
120
                DataServerExplorerParameters exParam = null;
121
                String name;
122
                List<String> dbExplorersNames = new ArrayList<String>();
123
                while (iter.hasNext()) {
124
                        name = iter.next();
125
                        try {
126
                                exParam = manager.createServerExplorerParameters(name);
127
                        } catch (DataException e) {
128
                                NotificationManager.addError(e);
129
                        }
130
                        if (exParam instanceof OracleSpatialServerExplorerParameters) {
131
                                dbExplorersNames.add(name);
132
                        }
133
                }
134
        }
135

    
136
        /**
137
         * This method initializes this
138
         * 
139
         */
140
        private void initialize() {
141
                winfo = new WindowInfo(WindowInfo.MODALDIALOG);
142
                winfo.setHeight(119 - 70);
143
                winfo.setWidth(395);
144
                winfo.setTitle(PluginServices.getText(this, "choose_connection"));
145

    
146
                this.setLayout(null);
147
                this.setSize(new java.awt.Dimension(395, 89));
148
                this.add(getAvailableConnectionsComboBox(), null);
149
                this.add(getJdbcButton(), null);
150
                this.add(getOkButton(), null);
151
                this.add(getCancelButton(), null);
152

    
153
                reloadCombo();
154
        }
155

    
156
        public WindowInfo getWindowInfo() {
157
                return winfo;
158
        }
159

    
160
        /**
161
         * This method initializes availableConnectionsComboBox
162
         * 
163
         * @return JComboBox
164
         */
165
        private JComboBox getAvailableConnectionsComboBox() {
166
                if (availableConnectionsComboBox == null) {
167
                        availableConnectionsComboBox = new JComboBox();
168
                        availableConnectionsComboBox.addActionListener(this);
169
                        availableConnectionsComboBox.setBounds(new java.awt.Rectangle(10,
170
                                        15, 371 - 31, 21));
171
                }
172
                return availableConnectionsComboBox;
173
        }
174

    
175
        /**
176
         * This method initializes okButton
177
         * 
178
         * @return JButton
179
         */
180
        private JButton getOkButton() {
181
                if (okButton == null) {
182
                        okButton = new JButton(PluginServices.getText(this, "ok"));
183
                        okButton.addActionListener(this);
184
                        okButton.addKeyListener(this);
185
                        okButton.setBounds(new java.awt.Rectangle(90, 50, 106, 26));
186
                }
187
                return okButton;
188
        }
189

    
190
        /**
191
         * This method initializes cancelButton
192
         * 
193
         * @return JButton
194
         */
195
        private JButton getCancelButton() {
196
                if (cancelButton == null) {
197
                        cancelButton = new JButton(PluginServices.getText(this, "cancel"));
198
                        cancelButton.addActionListener(this);
199
                        cancelButton.addKeyListener(this);
200
                        cancelButton.setBounds(new java.awt.Rectangle(200, 50, 106, 26));
201
                }
202
                return cancelButton;
203
        }
204

    
205
        public void actionPerformed(ActionEvent arg0) {
206
                Object src = arg0.getSource();
207

    
208
                if (src == okButton) {
209
                        okPressed = true;
210
                        PluginServices.getMDIManager().closeWindow(this);
211
                }
212

    
213
                if (src == cancelButton) {
214
                        okPressed = false;
215
                        PluginServices.getMDIManager().closeWindow(this);
216
                }
217

    
218
                if (src == availableConnectionsComboBox) {
219
                        OracleSpatialServerExplorerParameters selServerParams = getSelectedServerExplorerParams();
220

    
221
                        if (selServerParams == null) {
222
                                okButton.setEnabled(false);
223
                                return;
224
                        }
225

    
226
                        DataServerExplorer explorer = null;
227
                        try {
228
                                explorer = manager.createServerExplorer(selServerParams);
229
                        } catch (ValidateDataParametersException e) {
230
                                logger
231
                                                .error(
232
                                                                "Validate paramenters to create a Oracle server explorer",
233
                                                                e);
234
                        } catch (InitializeException e) {
235
                                logger.error("Creating Oracle server explorer ", e);
236
                        } catch (ProviderNotRegisteredException e) {
237
                                logger.error("Oracle Provider no registered", e);
238
                        }
239

    
240
                        if (explorer == null) {
241
                                if (!tryToConnect(selServerParams)) {
242
                                        okButton.setEnabled(false);
243
                                        availableConnectionsComboBox.setSelectedIndex(0);
244

    
245
                                        return;
246
                                }
247
                        }
248

    
249
                        // okButton.setEnabled(selServerParams.isConnected());
250
                        okButton.requestFocus();
251
                }
252

    
253
                if (src == jdbcButton) {
254
                        OracleSpatialServerExplorerParameters sel = addNewConnection();
255

    
256
                        if (sel != null) {
257
                                reloadCombo();
258
                                getAvailableConnectionsComboBox().setSelectedItem(sel);
259
                        }
260
                }
261

    
262
        }
263

    
264
        public OracleSpatialServerExplorerParameters getSelectedServerExplorerParams() {
265
                String servName = (String) availableConnectionsComboBox
266
                                .getSelectedItem();
267

    
268
                logger.debug("selected server name = " + servName);
269

    
270
                if (servName == null) {
271
                        return null;
272
                }
273
                OracleSpatialServerExplorerParameters params = null;
274
                try {
275
                        params = (OracleSpatialServerExplorerParameters) manager
276
                                        .createServerExplorerParameters(servName);
277
                } catch (DataException e) {
278
                        NotificationManager.addError(e);
279
                }
280
                return params;
281
        }
282

    
283
        public boolean isOkPressed() {
284
                return okPressed;
285
        }
286

    
287
        private boolean tryToConnect(
288
                        OracleSpatialServerExplorerParameters servParams) {
289
                JPasswordDlg dlg = new JPasswordDlg();
290
                String strMessage = PluginServices.getText(this, "conectar_jdbc");
291
                String strPassword = PluginServices.getText(this, "password");
292
                dlg.setMessage(strMessage + " [" + servParams.getJDBCDriverClassName()
293
                                + ", " + servParams.getHost() + ", " + servParams.getPort()
294
                                + ", " + servParams.getDBName() + ", " + servParams.getUser()
295
                                + "]. " + strPassword + "?");
296

    
297
                dlg.show();
298

    
299
                String clave = dlg.getPassword();
300

    
301
                if (clave == null) {
302
                        return false;
303
                }
304

    
305
                servParams.setPassword(clave);
306
                try {
307
                        manager.createServerExplorer(servParams);
308
                } catch (ValidateDataParametersException e) {
309
                        logger.error(
310
                                        "Validate paramenters to create a Oracle server explorer",
311
                                        e);
312
                        return false;
313
                } catch (InitializeException e) {
314
                        logger.error("Creating Oracle server explorer ", e);
315
                        return false;
316
                } catch (ProviderNotRegisteredException e) {
317
                        logger.error("Oracle Provider no registered", e);
318
                        return false;
319
                }
320

    
321
                return true;
322
        }
323

    
324
        private void showConnectionErrorMessage(String _msg) {
325
                String msg = (_msg.length() > 300) ? "" : (": " + _msg);
326
                String title = PluginServices.getText(this, "connection_error");
327
                JOptionPane.showMessageDialog(this, title + msg, title,
328
                                JOptionPane.ERROR_MESSAGE);
329
        }
330

    
331
        public void keyPressed(KeyEvent e) {
332
                Object src = e.getSource();
333

    
334
                if ((src == okButton) || (src == cancelButton)) {
335
                        ActionEvent aux = new ActionEvent(src,
336
                                        ActionEvent.ACTION_PERFORMED, "");
337
                        actionPerformed(aux);
338
                }
339
        }
340

    
341
        public void keyReleased(KeyEvent e) {
342
                // TODO Auto-generated method stub
343
        }
344

    
345
        public void keyTyped(KeyEvent e) {
346
                // TODO Auto-generated method stub
347
        }
348

    
349
        private OracleSpatialServerExplorerParameters addNewConnection() {
350
                OracleSpatialServerExplorerParameters servParams = null;
351

    
352
                VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
353
                newco.showDialog();
354

    
355
                if (newco.isOkPressed()) {
356
                        String _drvname = newco.getConnectionDriverName();
357
                        String _host = newco.getConnectionServerUrl();
358
                        String _port = newco.getConnectionPort();
359
                        String _dbname = newco.getConnectionDBName();
360
                        String _user = newco.getConnectionUser();
361
                        String _pw = newco.getConnectionPassword();
362
                        String _conn_usr_name = newco.getConnectionName();
363

    
364
                        boolean hasToBeCon = newco.hasToBeConnected();
365

    
366
                        try {
367
                                servParams = new OracleSpatialServerExplorerParameters();
368
                                servParams.setJDBCDriverClassName(_drvname);
369
                                servParams.setUser(_user);
370
                                servParams.setPassword(_pw);
371
                                servParams.setHost(_host);                                
372
                                servParams.setPort(new Integer(_port));
373
                                servParams.setDBName(_dbname);
374
                                
375
                                DataServerExplorer explorer = manager.createServerExplorer(servParams);
376
                                if(explorer == null){
377
                                        throw new Exception();
378
                                }
379
                        } catch (Exception e) {
380
                                showConnectionErrorMessage(e.getMessage(), newco);
381
                                return null;
382
                        }
383

    
384
                        return servParams;
385
                } else {
386
                        return null;
387
                }
388
        }
389

    
390
        private void showConnectionErrorMessage(String _msg, Component parent) {
391
                String msg = (_msg.length() > 300) ? "" : (": " + _msg);
392
                String title = PluginServices.getText(this, "connection_error");
393
                JOptionPane.showMessageDialog(parent, title + msg, title,
394
                                JOptionPane.ERROR_MESSAGE);
395
        }
396

    
397
        private JButton getJdbcButton() {
398
                if (jdbcButton == null) {
399
                        jdbcButton = new JButton();
400
                        jdbcButton.addActionListener(this);
401
                        jdbcButton.setToolTipText(PluginServices.getText(this,
402
                                        "add_connection"));
403
                        jdbcButton.setBounds(new java.awt.Rectangle(381 - 26, 15, 26, 21));
404
                        String _file = createResourceUrl("images/jdbc.png").getFile();
405
                        jdbcButton.setIcon(new ImageIcon(_file));
406
                }
407

    
408
                return jdbcButton;
409
        }
410

    
411
        private java.net.URL createResourceUrl(String path) {
412
                return getClass().getClassLoader().getResource(path);
413
        }
414

    
415
        public Object getWindowProfile() {
416
                return WindowInfo.DIALOG_PROFILE;
417
        }
418

    
419
}