Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / VectorialDBConnectionParamsDialog.java @ 24986

History | View | Annotate | Download (16.5 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 com.prodevelop.cit.gvsig.vectorialdb.wizard;
44

    
45
import java.awt.Color;
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

    
52
import javax.swing.JButton;
53
import javax.swing.JCheckBox;
54
import javax.swing.JComboBox;
55
import javax.swing.JLabel;
56
import javax.swing.JPanel;
57
import javax.swing.JPasswordField;
58
import javax.swing.JTextField;
59

    
60
import org.apache.log4j.Logger;
61

    
62
import com.hardcode.driverManager.DriverLoadException;
63
import com.iver.andami.PluginServices;
64
import com.iver.andami.ui.mdiManager.IWindow;
65
import com.iver.andami.ui.mdiManager.WindowInfo;
66
import com.iver.cit.gvsig.fmap.drivers.DBException;
67
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
68
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams;
69
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
70

    
71

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

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

    
113
    public void showDialog() {
114
        PluginServices.getMDIManager().addWindow(this);
115
    }
116

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

    
126
        this.setSize(new java.awt.Dimension(360, 329));
127
        this.setLayout(null);
128
        this.add(getCancelButton(), null);
129
        this.add(getOkButton(), null);
130
        this.add(getParamsPanel(), null);
131
    }
132

    
133
    public WindowInfo getWindowInfo() {
134
        return winfo;
135
    }
136

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

    
150
        return cancelButton;
151
    }
152

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

    
166
        return okButton;
167
    }
168

    
169
    /**
170
     * This method initializes paramsPanel
171
     *
172
     * @return javax.swing.JPanel
173
     */
174
    private JPanel getParamsPanel() {
175
        if (paramsPanel == null) {
176
            connNameLabel = new JLabel();
177
            connNameLabel.setBounds(new java.awt.Rectangle(10, 30, 141, 21));
178
            connNameLabel.setText(PluginServices.getText(this, "connection_name") +
179
                ":");
180
            connectedLabel = new JLabel();
181
            connectedLabel.setBounds(new java.awt.Rectangle(10, 247, 141, 21));
182
            connectedLabel.setText(PluginServices.getText(this, "connected") +
183
                ":");
184
            urlLabel = new JLabel();
185
            urlLabel.setBounds(new java.awt.Rectangle(10, 80, 141, 21));
186
            urlLabel.setText(PluginServices.getText(this, "server_url") + ":");
187
            pwLabel = new JLabel();
188
            pwLabel.setBounds(new java.awt.Rectangle(10, 222, 141, 21));
189
            pwLabel.setText(PluginServices.getText(this, "password") + ":");
190
            userLabel = new JLabel();
191
            userLabel.setBounds(new java.awt.Rectangle(10, 197, 141, 21));
192
            userLabel.setText(PluginServices.getText(this, "user") + ":");
193
            dbLabel = new JLabel();
194
            dbLabel.setBounds(new java.awt.Rectangle(10, 130, 141, 21));
195
            dbLabel.setText(PluginServices.getText(this, "database_name") +
196
                ":");
197
            dbLabelWarning = new JLabel();
198
            dbLabelWarning.setBounds(new java.awt.Rectangle(10, 155, 310, 41));
199
            dbLabelWarning.setText(PluginServices.getText(this, "warning_you_must_input_the_exact_name_this_difference_between_capital_letters_and_small_letters")
200
                );
201

    
202
            portLabel = new JLabel();
203
            portLabel.setBounds(new java.awt.Rectangle(10, 105, 141, 21));
204
            portLabel.setText(PluginServices.getText(this, "port") + ":");
205
            driverLabel = new JLabel();
206
            driverLabel.setBounds(new java.awt.Rectangle(10, 55, 141, 21));
207
            driverLabel.setText(PluginServices.getText(this, "driver") + ":");
208
            paramsPanel = new JPanel();
209
            paramsPanel.setBounds(new java.awt.Rectangle(10, 10, 336, 273));
210
            paramsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
211
                    null, PluginServices.getText(this, "connection_params"),
212
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
213
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
214
            paramsPanel.setLayout(null);
215
            paramsPanel.add(getPortTextField(), null);
216
            paramsPanel.add(getDriverComboBox(), null);
217
            paramsPanel.add(getDbTextField(), null);
218
            paramsPanel.add(getUserTextField(), null);
219
            paramsPanel.add(getPasswordField(), null);
220
            paramsPanel.add(driverLabel, null);
221
            paramsPanel.add(portLabel, null);
222
            paramsPanel.add(dbLabel, null);
223
            paramsPanel.add(dbLabelWarning, null);
224
            paramsPanel.add(userLabel, null);
225
            paramsPanel.add(pwLabel, null);
226
            paramsPanel.add(getUrlTextArea(), null);
227
            paramsPanel.add(urlLabel, null);
228
            paramsPanel.add(getConnectedCheckBox(), null);
229
            paramsPanel.add(connectedLabel, null);
230
            paramsPanel.add(connNameLabel, null);
231
            paramsPanel.add(getConnNameTextField(), null);
232
        }
233

    
234
        return paramsPanel;
235
    }
236

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

    
247
            String[] drvName = getDriverNames();
248

    
249
            for (int i = 0; i < drvName.length; i++)
250
                driverComboBox.addItem(drvName[i]);
251

    
252
            driverComboBox.setBounds(new java.awt.Rectangle(155, 55, 166, 21));
253
        }
254

    
255
        return driverComboBox;
256
    }
257

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

    
270
        return portTextField;
271
    }
272

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

    
285
        return dbTextField;
286
    }
287

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

    
300
        return userTextField;
301
    }
302

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

    
315
        return passwordField;
316
    }
317

    
318
    private String[] getDriverNames() {
319
        Class[] classes = new Class[] { IVectorialDatabaseDriver.class };
320

    
321
        ArrayList ret = new ArrayList();
322
        String[] driverNames = LayerFactory.getDM().getDriverNames();
323

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

    
332
        return (String[]) ret.toArray(new String[0]);
333
    }
334

    
335
    public void actionPerformed(ActionEvent arg0) {
336
        Object src = arg0.getSource();
337

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

    
350
        if (src == okButton) {
351
            okPressed = true;
352
            PluginServices.getMDIManager().closeWindow(this);
353

    
354
            return;
355
        }
356

    
357
        if (src == cancelButton) {
358
            okPressed = false;
359
            PluginServices.getMDIManager().closeWindow(this);
360

    
361
            return;
362
        }
363

    
364
        if (src == driverComboBox) {
365
            String driverName = driverComboBox.getSelectedItem().toString();
366
            IVectorialDatabaseDriver driver;
367

    
368
            try {
369
                driver = (IVectorialDatabaseDriver) LayerFactory.getDM()
370
                                                           .getDriver(driverName);
371
                portTextField.setText("" + driver.getDefaultPort());
372
            }
373
            catch (DriverLoadException e1) {
374
                portTextField.setText("");
375
            }
376

    
377
            return;
378
        }
379
    }
380

    
381
    public boolean isOkPressed() {
382
        return okPressed;
383
    }
384

    
385
    public boolean hasToBeConnected() {
386
        return connectedCheckBox.isSelected();
387
    }
388

    
389
    public String getConnectionDriverName() {
390
        return driverComboBox.getSelectedItem().toString();
391
    }
392

    
393
    public String getConnectionServerUrl() {
394
        return urlTextField.getText();
395
    }
396

    
397
    public String getConnectionPort() {
398
        return portTextField.getText();
399
    }
400

    
401
    public String getConnectionDBName() {
402
        return dbTextField.getText();
403
    }
404

    
405
    public String getConnectionUser() {
406
        return userTextField.getText();
407
    }
408

    
409
    public String getConnectionPassword() {
410
        String resp = new String(passwordField.getPassword());
411

    
412
        return resp;
413
    }
414

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

    
422
        return urlTextField;
423
    }
424

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

    
438
        return connectedCheckBox;
439
    }
440

    
441
    public String getConnectionName() {
442
        return getConnNameTextField().getText();
443
    }
444

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

    
457
        return connNameTextField;
458
    }
459

    
460
    public void keyPressed(KeyEvent e) {
461
    }
462

    
463
    public void keyReleased(KeyEvent e) {
464
        if (e.getKeyChar() != '\n') {
465
            return;
466
        }
467

    
468
        Object src = e.getSource();
469

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

    
482
    public void keyTyped(KeyEvent e) {
483
    }
484

    
485
    public void loadValues(ConnectionWithParams cwp) {
486
        getPortTextField().setText(cwp.getPort());
487
        selectThisInDriverCombo(cwp.getDrvName());
488
        getDbTextField().setText(cwp.getDb());
489
        getUserTextField().setText(cwp.getUser());
490

    
491
        if (cwp.getPw() == null) {
492
            getPasswordField().setText("");
493
        }
494
        else {
495
            getPasswordField().setText(cwp.getPw());
496
        }
497

    
498
        getUrlTextArea().setText(cwp.getHost());
499

    
500
        boolean connected = false;
501

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

    
511
        getConnectedCheckBox().setSelected(connected);
512
        getConnNameTextField().setText(cwp.getName());
513
    }
514

    
515
    private void selectThisInDriverCombo(String drvName) {
516
        int size = getDriverComboBox().getItemCount();
517

    
518
        for (int i = 0; i < size; i++) {
519
            Object item = getDriverComboBox().getItemAt(i);
520

    
521
            if (item.toString().compareToIgnoreCase(drvName) == 0) {
522
                getDriverComboBox().setSelectedIndex(i);
523

    
524
                return;
525
            }
526
        }
527
    }
528

    
529
        public Object getWindowProfile() {
530
                return WindowInfo.DIALOG_PROFILE;
531
        }
532
} //  @jve:decl-index=0:visual-constraint="10,10"