Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / VectorialDBConnectionParamsDialog.java @ 34697

History | View | Annotate | Download (20.2 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.geodb.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.Iterator;
51
import java.util.List;
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.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63

    
64
import org.gvsig.andami.PluginServices;
65
import org.gvsig.andami.messages.NotificationManager;
66
import org.gvsig.andami.ui.mdiManager.IWindow;
67
import org.gvsig.andami.ui.mdiManager.WindowInfo;
68
import org.gvsig.fmap.dal.DALLocator;
69
import org.gvsig.fmap.dal.DataManager;
70
import org.gvsig.fmap.dal.DataServerExplorerParameters;
71
import org.gvsig.fmap.dal.exception.DataException;
72
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
73
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters;
74
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.DynObjectEditor;
75
import org.gvsig.tools.service.ServiceException;
76

    
77

    
78

    
79
/**
80
 * Lets the user input the connection parameters.
81
 *
82
 * @author jldominguez
83
 *
84
 */
85
public class VectorialDBConnectionParamsDialog extends JPanel implements IWindow,
86
    ActionListener, KeyListener {
87

    
88
    private static final long serialVersionUID = -5493563028200403559L;
89

    
90
    private static final Logger LOG = LoggerFactory
91
        .getLogger(VectorialDBConnectionParamsDialog.class);
92

    
93
    private WindowInfo winfo = new WindowInfo(8); // MODAL only
94
    private JButton cancelButton = null;
95
    private JButton okButton = null;
96
    private JButton advancedButton = null;
97
    private JPanel paramsPanel = null;
98
    private JComboBox driverComboBox = null;
99
    private JTextField portTextField = null;
100
    private JTextField dbTextField = null;
101
    private JTextField userTextField = null;
102
    private JPasswordField passwordField = null;
103
    private JLabel driverLabel = null;
104
    private JLabel portLabel = null;
105
    private JLabel dbLabel = null;
106
    private JLabel dbLabelWarning = null;
107
    private JLabel userLabel = null;
108
    private JLabel pwLabel = null;
109
    private boolean okPressed = false;
110
    private JTextField urlTextField = null;
111
    private JLabel urlLabel = null;
112
    private JCheckBox connectedCheckBox = null;
113
    private JLabel connectedLabel = null;
114
    private JLabel connNameLabel = null;
115
    private JTextField connNameTextField = null;
116

    
117
    private DBServerExplorerParameters params = null;
118

    
119
    /**
120
     * This method initializes
121
     *
122
     */
123
    public VectorialDBConnectionParamsDialog() {
124
        super();
125
        initialize();
126
    }
127

    
128
    public void showDialog() {
129
        PluginServices.getMDIManager().addWindow(this);
130
    }
131

    
132
    /**
133
     * This method initializes this
134
     *
135
     */
136
    private void initialize() {
137
        winfo.setWidth(370);
138
        winfo.setHeight(317 - 25);
139
        winfo.setTitle(PluginServices.getText(this, "connection_parameters"));
140

    
141
        this.setSize(new java.awt.Dimension(360, 329));
142
        this.setLayout(null);
143
        this.add(getCancelButton(), null);
144
        this.add(getOkButton(), null);
145
        this.add(getAdvancedButton(), null);
146
        this.add(getParamsPanel(), null);
147
    }
148

    
149
    public WindowInfo getWindowInfo() {
150
        return winfo;
151
    }
152

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

    
166
        return cancelButton;
167
    }
168

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

    
182
        return okButton;
183
    }
184

    
185
        /**
186
         * This method initializes okButton
187
         *
188
         * @return javax.swing.JButton
189
         */
190
        private JButton getAdvancedButton() {
191
                if (advancedButton == null) {
192
                        advancedButton = new JButton();
193
                        advancedButton.setText(PluginServices.getText(this, "advanced"));
194
                        advancedButton.addActionListener(this);
195
                        advancedButton.setBounds(new java.awt.Rectangle(218, 292, 90, 26));
196
                }
197

    
198
                return advancedButton;
199
        }
200

    
201
        /**
202
         * This method initializes paramsPanel
203
         *
204
         * @return javax.swing.JPanel
205
         */
206
    private JPanel getParamsPanel() {
207
        if (paramsPanel == null) {
208
            connNameLabel = new JLabel();
209
            connNameLabel.setBounds(new java.awt.Rectangle(10, 30, 141, 21));
210
            connNameLabel.setText(PluginServices.getText(this, "connection_name") +
211
                ":");
212
            connectedLabel = new JLabel();
213
            connectedLabel.setBounds(new java.awt.Rectangle(10, 247, 141, 21));
214
            connectedLabel.setText(PluginServices.getText(this, "connected") +
215
                ":");
216
            urlLabel = new JLabel();
217
            urlLabel.setBounds(new java.awt.Rectangle(10, 80, 141, 21));
218
            urlLabel.setText(PluginServices.getText(this, "server_url") + ":");
219
            pwLabel = new JLabel();
220
            pwLabel.setBounds(new java.awt.Rectangle(10, 222, 141, 21));
221
            pwLabel.setText(PluginServices.getText(this, "password") + ":");
222
            userLabel = new JLabel();
223
            userLabel.setBounds(new java.awt.Rectangle(10, 197, 141, 21));
224
            userLabel.setText(PluginServices.getText(this, "user") + ":");
225
            dbLabel = new JLabel();
226
            dbLabel.setBounds(new java.awt.Rectangle(10, 130, 141, 21));
227
            dbLabel.setText(PluginServices.getText(this, "database_name") +
228
                ":");
229
            dbLabelWarning = new JLabel();
230
            dbLabelWarning.setBounds(new java.awt.Rectangle(10, 155, 310, 41));
231
            dbLabelWarning.setText(PluginServices.getText(this, "warning_you_must_input_the_exact_name_this_difference_between_capital_letters_and_small_letters")
232
                );
233

    
234
            portLabel = new JLabel();
235
            portLabel.setBounds(new java.awt.Rectangle(10, 105, 141, 21));
236
            portLabel.setText(PluginServices.getText(this, "port") + ":");
237
            driverLabel = new JLabel();
238
            driverLabel.setBounds(new java.awt.Rectangle(10, 55, 141, 21));
239
            driverLabel.setText(PluginServices.getText(this, "driver") + ":");
240
            paramsPanel = new JPanel();
241
            paramsPanel.setBounds(new java.awt.Rectangle(10, 10, 336, 273));
242
            paramsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
243
                    null, PluginServices.getText(this, "connection_params"),
244
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
245
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
246
            paramsPanel.setLayout(null);
247
            paramsPanel.add(getPortTextField(), null);
248
            paramsPanel.add(getDriverComboBox(), null);
249
            paramsPanel.add(getDbTextField(), null);
250
            paramsPanel.add(getUserTextField(), null);
251
            paramsPanel.add(getPasswordField(), null);
252
            paramsPanel.add(driverLabel, null);
253
            paramsPanel.add(portLabel, null);
254
            paramsPanel.add(dbLabel, null);
255
            paramsPanel.add(dbLabelWarning, null);
256
            paramsPanel.add(userLabel, null);
257
            paramsPanel.add(pwLabel, null);
258
            paramsPanel.add(getUrlTextArea(), null);
259
            paramsPanel.add(urlLabel, null);
260
            paramsPanel.add(getConnectedCheckBox(), null);
261
            paramsPanel.add(connectedLabel, null);
262
            paramsPanel.add(connNameLabel, null);
263
            paramsPanel.add(getConnNameTextField(), null);
264
        }
265

    
266
        return paramsPanel;
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.addActionListener(this);
278
            DataManager dm = DALLocator.getDataManager();
279
            List<String> explorers = dm.getExplorerProviders();
280
            Iterator<String> iter = explorers.iterator();
281
            String expName;
282

    
283
            DataServerExplorerParameters params;
284
            while (iter.hasNext()) {
285
                    expName = iter.next();
286
                                try {
287
                                        params = dm.createServerExplorerParameters(expName);
288
                                } catch (DataException e) {
289
                                        NotificationManager.addWarning(PluginServices.getText(null,
290
                                                        "DataExplorer_parameters_error")
291
                                                        + ": " + expName, e);
292
                                        continue;
293
                                }
294
                                if (params instanceof DBServerExplorerParameters) {
295
                                        DBServerExplorerParameters dbParams = (DBServerExplorerParameters) params;
296
                                        driverComboBox.addItem(new DriverComboBoxItem(
297
                                                        dbParams));
298
                                }
299

    
300
            }
301

    
302
            driverComboBox.setBounds(new java.awt.Rectangle(155, 55, 166, 21));
303

    
304
        }
305

    
306
        return driverComboBox;
307
    }
308

    
309
    private class DriverComboBoxItem {
310
            private DBServerExplorerParameters params;
311

    
312
            public DriverComboBoxItem(DBServerExplorerParameters dbParams) {
313
                        this.params = dbParams;
314
                }
315

    
316
                public String toString() {
317
                        // FIXME
318
                    return params.getExplorerName();
319
            }
320
    }
321

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

    
334
        return portTextField;
335
    }
336

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

    
349
        return dbTextField;
350
    }
351

    
352
    /**
353
     * This method initializes userTextField
354
     *
355
     * @return javax.swing.JTextField
356
     */
357
    private JTextField getUserTextField() {
358
        if (userTextField == null) {
359
            userTextField = new JTextField();
360
            userTextField.addKeyListener(this);
361
            userTextField.setBounds(new java.awt.Rectangle(155, 197, 166, 21));
362
        }
363

    
364
        return userTextField;
365
    }
366

    
367
    /**
368
     * This method initializes passwordField
369
     *
370
     * @return javax.swing.JPasswordField
371
     */
372
    private JPasswordField getPasswordField() {
373
        if (passwordField == null) {
374
            passwordField = new JPasswordField();
375
            passwordField.addKeyListener(this);
376
            passwordField.setBounds(new java.awt.Rectangle(155, 222, 166, 21));
377
        }
378

    
379
        return passwordField;
380
    }
381

    
382
//    private String[] getDriverNames() {
383
//        Class[] classes = new Class[] { IVectorialDatabaseDriver.class };
384
//
385
//        ArrayList ret = new ArrayList();
386
//        String[] driverNames = LayerFactory.getDM().getDriverNames();
387
//
388
//        for (int i = 0; i < driverNames.length; i++) {
389
//            for (int j = 0; j < classes.length; j++) {
390
//                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
391
//                    ret.add(driverNames[i]);
392
//                }
393
//            }
394
//        }
395
//
396
//        return (String[]) ret.toArray(new String[0]);
397
//    }
398

    
399
    public void actionPerformed(ActionEvent arg0) {
400
        Object src = arg0.getSource();
401

    
402
        if (src == connectedCheckBox) {
403
            if (connectedCheckBox.isSelected()) {
404
                passwordField.setEnabled(true);
405
                passwordField.setBackground(Color.WHITE);
406
            }
407
            else {
408
                passwordField.setText("");
409
                passwordField.setEnabled(false);
410
                passwordField.setBackground(Color.LIGHT_GRAY);
411
            }
412
        }
413

    
414
        if (src == okButton) {
415
            okPressed = true;
416
            PluginServices.getMDIManager().closeWindow(this);
417

    
418
            return;
419
        }
420

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

    
425
            return;
426
        }
427

    
428
        if (src == advancedButton) {
429
                        DBServerExplorerParameters myParams = getParameters();
430
                        try {
431
                                myParams.validate();
432
                        } catch (Exception e) {
433
                                // ignore... only for fill default values
434
                        }
435
            try {
436
                DynObjectEditor editor = new DynObjectEditor(myParams);
437
                editor.editObject(true);
438
            } catch (ServiceException ex) {
439
                LOG.error(
440
                    "Error creating a Swing component for the DynObject: "
441
                        + myParams, ex);
442
            }
443

    
444
                        return;
445
                }
446

    
447
        if (src == driverComboBox) {
448
                DBServerExplorerParameters params = ((DriverComboBoxItem) driverComboBox
449
                                        .getSelectedItem()).params;
450
                        try {
451
                                params.validate();
452
                        } catch (ValidateDataParametersException e) {
453
                        }
454

    
455
//            try {
456
//                    DataManager dm=DataManager.getManager();
457
//                featureStore = (FeatureStore) dm.createDataStore()LayerFactory.getDM()
458
//                                                           .getDriver(driverName);
459
                        if (params.getPort() != null) {
460
                                portTextField.setText(params.getPort() + "");
461
                        } else {
462
                                portTextField.setText("");
463
                        }
464
//            }
465
//            catch (DriverLoadException e1) {
466
//                portTextField.setText("");
467
//            }
468

    
469
            return;
470
        }
471
    }
472

    
473
    public boolean isOkPressed() {
474
        return okPressed;
475
    }
476

    
477
    public boolean hasToBeConnected() {
478
        return connectedCheckBox.isSelected();
479
    }
480

    
481
    public String getConnectionDriverName() {
482
        return ((DriverComboBoxItem) driverComboBox.getSelectedItem()).params
483
                                .getExplorerName();
484
    }
485

    
486
    public String getConnectionServerUrl() {
487
        return urlTextField.getText();
488
    }
489

    
490
    public String getConnectionPort() {
491
        return portTextField.getText();
492
    }
493

    
494
    public String getConnectionDBName() {
495
        return dbTextField.getText();
496
    }
497

    
498
    public String getConnectionUser() {
499
        return userTextField.getText();
500
    }
501

    
502
    public String getConnectionPassword() {
503
        String resp = new String(passwordField.getPassword());
504

    
505
        return resp;
506
    }
507

    
508
    private JTextField getUrlTextArea() {
509
        if (urlTextField == null) {
510
            urlTextField = new JTextField();
511
            urlTextField.addKeyListener(this);
512
            urlTextField.setBounds(new java.awt.Rectangle(155, 80, 166, 21));
513
        }
514

    
515
        return urlTextField;
516
    }
517

    
518
    /**
519
     * This method initializes connectedCheckBox
520
     *
521
     * @return javax.swing.JCheckBox
522
     */
523
    private JCheckBox getConnectedCheckBox() {
524
        if (connectedCheckBox == null) {
525
            connectedCheckBox = new JCheckBox();
526
            connectedCheckBox.setSelected(true);
527
            connectedCheckBox.addActionListener(this);
528
            connectedCheckBox.setBounds(new java.awt.Rectangle(155, 247, 26, 21));
529
        }
530

    
531
        return connectedCheckBox;
532
    }
533

    
534
    public String getConnectionName() {
535
        return getConnNameTextField().getText();
536
    }
537

    
538
    /**
539
     * This method initializes connNameTextField
540
     *
541
     * @return javax.swing.JTextField
542
     */
543
    private JTextField getConnNameTextField() {
544
        if (connNameTextField == null) {
545
            connNameTextField = new JTextField();
546
            connNameTextField.addKeyListener(this);
547
            connNameTextField.setBounds(new java.awt.Rectangle(155, 30, 166, 21));
548
        }
549

    
550
        return connNameTextField;
551
    }
552

    
553
    public void keyPressed(KeyEvent e) {
554
    }
555

    
556
    public void keyReleased(KeyEvent e) {
557
        if (e.getKeyChar() != '\n') {
558
            return;
559
        }
560

    
561
        Object src = e.getSource();
562

    
563
        if (src == passwordField) {
564
            ActionEvent aevt = new ActionEvent(okButton,
565
                    ActionEvent.ACTION_PERFORMED, "");
566
            actionPerformed(aevt);
567
        }
568
        else {
569
            if (src instanceof JTextField) {
570
                ((JTextField) src).transferFocus();
571
            }
572
        }
573
    }
574

    
575
    public void keyTyped(KeyEvent e) {
576
        }
577

    
578
    public void loadValues(DBServerExplorerParameters cwp) {
579
            if (cwp.getPort() != null){
580
                    getPortTextField().setText(cwp.getPort().toString());
581
            } else{
582
                    getPortTextField().setText("");
583
            }
584
        selectThisInDriverCombo(cwp.getExplorerName());
585
        getDbTextField().setText(cwp.getDBName());
586
        getUserTextField().setText(cwp.getUser());
587

    
588
        if (cwp.getPassword() == null) {
589
            getPasswordField().setText("");
590
        }
591
        else {
592
            getPasswordField().setText(cwp.getPassword());
593
        }
594

    
595
        getUrlTextArea().setText(cwp.getHost());
596

    
597
        // boolean connected = false;
598
        //
599
//        try {
600
//            connected = (cwp.getConnection() != null) &&
601
//                (!cwp.getConnection().isClosed());
602
//        }
603
//        catch (DBException e) {
604
        // LOG.error("While checking connection: " + e.getMessage());
605
//            connected = false;
606
//        }
607

    
608
//        getConnectedCheckBox().setSelected(connected);
609
                getConnNameTextField().setText(cwp.getExplorerName());
610
    }
611

    
612
    private void selectThisInDriverCombo(String drvName) {
613
        int size = getDriverComboBox().getItemCount();
614
        int curSel = getDriverComboBox().getSelectedIndex();
615

    
616
        for (int i = 0; i < size; i++) {
617
            DriverComboBoxItem item = (DriverComboBoxItem) getDriverComboBox()
618
                                        .getItemAt(i);
619

    
620
            if (item.params.getExplorerName().compareToIgnoreCase(drvName) == 0) {
621
                getDriverComboBox().setSelectedIndex(i);
622
                if (curSel != i) {
623
                                        this.params = null;
624
                                }
625

    
626
                return;
627
            }
628
        }
629
    }
630

    
631
        public Object getWindowProfile() {
632
                return WindowInfo.DIALOG_PROFILE;
633
        }
634

    
635
        public DBServerExplorerParameters getParameters(){
636
                if (params == null) {
637
                        DriverComboBoxItem item = (DriverComboBoxItem) getDriverComboBox()
638
                                        .getItemAt(getDriverComboBox().getSelectedIndex());
639
                        params = (DBServerExplorerParameters) item.params.getCopy();
640

    
641

    
642
                }
643
                String _host = getConnectionServerUrl();
644
                String _port = getConnectionPort();
645
                String _dbname = getConnectionDBName();
646
                String _user = getConnectionUser();
647
                String _pw = getConnectionPassword();
648

    
649
                params.setHost(_host);
650
                if (_port.trim().length() != 0) {
651
                        try {
652
                                params.setPort(Integer.parseInt(_port));
653
                        } catch (NumberFormatException e) {
654
                                NotificationManager.addError("Invalid port", e);
655
                        }
656
                }
657
                params.setDBName(_dbname);
658
                params.setUser(_user);
659
                params.setPassword(_pw);
660

    
661
                return params;
662
        }
663

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