001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.infos;
043:
044: import java.util.*;
045: import javax.swing.Action;
046: import javax.swing.event.ChangeEvent;
047: import javax.swing.event.ChangeListener;
048: import org.openide.filesystems.*;
049: import org.openide.loaders.DataFolder;
050: import org.openide.loaders.DataObject;
051:
052: import org.netbeans.lib.ddl.*;
053: import org.netbeans.api.db.explorer.DatabaseException;
054: import org.netbeans.modules.db.explorer.DatabaseConnection;
055: import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
056: import org.netbeans.modules.db.explorer.ConnectionList;
057: import org.netbeans.modules.db.explorer.DbActionLoaderSupport;
058: import org.netbeans.modules.db.explorer.DbNodeLoader;
059: import org.netbeans.modules.db.explorer.DbNodeLoaderSupport;
060: import org.netbeans.modules.db.explorer.nodes.*;
061: import org.openide.nodes.Children;
062: import org.openide.nodes.Node;
063: import org.openide.util.Exceptions;
064:
065: public class RootNodeInfo extends DatabaseNodeInfo implements
066: ConnectionOwnerOperations, ChangeListener {
067: static final long serialVersionUID = -8079386805046070315L;
068:
069: static RootNodeInfo rootInfo = null;
070:
071: private Collection<DbNodeLoader> nodeLoaders;
072:
073: public static RootNodeInfo getInstance() throws DatabaseException {
074: if (rootInfo == null) {
075: rootInfo = (RootNodeInfo) DatabaseNodeInfo.createNodeInfo(
076: null, "root"); //NOI18N
077: }
078: return rootInfo;
079: }
080:
081: public void initChildren(Vector children) throws DatabaseException {
082: try {
083: children.addAll(getRegisteredNodes());
084:
085: DatabaseConnection[] cinfos = ConnectionList.getDefault()
086: .getConnections();
087: for (int i = 0; i < cinfos.length; i++) {
088: DatabaseConnection cinfo = cinfos[i];
089: ConnectionNodeInfo ninfo = createConnectionNodeInfo(cinfo);
090: children.add(ninfo);
091: }
092:
093: Repository r = Repository.getDefault();
094: FileSystem rfs = r.getDefaultFileSystem();
095: FileObject rootFolder = rfs.getRoot();
096: FileObject databaseFileObject = rootFolder
097: .getFileObject("Database"); //NOI18N
098: if (databaseFileObject != null) {
099: FileObject adaptorsFolder = databaseFileObject
100: .getFileObject("Adaptors"); //NOI18N
101: DataObject dbdo = DataFolder.findFolder(adaptorsFolder);
102: if (dbdo != null)
103: children.add(dbdo.getNodeDelegate());
104: }
105:
106: } catch (Exception e) {
107: throw new DatabaseException(e);
108: }
109: }
110:
111: private List<Node> getRegisteredNodes() {
112: boolean registerListener = false;
113: if (nodeLoaders == null) {
114: nodeLoaders = DbNodeLoaderSupport.getLoaders();
115: registerListener = true;
116: }
117:
118: ArrayList<Node> nodes = new ArrayList<Node>();
119:
120: for (DbNodeLoader loader : nodeLoaders) {
121: if (registerListener) {
122: loader.addChangeListener(this );
123: }
124: nodes.addAll(loader.getAllNodes());
125: }
126:
127: return nodes;
128: }
129:
130: @Override
131: @SuppressWarnings("checked")
132: public Vector getActions() {
133: Vector<Action> actions = super .getActions();
134:
135: List<Action> loadedActions = DbActionLoaderSupport
136: .getAllActions();
137:
138: Vector<Action> allActions = new Vector<Action>();
139:
140: // TODO - it would be nice to enable ordering of actions, but this
141: // is going to require some thought. For now, put the actions in
142: // just before the divider
143: for (Action action : actions) {
144: if (action == null) {
145: allActions.addAll(loadedActions);
146: }
147:
148: allActions.add(action);
149: }
150:
151: return allActions;
152: }
153:
154: private ConnectionNodeInfo createConnectionNodeInfo(
155: DatabaseConnection dbconn) throws DatabaseException {
156: ConnectionNodeInfo ninfo = (ConnectionNodeInfo) createNodeInfo(
157: this , DatabaseNode.CONNECTION);
158: ninfo.setUser(dbconn.getUser());
159: ninfo.setDatabase(dbconn.getDatabase());
160: ninfo.setSchema(dbconn.getSchema());
161: ninfo.setName(dbconn.getName());
162: ninfo.setDatabaseConnection(dbconn);
163: return ninfo;
164: }
165:
166: public void refreshChildren() throws DatabaseException {
167: // refresh action is empty
168: }
169:
170: private void updateRegisteredNodes() throws DatabaseException {
171: getChildren();
172:
173: final DatabaseNodeChildren children = (DatabaseNodeChildren) getNode()
174: .getChildren();
175:
176: Node[] nodes = children.getNodes();
177:
178: final List<Node> newNodes = getRegisteredNodes();
179:
180: // Now add the non-registered nodes that currently exist
181: for (Node node : nodes) {
182: if (node instanceof DatabaseNode) {
183: newNodes.add(node);
184: }
185: }
186:
187: children.replaceNodes(newNodes.toArray(new Node[0]));
188: }
189:
190: private void postUpdateChildren(
191: final DatabaseNodeChildren children, final Node[] newNodes) {
192: // Replace the node list with the new one
193: Children.MUTEX.postWriteRequest(new Runnable() {
194: public void run() {
195: // remove current sub-tree
196: children.remove(children.getNodes());
197:
198: // add built sub-tree
199: children.add(newNodes);
200: }
201: });
202: }
203:
204: public void addConnectionNoConnect(DatabaseConnection dbconn)
205: throws DatabaseException {
206: getChildren(); // force restore
207:
208: if (ConnectionList.getDefault().contains(dbconn)) {
209: return;
210: }
211:
212: DatabaseNode node = getNode();
213: DatabaseNodeChildren children = (DatabaseNodeChildren) node
214: .getChildren();
215: ConnectionNodeInfo ninfo = createConnectionNodeInfo(dbconn);
216: ConnectionList.getDefault().add(dbconn);
217: children.createSubnode(ninfo, true);
218: }
219:
220: public void addConnection(DBConnection cinfo)
221: throws DatabaseException {
222: DatabaseConnection dbconn = (DatabaseConnection) cinfo;
223: getChildren(); // force restore
224:
225: if (ConnectionList.getDefault().contains(dbconn)) {
226: throw new DatabaseException(bundle().getString(
227: "EXC_ConnectionAlreadyExists"));
228: }
229:
230: DatabaseNode node = getNode();
231: DatabaseNodeChildren children = (DatabaseNodeChildren) node
232: .getChildren();
233:
234: // the nodes have to be initialized too, otherwise the node created
235: // for the new connection will not be added and the connection
236: // will be lost when the nodes are eventually initialized
237: children.getNodes(true);
238:
239: ConnectionNodeInfo ninfo = createConnectionNodeInfo(dbconn);
240: ConnectionList.getDefault().add(dbconn);
241: DatabaseNode cnode = children.createSubnode(ninfo, true);
242:
243: if (((DatabaseConnection) dbconn).getConnection() == null)
244: ((ConnectionNodeInfo) cnode.getInfo()).connect();
245: else
246: ((ConnectionNodeInfo) cnode.getInfo()).connect(dbconn);
247: }
248:
249: public void stateChanged(ChangeEvent evt) {
250: // One of the node loader's underlying nodes have changed, so let's
251: // do a refresh of our nodes
252: try {
253: updateRegisteredNodes();
254: } catch (DatabaseException dbe) {
255: Exceptions.printStackTrace(dbe);
256: }
257: }
258:
259: }
|