KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > NetworkConnectionManager


1 package net.suberic.pooka;
2
3 import java.util.*;
4
5 import net.suberic.util.*;
6
7 /**
8  * <p>An object which manages NetworkConnection resources.</p>
9  *
10  * @author Allen Petersen
11  * @version $Revision: 1296 $
12  */

13 public class NetworkConnectionManager implements ItemCreator, ItemListChangeListener {
14   
15   private ItemManager manager;
16   private LinkedList listenerList = new LinkedList();
17
18   /**
19    * <p>Creates a new NetworkConnectionManager.</p>
20    */

21   public NetworkConnectionManager() {
22     createConnectionList();
23   }
24
25   //-----------------------
26
// public interface.
27

28   /**
29    * This listens for ItemListChangeEvents, which result from changes to the
30    * "Connection" property. The result is that refreshConnections() is called,
31    * and then the event is passed to listeners to this object.
32    */

33   public void itemListChanged(ItemListChangeEvent e) {
34     fireItemListChanged(e);
35   }
36
37   /**
38    * This returns a Vector with all the currently registered NetworkConnection
39    * objects.
40    */

41   public java.util.Vector JavaDoc getConnectionList() {
42     return manager.getItems();
43   }
44   
45   /**
46    * This adds the connection with the given connectionName to the
47    * allConnections list.
48    */

49   public void addConnection(String JavaDoc connectionName) {
50     manager.addItem(connectionName);
51   }
52   
53   /**
54    * This adds the connections with the given connectionNames to the allConnections list.
55    */

56   public void addConnection(String JavaDoc[] connectionName) {
57     manager.addItem(connectionName);
58   }
59   
60   /**
61    * This removes the connection with the given connectionName.
62    */

63   public void removeConnection(String JavaDoc connectionName) {
64     manager.removeItem(connectionName);
65   }
66
67   /**
68    * This removes the connections with the given connectionNames.
69    */

70   public void removeConnection(String JavaDoc[] connectionNames) {
71     manager.removeItem(connectionNames);
72   }
73
74   /**
75    * This removes the given NetworkConnection.
76    */

77   public void removeConnection(NetworkConnection connection) {
78     manager.removeItem(connection);
79   }
80   
81   /**
82    * This removes the given NetworkConnections.
83    */

84   public void removeConnection(NetworkConnection[] connections) {
85     manager.removeItem(connections);
86   }
87
88   /**
89    * This returns the NetwordConnection with the given connectionName if it
90    * exists; otherwise, returns null.
91    */

92   public NetworkConnection getConnection(String JavaDoc connectionID) {
93     return (NetworkConnection) manager.getItem(connectionID);
94   }
95
96   /**
97    * This returns the NetwordConnection with the given connectionName if it
98    * exists; otherwise, returns null.
99    */

100   public NetworkConnection getDefaultConnection() {
101     String JavaDoc defaultName = Pooka.getProperty("Connection._default", "_default");
102     return (NetworkConnection) manager.getItem(defaultName);
103   }
104
105   /**
106    * This adds a ItemListChangeListener to the local listener list.
107    */

108   public void addItemListChangeListener(ItemListChangeListener ilcl) {
109     if (! listenerList.contains(ilcl))
110       listenerList.add(ilcl);
111   }
112   
113   /**
114    * This removes a ItemListChangeListener from the local listener list.
115    */

116   public void removeItemListChangeListener(ItemListChangeListener ilcl) {
117     listenerList.remove(ilcl);
118   }
119
120   /**
121    * This notifies all listeners that the ConnectionList has changed.
122    */

123   public void fireItemListChanged(ItemListChangeEvent e) {
124     for (int i = 0; i < listenerList.size(); i++)
125       ((ItemListChangeListener)listenerList.get(i)).itemListChanged(e);
126   }
127   
128
129   /**
130    * This creates a new NetworkConnection.
131    */

132   public Item createItem(VariableBundle sourceBundle, String JavaDoc resourceString, String JavaDoc itemID) {
133     NetworkConnection returnValue = new NetworkConnection(itemID);
134     returnValue.configure();
135     return returnValue;
136   }
137
138   //---------------------------
139
// the background stuff.
140

141   /**
142    * This loads and creates all the NetworkConnections using the "Connection"
143    * property of the main Pooka VariableBundle.
144    */

145   private void createConnectionList() {
146     manager = new ItemManager("Connection", Pooka.getResources(), this);
147     manager.addItemListChangeListener(this);
148   }
149 }
150
Popular Tags