Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / DBConnectionParamsDialog.java @ 38041

History | View | Annotate | Download (18.3 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
import java.util.Arrays;
52

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

    
61
import org.apache.log4j.Logger;
62

    
63
import com.hardcode.driverManager.Driver;
64
import com.hardcode.driverManager.DriverLoadException;
65
import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver;
66
import com.iver.andami.PluginServices;
67
import com.iver.andami.ui.mdiManager.IWindow;
68
import com.iver.andami.ui.mdiManager.WindowInfo;
69
import com.iver.cit.gvsig.fmap.drivers.DBException;
70
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
71
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams;
72
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
73

    
74

    
75
/**
76
 * Lets the user input the connection parameters.
77
 *
78
 * @author jldominguez
79
 *
80
 */
81
public class DBConnectionParamsDialog extends JPanel implements IWindow,
82
    ActionListener, KeyListener {
83
    private static Logger logger = Logger.getLogger(DBConnectionParamsDialog.class.getName());
84
    private WindowInfo winfo = new WindowInfo(WindowInfo.MODALDIALOG); // MODAL only
85
    private JButton cancelButton = null;
86
    private JButton okButton = null;
87
    private JPanel paramsPanel = null;
88
    private JComboBox driverComboBox = null;
89
    private JTextField portTextField = null;
90
    private JTextField dbTextField = null;
91
    private JTextField userTextField = null;
92
    private JPasswordField passwordField = null;
93
    private JLabel driverLabel = null;
94
    private JLabel portLabel = null;
95
    private JLabel dbLabel = null;
96
    private JLabel dbLabelWarning = 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
    // private JLabel schemaLabel = null;
109
    // private JTextField schemaTextField = null;
110
    // private JCheckBox schemaCheck = null;
111

    
112
    /**
113
     * This method initializes
114
     *
115
     */
116
    public DBConnectionParamsDialog() {
117
        super();
118
        initialize();
119
    }
120

    
121
    public void showDialog() {
122
        PluginServices.getMDIManager().addWindow(this);
123
    }
124

    
125
    /**
126
     * This method initializes this
127
     *
128
     */
129
    private void initialize() {
130
        winfo.setWidth(370);
131
        winfo.setHeight(317 );
132
        winfo.setTitle(PluginServices.getText(this, "connection_parameters"));
133

    
134
        this.setSize(new java.awt.Dimension(360, 329+25));
135
        this.setLayout(null);
136
        this.add(getCancelButton(), null);
137
        this.add(getOkButton(), null);
138
        this.add(getParamsPanel(), null);
139
    }
140

    
141
    public WindowInfo getWindowInfo() {
142
        return winfo;
143
    }
144

    
145
    /**
146
     * This method initializes cancelButton
147
     *
148
     * @return javax.swing.JButton
149
     */
150
    private JButton getCancelButton() {
151
        if (cancelButton == null) {
152
            cancelButton = new JButton();
153
            cancelButton.setText(PluginServices.getText(this, "cancel"));
154
            cancelButton.addActionListener(this);
155
            cancelButton.setBounds(new java.awt.Rectangle(185, 292+25, 106, 26));
156
        }
157

    
158
        return cancelButton;
159
    }
160

    
161
    /**
162
     * This method initializes okButton
163
     *
164
     * @return javax.swing.JButton
165
     */
166
    private JButton getOkButton() {
167
        if (okButton == null) {
168
            okButton = new JButton();
169
            okButton.setText(PluginServices.getText(this, "ok"));
170
            okButton.addActionListener(this);
171
            okButton.setBounds(new java.awt.Rectangle(70, 292+25, 106, 26));
172
        }
173

    
174
        return okButton;
175
    }
176

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

    
206

    
207
            // schemaLabel = new JLabel();
208
            // schemaLabel.setBounds(new java.awt.Rectangle(10, 130+25, 141,
209
            // 21));
210
            // schemaLabel.setText(PluginServices.getText(this, "Schema") +
211
            // ":");
212

    
213
            dbLabelWarning = new JLabel();
214
            dbLabelWarning.setBounds(new java.awt.Rectangle(10, 155+25, 310, 41));
215
            dbLabelWarning.setText(PluginServices.getText(this, "warning_you_must_input_the_exact_name_this_difference_between_capital_letters_and_small_letters")
216
                );
217

    
218
            portLabel = new JLabel();
219
            portLabel.setBounds(new java.awt.Rectangle(10, 105, 141, 21));
220
            portLabel.setText(PluginServices.getText(this, "port") + ":");
221
            driverLabel = new JLabel();
222
            driverLabel.setBounds(new java.awt.Rectangle(10, 55, 141, 21));
223
            driverLabel.setText(PluginServices.getText(this, "driver") + ":");
224
            paramsPanel = new JPanel();
225
            paramsPanel.setBounds(new java.awt.Rectangle(10, 10, 336, 273+25));
226
            paramsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
227
                    null, PluginServices.getText(this, "connection_parameters"),
228
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
229
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
230
            paramsPanel.setLayout(null);
231
            paramsPanel.add(getPortTextField(), null);
232
            paramsPanel.add(getDriverComboBox(), null);
233
            paramsPanel.add(getDbTextField(), null);
234
            paramsPanel.add(getUserTextField(), null);
235
            paramsPanel.add(getPasswordField(), null);
236
            paramsPanel.add(driverLabel, null);
237
            paramsPanel.add(portLabel, null);
238

    
239
            paramsPanel.add(dbLabel, null);
240
            // paramsPanel.add(schemaLabel, null);
241

    
242
// paramsPanel.add(getSchemaField(), null);
243

    
244
            paramsPanel.add(dbLabelWarning, null);
245
            paramsPanel.add(userLabel, null);
246
            paramsPanel.add(pwLabel, null);
247
            paramsPanel.add(getUrlTextArea(), null);
248
            paramsPanel.add(urlLabel, null);
249
            paramsPanel.add(getConnectedCheckBox(), null);
250
            paramsPanel.add(connectedLabel, null);
251
            paramsPanel.add(connNameLabel, null);
252
            paramsPanel.add(getConnNameTextField(), null);
253
        }
254

    
255
        return paramsPanel;
256
    }
257

    
258
    // private JTextField getSchemaField() {
259
    // if (schemaTextField == null) {
260
    // schemaTextField = new JTextField();
261
    // schemaTextField.addKeyListener(this);
262
    // schemaTextField.setBounds(new java.awt.Rectangle(155, 130+25, 166, 21));
263
    // }
264
    //
265
    // return schemaTextField;
266
    // }
267

    
268

    
269
        /**
270
     * This method initializes driverComboBox
271
     *
272
     * @return javax.swing.JComboBox
273
     */
274
    private JComboBox getDriverComboBox() {
275
        if (driverComboBox == null) {
276
            driverComboBox = new JComboBox();
277
            driverComboBox.setMaximumRowCount(20);
278
            driverComboBox.addActionListener(this);
279

    
280
            String[] drvName = getDriverNames();
281
            int len = drvName.length;
282
            String[] sorted = new String[len];
283
            for (int i=0; i<len; i++) {
284
                    sorted[i] = "" + drvName[i];
285
            }
286
            Arrays.sort(sorted);
287

    
288
            for (int i = 0; i < len; i++) {
289
                driverComboBox.addItem(sorted[ (i + (2*len-2)) % len]);
290
            }
291

    
292
            driverComboBox.setBounds(new java.awt.Rectangle(155, 55, 166, 21));
293
        }
294

    
295
        return driverComboBox;
296
    }
297

    
298
    /**
299
     * This method initializes portTextField
300
     *
301
     * @return javax.swing.JTextField
302
     */
303
    private JTextField getPortTextField() {
304
        if (portTextField == null) {
305
            portTextField = new JTextField();
306
            portTextField.addKeyListener(this);
307
            portTextField.setBounds(new java.awt.Rectangle(155, 105, 166, 21));
308
        }
309

    
310
        return portTextField;
311
    }
312

    
313
    /**
314
     * This method initializes dbTextField
315
     *
316
     * @return javax.swing.JTextField
317
     */
318
    private JTextField getDbTextField() {
319
        if (dbTextField == null) {
320
            dbTextField = new JTextField();
321
            dbTextField.addKeyListener(this);
322
            dbTextField.setBounds(new java.awt.Rectangle(155, 130, 166, 21));
323
        }
324

    
325
        return dbTextField;
326
    }
327

    
328
    /**
329
     * This method initializes userTextField
330
     *
331
     * @return javax.swing.JTextField
332
     */
333
    private JTextField getUserTextField() {
334
        if (userTextField == null) {
335
            userTextField = new JTextField();
336
            userTextField.addKeyListener(this);
337
            userTextField.setBounds(new java.awt.Rectangle(155, 197+25, 166, 21));
338
        }
339

    
340
        return userTextField;
341
    }
342

    
343
    /**
344
     * This method initializes passwordField
345
     *
346
     * @return javax.swing.JPasswordField
347
     */
348
    private JPasswordField getPasswordField() {
349
        if (passwordField == null) {
350
            passwordField = new JPasswordField();
351
            passwordField.addKeyListener(this);
352
            passwordField.setBounds(new java.awt.Rectangle(155, 222+25, 166, 21));
353
        }
354

    
355
        return passwordField;
356
    }
357

    
358
    private String[] getDriverNames() {
359
        Class[] classes = new Class[] {
360
                        IVectorialDatabaseDriver.class,
361
                        AlphanumericDBDriver.class };
362

    
363
        ArrayList ret = new ArrayList();
364
        String[] driverNames = LayerFactory.getDM().getDriverNames();
365

    
366
        for (int i = 0; i < driverNames.length; i++) {
367
            for (int j = 0; j < classes.length; j++) {
368
                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
369
                    ret.add(driverNames[i]);
370
                    continue;
371
                }
372
            }
373
        }
374

    
375
        return (String[]) ret.toArray(new String[0]);
376
    }
377

    
378
    public void actionPerformed(ActionEvent arg0) {
379
        Object src = arg0.getSource();
380

    
381
        if (src == connectedCheckBox) {
382
            if (connectedCheckBox.isSelected()) {
383
                passwordField.setEnabled(true);
384
                passwordField.setBackground(Color.WHITE);
385
            }
386
            else {
387
                passwordField.setText("");
388
                passwordField.setEnabled(false);
389
                passwordField.setBackground(Color.LIGHT_GRAY);
390
            }
391
        }
392

    
393
        if (src == okButton) {
394
            okPressed = true;
395
            PluginServices.getMDIManager().closeWindow(this);
396

    
397
            return;
398
        }
399

    
400
        if (src == cancelButton) {
401
            okPressed = false;
402
            PluginServices.getMDIManager().closeWindow(this);
403

    
404
            return;
405
        }
406

    
407
        if (src == driverComboBox) {
408
            String driverName = driverComboBox.getSelectedItem().toString();
409
            Driver _drv;
410

    
411
            try {
412
                _drv = LayerFactory.getDM().getDriver(driverName);
413

    
414
                if (_drv instanceof IVectorialDatabaseDriver) {
415
                        IVectorialDatabaseDriver geo_drv = (IVectorialDatabaseDriver) _drv;
416
                        portTextField.setText("" + geo_drv.getDefaultPort());
417
                } else {
418
                    if (_drv instanceof AlphanumericDBDriver) {
419
                            AlphanumericDBDriver alpha_drv = (AlphanumericDBDriver) _drv;
420
                            portTextField.setText(alpha_drv.getDefaultPort());
421
                    } else {
422
                            portTextField.setText("");
423
                    }
424
                }
425
            } catch (DriverLoadException e1) {
426
                portTextField.setText("");
427
            }
428

    
429
            return;
430
        }
431

    
432

    
433
    }
434

    
435
    public boolean isOkPressed() {
436
        return okPressed;
437
    }
438

    
439
    public boolean hasToBeConnected() {
440
        return connectedCheckBox.isSelected();
441
    }
442

    
443
    public String getConnectionDriverName() {
444
        return driverComboBox.getSelectedItem().toString();
445
    }
446

    
447
    public String getConnectionServerUrl() {
448
        return urlTextField.getText();
449
    }
450

    
451
    public String getConnectionPort() {
452
        return portTextField.getText();
453
    }
454

    
455
    public String getConnectionDBName() {
456
        return dbTextField.getText();
457
    }
458

    
459
    public String getConnectionUser() {
460
        return userTextField.getText();
461
    }
462

    
463
    public String getConnectionPassword() {
464
        String resp = new String(passwordField.getPassword());
465

    
466
        return resp;
467
    }
468

    
469
    private JTextField getUrlTextArea() {
470
        if (urlTextField == null) {
471
            urlTextField = new JTextField();
472
            urlTextField.addKeyListener(this);
473
            urlTextField.setBounds(new java.awt.Rectangle(155, 80, 166, 21));
474
        }
475

    
476
        return urlTextField;
477
    }
478

    
479
    /**
480
     * This method initializes connectedCheckBox
481
     *
482
     * @return javax.swing.JCheckBox
483
     */
484
    private JCheckBox getConnectedCheckBox() {
485
        if (connectedCheckBox == null) {
486
            connectedCheckBox = new JCheckBox();
487
            connectedCheckBox.setSelected(true);
488
            connectedCheckBox.addActionListener(this);
489
            connectedCheckBox.setBounds(new java.awt.Rectangle(155, 247+25, 26, 21));
490
        }
491

    
492
        return connectedCheckBox;
493
    }
494

    
495
    public String getConnectionName() {
496
        return getConnNameTextField().getText();
497
    }
498

    
499
    /**
500
     * This method initializes connNameTextField
501
     *
502
     * @return javax.swing.JTextField
503
     */
504
    private JTextField getConnNameTextField() {
505
        if (connNameTextField == null) {
506
            connNameTextField = new JTextField();
507
            connNameTextField.addKeyListener(this);
508
            connNameTextField.setBounds(new java.awt.Rectangle(155, 30, 166, 21));
509
        }
510

    
511
        return connNameTextField;
512
    }
513

    
514
    public void keyPressed(KeyEvent e) {
515
    }
516

    
517
    public void keyReleased(KeyEvent e) {
518
        if (e.getKeyChar() != '\n') {
519
            return;
520
        }
521

    
522
        Object src = e.getSource();
523

    
524
        if (src == passwordField) {
525
            ActionEvent aevt = new ActionEvent(okButton,
526
                    ActionEvent.ACTION_PERFORMED, "");
527
            actionPerformed(aevt);
528
        }
529
        else {
530
            if (src instanceof JTextField) {
531
                ((JTextField) src).transferFocus();
532
            }
533
        }
534
    }
535

    
536
    public void keyTyped(KeyEvent e) {
537
    }
538

    
539
    public void loadValues(ConnectionWithParams cwp) {
540
        getPortTextField().setText(cwp.getPort());
541
        selectThisInDriverCombo(cwp.getDrvName());
542
        getDbTextField().setText(cwp.getDb());
543
        getUserTextField().setText(cwp.getUser());
544

    
545
        if (cwp.getPw() == null) {
546
            getPasswordField().setText("");
547
        }
548
        else {
549
            getPasswordField().setText(cwp.getPw());
550
        }
551

    
552
        getUrlTextArea().setText(cwp.getHost());
553

    
554
        boolean connected = false;
555

    
556
        try {
557
            connected = (cwp.getConnection() != null) &&
558
                (!cwp.getConnection().isClosed());
559
        }
560
        catch (DBException e) {
561
            logger.error("While checking connection: " + e.getMessage());
562
            connected = false;
563
        }
564

    
565
        getConnectedCheckBox().setSelected(connected);
566
        getConnNameTextField().setText(cwp.getName());
567

    
568

    
569
        // if (cwp.getSchema() != null) {
570
        // getSchemaField().setText(cwp.getSchema());
571
        // }
572
    }
573

    
574
    private void selectThisInDriverCombo(String drvName) {
575
        int size = getDriverComboBox().getItemCount();
576

    
577
        for (int i = 0; i < size; i++) {
578
            Object item = getDriverComboBox().getItemAt(i);
579

    
580
            if (item.toString().compareToIgnoreCase(drvName) == 0) {
581
                getDriverComboBox().setSelectedIndex(i);
582

    
583
                return;
584
            }
585
        }
586
    }
587

    
588
        public Object getWindowProfile() {
589
                return WindowInfo.DIALOG_PROFILE;
590
        }
591

    
592
        public String getConnectionSchema() {
593
        // return this.schemaTextField.getText();
594
        return "";
595
        }
596
} //  @jve:decl-index=0:visual-constraint="10,10"
597

    
598
// [eiel-gestion-conexiones]