1 19 20 package org.netbeans.modules.db.explorer.infos; 21 import java.util.Vector ; 22 import java.text.MessageFormat ; 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 RequestProcessor.getDefault().post(new Runnable () { 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 children) throws DatabaseException { 59 JDBCDriver[] drvs = JDBCDriverManager.getDefault().getDrivers(); 60 boolean win = Utilities.isWindows(); 61 String file; 62 for (int i = 0; i < drvs.length; i++) { 63 StringBuffer sb = new StringBuffer (); 64 for (int j = 0; j < drvs[i].getURLs().length; j++) { 65 if (j != 0) 66 sb.append(", "); 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 84 public void addDriver(DatabaseDriver drv) throws DatabaseException { 85 DatabaseOption option = RootNode.getOption(); 86 Vector drvs = option.getAvailableDrivers(); 87 if (!drvs.contains(drv)) 88 drvs.add(drv); 89 else { 90 String message = MessageFormat.format(bundle().getString("EXC_DriverAlreadyExists"), new String [] {drv.toString()}); 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 |