KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import org.openide.util.RequestProcessor;
24
25 import org.openide.util.Utilities;
26
27 import org.netbeans.api.db.explorer.DatabaseException;
28 import org.netbeans.modules.db.explorer.DatabaseDriver;
29 import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
30 import org.netbeans.modules.db.explorer.DatabaseOption;
31 import org.netbeans.api.db.explorer.JDBCDriver;
32 import org.netbeans.api.db.explorer.JDBCDriverListener;
33 import org.netbeans.api.db.explorer.JDBCDriverManager;
34 import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
35 import org.netbeans.modules.db.explorer.nodes.RootNode;
36 import org.openide.ErrorManager;
37
38 public class DriverListNodeInfo extends DatabaseNodeInfo implements DriverOperations {
39     
40     static final long serialVersionUID =-7948529055260667590L;
41     
42     private JDBCDriverListener listener = new JDBCDriverListener() {
43         public void driversChanged() {
44             // fix for the deadlock in issue 69050: refresh in another thread
45
// refreshChildren() acquires Children.MUTEX write access
46
RequestProcessor.getDefault().post(new Runnable JavaDoc() {
47                 public void run() {
48                     try {
49                         refreshChildren();
50                     } catch (DatabaseException ex) {
51                         ErrorManager.getDefault().notify(ex);
52                     }
53                 }
54             });
55         }
56     };
57     
58     protected void initChildren(Vector JavaDoc children) throws DatabaseException {
59         JDBCDriver[] drvs = JDBCDriverManager.getDefault().getDrivers();
60         boolean win = Utilities.isWindows();
61         String JavaDoc file;
62         for (int i = 0; i < drvs.length; i++) {
63             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
64             for (int j = 0; j < drvs[i].getURLs().length; j++) {
65                 if (j != 0)
66                     sb.append(", "); //NOI18N
67
file = drvs[i].getURLs()[j].getFile();
68                 if (win)
69                     file = file.substring(1);
70                 sb.append(file);
71             }
72             DatabaseDriver drv = new DatabaseDriver(drvs[i].getDisplayName(), drvs[i].getClassName(), sb.toString(), drvs[i]);
73             DriverNodeInfo chinfo = (DriverNodeInfo) DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.DRIVER);
74             if (chinfo != null && drv != null) {
75                 chinfo.setDatabaseDriver(drv);
76                 children.add(chinfo);
77             }
78         }
79     }
80
81     /** Adds driver specified in drv into list.
82     * Creates new node info and adds node into node children.
83     */

84     public void addDriver(DatabaseDriver drv) throws DatabaseException {
85         DatabaseOption option = RootNode.getOption();
86         Vector JavaDoc drvs = option.getAvailableDrivers();
87         if (!drvs.contains(drv))
88             drvs.add(drv);
89         else {
90             String JavaDoc message = MessageFormat.format(bundle().getString("EXC_DriverAlreadyExists"), new String JavaDoc[] {drv.toString()}); // NOI18N
91
throw new DatabaseException(message);
92         }
93
94         DatabaseNodeChildren chld = (DatabaseNodeChildren)getNode().getChildren();
95         DriverNodeInfo ninfo = (DriverNodeInfo)createNodeInfo(this, DatabaseNodeInfo.DRIVER);
96         ninfo.setDatabaseDriver(drv);
97         chld.createSubnode(ninfo, true);
98     }
99     
100     public void setNode(DatabaseNode node) {
101         super.setNode(node);
102         JDBCDriverManager.getDefault().addDriverListener(listener);
103     }
104 }
105
Popular Tags