Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoDB / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / VectorialDBConnectionManagerDialog.java @ 27723

History | View | Annotate | Download (17 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.HashMap;
49

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

    
58
import org.apache.log4j.Logger;
59
import org.gvsig.fmap.dal.DataServerExplorerParameters;
60
import org.gvsig.fmap.data.feature.db.DBExplorerParameters;
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 VectorialDBConnectionManagerDialog extends JPanel implements IWindow,
76
    ActionListener {
77
    private static Logger logger = Logger.getLogger(VectorialDBConnectionManagerDialog.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 VectorialDBConnectionManagerDialog() {
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_db"));
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
//        ConnectionWithParams[] conns = SingleVectorialDBConnectionManager.instance()
215
//                                                                  .getAllConnections();
216

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

    
221
//        DataManager dm=DataManager.getManager();
222
//        String[] registers=dm.getRegistersExplorers();
223
//        for (int i = 0; i < registers.length; i++) {
224
//            String key = registers[i];
225
//
226
//            if (!aux_nodes.containsKey(key)) {
227
//                aux_nodes.put(key, new ArrayList());
228
//            }
229
//
230
//            ArrayList aux = (ArrayList) aux_nodes.get(key);
231
//            aux.add(registers);
232
//        }
233
//
234
//        Iterator iter = aux_nodes.keySet().iterator();
235
//        while (iter.hasNext()) {
236
//            String k = (String) iter.next();
237
//            VectorialDBConnectionTreeNode node = new VectorialDBConnectionTreeNode(k);
238
//            ArrayList cc = (ArrayList) aux_nodes.get(k);
239
//
240
//            for (int i = 0; i < cc.size(); i++) {
241
//                DataServerExplorerParameters conwp=null;
242
//                                for (int j=0;j<((String[])cc.get(i)).length;j++){
243
//                                        try {
244
//                                                conwp = (DataServerExplorerParameters) dm.createDataExplorerParameters(((String[])cc.get(i))[j]);
245
//                                                 VectorialDBConnectionTreeLeaf leaf = new VectorialDBConnectionTreeLeaf(conwp);
246
//                                        node.add(leaf);
247
//                                        } catch (InitializeException e) {
248
//                                                // TODO Auto-generated catch block
249
//                                                e.printStackTrace();
250
//                                        }
251
//                                }
252
//
253
//
254
//            }
255
//
256
//            root.add(node);
257
//        }
258

    
259
        return root;
260
    }
261

    
262
    private void addConnection(String _drvName, String _port, String _host,
263
        String _dbName, String _user, String _pw, String _conn_usr_name,
264
        boolean is_conn) {
265
//        SingleVectorialDBConnectionManager.instance()
266
//                                   .getConnection(_drvName, _user, _pw,
267
//            _conn_usr_name, _host, _port, _dbName, is_conn);
268
        refreshTree();
269
    }
270

    
271
    private void refreshTree() {
272
        connectionsTree = getConnectionsTree();
273

    
274
        for (int i = 0; i < connectionsTree.getRowCount(); i++) {
275
            connectionsTree.expandRow(i);
276
        }
277

    
278
        connectionsScrollPane.setViewportView(connectionsTree);
279
    }
280

    
281
    public void actionPerformed(ActionEvent arg0) {
282
        Object src = arg0.getSource();
283

    
284
        if (src == disconnButton) {
285
            if (connectionsTree.getSelectionCount() != 1) {
286
                return;
287
            }
288

    
289
            Object obj = connectionsTree.getSelectionPath()
290
                                        .getLastPathComponent();
291

    
292
            if (!(obj instanceof VectorialDBConnectionTreeLeaf)) {
293
                return;
294
            }
295

    
296
            VectorialDBConnectionTreeLeaf leaf = (VectorialDBConnectionTreeLeaf) obj;
297
            DataServerExplorerParameters _cwp = leaf.getConnectionWithParams();
298

    
299
//            if (_cwp.isConnected()) {
300
//                _cwp.disconnect();
301
//                refreshTree();
302
//            }
303
        }
304

    
305
        if (src == connectButton) {
306
            if (connectionsTree.getSelectionCount() != 1) {
307
                return;
308
            }
309

    
310
            Object obj = connectionsTree.getSelectionPath()
311
                                        .getLastPathComponent();
312

    
313
            if (!(obj instanceof VectorialDBConnectionTreeLeaf)) {
314
                return;
315
            }
316

    
317
            VectorialDBConnectionTreeLeaf leaf = (VectorialDBConnectionTreeLeaf) obj;
318
            DBExplorerParameters _cwp = (DBExplorerParameters)leaf.getConnectionWithParams();
319

    
320
            JPasswordDlg dlg = new JPasswordDlg();
321
            dlg.setLocationRelativeTo((Component)PluginServices.getMainFrame());
322
            String strMessage = PluginServices.getText(this, "conectar_db");
323
            String strPassword = PluginServices.getText(this, "password");
324
            dlg.setMessage(strMessage + " [" + //_cwp.getTableName() + ", " +
325
                _cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() +
326
                ", " + _cwp.getUser() + "]. " + strPassword + "?");
327
            dlg.setVisible(true);
328

    
329
            String clave = dlg.getPassword();
330

    
331
            if (clave == null) {
332
                return;
333
            }
334

    
335
//            try {
336
//                _cwp.connect(clave);
337
                refreshTree();
338
//            }
339
//            catch (DBException e) {
340
//                showConnectionErrorMessage(e.getMessage(), false);
341
//            }
342
        }
343

    
344
        if (src == closeButton) {
345
            PluginServices.getMDIManager().closeWindow(this);
346
        }
347

    
348
        if (src == newButton) {
349
            VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
350
            newco.showDialog();
351

    
352
            if (newco.isOkPressed()) {
353
                String _drvname = newco.getConnectionDriverName();
354
                String _host = newco.getConnectionServerUrl();
355
                String _port = newco.getConnectionPort();
356
                String _dbname = newco.getConnectionDBName();
357
                String _user = newco.getConnectionUser();
358
                String _pw = newco.getConnectionPassword();
359
                String _conn_usr_name = newco.getConnectionName();
360

    
361
                boolean hasToBeCon = newco.hasToBeConnected();
362

    
363
//                try {
364
                    addConnection(_drvname, _port, _host, _dbname, _user, _pw,
365
                        _conn_usr_name, hasToBeCon);
366
//                }
367
//                catch (DBException e) {
368
//                    showConnectionErrorMessage(e.getMessage(), false);
369
//                }
370
            }
371
        }
372

    
373
        if (src == removeButton) {
374
            if (connectionsTree.getSelectionCount() != 1) {
375
                return;
376
            }
377

    
378
            Object obj = connectionsTree.getSelectionPath()
379
                                        .getLastPathComponent();
380

    
381
            if (!(obj instanceof VectorialDBConnectionTreeLeaf)) {
382
                return;
383
            }
384

    
385
            int confirm = JOptionPane.showConfirmDialog(this,
386
                    PluginServices.getText(this, "confirm_remove"),
387
                    PluginServices.getText(this, "Remove"),
388
                    JOptionPane.YES_NO_OPTION);
389

    
390
            if (confirm != JOptionPane.YES_OPTION) {
391
                return;
392
            }
393

    
394
            VectorialDBConnectionTreeLeaf leaf = (VectorialDBConnectionTreeLeaf) obj;
395
            DataServerExplorerParameters _cwp = leaf.getConnectionWithParams();
396
//            SingleVectorialDBConnectionManager.instance().closeAndRemove(_cwp);
397
            refreshTree();
398
        }
399

    
400
        if (src == editButton) {
401
            if (connectionsTree.getSelectionCount() != 1) {
402
                return;
403
            }
404

    
405
            Object obj = connectionsTree.getSelectionPath()
406
                                        .getLastPathComponent();
407

    
408
            if (!(obj instanceof VectorialDBConnectionTreeLeaf)) {
409
                return;
410
            }
411

    
412
            VectorialDBConnectionTreeLeaf leaf = (VectorialDBConnectionTreeLeaf) obj;
413
            DataServerExplorerParameters old_cwp = leaf.getConnectionWithParams();
414

    
415
            VectorialDBConnectionParamsDialog modifyco = new VectorialDBConnectionParamsDialog();
416
            modifyco.loadValues((DBExplorerParameters)old_cwp);
417
            modifyco.showDialog();
418

    
419
            if (modifyco.isOkPressed()) {
420
                String _drvname = modifyco.getConnectionDriverName();
421
                String _host = modifyco.getConnectionServerUrl();
422
                String _port = modifyco.getConnectionPort();
423
                String _dbname = modifyco.getConnectionDBName();
424
                String _user = modifyco.getConnectionUser();
425
                String _pw = modifyco.getConnectionPassword();
426
                String _conn_usr_name = modifyco.getConnectionName();
427

    
428
                boolean hasToBeCon = modifyco.hasToBeConnected();
429

    
430
                boolean old_was_open = true;
431

    
432
//                try {
433
//                    old_was_open = SingleVectorialDBConnectionManager.instance()
434
//                                                              .closeAndRemove(old_cwp);
435

    
436
                    addConnection(_drvname, _port, _host, _dbname, _user, _pw,
437
                        _conn_usr_name, hasToBeCon);
438
//                }
439
//                catch (DBException e) {
440
//                    showConnectionErrorMessage(e.getMessage(), false);
441
//
442
//                    try {
443
//                        addConnection(old_cwp.getDrvName(), old_cwp.getPort(),
444
//                            old_cwp.getHost(), old_cwp.getDb(),
445
//                            old_cwp.getUser(), old_cwp.getPw(),
446
//                            old_cwp.getName(), old_was_open);
447
//                    }
448
//                    catch (DBException e1) {
449
//                        showConnectionErrorMessage(e.getMessage(), true);
450
//                    }
451
//                }
452
            }
453
        }
454
    }
455

    
456
    private void showConnectionErrorMessage(String _msg, boolean reconnect) {
457
        String msg = (_msg.length() > 300) ? "" : (": " + _msg);
458
        String title = "";
459

    
460
        if (reconnect) {
461
            title = PluginServices.getText(this, "reconnection_error");
462
        }
463
        else {
464
            title = PluginServices.getText(this, "connection_error");
465
        }
466

    
467
        JOptionPane.showMessageDialog(this, title + msg, title,
468
            JOptionPane.ERROR_MESSAGE);
469
    }
470

    
471
    /**
472
     * This method initializes connectButton
473
     *
474
     * @return javax.swing.JButton
475
     */
476
    private JButton getConnectButton() {
477
        if (connectButton == null) {
478
            connectButton = new JButton();
479
            connectButton.addActionListener(this);
480
            connectButton.setBounds(new java.awt.Rectangle(10, 380, 106, 26));
481
            connectButton.setText(PluginServices.getText(this, "connect"));
482
        }
483

    
484
        return connectButton;
485
    }
486

    
487
    /**
488
     * This method initializes disconnButton
489
     *
490
     * @return javax.swing.JButton
491
     */
492
    private JButton getDisconnButton() {
493
        if (disconnButton == null) {
494
            disconnButton = new JButton();
495
            disconnButton.addActionListener(this);
496
            disconnButton.setBounds(new java.awt.Rectangle(120, 380, 106, 26));
497
            disconnButton.setText(PluginServices.getText(this, "disconnect"));
498
        }
499

    
500
        return disconnButton;
501
    }
502

    
503
    /**
504
     * This method initializes editButton
505
     *
506
     * @return javax.swing.JButton
507
     */
508
    private JButton getEditButton() {
509
        if (editButton == null) {
510
            editButton = new JButton();
511
            editButton.addActionListener(this);
512
            editButton.setBounds(new java.awt.Rectangle(230, 350, 106, 26));
513
            editButton.setText(PluginServices.getText(this, "edit_settings"));
514
        }
515

    
516
        return editButton;
517
    }
518
        public Object getWindowProfile() {
519
                return WindowInfo.DIALOG_PROFILE;
520
        }
521

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