Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / vectorialdb / wizard / VectorialDBConnectionParamsDialog.java @ 40557

History | View | Annotate | Download (21.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geodb.vectorialdb.wizard;
25

    
26
import java.awt.Color;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.awt.event.KeyEvent;
30
import java.awt.event.KeyListener;
31
import java.util.Iterator;
32
import java.util.List;
33

    
34
import javax.swing.JButton;
35
import javax.swing.JCheckBox;
36
import javax.swing.JComboBox;
37
import javax.swing.JLabel;
38
import javax.swing.JOptionPane;
39
import javax.swing.JPanel;
40
import javax.swing.JPasswordField;
41
import javax.swing.JTextField;
42

    
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

    
46
import org.gvsig.andami.PluginServices;
47
import org.gvsig.andami.messages.NotificationManager;
48
import org.gvsig.andami.ui.mdiManager.IWindow;
49
import org.gvsig.andami.ui.mdiManager.WindowInfo;
50
import org.gvsig.fmap.dal.DALLocator;
51
import org.gvsig.fmap.dal.DataManager;
52
import org.gvsig.fmap.dal.DataServerExplorerParameters;
53
import org.gvsig.fmap.dal.exception.DataException;
54
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
55
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters;
56
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
57
import org.gvsig.gui.beans.Messages;
58
import org.gvsig.tools.service.ServiceException;
59

    
60

    
61

    
62
/**
63
 * Lets the user input the connection parameters.
64
 *
65
 * @author jldominguez
66
 *
67
 */
68
public class VectorialDBConnectionParamsDialog extends JPanel implements IWindow,
69
    ActionListener, KeyListener {
70

    
71
    private static final long serialVersionUID = -5493563028200403559L;
72

    
73
    private static final Logger LOG = LoggerFactory
74
        .getLogger(VectorialDBConnectionParamsDialog.class);
75

    
76
    private WindowInfo winfo = new WindowInfo(8); // MODAL only
77
    private JButton cancelButton = null;
78
    private JButton okButton = null;
79
    private JButton advancedButton = null;
80
    private JPanel paramsPanel = null;
81
    private JComboBox driverComboBox = null;
82
    private JTextField portTextField = null;
83
    private JTextField dbTextField = null;
84
    private JTextField userTextField = null;
85
    private JPasswordField passwordField = null;
86
    private JLabel driverLabel = null;
87
    private JLabel portLabel = null;
88
    private JLabel dbLabel = null;
89
    private JLabel dbLabelWarning = null;
90
    private JLabel userLabel = null;
91
    private JLabel pwLabel = null;
92
    private boolean okPressed = false;
93
    private JTextField urlTextField = null;
94
    private JLabel urlLabel = null;
95
    private JCheckBox connectedCheckBox = null;
96
    private JLabel connectedLabel = null;
97
    private JLabel connNameLabel = null;
98
    private JTextField connNameTextField = null;
99

    
100
    private DBServerExplorerParameters params = null;
101

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

    
111
    public void showDialog() {
112
        PluginServices.getMDIManager().addWindow(this);
113
    }
114

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

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

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

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

    
149
        return cancelButton;
150
    }
151

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

    
165
        return okButton;
166
    }
167

    
168
        /**
169
         * This method initializes okButton
170
         *
171
         * @return javax.swing.JButton
172
         */
173
        private JButton getAdvancedButton() {
174
                if (advancedButton == null) {
175
                        advancedButton = new JButton();
176
                        advancedButton.setText(PluginServices.getText(this, "advanced"));
177
                        advancedButton.addActionListener(this);
178
                        advancedButton.setBounds(new java.awt.Rectangle(218, 292, 90, 26));
179
                }
180

    
181
                return advancedButton;
182
        }
183

    
184
        /**
185
         * This method initializes paramsPanel
186
         *
187
         * @return javax.swing.JPanel
188
         */
189
    private JPanel getParamsPanel() {
190
        if (paramsPanel == null) {
191
            connNameLabel = new JLabel();
192
            connNameLabel.setBounds(new java.awt.Rectangle(10, 30, 141, 21));
193
            connNameLabel.setText(PluginServices.getText(this, "_Connection_name") +
194
                ":");
195
            connectedLabel = new JLabel();
196
            connectedLabel.setBounds(new java.awt.Rectangle(10, 247, 141, 21));
197
            connectedLabel.setText(PluginServices.getText(this, "_Connected") +
198
                ":");
199
            urlLabel = new JLabel();
200
            urlLabel.setBounds(new java.awt.Rectangle(10, 80, 141, 21));
201
            urlLabel.setText(PluginServices.getText(this, "_Host") + ":");
202
            pwLabel = new JLabel();
203
            pwLabel.setBounds(new java.awt.Rectangle(10, 222, 141, 21));
204
            pwLabel.setText(PluginServices.getText(this, "_Password") + ":");
205
            userLabel = new JLabel();
206
            userLabel.setBounds(new java.awt.Rectangle(10, 197, 141, 21));
207
            userLabel.setText(PluginServices.getText(this, "_User") + ":");
208
            dbLabel = new JLabel();
209
            dbLabel.setBounds(new java.awt.Rectangle(10, 130, 141, 21));
210
            dbLabel.setText(PluginServices.getText(this, "_Database") +
211
                ":");
212
            dbLabelWarning = new JLabel();
213
            dbLabelWarning.setBounds(new java.awt.Rectangle(10, 155, 310, 41));
214
            dbLabelWarning.setText(PluginServices.getText(this, "warning_you_must_input_the_exact_name_this_difference_between_capital_letters_and_small_letters")
215
                );
216

    
217
            portLabel = new JLabel();
218
            portLabel.setBounds(new java.awt.Rectangle(10, 105, 141, 21));
219
            portLabel.setText(PluginServices.getText(this, "_Port") + ":");
220
            driverLabel = new JLabel();
221
            driverLabel.setBounds(new java.awt.Rectangle(10, 55, 141, 21));
222
            driverLabel.setText(PluginServices.getText(this, "_Driver_type") + ":");
223
            paramsPanel = new JPanel();
224
            paramsPanel.setBounds(new java.awt.Rectangle(10, 10, 336, 273));
225
            paramsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
226
                    null, PluginServices.getText(this, "_Connection_parameters"),
227
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
228
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
229
            paramsPanel.setLayout(null);
230
            paramsPanel.add(getPortTextField(), null);
231
            paramsPanel.add(getDriverComboBox(), null);
232
            paramsPanel.add(getDbTextField(), null);
233
            paramsPanel.add(getUserTextField(), null);
234
            paramsPanel.add(getPasswordField(), null);
235
            paramsPanel.add(driverLabel, null);
236
            paramsPanel.add(portLabel, null);
237
            paramsPanel.add(dbLabel, null);
238
            paramsPanel.add(dbLabelWarning, null);
239
            paramsPanel.add(userLabel, null);
240
            paramsPanel.add(pwLabel, null);
241
            paramsPanel.add(getUrlTextArea(), null);
242
            paramsPanel.add(urlLabel, null);
243
            paramsPanel.add(getConnectedCheckBox(), null);
244
            paramsPanel.add(connectedLabel, null);
245
            paramsPanel.add(connNameLabel, null);
246
            paramsPanel.add(getConnNameTextField(), null);
247
        }
248

    
249
        return paramsPanel;
250
    }
251

    
252
    /**
253
     * This method initializes driverComboBox
254
     *
255
     * @return javax.swing.JComboBox
256
     */
257
    private JComboBox getDriverComboBox() {
258
        if (driverComboBox == null) {
259
            driverComboBox = new JComboBox();
260
            driverComboBox.addActionListener(this);
261
            DataManager dm = DALLocator.getDataManager();
262
            List<String> explorers = dm.getExplorerProviders();
263
            Iterator<String> iter = explorers.iterator();
264
            String expName;
265

    
266
            DataServerExplorerParameters params;
267
            while (iter.hasNext()) {
268
                    expName = iter.next();
269
                                try {
270
                                        params = dm.createServerExplorerParameters(expName);
271
                                } catch (DataException e) {
272
                                        NotificationManager.addWarning(PluginServices.getText(null,
273
                                                        "DataExplorer_parameters_error")
274
                                                        + ": " + expName, e);
275
                                        continue;
276
                                }
277
                                if (params instanceof DBServerExplorerParameters) {
278
                                        DBServerExplorerParameters dbParams = (DBServerExplorerParameters) params;
279
                                        driverComboBox.addItem(new DriverComboBoxItem(
280
                                                        dbParams));
281
                                }
282

    
283
            }
284

    
285
            driverComboBox.setBounds(new java.awt.Rectangle(155, 55, 166, 21));
286

    
287
        }
288

    
289
        return driverComboBox;
290
    }
291

    
292
    private class DriverComboBoxItem {
293
            private DBServerExplorerParameters params;
294

    
295
            public DriverComboBoxItem(DBServerExplorerParameters dbParams) {
296
                        this.params = dbParams;
297
                }
298

    
299
                public String toString() {
300
                        // FIXME
301
                    return params.getExplorerName();
302
            }
303
    }
304

    
305
    /**
306
     * This method initializes portTextField
307
     *
308
     * @return javax.swing.JTextField
309
     */
310
    private JTextField getPortTextField() {
311
        if (portTextField == null) {
312
            portTextField = new JTextField();
313
            portTextField.addKeyListener(this);
314
            portTextField.setBounds(new java.awt.Rectangle(155, 105, 166, 21));
315
        }
316

    
317
        return portTextField;
318
    }
319

    
320
    /**
321
     * This method initializes dbTextField
322
     *
323
     * @return javax.swing.JTextField
324
     */
325
    private JTextField getDbTextField() {
326
        if (dbTextField == null) {
327
            dbTextField = new JTextField();
328
            dbTextField.addKeyListener(this);
329
            dbTextField.setBounds(new java.awt.Rectangle(155, 130, 166, 21));
330
        }
331

    
332
        return dbTextField;
333
    }
334

    
335
    /**
336
     * This method initializes userTextField
337
     *
338
     * @return javax.swing.JTextField
339
     */
340
    private JTextField getUserTextField() {
341
        if (userTextField == null) {
342
            userTextField = new JTextField();
343
            userTextField.addKeyListener(this);
344
            userTextField.setBounds(new java.awt.Rectangle(155, 197, 166, 21));
345
        }
346

    
347
        return userTextField;
348
    }
349

    
350
    /**
351
     * This method initializes passwordField
352
     *
353
     * @return javax.swing.JPasswordField
354
     */
355
    private JPasswordField getPasswordField() {
356
        if (passwordField == null) {
357
            passwordField = new JPasswordField();
358
            passwordField.addKeyListener(this);
359
            passwordField.setBounds(new java.awt.Rectangle(155, 222, 166, 21));
360
        }
361

    
362
        return passwordField;
363
    }
364

    
365
//    private String[] getDriverNames() {
366
//        Class[] classes = new Class[] { IVectorialDatabaseDriver.class };
367
//
368
//        ArrayList ret = new ArrayList();
369
//        String[] driverNames = LayerFactory.getDM().getDriverNames();
370
//
371
//        for (int i = 0; i < driverNames.length; i++) {
372
//            for (int j = 0; j < classes.length; j++) {
373
//                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
374
//                    ret.add(driverNames[i]);
375
//                }
376
//            }
377
//        }
378
//
379
//        return (String[]) ret.toArray(new String[0]);
380
//    }
381

    
382
    public void actionPerformed(ActionEvent arg0) {
383
        Object src = arg0.getSource();
384

    
385
        if (src == connectedCheckBox) {
386
            if (connectedCheckBox.isSelected()) {
387
                passwordField.setEnabled(true);
388
                passwordField.setBackground(Color.WHITE);
389
            }
390
            else {
391
                passwordField.setText("");
392
                passwordField.setEnabled(false);
393
                passwordField.setBackground(Color.LIGHT_GRAY);
394
            }
395
        }
396

    
397
        if (src == okButton) {
398
            DBServerExplorerParameters myParams = getParameters();
399
            try {
400
                myParams.validate();
401

    
402
                okPressed = true;
403
                PluginServices.getMDIManager().closeWindow(this);
404
            } catch (Exception e) {
405
                // There are errors in the parameters, maybe more required ones.
406
                // Warn the user and open the advanced parameters panel
407
                JOptionPane
408
                    .showMessageDialog(this, Messages
409
                        .getText("more_provider_parameters_needed_message"),
410
                        Messages
411
                            .getText("more_provider_parameters_needed_title"),
412
                        JOptionPane.WARNING_MESSAGE);
413
                showAdvancedParametersPanel();
414
            }
415

    
416
            return;
417
        }
418

    
419
        if (src == cancelButton) {
420
            okPressed = false;
421
            PluginServices.getMDIManager().closeWindow(this);
422

    
423
            return;
424
        }
425

    
426
        if (src == advancedButton) {
427
            showAdvancedParametersPanel();
428

    
429
                        return;
430
                }
431

    
432
        if (src == driverComboBox) {
433
                DBServerExplorerParameters params = ((DriverComboBoxItem) driverComboBox
434
                                        .getSelectedItem()).params;
435
                        try {
436
                                params.validate();
437
                        } catch (ValidateDataParametersException e) {
438
                        }
439

    
440
//            try {
441
//                    DataManager dm=DataManager.getManager();
442
//                featureStore = (FeatureStore) dm.createDataStore()LayerFactory.getDM()
443
//                                                           .getDriver(driverName);
444
                        if (params.getPort() != null) {
445
                                portTextField.setText(params.getPort() + "");
446
                        } else {
447
                                portTextField.setText("");
448
                        }
449
//            }
450
//            catch (DriverLoadException e1) {
451
//                portTextField.setText("");
452
//            }
453

    
454
            return;
455
        }
456
    }
457

    
458
    /**
459
     * @param myParams
460
     */
461
    private void showAdvancedParametersPanel() {
462
        DBServerExplorerParameters myParams = getParameters();
463
        try {
464
            myParams.validate();
465
        } catch (Exception e) {
466
            // ignore... only for fill default values
467
        }
468
        try {
469
            DynObjectEditor editor = new DynObjectEditor(myParams);
470
            editor.editObject(true);
471
            refreshFormParameters();
472
        } catch (ServiceException ex) {
473
            LOG.error("Error creating a Swing component for the DynObject: "
474
                + myParams, ex);
475
        }
476
    }
477

    
478
    private void refreshFormParameters() {
479
        DBServerExplorerParameters myParams = internalGetParameters();
480
        getDbTextField().setText(myParams.getDBName());
481
        getPasswordField().setText(myParams.getPassword());
482
        Integer port = myParams.getPort();
483
        getPortTextField().setText(port == null ? "" : port.toString());
484
        getUrlTextArea().setText(myParams.getHost());
485
        getUserTextField().setText(myParams.getUser());
486
        this.updateUI();
487
    }
488

    
489
    public boolean isOkPressed() {
490
        return okPressed;
491
    }
492

    
493
    public boolean hasToBeConnected() {
494
        return connectedCheckBox.isSelected();
495
    }
496

    
497
    public String getConnectionDriverName() {
498
        return ((DriverComboBoxItem) driverComboBox.getSelectedItem()).params
499
                                .getExplorerName();
500
    }
501

    
502
    public String getConnectionServerUrl() {
503
        return urlTextField.getText();
504
    }
505

    
506
    public String getConnectionPort() {
507
        return portTextField.getText();
508
    }
509

    
510
    public String getConnectionDBName() {
511
        return dbTextField.getText();
512
    }
513

    
514
    public String getConnectionUser() {
515
        return userTextField.getText();
516
    }
517

    
518
    public String getConnectionPassword() {
519
        String resp = new String(passwordField.getPassword());
520

    
521
        return resp;
522
    }
523

    
524
    private JTextField getUrlTextArea() {
525
        if (urlTextField == null) {
526
            urlTextField = new JTextField();
527
            urlTextField.addKeyListener(this);
528
            urlTextField.setBounds(new java.awt.Rectangle(155, 80, 166, 21));
529
        }
530

    
531
        return urlTextField;
532
    }
533

    
534
    /**
535
     * This method initializes connectedCheckBox
536
     *
537
     * @return javax.swing.JCheckBox
538
     */
539
    private JCheckBox getConnectedCheckBox() {
540
        if (connectedCheckBox == null) {
541
            connectedCheckBox = new JCheckBox();
542
            connectedCheckBox.setSelected(true);
543
            connectedCheckBox.addActionListener(this);
544
            connectedCheckBox.setBounds(new java.awt.Rectangle(155, 247, 26, 21));
545
        }
546

    
547
        return connectedCheckBox;
548
    }
549

    
550
    public String getConnectionName() {
551
        return getConnNameTextField().getText();
552
    }
553

    
554
    /**
555
     * This method initializes connNameTextField
556
     *
557
     * @return javax.swing.JTextField
558
     */
559
    private JTextField getConnNameTextField() {
560
        if (connNameTextField == null) {
561
            connNameTextField = new JTextField();
562
            connNameTextField.addKeyListener(this);
563
            connNameTextField.setBounds(new java.awt.Rectangle(155, 30, 166, 21));
564
        }
565

    
566
        return connNameTextField;
567
    }
568

    
569
    public void keyPressed(KeyEvent e) {
570
    }
571

    
572
    public void keyReleased(KeyEvent e) {
573
        if (e.getKeyChar() != '\n') {
574
            return;
575
        }
576

    
577
        Object src = e.getSource();
578

    
579
        if (src == passwordField) {
580
            ActionEvent aevt = new ActionEvent(okButton,
581
                    ActionEvent.ACTION_PERFORMED, "");
582
            actionPerformed(aevt);
583
        }
584
        else {
585
            if (src instanceof JTextField) {
586
                ((JTextField) src).transferFocus();
587
            }
588
        }
589
    }
590

    
591
    public void keyTyped(KeyEvent e) {
592
        }
593

    
594
    public void loadValues(DBServerExplorerParameters cwp) {
595
            if (cwp.getPort() != null){
596
                    getPortTextField().setText(cwp.getPort().toString());
597
            } else{
598
                    getPortTextField().setText("");
599
            }
600
        selectThisInDriverCombo(cwp.getExplorerName());
601
        getDbTextField().setText(cwp.getDBName());
602
        getUserTextField().setText(cwp.getUser());
603

    
604
        if (cwp.getPassword() == null) {
605
            getPasswordField().setText("");
606
        }
607
        else {
608
            getPasswordField().setText(cwp.getPassword());
609
        }
610

    
611
        getUrlTextArea().setText(cwp.getHost());
612

    
613
        // boolean connected = false;
614
        //
615
//        try {
616
//            connected = (cwp.getConnection() != null) &&
617
//                (!cwp.getConnection().isClosed());
618
//        }
619
//        catch (DBException e) {
620
        // LOG.error("While checking connection: " + e.getMessage());
621
//            connected = false;
622
//        }
623

    
624
//        getConnectedCheckBox().setSelected(connected);
625
                getConnNameTextField().setText(cwp.getExplorerName());
626
    }
627

    
628
    private void selectThisInDriverCombo(String drvName) {
629
        int size = getDriverComboBox().getItemCount();
630
        int curSel = getDriverComboBox().getSelectedIndex();
631

    
632
        for (int i = 0; i < size; i++) {
633
            DriverComboBoxItem item = (DriverComboBoxItem) getDriverComboBox()
634
                                        .getItemAt(i);
635

    
636
            if (item.params.getExplorerName().compareToIgnoreCase(drvName) == 0) {
637
                getDriverComboBox().setSelectedIndex(i);
638
                if (curSel != i) {
639
                                        this.params = null;
640
                                }
641

    
642
                return;
643
            }
644
        }
645
    }
646

    
647
        public Object getWindowProfile() {
648
                return WindowInfo.DIALOG_PROFILE;
649
        }
650

    
651
        public DBServerExplorerParameters getParameters(){
652

    
653
        DBServerExplorerParameters params = internalGetParameters();
654

    
655
                String _host = getConnectionServerUrl();
656
                String _port = getConnectionPort();
657
                String _dbname = getConnectionDBName();
658
                String _user = getConnectionUser();
659
                String _pw = getConnectionPassword();
660

    
661
                params.setHost(_host);
662
                if (_port.trim().length() != 0) {
663
                        try {
664
                                params.setPort(Integer.parseInt(_port));
665
                        } catch (NumberFormatException e) {
666
                                NotificationManager.addError("Invalid port", e);
667
                        }
668
                }
669
                params.setDBName(_dbname);
670
                params.setUser(_user);
671
                params.setPassword(_pw);
672

    
673
                return params;
674
        }
675

    
676
    private DBServerExplorerParameters internalGetParameters() {
677
        if (params == null) {
678
            DriverComboBoxItem item =
679
                (DriverComboBoxItem) getDriverComboBox().getItemAt(
680
                    getDriverComboBox().getSelectedIndex());
681
            params = (DBServerExplorerParameters) item.params.getCopy();
682

    
683
        }
684

    
685
        return params;
686
    }
687

    
688
} //  @jve:decl-index=0:visual-constraint="10,10"