KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > ListenerSet


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube;
28
29 import org.osgi.framework.Bundle;
30
31 import java.util.Hashtable;
32 import java.util.Vector;
33 import java.util.Enumeration;
34
35 // A utility class used to keep a mapping between bundles that register event listeners
36
// and the event listeners that are registered.
37
// Bundles are responsible for registering and unregistering their listeners.
38
public abstract class ListenerSet {
39
40    // Hashtable of listeners: registering bundle --> vector of listeners
41
private Hashtable listeners;
42
43    public ListenerSet() {
44       // Create the hashtable of listeners.
45
listeners = new Hashtable();
46    }
47
48    // Add a listener on behalf of a bundle.
49
protected synchronized void addListener( Bundle bundle, Object listener ) {
50       Vector v = getListenersRegisteredBy( bundle, true );
51       // Do not register listener multiple times
52
if ( v.indexOf( listener ) == -1 ) {
53          v.addElement( listener );
54       }
55    }
56
57    // Remove a listener of on behalf of a bundle.
58
protected synchronized void removeListener( Bundle bundle, Object listener ) {
59       getListenersRegisteredBy( bundle, true ).removeElement( listener );
60    }
61
62    // Remove all listeners registered by the specified bundle.
63
protected synchronized void removeAllListenersRegisteredBy( Bundle bundle ) {
64       listeners.remove( bundle );
65    }
66
67    // Return a list of all the listeners registered registered.
68
protected synchronized Enumeration getAllListeners() {
69       Enumeration enum = listeners.elements();
70       Vector v;
71       // Create a vector to hold the list of listeners.
72
Vector tmp = new Vector();
73       // For each event source, add each of its listeners to the list of listeners.
74
while ( enum.hasMoreElements() ) {
75          v = (Vector)enum.nextElement();
76          for ( int i = 0; i < v.size(); i++ ) {
77             tmp.addElement( v.elementAt( i ) );
78          }
79       }
80       return tmp.elements();
81    }
82
83    // Return a list of all the bundles that have registered listeners.
84
protected synchronized Enumeration getBundles() {
85       return ( (Hashtable)listeners.clone() ).keys();
86    }
87
88    // Return a list of all the listeners registered by the specified bundle.
89
// If no listener has been registered by the specified bundle, return an empty
90
// list.
91
protected synchronized Object[] getListenersRegisteredBy( Bundle bundle ) {
92       // Get the list of listeners registered by the given bundle.
93
Vector v = getListenersRegisteredBy( bundle, false );
94       // If the list of listeners is null, create it.
95
if ( v == null ) {
96          v = new Vector();
97       }
98       // Return the list elements in the form of an array.
99
Object[] tmp = new Object[ v.size() ];
100       v.copyInto( tmp );
101       return tmp;
102    }
103
104    // Return a list of all the listeners registered by a specified bundle.
105
//
106
// If no listener has been registered by the the specified bundle, then:
107
// if createIfNull is false, return null
108
// if createIfNull is true, create a new list of listeners for the bundle and return
109
// that empty list
110
//
111
// This method is for the private use of this class; the listeners registered by a given
112
// bundle can be obtained by other classes by calling the other getListenersRegisteredBy
113
// method, which takes a single argument (the bundle).
114
protected synchronized Vector getListenersRegisteredBy( Bundle bundle, boolean createIfNull ) {
115       // If no listener list exists for the bundle and if createIfNull is true, create
116
// a new list of listeners for the given bundle.
117
if ( listeners.get( bundle ) == null && createIfNull ) {
118          listeners.put( bundle, new Vector() );
119       }
120       // Return the list of listeners registered by the bundle, or null if one does not exist.
121
return (Vector)listeners.get( bundle );
122    }
123
124 }
Popular Tags