001: package net.suberic.pooka;
002:
003: import java.util.*;
004:
005: import net.suberic.util.*;
006:
007: /**
008: * <p>An object which manages NetworkConnection resources.</p>
009: *
010: * @author Allen Petersen
011: * @version $Revision: 1.1 $
012: */
013: public class NetworkConnectionManager implements ItemCreator,
014: ItemListChangeListener {
015:
016: private ItemManager manager;
017: private LinkedList listenerList = new LinkedList();
018:
019: /**
020: * <p>Creates a new NetworkConnectionManager.</p>
021: */
022: public NetworkConnectionManager() {
023: createConnectionList();
024: }
025:
026: //-----------------------
027: // public interface.
028:
029: /**
030: * This listens for ItemListChangeEvents, which result from changes to the
031: * "Connection" property. The result is that refreshConnections() is called,
032: * and then the event is passed to listeners to this object.
033: */
034: public void itemListChanged(ItemListChangeEvent e) {
035: fireItemListChanged(e);
036: }
037:
038: /**
039: * This returns a Vector with all the currently registered NetworkConnection
040: * objects.
041: */
042: public java.util.Vector getConnectionList() {
043: return manager.getItems();
044: }
045:
046: /**
047: * This adds the connection with the given connectionName to the
048: * allConnections list.
049: */
050: public void addConnection(String connectionName) {
051: manager.addItem(connectionName);
052: }
053:
054: /**
055: * This adds the connections with the given connectionNames to the allConnections list.
056: */
057: public void addConnection(String[] connectionName) {
058: manager.addItem(connectionName);
059: }
060:
061: /**
062: * This removes the connection with the given connectionName.
063: */
064: public void removeConnection(String connectionName) {
065: manager.removeItem(connectionName);
066: }
067:
068: /**
069: * This removes the connections with the given connectionNames.
070: */
071: public void removeConnection(String[] connectionNames) {
072: manager.removeItem(connectionNames);
073: }
074:
075: /**
076: * This removes the given NetworkConnection.
077: */
078: public void removeConnection(NetworkConnection connection) {
079: manager.removeItem(connection);
080: }
081:
082: /**
083: * This removes the given NetworkConnections.
084: */
085: public void removeConnection(NetworkConnection[] connections) {
086: manager.removeItem(connections);
087: }
088:
089: /**
090: * This returns the NetwordConnection with the given connectionName if it
091: * exists; otherwise, returns null.
092: */
093: public NetworkConnection getConnection(String connectionID) {
094: return (NetworkConnection) manager.getItem(connectionID);
095: }
096:
097: /**
098: * This returns the NetwordConnection with the given connectionName if it
099: * exists; otherwise, returns null.
100: */
101: public NetworkConnection getDefaultConnection() {
102: String defaultName = Pooka.getProperty("Connection._default",
103: "_default");
104: return (NetworkConnection) manager.getItem(defaultName);
105: }
106:
107: /**
108: * This adds a ItemListChangeListener to the local listener list.
109: */
110: public void addItemListChangeListener(ItemListChangeListener ilcl) {
111: if (!listenerList.contains(ilcl))
112: listenerList.add(ilcl);
113: }
114:
115: /**
116: * This removes a ItemListChangeListener from the local listener list.
117: */
118: public void removeItemListChangeListener(ItemListChangeListener ilcl) {
119: listenerList.remove(ilcl);
120: }
121:
122: /**
123: * This notifies all listeners that the ConnectionList has changed.
124: */
125: public void fireItemListChanged(ItemListChangeEvent e) {
126: for (int i = 0; i < listenerList.size(); i++)
127: ((ItemListChangeListener) listenerList.get(i))
128: .itemListChanged(e);
129: }
130:
131: /**
132: * This creates a new NetworkConnection.
133: */
134: public Item createItem(VariableBundle sourceBundle,
135: String resourceString, String itemID) {
136: NetworkConnection returnValue = new NetworkConnection(itemID);
137: returnValue.configure();
138: return returnValue;
139: }
140:
141: //---------------------------
142: // the background stuff.
143:
144: /**
145: * This loads and creates all the NetworkConnections using the "Connection"
146: * property of the main Pooka VariableBundle.
147: */
148: private void createConnectionList() {
149: manager = new ItemManager("Connection", Pooka.getResources(),
150: this);
151: manager.addItemListChangeListener(this);
152: }
153: }
|