Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / VectorialDBConnectionManagerDialog.java @ 24986

History | View | Annotate | Download (16.6 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.Component;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
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.cit.gvsig.fmap.drivers.DBException;
66
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams;
67
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleVectorialDBConnectionManager;
68
import com.iver.utiles.swing.JPasswordDlg;
69

    
70

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

    
92
    /**
93
     * This method initializes
94
     *
95
     */
96
    public VectorialDBConnectionManagerDialog() {
97
        super();
98
        initialize();
99
    }
100

    
101
    public void showDialog() {
102
        PluginServices.getMDIManager().addWindow(this);
103
    }
104

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

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

    
124
        connTreeRenderer = new ConnectionTreeRenderer();
125
        refreshTree();
126
    }
127

    
128
    public WindowInfo getWindowInfo() {
129
        return winfo;
130
    }
131

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

    
145
        return connectionsScrollPane;
146
    }
147

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

    
161
        return closeButton;
162
    }
163

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

    
177
        return removeButton;
178
    }
179

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

    
193
        return newButton;
194
    }
195

    
196
    /**
197
     * This method initializes connectionsTree
198
     *
199
     * @return javax.swing.JTree
200
     */
201
    private JTree getConnectionsTree() {
202

    
203
            JTree resp = new JTree(getTreeRoot());
204
            resp.setRowHeight(20);
205
            resp.setCellRenderer(connTreeRenderer);
206
        return resp;
207

    
208
    }
209

    
210
    private DefaultMutableTreeNode getTreeRoot() {
211
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
212
        root.setUserObject(new String(PluginServices.getText(this,
213
                    "geodb_connections")));
214

    
215
        HashMap aux_nodes = new HashMap();
216

    
217
        ConnectionWithParams[] conns = SingleVectorialDBConnectionManager.instance()
218
                                                                  .getAllConnections();
219

    
220
        if (conns == null) {
221
            return root;
222
        }
223

    
224
        for (int i = 0; i < conns.length; i++) {
225
            String key = conns[i].getDrvName();
226

    
227
            if (!aux_nodes.containsKey(key)) {
228
                aux_nodes.put(key, new ArrayList());
229
            }
230

    
231
            ArrayList aux = (ArrayList) aux_nodes.get(key);
232
            aux.add(conns[i]);
233
        }
234

    
235
        Iterator iter = aux_nodes.keySet().iterator();
236

    
237
        while (iter.hasNext()) {
238
            String k = (String) iter.next();
239
            VectorialDBConnectionTreeNode node = new VectorialDBConnectionTreeNode(k);
240
            ArrayList cc = (ArrayList) aux_nodes.get(k);
241

    
242
            for (int i = 0; i < cc.size(); i++) {
243
                ConnectionWithParams conwp = (ConnectionWithParams) cc.get(i);
244
                VectorialDBConnectionTreeLeaf leaf = new VectorialDBConnectionTreeLeaf(conwp);
245
                node.add(leaf);
246
            }
247

    
248
            root.add(node);
249
        }
250

    
251
        return root;
252
    }
253

    
254
    private void addConnection(String _drvName, String _port, String _host,
255
        String _dbName, String _user, String _pw, String _conn_usr_name,
256
        boolean is_conn) throws DBException {
257
        SingleVectorialDBConnectionManager.instance()
258
                                   .getConnection(_drvName, _user, _pw,
259
            _conn_usr_name, _host, _port, _dbName, is_conn);
260
        refreshTree();
261
    }
262

    
263
    private void refreshTree() {
264
        connectionsTree = getConnectionsTree();
265

    
266
        for (int i = 0; i < connectionsTree.getRowCount(); i++) {
267
            connectionsTree.expandRow(i);
268
        }
269

    
270
        connectionsScrollPane.setViewportView(connectionsTree);
271
    }
272

    
273
    public void actionPerformed(ActionEvent arg0) {
274
        Object src = arg0.getSource();
275

    
276
        if (src == disconnButton) {
277
            if (connectionsTree.getSelectionCount() != 1) {
278
                return;
279
            }
280

    
281
            Object obj = connectionsTree.getSelectionPath()
282
                                        .getLastPathComponent();
283

    
284
            if (!(obj instanceof VectorialDBConnectionTreeLeaf)) {
285
                return;
286
            }
287

    
288
            VectorialDBConnectionTreeLeaf leaf = (VectorialDBConnectionTreeLeaf) obj;
289
            ConnectionWithParams _cwp = leaf.getConnectionWithParams();
290

    
291
            if (_cwp.isConnected()) {
292
                _cwp.disconnect();
293
                refreshTree();
294
            }
295
        }
296

    
297
        if (src == connectButton) {
298
            if (connectionsTree.getSelectionCount() != 1) {
299
                return;
300
            }
301

    
302
            Object obj = connectionsTree.getSelectionPath()
303
                                        .getLastPathComponent();
304

    
305
            if (!(obj instanceof VectorialDBConnectionTreeLeaf)) {
306
                return;
307
            }
308

    
309
            VectorialDBConnectionTreeLeaf leaf = (VectorialDBConnectionTreeLeaf) obj;
310
            ConnectionWithParams _cwp = leaf.getConnectionWithParams();
311

    
312
            JPasswordDlg dlg = new JPasswordDlg();
313
            dlg.setLocationRelativeTo((Component)PluginServices.getMainFrame());
314
            String strMessage = PluginServices.getText(this, "conectar_db");
315
            String strPassword = PluginServices.getText(this, "password");
316
            dlg.setMessage(strMessage + " [" + _cwp.getDrvName() + ", " +
317
                _cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() +
318
                ", " + _cwp.getUser() + "]. " + strPassword + "?");
319
            dlg.setVisible(true);
320

    
321
            String clave = dlg.getPassword();
322

    
323
            if (clave == null) {
324
                return;
325
            }
326

    
327
            try {
328
                _cwp.connect(clave);
329
                refreshTree();
330
            }
331
            catch (DBException e) {
332
                showConnectionErrorMessage(e.getMessage(), false);
333
            }
334
        }
335

    
336
        if (src == closeButton) {
337
            PluginServices.getMDIManager().closeWindow(this);
338
        }
339

    
340
        if (src == newButton) {
341
            VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
342
            newco.showDialog();
343

    
344
            if (newco.isOkPressed()) {
345
                String _drvname = newco.getConnectionDriverName();
346
                String _host = newco.getConnectionServerUrl();
347
                String _port = newco.getConnectionPort();
348
                String _dbname = newco.getConnectionDBName();
349
                String _user = newco.getConnectionUser();
350
                String _pw = newco.getConnectionPassword();
351
                String _conn_usr_name = newco.getConnectionName();
352

    
353
                boolean hasToBeCon = newco.hasToBeConnected();
354

    
355
                try {
356
                    addConnection(_drvname, _port, _host, _dbname, _user, _pw,
357
                        _conn_usr_name, hasToBeCon);
358
                }
359
                catch (DBException e) {
360
                    showConnectionErrorMessage(e.getMessage(), false);
361
                }
362
            }
363
        }
364

    
365
        if (src == removeButton) {
366
            if (connectionsTree.getSelectionCount() != 1) {
367
                return;
368
            }
369

    
370
            Object obj = connectionsTree.getSelectionPath()
371
                                        .getLastPathComponent();
372

    
373
            if (!(obj instanceof VectorialDBConnectionTreeLeaf)) {
374
                return;
375
            }
376

    
377
            int confirm = JOptionPane.showConfirmDialog(this,
378
                    PluginServices.getText(this, "confirm_remove"),
379
                    PluginServices.getText(this, "Remove"),
380
                    JOptionPane.YES_NO_OPTION);
381

    
382
            if (confirm != JOptionPane.YES_OPTION) {
383
                return;
384
            }
385

    
386
            VectorialDBConnectionTreeLeaf leaf = (VectorialDBConnectionTreeLeaf) obj;
387
            ConnectionWithParams _cwp = leaf.getConnectionWithParams();
388
            SingleVectorialDBConnectionManager.instance().closeAndRemove(_cwp);
389
            refreshTree();
390
        }
391

    
392
        if (src == editButton) {
393
            if (connectionsTree.getSelectionCount() != 1) {
394
                return;
395
            }
396

    
397
            Object obj = connectionsTree.getSelectionPath()
398
                                        .getLastPathComponent();
399

    
400
            if (!(obj instanceof VectorialDBConnectionTreeLeaf)) {
401
                return;
402
            }
403

    
404
            VectorialDBConnectionTreeLeaf leaf = (VectorialDBConnectionTreeLeaf) obj;
405
            ConnectionWithParams old_cwp = leaf.getConnectionWithParams();
406

    
407
            VectorialDBConnectionParamsDialog modifyco = new VectorialDBConnectionParamsDialog();
408
            modifyco.loadValues(old_cwp);
409
            modifyco.showDialog();
410

    
411
            if (modifyco.isOkPressed()) {
412
                String _drvname = modifyco.getConnectionDriverName();
413
                String _host = modifyco.getConnectionServerUrl();
414
                String _port = modifyco.getConnectionPort();
415
                String _dbname = modifyco.getConnectionDBName();
416
                String _user = modifyco.getConnectionUser();
417
                String _pw = modifyco.getConnectionPassword();
418
                String _conn_usr_name = modifyco.getConnectionName();
419

    
420
                boolean hasToBeCon = modifyco.hasToBeConnected();
421

    
422
                boolean old_was_open = true;
423

    
424
                try {
425
                    old_was_open = SingleVectorialDBConnectionManager.instance()
426
                                                              .closeAndRemove(old_cwp);
427

    
428
                    addConnection(_drvname, _port, _host, _dbname, _user, _pw,
429
                        _conn_usr_name, hasToBeCon);
430
                }
431
                catch (DBException e) {
432
                    showConnectionErrorMessage(e.getMessage(), false);
433

    
434
                    try {
435
                        addConnection(old_cwp.getDrvName(), old_cwp.getPort(),
436
                            old_cwp.getHost(), old_cwp.getDb(),
437
                            old_cwp.getUser(), old_cwp.getPw(),
438
                            old_cwp.getName(), old_was_open);
439
                    }
440
                    catch (DBException e1) {
441
                        showConnectionErrorMessage(e.getMessage(), true);
442
                    }
443
                }
444
            }
445
        }
446
    }
447

    
448
    private void showConnectionErrorMessage(String _msg, boolean reconnect) {
449
        String msg = (_msg.length() > 300) ? "" : (": " + _msg);
450
        String title = "";
451

    
452
        if (reconnect) {
453
            title = PluginServices.getText(this, "reconnection_error");
454
        }
455
        else {
456
            title = PluginServices.getText(this, "connection_error");
457
        }
458

    
459
        JOptionPane.showMessageDialog(this, title + msg, title,
460
            JOptionPane.ERROR_MESSAGE);
461
    }
462

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

    
476
        return connectButton;
477
    }
478

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

    
492
        return disconnButton;
493
    }
494

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

    
508
        return editButton;
509
    }
510

    
511
        public Object getWindowProfile() {
512
                return WindowInfo.DIALOG_PROFILE;
513
        }
514
} //  @jve:decl-index=0:visual-constraint="10,10"