Statistics
| Revision:

root / branches / v10 / extensions / extSDE / src / com / iver / cit / gvsig / sde / gui / sdewizard2 / SDEConnectionParamsDialog.java @ 11012

History | View | Annotate | Download (16 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.iver.cit.gvsig.sde.gui.sdewizard2;
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

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

    
60
import org.apache.log4j.Logger;
61

    
62
import com.hardcode.driverManager.DriverLoadException;
63
import com.iver.andami.PluginServices;
64
import com.iver.andami.ui.mdiManager.IWindow;
65
import com.iver.andami.ui.mdiManager.WindowInfo;
66
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
67
import com.iver.cit.gvsig.fmap.drivers.sde.ArcSdeDriver;
68
import com.iver.cit.gvsig.fmap.drivers.sde.VectorialSDEDriver;
69
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
70

    
71

    
72
/**
73
 * Lets the user input the connection parameters.
74
 *
75
 * @author jldominguez
76
 *
77
 */
78
public class SDEConnectionParamsDialog extends JPanel implements IWindow,
79
    ActionListener, KeyListener {
80
    private static Logger logger = Logger.getLogger(SDEConnectionParamsDialog.class.getName());
81
    private WindowInfo winfo = new WindowInfo(8); // MODAL only
82
    private JButton cancelButton = null;
83
    private JButton okButton = null;
84
    private JPanel paramsPanel = null;
85
    private JComboBox driverComboBox = null;
86
    private JTextField portTextField = null;
87
    private JTextField dbTextField = null;
88
    private JTextField userTextField = null;
89
    private JPasswordField passwordField = null;
90
    private JLabel driverLabel = null;
91
    private JLabel portLabel = null;
92
    private JLabel dbLabel = 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 SDEConnectionParamsDialog() {
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(275 - 25);
123
        winfo.setTitle(PluginServices.getText(this, "connection_parameters"));
124

    
125
        this.setSize(new java.awt.Dimension(360, 287));
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, 250, 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, 250, 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, 205, 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, 180, 141, 21));
188
            pwLabel.setText(PluginServices.getText(this, "password") + ":");
189
            userLabel = new JLabel();
190
            userLabel.setBounds(new java.awt.Rectangle(10, 155, 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
            portLabel = new JLabel();
197
            portLabel.setBounds(new java.awt.Rectangle(10, 105, 141, 21));
198
            portLabel.setText(PluginServices.getText(this, "port") + ":");
199
            driverLabel = new JLabel();
200
            driverLabel.setBounds(new java.awt.Rectangle(10, 55, 141, 21));
201
            driverLabel.setText(PluginServices.getText(this, "driver") + ":");
202
            paramsPanel = new JPanel();
203
            paramsPanel.setBounds(new java.awt.Rectangle(10, 10, 336, 231));
204
            paramsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
205
                    null, PluginServices.getText(this, "connection_params"),
206
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
207
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
208
            paramsPanel.setLayout(null);
209
            paramsPanel.add(getPortTextField(), null);
210
            paramsPanel.add(getDriverComboBox(), null);
211
            paramsPanel.add(getDbTextField(), null);
212
            paramsPanel.add(getUserTextField(), null);
213
            paramsPanel.add(getPasswordField(), null);
214
            paramsPanel.add(driverLabel, null);
215
            paramsPanel.add(portLabel, null);
216
            paramsPanel.add(dbLabel, null);
217
            paramsPanel.add(userLabel, null);
218
            paramsPanel.add(pwLabel, null);
219
            paramsPanel.add(getUrlTextArea(), null);
220
            paramsPanel.add(urlLabel, null);
221
            paramsPanel.add(getConnectedCheckBox(), null);
222
            paramsPanel.add(connectedLabel, null);
223
            paramsPanel.add(connNameLabel, null);
224
            paramsPanel.add(getConnNameTextField(), null);
225
        }
226

    
227
        return paramsPanel;
228
    }
229

    
230
    /**
231
     * This method initializes driverComboBox
232
     *
233
     * @return javax.swing.JComboBox
234
     */
235
    private JComboBox getDriverComboBox() {
236
        if (driverComboBox == null) {
237
            driverComboBox = new JComboBox();
238
            driverComboBox.addActionListener(this);
239

    
240
            String[] drvName = getDriverNames();
241

    
242
            for (int i = 0; i < drvName.length; i++)
243
                driverComboBox.addItem(drvName[i]);
244

    
245
            driverComboBox.setBounds(new java.awt.Rectangle(155, 55, 166, 21));
246
        }
247

    
248
        return driverComboBox;
249
    }
250

    
251
    /**
252
     * This method initializes portTextField
253
     *
254
     * @return javax.swing.JTextField
255
     */
256
    private JTextField getPortTextField() {
257
        if (portTextField == null) {
258
            portTextField = new JTextField();
259
            portTextField.addKeyListener(this);
260
            portTextField.setBounds(new java.awt.Rectangle(155, 105, 166, 21));
261
        }
262

    
263
        return portTextField;
264
    }
265

    
266
    /**
267
     * This method initializes dbTextField
268
     *
269
     * @return javax.swing.JTextField
270
     */
271
    private JTextField getDbTextField() {
272
        if (dbTextField == null) {
273
            dbTextField = new JTextField();
274
            dbTextField.addKeyListener(this);
275
            dbTextField.setBounds(new java.awt.Rectangle(155, 130, 166, 21));
276
        }
277

    
278
        return dbTextField;
279
    }
280

    
281
    /**
282
     * This method initializes userTextField
283
     *
284
     * @return javax.swing.JTextField
285
     */
286
    private JTextField getUserTextField() {
287
        if (userTextField == null) {
288
            userTextField = new JTextField();
289
            userTextField.addKeyListener(this);
290
            userTextField.setBounds(new java.awt.Rectangle(155, 155, 166, 21));
291
        }
292

    
293
        return userTextField;
294
    }
295

    
296
    /**
297
     * This method initializes passwordField
298
     *
299
     * @return javax.swing.JPasswordField
300
     */
301
    private JPasswordField getPasswordField() {
302
        if (passwordField == null) {
303
            passwordField = new JPasswordField();
304
            passwordField.addKeyListener(this);
305
            passwordField.setBounds(new java.awt.Rectangle(155, 180, 166, 21));
306
        }
307

    
308
        return passwordField;
309
    }
310

    
311
    private String[] getDriverNames() {
312
        Class[] classes = new Class[] { VectorialSDEDriver.class };
313

    
314
        ArrayList ret = new ArrayList();
315
        String[] driverNames = LayerFactory.getDM().getDriverNames();
316

    
317
        for (int i = 0; i < driverNames.length; i++) {
318
            for (int j = 0; j < classes.length; j++) {
319
                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
320
                    ret.add(driverNames[i]);
321
                }
322
            }
323
        }
324

    
325
        return (String[]) ret.toArray(new String[0]);
326
    }
327

    
328
    public void actionPerformed(ActionEvent arg0) {
329
        Object src = arg0.getSource();
330

    
331
        if (src == connectedCheckBox) {
332
            if (connectedCheckBox.isSelected()) {
333
                passwordField.setEnabled(true);
334
                passwordField.setBackground(Color.WHITE);
335
            }
336
            else {
337
                passwordField.setText("");
338
                passwordField.setEnabled(false);
339
                passwordField.setBackground(Color.LIGHT_GRAY);
340
            }
341
        }
342

    
343
        if (src == okButton) {
344
            okPressed = true;
345
            PluginServices.getMDIManager().closeWindow(this);
346

    
347
            return;
348
        }
349

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

    
354
            return;
355
        }
356

    
357
        if (src == driverComboBox) {
358
            String driverName = driverComboBox.getSelectedItem().toString();
359
            VectorialSDEDriver driver;
360

    
361
            try {
362
                driver = (VectorialSDEDriver) LayerFactory.getDM()
363
                                                           .getDriver(driverName);
364
                portTextField.setText("" + driver.getDefaultPort());
365
            }
366
            catch (DriverLoadException e1) {
367
                portTextField.setText("");
368
            }
369

    
370
            return;
371
        }
372
    }
373

    
374
    public boolean isOkPressed() {
375
        return okPressed;
376
    }
377

    
378
    public boolean hasToBeConnected() {
379
        return connectedCheckBox.isSelected();
380
    }
381

    
382
    public String getConnectionDriverName() {
383
        return driverComboBox.getSelectedItem().toString();
384
    }
385

    
386
    public String getConnectionServerUrl() {
387
        return urlTextField.getText();
388
    }
389

    
390
    public String getConnectionPort() {
391
        return portTextField.getText();
392
    }
393

    
394
    public String getConnectionDBName() {
395
        return dbTextField.getText();
396
    }
397

    
398
    public String getConnectionUser() {
399
        return userTextField.getText();
400
    }
401

    
402
    public String getConnectionPassword() {
403
        String resp = new String(passwordField.getPassword());
404

    
405
        return resp;
406
    }
407

    
408
    private JTextField getUrlTextArea() {
409
        if (urlTextField == null) {
410
            urlTextField = new JTextField();
411
            urlTextField.addKeyListener(this);
412
            urlTextField.setBounds(new java.awt.Rectangle(155, 80, 166, 21));
413
        }
414

    
415
        return urlTextField;
416
    }
417

    
418
    /**
419
     * This method initializes connectedCheckBox
420
     *
421
     * @return javax.swing.JCheckBox
422
     */
423
    private JCheckBox getConnectedCheckBox() {
424
        if (connectedCheckBox == null) {
425
            connectedCheckBox = new JCheckBox();
426
            connectedCheckBox.setSelected(true);
427
            connectedCheckBox.addActionListener(this);
428
            connectedCheckBox.setBounds(new java.awt.Rectangle(155, 205, 26, 21));
429
        }
430

    
431
        return connectedCheckBox;
432
    }
433

    
434
    public String getConnectionName() {
435
        return getConnNameTextField().getText();
436
    }
437

    
438
    /**
439
     * This method initializes connNameTextField
440
     *
441
     * @return javax.swing.JTextField
442
     */
443
    private JTextField getConnNameTextField() {
444
        if (connNameTextField == null) {
445
            connNameTextField = new JTextField();
446
            connNameTextField.addKeyListener(this);
447
            connNameTextField.setBounds(new java.awt.Rectangle(155, 30, 166, 21));
448
        }
449

    
450
        return connNameTextField;
451
    }
452

    
453
    public void keyPressed(KeyEvent e) {
454
    }
455

    
456
    public void keyReleased(KeyEvent e) {
457
        if (e.getKeyChar() != '\n') {
458
            return;
459
        }
460

    
461
        Object src = e.getSource();
462

    
463
        if (src == passwordField) {
464
            ActionEvent aevt = new ActionEvent(okButton,
465
                    ActionEvent.ACTION_PERFORMED, "");
466
            actionPerformed(aevt);
467
        }
468
        else {
469
            if (src instanceof JTextField) {
470
                ((JTextField) src).transferFocus();
471
            }
472
        }
473
    }
474

    
475
    public void keyTyped(KeyEvent e) {
476
    }
477

    
478
    public void loadValues(ConnectionWithParamsSDE cwp) {
479
        getPortTextField().setText(cwp.getPort());
480
        selectThisInDriverCombo(cwp.getDrvName());
481
        getDbTextField().setText(cwp.getDb());
482
        getUserTextField().setText(cwp.getUser());
483

    
484
        if (cwp.getPw() == null) {
485
            getPasswordField().setText("");
486
        }
487
        else {
488
            getPasswordField().setText(cwp.getPw());
489
        }
490

    
491
        getUrlTextArea().setText(cwp.getHost());
492

    
493
        boolean connected = false;
494

    
495
//        try {
496
            connected = (cwp.getConnection() != null) &&
497
                (!cwp.getConnection().isClosed());
498
//        }
499
//        catch (SQLException e) {
500
//            logger.error("While checking connection: " + e.getMessage());
501
//            connected = false;
502
//        }
503

    
504
        getConnectedCheckBox().setSelected(connected);
505
        getConnNameTextField().setText(cwp.getName());
506
    }
507

    
508
    private void selectThisInDriverCombo(String drvName) {
509
        int size = getDriverComboBox().getItemCount();
510

    
511
        for (int i = 0; i < size; i++) {
512
            Object item = getDriverComboBox().getItemAt(i);
513

    
514
            if (item.toString().compareToIgnoreCase(drvName) == 0) {
515
                getDriverComboBox().setSelectedIndex(i);
516

    
517
                return;
518
            }
519
        }
520
    }
521
} //  @jve:decl-index=0:visual-constraint="10,10"