Statistics
| Revision:

root / branches / v10 / extensions / extOracleSpatial / src / es / prodevelop / cit / gvsig / jdbc_spatial / gui / jdbcwizard / JDBConnectionParamsDialog.java @ 13991

History | View | Annotate | Download (15.9 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 es.prodevelop.cit.gvsig.jdbc_spatial.gui.jdbcwizard;
44

    
45
import com.hardcode.driverManager.DriverLoadException;
46

    
47
import com.iver.andami.PluginServices;
48
import com.iver.andami.ui.mdiManager.IWindow;
49
import com.iver.andami.ui.mdiManager.WindowInfo;
50

    
51
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
52
import com.iver.cit.gvsig.fmap.drivers.jdbc.utils.ConnectionWithParams;
53
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
54

    
55
import org.apache.log4j.Logger;
56

    
57
import java.awt.Color;
58
import java.awt.event.ActionEvent;
59
import java.awt.event.ActionListener;
60
import java.awt.event.KeyEvent;
61
import java.awt.event.KeyListener;
62

    
63
import java.sql.SQLException;
64

    
65
import java.util.ArrayList;
66

    
67
import javax.swing.JButton;
68
import javax.swing.JCheckBox;
69
import javax.swing.JComboBox;
70
import javax.swing.JLabel;
71
import javax.swing.JPanel;
72
import javax.swing.JPasswordField;
73
import javax.swing.JTextField;
74

    
75

    
76
/**
77
 * Lets the user input the connection parameters.
78
 *
79
 * @author jldominguez
80
 *
81
 */
82
public class JDBConnectionParamsDialog extends JPanel implements IWindow,
83
    ActionListener, KeyListener {
84
    private static Logger logger = Logger.getLogger(JDBConnectionParamsDialog.class.getName());
85
    private WindowInfo winfo = new WindowInfo(8); // MODAL only
86
    private JButton cancelButton = null;
87
    private JButton okButton = null;
88
    private JPanel paramsPanel = null;
89
    private JComboBox driverComboBox = null;
90
    private JTextField portTextField = null;
91
    private JTextField dbTextField = null;
92
    private JTextField userTextField = null;
93
    private JPasswordField passwordField = null;
94
    private JLabel driverLabel = null;
95
    private JLabel portLabel = null;
96
    private JLabel dbLabel = null;
97
    private JLabel userLabel = null;
98
    private JLabel pwLabel = null;
99
    private boolean okPressed = false;
100
    private JTextField urlTextField = null;
101
    private JLabel urlLabel = null;
102
    private JCheckBox connectedCheckBox = null;
103
    private JLabel connectedLabel = null;
104
    private JLabel connNameLabel = null;
105
    private JTextField connNameTextField = null;
106

    
107
    /**
108
     * This method initializes
109
     *
110
     */
111
    public JDBConnectionParamsDialog() {
112
        super();
113
        initialize();
114
    }
115

    
116
    public void showDialog() {
117
        PluginServices.getMDIManager().addWindow(this);
118
    }
119

    
120
    /**
121
     * This method initializes this
122
     *
123
     */
124
    private void initialize() {
125
        winfo.setWidth(370);
126
        winfo.setHeight(275 - 25);
127
        winfo.setTitle(PluginServices.getText(this, "connection_parameters"));
128

    
129
        this.setSize(new java.awt.Dimension(360, 287));
130
        this.setLayout(null);
131
        this.add(getCancelButton(), null);
132
        this.add(getOkButton(), null);
133
        this.add(getParamsPanel(), null);
134
    }
135

    
136
    public WindowInfo getWindowInfo() {
137
        return winfo;
138
    }
139

    
140
    /**
141
     * This method initializes cancelButton
142
     *
143
     * @return javax.swing.JButton
144
     */
145
    private JButton getCancelButton() {
146
        if (cancelButton == null) {
147
            cancelButton = new JButton();
148
            cancelButton.setText(PluginServices.getText(this, "cancel"));
149
            cancelButton.addActionListener(this);
150
            cancelButton.setBounds(new java.awt.Rectangle(185, 250, 106, 26));
151
        }
152

    
153
        return cancelButton;
154
    }
155

    
156
    /**
157
     * This method initializes okButton
158
     *
159
     * @return javax.swing.JButton
160
     */
161
    private JButton getOkButton() {
162
        if (okButton == null) {
163
            okButton = new JButton();
164
            okButton.setText(PluginServices.getText(this, "ok"));
165
            okButton.addActionListener(this);
166
            okButton.setBounds(new java.awt.Rectangle(70, 250, 106, 26));
167
        }
168

    
169
        return okButton;
170
    }
171

    
172
    /**
173
     * This method initializes paramsPanel
174
     *
175
     * @return javax.swing.JPanel
176
     */
177
    private JPanel getParamsPanel() {
178
        if (paramsPanel == null) {
179
            connNameLabel = new JLabel();
180
            connNameLabel.setBounds(new java.awt.Rectangle(10, 30, 141, 21));
181
            connNameLabel.setText(PluginServices.getText(this, "connection_name") +
182
                ":");
183
            connectedLabel = new JLabel();
184
            connectedLabel.setBounds(new java.awt.Rectangle(10, 205, 141, 21));
185
            connectedLabel.setText(PluginServices.getText(this, "connected") +
186
                ":");
187
            urlLabel = new JLabel();
188
            urlLabel.setBounds(new java.awt.Rectangle(10, 80, 141, 21));
189
            urlLabel.setText(PluginServices.getText(this, "server_url") + ":");
190
            pwLabel = new JLabel();
191
            pwLabel.setBounds(new java.awt.Rectangle(10, 180, 141, 21));
192
            pwLabel.setText(PluginServices.getText(this, "password") + ":");
193
            userLabel = new JLabel();
194
            userLabel.setBounds(new java.awt.Rectangle(10, 155, 141, 21));
195
            userLabel.setText(PluginServices.getText(this, "user") + ":");
196
            dbLabel = new JLabel();
197
            dbLabel.setBounds(new java.awt.Rectangle(10, 130, 141, 21));
198
            dbLabel.setText(PluginServices.getText(this, "database_name") +
199
                ":");
200
            portLabel = new JLabel();
201
            portLabel.setBounds(new java.awt.Rectangle(10, 105, 141, 21));
202
            portLabel.setText(PluginServices.getText(this, "port") + ":");
203
            driverLabel = new JLabel();
204
            driverLabel.setBounds(new java.awt.Rectangle(10, 55, 141, 21));
205
            driverLabel.setText(PluginServices.getText(this, "driver") + ":");
206
            paramsPanel = new JPanel();
207
            paramsPanel.setBounds(new java.awt.Rectangle(10, 10, 336, 231));
208
            paramsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
209
                    null, PluginServices.getText(this, "connection_params"),
210
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
211
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
212
            paramsPanel.setLayout(null);
213
            paramsPanel.add(getPortTextField(), null);
214
            paramsPanel.add(getDriverComboBox(), null);
215
            paramsPanel.add(getDbTextField(), null);
216
            paramsPanel.add(getUserTextField(), null);
217
            paramsPanel.add(getPasswordField(), null);
218
            paramsPanel.add(driverLabel, null);
219
            paramsPanel.add(portLabel, null);
220
            paramsPanel.add(dbLabel, null);
221
            paramsPanel.add(userLabel, null);
222
            paramsPanel.add(pwLabel, null);
223
            paramsPanel.add(getUrlTextArea(), null);
224
            paramsPanel.add(urlLabel, null);
225
            paramsPanel.add(getConnectedCheckBox(), null);
226
            paramsPanel.add(connectedLabel, null);
227
            paramsPanel.add(connNameLabel, null);
228
            paramsPanel.add(getConnNameTextField(), null);
229
        }
230

    
231
        return paramsPanel;
232
    }
233

    
234
    /**
235
     * This method initializes driverComboBox
236
     *
237
     * @return javax.swing.JComboBox
238
     */
239
    private JComboBox getDriverComboBox() {
240
        if (driverComboBox == null) {
241
            driverComboBox = new JComboBox();
242
            driverComboBox.addActionListener(this);
243

    
244
            String[] drvName = getDriverNames();
245

    
246
            for (int i = 0; i < drvName.length; i++)
247
                driverComboBox.addItem(drvName[i]);
248

    
249
            driverComboBox.setBounds(new java.awt.Rectangle(155, 55, 166, 21));
250
        }
251

    
252
        return driverComboBox;
253
    }
254

    
255
    /**
256
     * This method initializes portTextField
257
     *
258
     * @return javax.swing.JTextField
259
     */
260
    private JTextField getPortTextField() {
261
        if (portTextField == null) {
262
            portTextField = new JTextField();
263
            portTextField.addKeyListener(this);
264
            portTextField.setBounds(new java.awt.Rectangle(155, 105, 166, 21));
265
        }
266

    
267
        return portTextField;
268
    }
269

    
270
    /**
271
     * This method initializes dbTextField
272
     *
273
     * @return javax.swing.JTextField
274
     */
275
    private JTextField getDbTextField() {
276
        if (dbTextField == null) {
277
            dbTextField = new JTextField();
278
            dbTextField.addKeyListener(this);
279
            dbTextField.setBounds(new java.awt.Rectangle(155, 130, 166, 21));
280
        }
281

    
282
        return dbTextField;
283
    }
284

    
285
    /**
286
     * This method initializes userTextField
287
     *
288
     * @return javax.swing.JTextField
289
     */
290
    private JTextField getUserTextField() {
291
        if (userTextField == null) {
292
            userTextField = new JTextField();
293
            userTextField.addKeyListener(this);
294
            userTextField.setBounds(new java.awt.Rectangle(155, 155, 166, 21));
295
        }
296

    
297
        return userTextField;
298
    }
299

    
300
    /**
301
     * This method initializes passwordField
302
     *
303
     * @return javax.swing.JPasswordField
304
     */
305
    private JPasswordField getPasswordField() {
306
        if (passwordField == null) {
307
            passwordField = new JPasswordField();
308
            passwordField.addKeyListener(this);
309
            passwordField.setBounds(new java.awt.Rectangle(155, 180, 166, 21));
310
        }
311

    
312
        return passwordField;
313
    }
314

    
315
    private String[] getDriverNames() {
316
        Class[] classes = new Class[] { VectorialJDBCDriver.class };
317

    
318
        ArrayList ret = new ArrayList();
319
        String[] driverNames = LayerFactory.getDM().getDriverNames();
320

    
321
        for (int i = 0; i < driverNames.length; i++) {
322
            for (int j = 0; j < classes.length; j++) {
323
                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
324
                    ret.add(driverNames[i]);
325
                }
326
            }
327
        }
328

    
329
        return (String[]) ret.toArray(new String[0]);
330
    }
331

    
332
    public void actionPerformed(ActionEvent arg0) {
333
        Object src = arg0.getSource();
334

    
335
        if (src == connectedCheckBox) {
336
            if (connectedCheckBox.isSelected()) {
337
                passwordField.setEnabled(true);
338
                passwordField.setBackground(Color.WHITE);
339
            }
340
            else {
341
                passwordField.setText("");
342
                passwordField.setEnabled(false);
343
                passwordField.setBackground(Color.LIGHT_GRAY);
344
            }
345
        }
346

    
347
        if (src == okButton) {
348
            okPressed = true;
349
            PluginServices.getMDIManager().closeWindow(this);
350

    
351
            return;
352
        }
353

    
354
        if (src == cancelButton) {
355
            okPressed = false;
356
            PluginServices.getMDIManager().closeWindow(this);
357

    
358
            return;
359
        }
360

    
361
        if (src == driverComboBox) {
362
            String driverName = driverComboBox.getSelectedItem().toString();
363
            VectorialJDBCDriver driver;
364

    
365
            try {
366
                driver = (VectorialJDBCDriver) LayerFactory.getDM()
367
                                                           .getDriver(driverName);
368
                portTextField.setText("" + driver.getDefaultPort());
369
            }
370
            catch (DriverLoadException e1) {
371
                portTextField.setText("");
372
            }
373

    
374
            return;
375
        }
376
    }
377

    
378
    public boolean isOkPressed() {
379
        return okPressed;
380
    }
381

    
382
    public boolean hasToBeConnected() {
383
        return connectedCheckBox.isSelected();
384
    }
385

    
386
    public String getConnectionDriverName() {
387
        return driverComboBox.getSelectedItem().toString();
388
    }
389

    
390
    public String getConnectionServerUrl() {
391
        return urlTextField.getText();
392
    }
393

    
394
    public String getConnectionPort() {
395
        return portTextField.getText();
396
    }
397

    
398
    public String getConnectionDBName() {
399
        return dbTextField.getText();
400
    }
401

    
402
    public String getConnectionUser() {
403
        return userTextField.getText();
404
    }
405

    
406
    public String getConnectionPassword() {
407
        String resp = new String(passwordField.getPassword());
408

    
409
        return resp;
410
    }
411

    
412
    private JTextField getUrlTextArea() {
413
        if (urlTextField == null) {
414
            urlTextField = new JTextField();
415
            urlTextField.addKeyListener(this);
416
            urlTextField.setBounds(new java.awt.Rectangle(155, 80, 166, 21));
417
        }
418

    
419
        return urlTextField;
420
    }
421

    
422
    /**
423
     * This method initializes connectedCheckBox
424
     *
425
     * @return javax.swing.JCheckBox
426
     */
427
    private JCheckBox getConnectedCheckBox() {
428
        if (connectedCheckBox == null) {
429
            connectedCheckBox = new JCheckBox();
430
            connectedCheckBox.setSelected(true);
431
            connectedCheckBox.addActionListener(this);
432
            connectedCheckBox.setBounds(new java.awt.Rectangle(155, 205, 26, 21));
433
        }
434

    
435
        return connectedCheckBox;
436
    }
437

    
438
    public String getConnectionName() {
439
        return getConnNameTextField().getText();
440
    }
441

    
442
    /**
443
     * This method initializes connNameTextField
444
     *
445
     * @return javax.swing.JTextField
446
     */
447
    private JTextField getConnNameTextField() {
448
        if (connNameTextField == null) {
449
            connNameTextField = new JTextField();
450
            connNameTextField.addKeyListener(this);
451
            connNameTextField.setBounds(new java.awt.Rectangle(155, 30, 166, 21));
452
        }
453

    
454
        return connNameTextField;
455
    }
456

    
457
    public void keyPressed(KeyEvent e) {
458
    }
459

    
460
    public void keyReleased(KeyEvent e) {
461
        if (e.getKeyChar() != '\n') {
462
            return;
463
        }
464

    
465
        Object src = e.getSource();
466

    
467
        if (src == passwordField) {
468
            ActionEvent aevt = new ActionEvent(okButton,
469
                    ActionEvent.ACTION_PERFORMED, "");
470
            actionPerformed(aevt);
471
        }
472
        else {
473
            if (src instanceof JTextField) {
474
                ((JTextField) src).transferFocus();
475
            }
476
        }
477
    }
478

    
479
    public void keyTyped(KeyEvent e) {
480
    }
481

    
482
    public void loadValues(ConnectionWithParams cwp) {
483
        getPortTextField().setText(cwp.getPort());
484
        selectThisInDriverCombo(cwp.getDrvName());
485
        getDbTextField().setText(cwp.getDb());
486
        getUserTextField().setText(cwp.getUser());
487

    
488
        if (cwp.getPw() == null) {
489
            getPasswordField().setText("");
490
        }
491
        else {
492
            getPasswordField().setText(cwp.getPw());
493
        }
494

    
495
        getUrlTextArea().setText(cwp.getHost());
496

    
497
        boolean connected = false;
498

    
499
        try {
500
            connected = (cwp.getConnection() != null) &&
501
                (!cwp.getConnection().isClosed());
502
        }
503
        catch (SQLException e) {
504
            logger.error("While checking connection: " + e.getMessage());
505
            connected = false;
506
        }
507

    
508
        getConnectedCheckBox().setSelected(connected);
509
        getConnNameTextField().setText(cwp.getName());
510
    }
511

    
512
    private void selectThisInDriverCombo(String drvName) {
513
        int size = getDriverComboBox().getItemCount();
514

    
515
        for (int i = 0; i < size; i++) {
516
            Object item = getDriverComboBox().getItemAt(i);
517

    
518
            if (item.toString().compareToIgnoreCase(drvName) == 0) {
519
                getDriverComboBox().setSelectedIndex(i);
520

    
521
                return;
522
            }
523
        }
524
    }
525
} //  @jve:decl-index=0:visual-constraint="10,10"