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.Vector;
045: import java.text.MessageFormat;
046: import org.openide.util.RequestProcessor;
047:
048: import org.openide.util.Utilities;
049:
050: import org.netbeans.api.db.explorer.DatabaseException;
051: import org.netbeans.modules.db.explorer.DatabaseDriver;
052: import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
053: import org.netbeans.modules.db.explorer.DatabaseOption;
054: import org.netbeans.api.db.explorer.JDBCDriver;
055: import org.netbeans.api.db.explorer.JDBCDriverListener;
056: import org.netbeans.api.db.explorer.JDBCDriverManager;
057: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
058: import org.netbeans.modules.db.explorer.nodes.RootNode;
059: import org.openide.util.Exceptions;
060:
061: public class DriverListNodeInfo extends DatabaseNodeInfo implements
062: DriverOperations {
063:
064: static final long serialVersionUID = -7948529055260667590L;
065:
066: private JDBCDriverListener listener = new JDBCDriverListener() {
067: public void driversChanged() {
068: // fix for the deadlock in issue 69050: refresh in another thread
069: // refreshChildren() acquires Children.MUTEX write access
070: RequestProcessor.getDefault().post(new Runnable() {
071: public void run() {
072: try {
073: refreshChildren();
074: } catch (DatabaseException ex) {
075: Exceptions.printStackTrace(ex);
076: }
077: }
078: });
079: }
080: };
081:
082: protected void initChildren(Vector children)
083: throws DatabaseException {
084: JDBCDriver[] drvs = JDBCDriverManager.getDefault().getDrivers();
085: boolean win = Utilities.isWindows();
086: String file;
087: for (int i = 0; i < drvs.length; i++) {
088: StringBuffer sb = new StringBuffer();
089: for (int j = 0; j < drvs[i].getURLs().length; j++) {
090: if (j != 0)
091: sb.append(", "); //NOI18N
092: file = drvs[i].getURLs()[j].getFile();
093: if (win)
094: file = file.substring(1);
095: sb.append(file);
096: }
097: DatabaseDriver drv = new DatabaseDriver(drvs[i]
098: .getDisplayName(), drvs[i].getClassName(), sb
099: .toString(), drvs[i]);
100: DriverNodeInfo chinfo = (DriverNodeInfo) DatabaseNodeInfo
101: .createNodeInfo(this , DatabaseNode.DRIVER);
102: if (chinfo != null && drv != null) {
103: chinfo.setDatabaseDriver(drv);
104: children.add(chinfo);
105: }
106: }
107: }
108:
109: /** Adds driver specified in drv into list.
110: * Creates new node info and adds node into node children.
111: */
112: public void addDriver(DatabaseDriver drv) throws DatabaseException {
113: DatabaseOption option = RootNode.getOption();
114: Vector drvs = option.getAvailableDrivers();
115: if (!drvs.contains(drv))
116: drvs.add(drv);
117: else {
118: String message = MessageFormat.format(bundle().getString(
119: "EXC_DriverAlreadyExists"), new String[] { drv
120: .toString() }); // NOI18N
121: throw new DatabaseException(message);
122: }
123:
124: DatabaseNodeChildren chld = (DatabaseNodeChildren) getNode()
125: .getChildren();
126: DriverNodeInfo ninfo = (DriverNodeInfo) createNodeInfo(this ,
127: DatabaseNodeInfo.DRIVER);
128: ninfo.setDatabaseDriver(drv);
129: chld.createSubnode(ninfo, true);
130: }
131:
132: public void setNode(DatabaseNode node) {
133: super.setNode(node);
134: JDBCDriverManager.getDefault().addDriverListener(listener);
135: }
136: }
|