Statistics
| Revision:

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

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

    
443
                        return;
444
                }
445

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

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

    
468
            return;
469
        }
470
    }
471

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

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

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

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

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

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

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

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

    
504
        return resp;
505
    }
506

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

    
514
        return urlTextField;
515
    }
516

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

    
530
        return connectedCheckBox;
531
    }
532

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

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

    
549
        return connNameTextField;
550
    }
551

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

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

    
560
        Object src = e.getSource();
561

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

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

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

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

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

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

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

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

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

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

    
625
                return;
626
            }
627
        }
628
    }
629

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

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

    
640

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

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

    
660
                return params;
661
        }
662

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