Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / VectorialDBConnectionParamsDialog.java @ 33662

History | View | Annotate | Download (20.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 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.ArrayList;
51
import java.util.Iterator;
52
import java.util.List;
53

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

    
62
import org.apache.log4j.Logger;
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
    private static Logger LOG = Logger
90
        .getLogger(VectorialDBConnectionParamsDialog.class);
91

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

    
116
    private DBServerExplorerParameters params = null;
117

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

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

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

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

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

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

    
165
        return cancelButton;
166
    }
167

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

    
181
        return okButton;
182
    }
183

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

    
197
                return advancedButton;
198
        }
199

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

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

    
265
        return paramsPanel;
266
    }
267

    
268
    /**
269
     * This method initializes driverComboBox
270
     *
271
     * @return javax.swing.JComboBox
272
     */
273
    private JComboBox getDriverComboBox() {
274
        if (driverComboBox == null) {
275
            driverComboBox = new JComboBox();
276
            driverComboBox.addActionListener(this);
277
            DataManager dm = DALLocator.getDataManager();
278
            List<String> explorers = dm.getExplorerProviders();
279
            Iterator<String> iter = explorers.iterator();
280
            String expName;
281

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

    
299
            }
300

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

    
303
        }
304

    
305
        return driverComboBox;
306
    }
307

    
308
    private class DriverComboBoxItem {
309
            private DBServerExplorerParameters params;
310

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

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

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

    
333
        return portTextField;
334
    }
335

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

    
348
        return dbTextField;
349
    }
350

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

    
363
        return userTextField;
364
    }
365

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

    
378
        return passwordField;
379
    }
380

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

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

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

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

    
417
            return;
418
        }
419

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

    
424
            return;
425
        }
426

    
427
        if (src == advancedButton) {
428
                List<String> toHide = new ArrayList<String>();
429
                toHide.add(DBServerExplorerParameters.PASSWORD_PARAMTER_NAME);
430
                toHide.add(DBServerExplorerParameters.HOST_PARAMTER_NAME);
431
                        toHide.add(DBServerExplorerParameters.PORT_PARAMTER_NAME);
432
                        toHide.add(DBServerExplorerParameters.USER_PARAMTER_NAME);
433
                        toHide.add(DBServerExplorerParameters.DBNAME_PARAMTER_NAME);
434
                        DBServerExplorerParameters myParams = getParameters();
435
                        try {
436
                                myParams.validate();
437
                        } catch (Exception e) {
438
                                // ignore... only for fill default values
439
                        }
440
            try {
441
                DynObjectEditor editor = new DynObjectEditor(myParams);
442
                editor.editObject(true);
443
            } catch (ServiceException ex) {
444
                LOG.error(
445
                    "Error creating a Swing component for the DynObject: "
446
                        + myParams, ex);
447
            }
448

    
449
                        return;
450
                }
451

    
452
        if (src == driverComboBox) {
453
                DBServerExplorerParameters params = ((DriverComboBoxItem) driverComboBox
454
                                        .getSelectedItem()).params;
455
                        try {
456
                                params.validate();
457
                        } catch (ValidateDataParametersException e) {
458
                        }
459

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

    
474
            return;
475
        }
476
    }
477

    
478
    public boolean isOkPressed() {
479
        return okPressed;
480
    }
481

    
482
    public boolean hasToBeConnected() {
483
        return connectedCheckBox.isSelected();
484
    }
485

    
486
    public String getConnectionDriverName() {
487
        return ((DriverComboBoxItem) driverComboBox.getSelectedItem()).params
488
                                .getExplorerName();
489
    }
490

    
491
    public String getConnectionServerUrl() {
492
        return urlTextField.getText();
493
    }
494

    
495
    public String getConnectionPort() {
496
        return portTextField.getText();
497
    }
498

    
499
    public String getConnectionDBName() {
500
        return dbTextField.getText();
501
    }
502

    
503
    public String getConnectionUser() {
504
        return userTextField.getText();
505
    }
506

    
507
    public String getConnectionPassword() {
508
        String resp = new String(passwordField.getPassword());
509

    
510
        return resp;
511
    }
512

    
513
    private JTextField getUrlTextArea() {
514
        if (urlTextField == null) {
515
            urlTextField = new JTextField();
516
            urlTextField.addKeyListener(this);
517
            urlTextField.setBounds(new java.awt.Rectangle(155, 80, 166, 21));
518
        }
519

    
520
        return urlTextField;
521
    }
522

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

    
536
        return connectedCheckBox;
537
    }
538

    
539
    public String getConnectionName() {
540
        return getConnNameTextField().getText();
541
    }
542

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

    
555
        return connNameTextField;
556
    }
557

    
558
    public void keyPressed(KeyEvent e) {
559
    }
560

    
561
    public void keyReleased(KeyEvent e) {
562
        if (e.getKeyChar() != '\n') {
563
            return;
564
        }
565

    
566
        Object src = e.getSource();
567

    
568
        if (src == passwordField) {
569
            ActionEvent aevt = new ActionEvent(okButton,
570
                    ActionEvent.ACTION_PERFORMED, "");
571
            actionPerformed(aevt);
572
        }
573
        else {
574
            if (src instanceof JTextField) {
575
                ((JTextField) src).transferFocus();
576
            }
577
        }
578
    }
579

    
580
    public void keyTyped(KeyEvent e) {
581
        }
582

    
583
    public void loadValues(DBServerExplorerParameters cwp) {
584
            if (cwp.getPort() != null){
585
                    getPortTextField().setText(cwp.getPort().toString());
586
            } else{
587
                    getPortTextField().setText("");
588
            }
589
        selectThisInDriverCombo(cwp.getExplorerName());
590
        getDbTextField().setText(cwp.getDBName());
591
        getUserTextField().setText(cwp.getUser());
592

    
593
        if (cwp.getPassword() == null) {
594
            getPasswordField().setText("");
595
        }
596
        else {
597
            getPasswordField().setText(cwp.getPassword());
598
        }
599

    
600
        getUrlTextArea().setText(cwp.getHost());
601

    
602
        // boolean connected = false;
603
        //
604
//        try {
605
//            connected = (cwp.getConnection() != null) &&
606
//                (!cwp.getConnection().isClosed());
607
//        }
608
//        catch (DBException e) {
609
//            logger.error("While checking connection: " + e.getMessage());
610
//            connected = false;
611
//        }
612

    
613
//        getConnectedCheckBox().setSelected(connected);
614
                getConnNameTextField().setText(cwp.getExplorerName());
615
    }
616

    
617
    private void selectThisInDriverCombo(String drvName) {
618
        int size = getDriverComboBox().getItemCount();
619
        int curSel = getDriverComboBox().getSelectedIndex();
620

    
621
        for (int i = 0; i < size; i++) {
622
            DriverComboBoxItem item = (DriverComboBoxItem) getDriverComboBox()
623
                                        .getItemAt(i);
624

    
625
            if (item.params.getExplorerName().compareToIgnoreCase(drvName) == 0) {
626
                getDriverComboBox().setSelectedIndex(i);
627
                if (curSel != i) {
628
                                        this.params = null;
629
                                }
630

    
631
                return;
632
            }
633
        }
634
    }
635

    
636
        public Object getWindowProfile() {
637
                return WindowInfo.DIALOG_PROFILE;
638
        }
639

    
640
        public DBServerExplorerParameters getParameters(){
641
                if (params == null) {
642
                        DriverComboBoxItem item = (DriverComboBoxItem) getDriverComboBox()
643
                                        .getItemAt(getDriverComboBox().getSelectedIndex());
644
                        params = (DBServerExplorerParameters) item.params.getCopy();
645

    
646

    
647
                }
648
                String _host = getConnectionServerUrl();
649
                String _port = getConnectionPort();
650
                String _dbname = getConnectionDBName();
651
                String _user = getConnectionUser();
652
                String _pw = getConnectionPassword();
653

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

    
666
                return params;
667
        }
668

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