Statistics
| Revision:

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

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
import org.gvsig.andami.PluginServices;
64
import org.gvsig.andami.messages.NotificationManager;
65
import org.gvsig.andami.ui.mdiManager.IWindow;
66
import org.gvsig.andami.ui.mdiManager.WindowInfo;
67
import org.gvsig.fmap.dal.DALLocator;
68
import org.gvsig.fmap.dal.DataManager;
69
import org.gvsig.fmap.dal.DataServerExplorerParameters;
70
import org.gvsig.fmap.dal.exception.DataException;
71
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
72
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters;
73
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.DynObjectEditor;
74

    
75

    
76

    
77
/**
78
 * Lets the user input the connection parameters.
79
 *
80
 * @author jldominguez
81
 *
82
 */
83
public class VectorialDBConnectionParamsDialog extends JPanel implements IWindow,
84
    ActionListener, KeyListener {
85
    private static Logger logger = Logger.getLogger(VectorialDBConnectionParamsDialog.class.getName());
86
    private WindowInfo winfo = new WindowInfo(8); // MODAL only
87
    private JButton cancelButton = null;
88
    private JButton okButton = null;
89
    private JButton advancedButton = null;
90
    private JPanel paramsPanel = null;
91
    private JComboBox driverComboBox = null;
92
    private JTextField portTextField = null;
93
    private JTextField dbTextField = null;
94
    private JTextField userTextField = null;
95
    private JPasswordField passwordField = null;
96
    private JLabel driverLabel = null;
97
    private JLabel portLabel = null;
98
    private JLabel dbLabel = null;
99
    private JLabel dbLabelWarning = null;
100
    private JLabel userLabel = null;
101
    private JLabel pwLabel = null;
102
    private boolean okPressed = false;
103
    private JTextField urlTextField = null;
104
    private JLabel urlLabel = null;
105
    private JCheckBox connectedCheckBox = null;
106
    private JLabel connectedLabel = null;
107
    private JLabel connNameLabel = null;
108
    private JTextField connNameTextField = null;
109

    
110
    private DBServerExplorerParameters params = null;
111

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

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

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

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

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

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

    
159
        return cancelButton;
160
    }
161

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

    
175
        return okButton;
176
    }
177

    
178
        /**
179
         * This method initializes okButton
180
         *
181
         * @return javax.swing.JButton
182
         */
183
        private JButton getAdvancedButton() {
184
                if (advancedButton == null) {
185
                        advancedButton = new JButton();
186
                        advancedButton.setText(PluginServices.getText(this, "advanced"));
187
                        advancedButton.addActionListener(this);
188
                        advancedButton.setBounds(new java.awt.Rectangle(218, 292, 90, 26));
189
                }
190

    
191
                return advancedButton;
192
        }
193

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

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

    
259
        return paramsPanel;
260
    }
261

    
262
    /**
263
     * This method initializes driverComboBox
264
     *
265
     * @return javax.swing.JComboBox
266
     */
267
    private JComboBox getDriverComboBox() {
268
        if (driverComboBox == null) {
269
            driverComboBox = new JComboBox();
270
            driverComboBox.addActionListener(this);
271
            DataManager dm = DALLocator.getDataManager();
272
            List<String> explorers = dm.getExplorerProviders();
273
            Iterator<String> iter = explorers.iterator();
274
            String expName;
275

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

    
293
            }
294

    
295
            driverComboBox.setBounds(new java.awt.Rectangle(155, 55, 166, 21));
296

    
297
        }
298

    
299
        return driverComboBox;
300
    }
301

    
302
    private class DriverComboBoxItem {
303
            private DBServerExplorerParameters params;
304

    
305
            public DriverComboBoxItem(DBServerExplorerParameters dbParams) {
306
                        this.params = dbParams;
307
                }
308

    
309
                public String toString() {
310
                        // FIXME
311
                    return params.getExplorerName();
312
            }
313
    }
314

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

    
327
        return portTextField;
328
    }
329

    
330
    /**
331
     * This method initializes dbTextField
332
     *
333
     * @return javax.swing.JTextField
334
     */
335
    private JTextField getDbTextField() {
336
        if (dbTextField == null) {
337
            dbTextField = new JTextField();
338
            dbTextField.addKeyListener(this);
339
            dbTextField.setBounds(new java.awt.Rectangle(155, 130, 166, 21));
340
        }
341

    
342
        return dbTextField;
343
    }
344

    
345
    /**
346
     * This method initializes userTextField
347
     *
348
     * @return javax.swing.JTextField
349
     */
350
    private JTextField getUserTextField() {
351
        if (userTextField == null) {
352
            userTextField = new JTextField();
353
            userTextField.addKeyListener(this);
354
            userTextField.setBounds(new java.awt.Rectangle(155, 197, 166, 21));
355
        }
356

    
357
        return userTextField;
358
    }
359

    
360
    /**
361
     * This method initializes passwordField
362
     *
363
     * @return javax.swing.JPasswordField
364
     */
365
    private JPasswordField getPasswordField() {
366
        if (passwordField == null) {
367
            passwordField = new JPasswordField();
368
            passwordField.addKeyListener(this);
369
            passwordField.setBounds(new java.awt.Rectangle(155, 222, 166, 21));
370
        }
371

    
372
        return passwordField;
373
    }
374

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

    
392
    public void actionPerformed(ActionEvent arg0) {
393
        Object src = arg0.getSource();
394

    
395
        if (src == connectedCheckBox) {
396
            if (connectedCheckBox.isSelected()) {
397
                passwordField.setEnabled(true);
398
                passwordField.setBackground(Color.WHITE);
399
            }
400
            else {
401
                passwordField.setText("");
402
                passwordField.setEnabled(false);
403
                passwordField.setBackground(Color.LIGHT_GRAY);
404
            }
405
        }
406

    
407
        if (src == okButton) {
408
            okPressed = true;
409
            PluginServices.getMDIManager().closeWindow(this);
410

    
411
            return;
412
        }
413

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

    
418
            return;
419
        }
420

    
421
        if (src == advancedButton) {
422
                List<String> toHide = new ArrayList<String>();
423
                toHide.add(DBServerExplorerParameters.PASSWORD_PARAMTER_NAME);
424
                toHide.add(DBServerExplorerParameters.HOST_PARAMTER_NAME);
425
                        toHide.add(DBServerExplorerParameters.PORT_PARAMTER_NAME);
426
                        toHide.add(DBServerExplorerParameters.USER_PARAMTER_NAME);
427
                        toHide.add(DBServerExplorerParameters.DBNAME_PARAMTER_NAME);
428
                        DBServerExplorerParameters myParams = getParameters();
429
                        try {
430
                                myParams.validate();
431
                        } catch (Exception e) {
432
                                // ignore... only for fill default values
433
                        }
434
                        DynObjectEditor editor = new DynObjectEditor(myParams,
435
                                        DynObjectEditor.HIDDE_THIS_PARAMS, toHide);
436
                        editor.editObject(true);
437

    
438
                        return;
439
                }
440

    
441
        if (src == driverComboBox) {
442
                DBServerExplorerParameters params = ((DriverComboBoxItem) driverComboBox
443
                                        .getSelectedItem()).params;
444
                        try {
445
                                params.validate();
446
                        } catch (ValidateDataParametersException e) {
447
                        }
448

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

    
463
            return;
464
        }
465
    }
466

    
467
    public boolean isOkPressed() {
468
        return okPressed;
469
    }
470

    
471
    public boolean hasToBeConnected() {
472
        return connectedCheckBox.isSelected();
473
    }
474

    
475
    public String getConnectionDriverName() {
476
        return ((DriverComboBoxItem) driverComboBox.getSelectedItem()).params
477
                                .getExplorerName();
478
    }
479

    
480
    public String getConnectionServerUrl() {
481
        return urlTextField.getText();
482
    }
483

    
484
    public String getConnectionPort() {
485
        return portTextField.getText();
486
    }
487

    
488
    public String getConnectionDBName() {
489
        return dbTextField.getText();
490
    }
491

    
492
    public String getConnectionUser() {
493
        return userTextField.getText();
494
    }
495

    
496
    public String getConnectionPassword() {
497
        String resp = new String(passwordField.getPassword());
498

    
499
        return resp;
500
    }
501

    
502
    private JTextField getUrlTextArea() {
503
        if (urlTextField == null) {
504
            urlTextField = new JTextField();
505
            urlTextField.addKeyListener(this);
506
            urlTextField.setBounds(new java.awt.Rectangle(155, 80, 166, 21));
507
        }
508

    
509
        return urlTextField;
510
    }
511

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

    
525
        return connectedCheckBox;
526
    }
527

    
528
    public String getConnectionName() {
529
        return getConnNameTextField().getText();
530
    }
531

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

    
544
        return connNameTextField;
545
    }
546

    
547
    public void keyPressed(KeyEvent e) {
548
    }
549

    
550
    public void keyReleased(KeyEvent e) {
551
        if (e.getKeyChar() != '\n') {
552
            return;
553
        }
554

    
555
        Object src = e.getSource();
556

    
557
        if (src == passwordField) {
558
            ActionEvent aevt = new ActionEvent(okButton,
559
                    ActionEvent.ACTION_PERFORMED, "");
560
            actionPerformed(aevt);
561
        }
562
        else {
563
            if (src instanceof JTextField) {
564
                ((JTextField) src).transferFocus();
565
            }
566
        }
567
    }
568

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

    
572
    public void loadValues(DBServerExplorerParameters cwp) {
573
            if (cwp.getPort() != null){
574
                    getPortTextField().setText(cwp.getPort().toString());
575
            } else{
576
                    getPortTextField().setText("");
577
            }
578
        selectThisInDriverCombo(cwp.getExplorerName());
579
        getDbTextField().setText(cwp.getDBName());
580
        getUserTextField().setText(cwp.getUser());
581

    
582
        if (cwp.getPassword() == null) {
583
            getPasswordField().setText("");
584
        }
585
        else {
586
            getPasswordField().setText(cwp.getPassword());
587
        }
588

    
589
        getUrlTextArea().setText(cwp.getHost());
590

    
591
        boolean connected = false;
592

    
593
//        try {
594
//            connected = (cwp.getConnection() != null) &&
595
//                (!cwp.getConnection().isClosed());
596
//        }
597
//        catch (DBException e) {
598
//            logger.error("While checking connection: " + e.getMessage());
599
//            connected = false;
600
//        }
601

    
602
//        getConnectedCheckBox().setSelected(connected);
603
                getConnNameTextField().setText(cwp.getExplorerName());
604
    }
605

    
606
    private void selectThisInDriverCombo(String drvName) {
607
        int size = getDriverComboBox().getItemCount();
608
        int curSel = getDriverComboBox().getSelectedIndex();
609

    
610
        for (int i = 0; i < size; i++) {
611
            DriverComboBoxItem item = (DriverComboBoxItem) getDriverComboBox()
612
                                        .getItemAt(i);
613

    
614
            if (item.params.getExplorerName().compareToIgnoreCase(drvName) == 0) {
615
                getDriverComboBox().setSelectedIndex(i);
616
                if (curSel != i) {
617
                                        this.params = null;
618
                                }
619

    
620
                return;
621
            }
622
        }
623
    }
624

    
625
        public Object getWindowProfile() {
626
                return WindowInfo.DIALOG_PROFILE;
627
        }
628

    
629
        public DBServerExplorerParameters getParameters(){
630
                if (params == null) {
631
                        DriverComboBoxItem item = (DriverComboBoxItem) getDriverComboBox()
632
                                        .getItemAt(getDriverComboBox().getSelectedIndex());
633
                        params = (DBServerExplorerParameters) item.params.getCopy();
634

    
635

    
636
                }
637
                String _host = getConnectionServerUrl();
638
                String _port = getConnectionPort();
639
                String _dbname = getConnectionDBName();
640
                String _user = getConnectionUser();
641
                String _pw = getConnectionPassword();
642

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

    
655
                return params;
656
        }
657

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