Statistics
| Revision:

root / trunk / extensions / extSDE / src / com / iver / cit / gvsig / sde / gui / sdewizard2 / SDEConnectionManagerDialog.java @ 11193

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.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.sql.SQLException;
48
import java.util.ArrayList;
49
import java.util.HashMap;
50
import java.util.Iterator;
51

    
52
import javax.swing.JButton;
53
import javax.swing.JOptionPane;
54
import javax.swing.JPanel;
55
import javax.swing.JScrollPane;
56
import javax.swing.JTree;
57
import javax.swing.tree.DefaultMutableTreeNode;
58
import javax.swing.tree.TreeCellRenderer;
59

    
60
import org.apache.log4j.Logger;
61

    
62
import com.iver.andami.PluginServices;
63
import com.iver.andami.ui.mdiManager.IWindow;
64
import com.iver.andami.ui.mdiManager.WindowInfo;
65
import com.iver.utiles.swing.JPasswordDlg;
66

    
67

    
68
/**
69
 * Single connection manager main dialog. Lists available connections. Open connections
70
 * are marked "[C]" before their names.
71
 *
72
 * @author jldominguez
73
 *
74
 */
75
public class SDEConnectionManagerDialog extends JPanel implements IWindow,
76
    ActionListener {
77
    private static Logger logger = Logger.getLogger(SDEConnectionManagerDialog.class.getName());
78
    private JScrollPane connectionsScrollPane = null;
79
    private JButton closeButton = null;
80
    private JButton removeButton = null;
81
    private JButton newButton = null;
82
    private JTree connectionsTree = null;
83
    private WindowInfo winfo = new WindowInfo(8); // all false except MODAL
84
    private JButton connectButton = null;
85
    private JButton disconnButton = null;
86
    private JButton editButton = null;
87
        private TreeCellRenderer connTreeRenderer;
88

    
89
    /**
90
     * This method initializes
91
     *
92
     */
93
    public SDEConnectionManagerDialog() {
94
        super();
95
        initialize();
96
    }
97

    
98
    public void showDialog() {
99
        PluginServices.getMDIManager().addWindow(this);
100
    }
101

    
102
    /**
103
     * This method initializes this
104
     *
105
     */
106
    private void initialize() {
107
        winfo.setHeight(422 - 42);
108
        winfo.setWidth(347 + 6);
109
        winfo.setTitle(PluginServices.getText(this, "gestor_sde"));
110

    
111
        this.setSize(new java.awt.Dimension(347, 420));
112
        this.setLayout(null);
113
        this.add(getConnectionsScrollPane(), null);
114
        this.add(getCloseButton(), null);
115
        this.add(getRemoveButton(), null);
116
        this.add(getNewButton(), null);
117
        this.add(getConnectButton(), null);
118
        this.add(getDisconnButton(), null);
119
        this.add(getEditButton(), null);
120

    
121
        connTreeRenderer = new ConnectionTreeRenderer();
122
        refreshTree();
123
    }
124

    
125
    public WindowInfo getWindowInfo() {
126
        return winfo;
127
    }
128

    
129
    /**
130
     * This method initializes connectionsScrollPane
131
     *
132
     * @return javax.swing.JScrollPane
133
     */
134
    private JScrollPane getConnectionsScrollPane() {
135
        if (connectionsScrollPane == null) {
136
            connectionsScrollPane = new JScrollPane();
137
            connectionsScrollPane.setBounds(new java.awt.Rectangle(5, 5, 336,
138
                    336));
139
            connectionsScrollPane.setViewportView(getConnectionsTree());
140
        }
141

    
142
        return connectionsScrollPane;
143
    }
144

    
145
    /**
146
     * This method initializes closeButton
147
     *
148
     * @return javax.swing.JButton
149
     */
150
    private JButton getCloseButton() {
151
        if (closeButton == null) {
152
            closeButton = new JButton();
153
            closeButton.addActionListener(this);
154
            closeButton.setBounds(new java.awt.Rectangle(230, 380, 106, 26));
155
            closeButton.setText(PluginServices.getText(this, "aceptar"));
156
        }
157

    
158
        return closeButton;
159
    }
160

    
161
    /**
162
     * This method initializes removeButton
163
     *
164
     * @return javax.swing.JButton
165
     */
166
    private JButton getRemoveButton() {
167
        if (removeButton == null) {
168
            removeButton = new JButton();
169
            removeButton.addActionListener(this);
170
            removeButton.setBounds(new java.awt.Rectangle(120, 350, 106, 26));
171
            removeButton.setText(PluginServices.getText(this, "remove"));
172
        }
173

    
174
        return removeButton;
175
    }
176

    
177
    /**
178
     * This method initializes newButton
179
     *
180
     * @return javax.swing.JButton
181
     */
182
    private JButton getNewButton() {
183
        if (newButton == null) {
184
            newButton = new JButton();
185
            newButton.addActionListener(this);
186
            newButton.setBounds(new java.awt.Rectangle(10, 350, 106, 26));
187
            newButton.setText(PluginServices.getText(this, "add"));
188
        }
189

    
190
        return newButton;
191
    }
192

    
193
    /**
194
     * This method initializes connectionsTree
195
     *
196
     * @return javax.swing.JTree
197
     */
198
    private JTree getConnectionsTree() {
199

    
200
            JTree resp = new JTree(getTreeRoot());
201
            resp.setRowHeight(20);
202
            resp.setCellRenderer(connTreeRenderer);
203
        return resp;
204

    
205
    }
206

    
207
    private DefaultMutableTreeNode getTreeRoot() {
208
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
209
        root.setUserObject(new String(PluginServices.getText(this,
210
                    "geodb_connections")));
211

    
212
        HashMap aux_nodes = new HashMap();
213

    
214
        ConnectionWithParamsSDE[] conns = SingleSDEConnectionManager.instance()
215
                                                                  .getAllConnections();
216

    
217
        if (conns == null) {
218
            return root;
219
        }
220

    
221
        for (int i = 0; i < conns.length; i++) {
222
            String key = conns[i].getDrvName();
223

    
224
            if (!aux_nodes.containsKey(key)) {
225
                aux_nodes.put(key, new ArrayList());
226
            }
227

    
228
            ArrayList aux = (ArrayList) aux_nodes.get(key);
229
            aux.add(conns[i]);
230
        }
231

    
232
        Iterator iter = aux_nodes.keySet().iterator();
233

    
234
        while (iter.hasNext()) {
235
            String k = (String) iter.next();
236
            SDEConnectionTreeNode node = new SDEConnectionTreeNode(k);
237
            ArrayList cc = (ArrayList) aux_nodes.get(k);
238

    
239
            for (int i = 0; i < cc.size(); i++) {
240
                ConnectionWithParamsSDE conwp = (ConnectionWithParamsSDE) cc.get(i);
241
                SDEConnectionTreeLeaf leaf = new SDEConnectionTreeLeaf(conwp);
242
                node.add(leaf);
243
            }
244

    
245
            root.add(node);
246
        }
247

    
248
        return root;
249
    }
250

    
251
    private void addConnection(String _drvName, String _port, String _host,
252
        String _dbName, String _user, String _pw, String _conn_usr_name,
253
        boolean is_conn) throws SQLException {
254
        SingleSDEConnectionManager.instance()
255
                                   .getConnection(_drvName, _user, _pw,
256
            _conn_usr_name, _host, _port, _dbName, is_conn);
257
        refreshTree();
258
    }
259

    
260
    private void refreshTree() {
261
        connectionsTree = getConnectionsTree();
262

    
263
        for (int i = 0; i < connectionsTree.getRowCount(); i++) {
264
            connectionsTree.expandRow(i);
265
        }
266

    
267
        connectionsScrollPane.setViewportView(connectionsTree);
268
    }
269

    
270
    public void actionPerformed(ActionEvent arg0) {
271
        Object src = arg0.getSource();
272

    
273
        if (src == disconnButton) {
274
            if (connectionsTree.getSelectionCount() != 1) {
275
                return;
276
            }
277

    
278
            Object obj = connectionsTree.getSelectionPath()
279
                                        .getLastPathComponent();
280

    
281
            if (!(obj instanceof SDEConnectionTreeLeaf)) {
282
                return;
283
            }
284

    
285
            SDEConnectionTreeLeaf leaf = (SDEConnectionTreeLeaf) obj;
286
            ConnectionWithParamsSDE _cwp = leaf.getConnectionWithParams();
287

    
288
            if (_cwp.isConnected()) {
289
                _cwp.disconnect();
290
                refreshTree();
291
            }
292
        }
293

    
294
        if (src == connectButton) {
295
            if (connectionsTree.getSelectionCount() != 1) {
296
                return;
297
            }
298

    
299
            Object obj = connectionsTree.getSelectionPath()
300
                                        .getLastPathComponent();
301

    
302
            if (!(obj instanceof SDEConnectionTreeLeaf)) {
303
                return;
304
            }
305

    
306
            SDEConnectionTreeLeaf leaf = (SDEConnectionTreeLeaf) obj;
307
            ConnectionWithParamsSDE _cwp = leaf.getConnectionWithParams();
308

    
309
            JPasswordDlg dlg = new JPasswordDlg();
310
            String strMessage = PluginServices.getText(this, "conectar_sde");
311
            String strPassword = PluginServices.getText(this, "password");
312
            dlg.setMessage(strMessage + " [" + _cwp.getDrvName() + ", " +
313
                _cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() +
314
                ", " + _cwp.getUser() + "]. " + strPassword + "?");
315
            dlg.show();
316

    
317
            String clave = dlg.getPassword();
318

    
319
            if (clave == null) {
320
                return;
321
            }
322

    
323
            try {
324
                _cwp.connect(clave);
325
                refreshTree();
326
            }
327
            catch (SQLException e) {
328
                showConnectionErrorMessage(e.getMessage(), false);
329
            }
330
        }
331

    
332
        if (src == closeButton) {
333
            PluginServices.getMDIManager().closeWindow(this);
334
        }
335

    
336
        if (src == newButton) {
337
            SDEConnectionParamsDialog newco = new SDEConnectionParamsDialog();
338
            newco.showDialog();
339

    
340
            if (newco.isOkPressed()) {
341
                String _drvname = newco.getConnectionDriverName();
342
                String _host = newco.getConnectionServerUrl();
343
                String _port = newco.getConnectionPort();
344
                String _dbname = newco.getConnectionDBName();
345
                String _user = newco.getConnectionUser();
346
                String _pw = newco.getConnectionPassword();
347
                String _conn_usr_name = newco.getConnectionName();
348

    
349
                boolean hasToBeCon = newco.hasToBeConnected();
350

    
351
                try {
352
                    addConnection(_drvname, _port, _host, _dbname, _user, _pw,
353
                        _conn_usr_name, hasToBeCon);
354
                }
355
                catch (SQLException e) {
356
                    showConnectionErrorMessage(e.getMessage(), false);
357
                }
358
            }
359
        }
360

    
361
        if (src == removeButton) {
362
            if (connectionsTree.getSelectionCount() != 1) {
363
                return;
364
            }
365

    
366
            Object obj = connectionsTree.getSelectionPath()
367
                                        .getLastPathComponent();
368

    
369
            if (!(obj instanceof SDEConnectionTreeLeaf)) {
370
                return;
371
            }
372

    
373
            int confirm = JOptionPane.showConfirmDialog(this,
374
                    PluginServices.getText(this, "confirm_remove"),
375
                    PluginServices.getText(this, "Remove"),
376
                    JOptionPane.YES_NO_OPTION);
377

    
378
            if (confirm != JOptionPane.YES_OPTION) {
379
                return;
380
            }
381

    
382
            SDEConnectionTreeLeaf leaf = (SDEConnectionTreeLeaf) obj;
383
            ConnectionWithParamsSDE _cwp = leaf.getConnectionWithParams();
384
            SingleSDEConnectionManager.instance().closeAndRemove(_cwp);
385
            refreshTree();
386
        }
387

    
388
        if (src == editButton) {
389
            if (connectionsTree.getSelectionCount() != 1) {
390
                return;
391
            }
392

    
393
            Object obj = connectionsTree.getSelectionPath()
394
                                        .getLastPathComponent();
395

    
396
            if (!(obj instanceof SDEConnectionTreeLeaf)) {
397
                return;
398
            }
399

    
400
            SDEConnectionTreeLeaf leaf = (SDEConnectionTreeLeaf) obj;
401
            ConnectionWithParamsSDE old_cwp = leaf.getConnectionWithParams();
402

    
403
            SDEConnectionParamsDialog modifyco = new SDEConnectionParamsDialog();
404
            modifyco.loadValues(old_cwp);
405
            modifyco.showDialog();
406

    
407
            if (modifyco.isOkPressed()) {
408
                String _drvname = modifyco.getConnectionDriverName();
409
                String _host = modifyco.getConnectionServerUrl();
410
                String _port = modifyco.getConnectionPort();
411
                String _dbname = modifyco.getConnectionDBName();
412
                String _user = modifyco.getConnectionUser();
413
                String _pw = modifyco.getConnectionPassword();
414
                String _conn_usr_name = modifyco.getConnectionName();
415

    
416
                boolean hasToBeCon = modifyco.hasToBeConnected();
417

    
418
                boolean old_was_open = true;
419

    
420
                try {
421
                    old_was_open = SingleSDEConnectionManager.instance()
422
                                                              .closeAndRemove(old_cwp);
423

    
424
                    addConnection(_drvname, _port, _host, _dbname, _user, _pw,
425
                        _conn_usr_name, hasToBeCon);
426
                }
427
                catch (SQLException e) {
428
                    showConnectionErrorMessage(e.getMessage(), false);
429

    
430
                    try {
431
                        addConnection(old_cwp.getDrvName(), old_cwp.getPort(),
432
                            old_cwp.getHost(), old_cwp.getDb(),
433
                            old_cwp.getUser(), old_cwp.getPw(),
434
                            old_cwp.getName(), old_was_open);
435
                    }
436
                    catch (SQLException e1) {
437
                        showConnectionErrorMessage(e.getMessage(), true);
438
                    }
439
                }
440
            }
441
        }
442
    }
443

    
444
    private void showConnectionErrorMessage(String _msg, boolean reconnect) {
445
        String msg = (_msg.length() > 300) ? "" : (": " + _msg);
446
        String title = "";
447

    
448
        if (reconnect) {
449
            title = PluginServices.getText(this, "reconnection_error");
450
        }
451
        else {
452
            title = PluginServices.getText(this, "connection_error");
453
        }
454

    
455
        JOptionPane.showMessageDialog(this, title + msg, title,
456
            JOptionPane.ERROR_MESSAGE);
457
    }
458

    
459
    /**
460
     * This method initializes connectButton
461
     *
462
     * @return javax.swing.JButton
463
     */
464
    private JButton getConnectButton() {
465
        if (connectButton == null) {
466
            connectButton = new JButton();
467
            connectButton.addActionListener(this);
468
            connectButton.setBounds(new java.awt.Rectangle(10, 380, 106, 26));
469
            connectButton.setText(PluginServices.getText(this, "connect"));
470
        }
471

    
472
        return connectButton;
473
    }
474

    
475
    /**
476
     * This method initializes disconnButton
477
     *
478
     * @return javax.swing.JButton
479
     */
480
    private JButton getDisconnButton() {
481
        if (disconnButton == null) {
482
            disconnButton = new JButton();
483
            disconnButton.addActionListener(this);
484
            disconnButton.setBounds(new java.awt.Rectangle(120, 380, 106, 26));
485
            disconnButton.setText(PluginServices.getText(this, "disconnect"));
486
        }
487

    
488
        return disconnButton;
489
    }
490

    
491
    /**
492
     * This method initializes editButton
493
     *
494
     * @return javax.swing.JButton
495
     */
496
    private JButton getEditButton() {
497
        if (editButton == null) {
498
            editButton = new JButton();
499
            editButton.addActionListener(this);
500
            editButton.setBounds(new java.awt.Rectangle(230, 350, 106, 26));
501
            editButton.setText(PluginServices.getText(this, "edit_settings"));
502
        }
503

    
504
        return editButton;
505
    }
506
} //  @jve:decl-index=0:visual-constraint="10,10"