Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / VectorialDBConnectionParamsDialog.java @ 22149

History | View | Annotate | Download (17.3 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 com.prodevelop.cit.gvsig.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

    
51
import javax.swing.JButton;
52
import javax.swing.JCheckBox;
53
import javax.swing.JComboBox;
54
import javax.swing.JLabel;
55
import javax.swing.JPanel;
56
import javax.swing.JPasswordField;
57
import javax.swing.JTextField;
58

    
59
import org.apache.log4j.Logger;
60
import org.gvsig.fmap.data.DataManager;
61
import org.gvsig.fmap.data.InitializeException;
62
import org.gvsig.fmap.data.datastores.vectorial.db.DBExplorerParameters;
63
import org.gvsig.fmap.data.datastores.vectorial.db.DBParameters;
64
import org.gvsig.fmap.data.vectorial.FeatureStore;
65

    
66
import com.iver.andami.PluginServices;
67
import com.iver.andami.ui.mdiManager.IWindow;
68
import com.iver.andami.ui.mdiManager.WindowInfo;
69

    
70

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

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

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

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

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

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

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

    
149
        return cancelButton;
150
    }
151

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

    
165
        return okButton;
166
    }
167

    
168
    /**
169
     * This method initializes paramsPanel
170
     *
171
     * @return javax.swing.JPanel
172
     */
173
    private JPanel getParamsPanel() {
174
        if (paramsPanel == null) {
175
            connNameLabel = new JLabel();
176
            connNameLabel.setBounds(new java.awt.Rectangle(10, 30, 141, 21));
177
            connNameLabel.setText(PluginServices.getText(this, "connection_name") +
178
                ":");
179
            connectedLabel = new JLabel();
180
            connectedLabel.setBounds(new java.awt.Rectangle(10, 247, 141, 21));
181
            connectedLabel.setText(PluginServices.getText(this, "connected") +
182
                ":");
183
            urlLabel = new JLabel();
184
            urlLabel.setBounds(new java.awt.Rectangle(10, 80, 141, 21));
185
            urlLabel.setText(PluginServices.getText(this, "server_url") + ":");
186
            pwLabel = new JLabel();
187
            pwLabel.setBounds(new java.awt.Rectangle(10, 222, 141, 21));
188
            pwLabel.setText(PluginServices.getText(this, "password") + ":");
189
            userLabel = new JLabel();
190
            userLabel.setBounds(new java.awt.Rectangle(10, 197, 141, 21));
191
            userLabel.setText(PluginServices.getText(this, "user") + ":");
192
            dbLabel = new JLabel();
193
            dbLabel.setBounds(new java.awt.Rectangle(10, 130, 141, 21));
194
            dbLabel.setText(PluginServices.getText(this, "database_name") +
195
                ":");
196
            dbLabelWarning = new JLabel();
197
            dbLabelWarning.setBounds(new java.awt.Rectangle(10, 155, 310, 41));
198
            dbLabelWarning.setText(PluginServices.getText(this, "warning_you_must_input_the_exact_name_this_difference_between_capital_letters_and_small_letters")
199
                );
200

    
201
            portLabel = new JLabel();
202
            portLabel.setBounds(new java.awt.Rectangle(10, 105, 141, 21));
203
            portLabel.setText(PluginServices.getText(this, "port") + ":");
204
            driverLabel = new JLabel();
205
            driverLabel.setBounds(new java.awt.Rectangle(10, 55, 141, 21));
206
            driverLabel.setText(PluginServices.getText(this, "driver") + ":");
207
            paramsPanel = new JPanel();
208
            paramsPanel.setBounds(new java.awt.Rectangle(10, 10, 336, 273));
209
            paramsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
210
                    null, PluginServices.getText(this, "connection_params"),
211
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
212
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
213
            paramsPanel.setLayout(null);
214
            paramsPanel.add(getPortTextField(), null);
215
            paramsPanel.add(getDriverComboBox(), null);
216
            paramsPanel.add(getDbTextField(), null);
217
            paramsPanel.add(getUserTextField(), null);
218
            paramsPanel.add(getPasswordField(), null);
219
            paramsPanel.add(driverLabel, null);
220
            paramsPanel.add(portLabel, null);
221
            paramsPanel.add(dbLabel, null);
222
            paramsPanel.add(dbLabelWarning, null);
223
            paramsPanel.add(userLabel, null);
224
            paramsPanel.add(pwLabel, null);
225
            paramsPanel.add(getUrlTextArea(), null);
226
            paramsPanel.add(urlLabel, null);
227
            paramsPanel.add(getConnectedCheckBox(), null);
228
            paramsPanel.add(connectedLabel, null);
229
            paramsPanel.add(connNameLabel, null);
230
            paramsPanel.add(getConnNameTextField(), null);
231
        }
232

    
233
        return paramsPanel;
234
    }
235

    
236
    /**
237
     * This method initializes driverComboBox
238
     *
239
     * @return javax.swing.JComboBox
240
     */
241
    private JComboBox getDriverComboBox() {
242
        if (driverComboBox == null) {
243
            driverComboBox = new JComboBox();
244
            driverComboBox.addActionListener(this);
245
            DataManager dm=DataManager.getManager();
246
            String[] drvName = dm.getRegistersExplorers();
247

    
248
            for (int i = 0; i < drvName.length; i++){
249
                    driverComboBox.addItem(drvName[i]);
250
            }
251

    
252
            driverComboBox.setBounds(new java.awt.Rectangle(155, 55, 166, 21));
253
        }
254

    
255
        return driverComboBox;
256
    }
257

    
258
    /**
259
     * This method initializes portTextField
260
     *
261
     * @return javax.swing.JTextField
262
     */
263
    private JTextField getPortTextField() {
264
        if (portTextField == null) {
265
            portTextField = new JTextField();
266
            portTextField.addKeyListener(this);
267
            portTextField.setBounds(new java.awt.Rectangle(155, 105, 166, 21));
268
        }
269

    
270
        return portTextField;
271
    }
272

    
273
    /**
274
     * This method initializes dbTextField
275
     *
276
     * @return javax.swing.JTextField
277
     */
278
    private JTextField getDbTextField() {
279
        if (dbTextField == null) {
280
            dbTextField = new JTextField();
281
            dbTextField.addKeyListener(this);
282
            dbTextField.setBounds(new java.awt.Rectangle(155, 130, 166, 21));
283
        }
284

    
285
        return dbTextField;
286
    }
287

    
288
    /**
289
     * This method initializes userTextField
290
     *
291
     * @return javax.swing.JTextField
292
     */
293
    private JTextField getUserTextField() {
294
        if (userTextField == null) {
295
            userTextField = new JTextField();
296
            userTextField.addKeyListener(this);
297
            userTextField.setBounds(new java.awt.Rectangle(155, 197, 166, 21));
298
        }
299

    
300
        return userTextField;
301
    }
302

    
303
    /**
304
     * This method initializes passwordField
305
     *
306
     * @return javax.swing.JPasswordField
307
     */
308
    private JPasswordField getPasswordField() {
309
        if (passwordField == null) {
310
            passwordField = new JPasswordField();
311
            passwordField.addKeyListener(this);
312
            passwordField.setBounds(new java.awt.Rectangle(155, 222, 166, 21));
313
        }
314

    
315
        return passwordField;
316
    }
317

    
318
//    private String[] getDriverNames() {
319
//        Class[] classes = new Class[] { IVectorialDatabaseDriver.class };
320
//
321
//        ArrayList ret = new ArrayList();
322
//        String[] driverNames = LayerFactory.getDM().getDriverNames();
323
//
324
//        for (int i = 0; i < driverNames.length; i++) {
325
//            for (int j = 0; j < classes.length; j++) {
326
//                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
327
//                    ret.add(driverNames[i]);
328
//                }
329
//            }
330
//        }
331
//
332
//        return (String[]) ret.toArray(new String[0]);
333
//    }
334

    
335
    public void actionPerformed(ActionEvent arg0) {
336
        Object src = arg0.getSource();
337

    
338
        if (src == connectedCheckBox) {
339
            if (connectedCheckBox.isSelected()) {
340
                passwordField.setEnabled(true);
341
                passwordField.setBackground(Color.WHITE);
342
            }
343
            else {
344
                passwordField.setText("");
345
                passwordField.setEnabled(false);
346
                passwordField.setBackground(Color.LIGHT_GRAY);
347
            }
348
        }
349

    
350
        if (src == okButton) {
351
            okPressed = true;
352
            PluginServices.getMDIManager().closeWindow(this);
353

    
354
            return;
355
        }
356

    
357
        if (src == cancelButton) {
358
            okPressed = false;
359
            PluginServices.getMDIManager().closeWindow(this);
360

    
361
            return;
362
        }
363

    
364
        if (src == driverComboBox) {
365
            String driverName = driverComboBox.getSelectedItem().toString();
366
            FeatureStore featureStore;
367

    
368
//            try {
369
//                    DataManager dm=DataManager.getManager();
370
//                featureStore = (FeatureStore) dm.createDataStore()LayerFactory.getDM()
371
//                                                           .getDriver(driverName);
372
                portTextField.setText("" + "Puerto hay que sacarlo de alg?n sitio");
373
//            }
374
//            catch (DriverLoadException e1) {
375
//                portTextField.setText("");
376
//            }
377

    
378
            return;
379
        }
380
    }
381

    
382
    public boolean isOkPressed() {
383
        return okPressed;
384
    }
385

    
386
    public boolean hasToBeConnected() {
387
        return connectedCheckBox.isSelected();
388
    }
389

    
390
    public String getConnectionDriverName() {
391
        return driverComboBox.getSelectedItem().toString();
392
    }
393

    
394
    public String getConnectionServerUrl() {
395
        return urlTextField.getText();
396
    }
397

    
398
    public String getConnectionPort() {
399
        return portTextField.getText();
400
    }
401

    
402
    public String getConnectionDBName() {
403
        return dbTextField.getText();
404
    }
405

    
406
    public String getConnectionUser() {
407
        return userTextField.getText();
408
    }
409

    
410
    public String getConnectionPassword() {
411
        String resp = new String(passwordField.getPassword());
412

    
413
        return resp;
414
    }
415

    
416
    public DBExplorerParameters getParameters() throws InitializeException{
417
            String _drvname = getConnectionDriverName();
418
        String _host = getConnectionServerUrl();
419
        String _port = getConnectionPort();
420
        String _dbname = getConnectionDBName();
421
        String _user = getConnectionUser();
422
        String _pw = getConnectionPassword();
423
//        String _conn_usr_name = getConnectionName();
424
        DataManager dm=DataManager.getManager();
425
            DBExplorerParameters params=(DBExplorerParameters)dm.createDataExplorerParameters(_drvname);
426
            params.setHost(_host);
427
            params.setPort(_port);
428
            params.setDb(_dbname);
429
            params.setUser(_user);
430
            params.setPassw(_pw);
431
//            params.setCatalog("public");
432
            return params;
433
    }
434

    
435
    private JTextField getUrlTextArea() {
436
        if (urlTextField == null) {
437
            urlTextField = new JTextField();
438
            urlTextField.addKeyListener(this);
439
            urlTextField.setBounds(new java.awt.Rectangle(155, 80, 166, 21));
440
        }
441

    
442
        return urlTextField;
443
    }
444

    
445
    /**
446
     * This method initializes connectedCheckBox
447
     *
448
     * @return javax.swing.JCheckBox
449
     */
450
    private JCheckBox getConnectedCheckBox() {
451
        if (connectedCheckBox == null) {
452
            connectedCheckBox = new JCheckBox();
453
            connectedCheckBox.setSelected(true);
454
            connectedCheckBox.addActionListener(this);
455
            connectedCheckBox.setBounds(new java.awt.Rectangle(155, 247, 26, 21));
456
        }
457

    
458
        return connectedCheckBox;
459
    }
460

    
461
    public String getConnectionName() {
462
        return getConnNameTextField().getText();
463
    }
464

    
465
    /**
466
     * This method initializes connNameTextField
467
     *
468
     * @return javax.swing.JTextField
469
     */
470
    private JTextField getConnNameTextField() {
471
        if (connNameTextField == null) {
472
            connNameTextField = new JTextField();
473
            connNameTextField.addKeyListener(this);
474
            connNameTextField.setBounds(new java.awt.Rectangle(155, 30, 166, 21));
475
        }
476

    
477
        return connNameTextField;
478
    }
479

    
480
    public void keyPressed(KeyEvent e) {
481
    }
482

    
483
    public void keyReleased(KeyEvent e) {
484
        if (e.getKeyChar() != '\n') {
485
            return;
486
        }
487

    
488
        Object src = e.getSource();
489

    
490
        if (src == passwordField) {
491
            ActionEvent aevt = new ActionEvent(okButton,
492
                    ActionEvent.ACTION_PERFORMED, "");
493
            actionPerformed(aevt);
494
        }
495
        else {
496
            if (src instanceof JTextField) {
497
                ((JTextField) src).transferFocus();
498
            }
499
        }
500
    }
501

    
502
    public void keyTyped(KeyEvent e) {
503
    }
504

    
505
    public void loadValues(DBParameters cwp) {
506
        getPortTextField().setText(cwp.getPort());
507
        selectThisInDriverCombo(cwp.toString());
508
        getDbTextField().setText(cwp.getDb());
509
        getUserTextField().setText(cwp.getUser());
510

    
511
        if (cwp.getPassw() == null) {
512
            getPasswordField().setText("");
513
        }
514
        else {
515
            getPasswordField().setText(cwp.getPassw());
516
        }
517

    
518
        getUrlTextArea().setText(cwp.getHost());
519

    
520
        boolean connected = false;
521

    
522
//        try {
523
//            connected = (cwp.getConnection() != null) &&
524
//                (!cwp.getConnection().isClosed());
525
//        }
526
//        catch (DBException e) {
527
//            logger.error("While checking connection: " + e.getMessage());
528
//            connected = false;
529
//        }
530

    
531
//        getConnectedCheckBox().setSelected(connected);
532
        getConnNameTextField().setText(cwp.getTableName());
533
    }
534

    
535
    private void selectThisInDriverCombo(String drvName) {
536
        int size = getDriverComboBox().getItemCount();
537

    
538
        for (int i = 0; i < size; i++) {
539
            Object item = getDriverComboBox().getItemAt(i);
540

    
541
            if (item.toString().compareToIgnoreCase(drvName) == 0) {
542
                getDriverComboBox().setSelectedIndex(i);
543

    
544
                return;
545
            }
546
        }
547
    }
548
} //  @jve:decl-index=0:visual-constraint="10,10"