KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > ConnectionList


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;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
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 /**
40  * Class which encapsulates the Vector used to keep a list of connections
41  * in the Database Explorer. It also can fire events when connections are added
42  * and removed through ConnectionListener.
43  *
44  * This class only maintains a list of DBConnection objects. It has no links
45  * to the UI (nodes representing these objects), therefore adding a DBConnection
46  * doesn't create a node for it.
47  *
48  * @author Andrei Badea
49  */

50 public class ConnectionList {
51     
52     private static ConnectionList DEFAULT;
53     
54     private Lookup.Result result = getLookupResult();
55     
56     private List JavaDoc/*<ConnectionListener>*/ listeners = new ArrayList JavaDoc(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 JavaDoc 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 JavaDoc();
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 JavaDoc();
96         }
97         try {
98             DatabaseConnectionConvertor.create(dbconn);
99         } catch (IOException JavaDoc 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 JavaDoc();
111         }
112         try {
113             DatabaseConnectionConvertor.remove(dbconn);
114         } catch (IOException JavaDoc 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 JavaDoc listenersCopy;
133         
134         synchronized (listeners) {
135             listenersCopy = new ArrayList JavaDoc(listeners);
136         }
137         
138         for (Iterator JavaDoc 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