KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > infos > RootNodeInfo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer.infos;
21
22 import java.util.*;
23 import org.openide.filesystems.*;
24 import org.openide.loaders.DataFolder;
25 import org.openide.loaders.DataObject;
26
27 import org.netbeans.lib.ddl.*;
28 import org.netbeans.api.db.explorer.DatabaseException;
29 import org.netbeans.modules.db.explorer.DatabaseConnection;
30 import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
31 import org.netbeans.modules.db.explorer.ConnectionList;
32 import org.netbeans.modules.db.explorer.nodes.*;
33
34 public class RootNodeInfo extends DatabaseNodeInfo implements ConnectionOwnerOperations {
35     static final long serialVersionUID =-8079386805046070315L;
36     
37     static RootNodeInfo rootInfo = null;
38     public static RootNodeInfo getInstance() throws DatabaseException {
39         if (rootInfo == null) {
40             rootInfo = (RootNodeInfo) DatabaseNodeInfo.createNodeInfo(null, "root"); //NOI18N
41
}
42         return rootInfo;
43     }
44     public void initChildren(Vector children) throws DatabaseException {
45         try {
46             DatabaseConnection[] cinfos = ConnectionList.getDefault().getConnections();
47             for (int i = 0; i < cinfos.length; i++) {
48                 DatabaseConnection cinfo = cinfos[i];
49                 ConnectionNodeInfo ninfo = createConnectionNodeInfo(cinfo);
50                 children.add(ninfo);
51             }
52
53             Repository r = Repository.getDefault();
54             FileSystem rfs = r.getDefaultFileSystem();
55             FileObject rootFolder = rfs.getRoot();
56             FileObject databaseFileObject = rootFolder.getFileObject("Database"); //NOI18N
57
if (databaseFileObject != null) {
58                 FileObject adaptorsFolder = databaseFileObject.getFileObject("Adaptors"); //NOI18N
59
DataObject dbdo = DataFolder.findFolder(adaptorsFolder);
60                 if (dbdo != null)
61                     children.add(dbdo.getNodeDelegate());
62             }
63
64         } catch (Exception JavaDoc e) {
65             throw new DatabaseException(e.getMessage());
66         }
67     }
68
69     private ConnectionNodeInfo createConnectionNodeInfo(DatabaseConnection dbconn) throws DatabaseException {
70         ConnectionNodeInfo ninfo = (ConnectionNodeInfo)createNodeInfo(this, DatabaseNode.CONNECTION);
71         ninfo.setUser(dbconn.getUser());
72         ninfo.setDatabase(dbconn.getDatabase());
73         ninfo.setSchema(dbconn.getSchema());
74         ninfo.setName(dbconn.getName());
75         ninfo.setDatabaseConnection(dbconn);
76         return ninfo;
77     }
78
79     public void refreshChildren() throws DatabaseException {
80         // refresh action is empty
81
}
82
83     public void addConnectionNoConnect(DatabaseConnection dbconn) throws DatabaseException {
84         getChildren(); // force restore
85

86         if (ConnectionList.getDefault().contains(dbconn)) {
87             return;
88         }
89
90         DatabaseNode node = getNode();
91         DatabaseNodeChildren children = (DatabaseNodeChildren) node.getChildren();
92         ConnectionNodeInfo ninfo = createConnectionNodeInfo(dbconn);
93         ConnectionList.getDefault().add(dbconn);
94         children.createSubnode(ninfo, true);
95     }
96     
97     public void addConnection(DBConnection cinfo) throws DatabaseException {
98         DatabaseConnection dbconn = (DatabaseConnection)cinfo;
99         getChildren(); // force restore
100

101         if (ConnectionList.getDefault().contains(dbconn)) {
102             throw new DatabaseException(bundle().getString("EXC_ConnectionAlreadyExists"));
103         }
104
105         DatabaseNode node = getNode();
106         DatabaseNodeChildren children = (DatabaseNodeChildren) node.getChildren();
107         
108         // the nodes have to be initialized too, otherwise the node created
109
// for the new connection will not be added and the connection
110
// will be lost when the nodes are eventually initialized
111
children.getNodes(true);
112         
113         ConnectionNodeInfo ninfo = createConnectionNodeInfo(dbconn);
114         ConnectionList.getDefault().add(dbconn);
115         DatabaseNode cnode = children.createSubnode(ninfo, true);
116         
117         if (((DatabaseConnection) dbconn).getConnection() == null)
118             ((ConnectionNodeInfo) cnode.getInfo()).connect();
119         else
120             ((ConnectionNodeInfo) cnode.getInfo()).connect(dbconn);
121     }
122 }
123
Popular Tags