1 19 20 package org.netbeans.modules.db.explorer; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import org.netbeans.api.db.explorer.ConnectionListener; 28 import org.netbeans.api.db.explorer.DatabaseException; 29 import org.netbeans.modules.db.explorer.nodes.RootNode; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.Repository; 32 import org.openide.loaders.DataFolder; 33 import org.openide.loaders.FolderLookup; 34 import org.openide.util.Lookup; 35 import org.openide.util.LookupEvent; 36 import org.openide.util.LookupListener; 37 38 39 50 public class ConnectionList { 51 52 private static ConnectionList DEFAULT; 53 54 private Lookup.Result result = getLookupResult(); 55 56 private List listeners = new ArrayList (1); 57 58 public static synchronized ConnectionList getDefault() { 59 if (DEFAULT == null) { 60 DatabaseConnectionConvertor.importOldConnections(); 61 RootNode.getOption().save(); 62 DEFAULT = new ConnectionList(); 63 } 64 return DEFAULT; 65 } 66 67 private ConnectionList() { 68 result.addLookupListener(new LookupListener() { 69 public void resultChanged(LookupEvent e) { 70 fireListeners(); 71 } 72 }); 73 } 74 75 public DatabaseConnection[] getConnections() { 76 Collection dbconns = result.allInstances(); 77 return (DatabaseConnection[])dbconns.toArray(new DatabaseConnection[dbconns.size()]); 78 } 79 80 public DatabaseConnection getConnection(DatabaseConnection impl) { 81 if (impl == null) { 82 throw new NullPointerException (); 83 } 84 DatabaseConnection[] dbconns = getConnections(); 85 for (int i = 0; i < dbconns.length; i++) { 86 if (impl.equals(dbconns[i])) { 87 return dbconns[i]; 88 } 89 } 90 return null; 91 } 92 93 public void add(DatabaseConnection dbconn) throws DatabaseException { 94 if (dbconn == null) { 95 throw new NullPointerException (); 96 } 97 try { 98 DatabaseConnectionConvertor.create(dbconn); 99 } catch (IOException e) { 100 throw new DatabaseException(e); 101 } 102 } 103 104 public boolean contains(DatabaseConnection dbconn) { 105 return getConnection(dbconn) != null; 106 } 107 108 public void remove(DatabaseConnection dbconn) throws DatabaseException { 109 if (dbconn == null) { 110 throw new NullPointerException (); 111 } 112 try { 113 DatabaseConnectionConvertor.remove(dbconn); 114 } catch (IOException e) { 115 throw new DatabaseException(e); 116 } 117 } 118 119 public void addConnectionListener(ConnectionListener listener) { 120 synchronized (listeners) { 121 listeners.add(listener); 122 } 123 } 124 125 public void removeConnectionListener(ConnectionListener listener) { 126 synchronized (listeners) { 127 listeners.remove(listener); 128 } 129 } 130 131 private void fireListeners() { 132 List listenersCopy; 133 134 synchronized (listeners) { 135 listenersCopy = new ArrayList (listeners); 136 } 137 138 for (Iterator i = listenersCopy.iterator(); i.hasNext();) { 139 ConnectionListener l = (ConnectionListener)i.next(); 140 l.connectionsChanged(); 141 } 142 } 143 144 private synchronized Lookup.Result getLookupResult() { 145 if (result == null) { 146 FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(DatabaseConnectionConvertor.CONNECTIONS_PATH); 147 DataFolder folder = DataFolder.findFolder(fo); 148 result = new FolderLookup(folder).getLookup().lookup(new Lookup.Template(DatabaseConnection.class)); 149 } 150 return result; 151 } 152 } 153 | Popular Tags |