1 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 public abstract class ListenerSet { 39 40 private Hashtable listeners; 42 43 public ListenerSet() { 44 listeners = new Hashtable(); 46 } 47 48 protected synchronized void addListener( Bundle bundle, Object listener ) { 50 Vector v = getListenersRegisteredBy( bundle, true ); 51 if ( v.indexOf( listener ) == -1 ) { 53 v.addElement( listener ); 54 } 55 } 56 57 protected synchronized void removeListener( Bundle bundle, Object listener ) { 59 getListenersRegisteredBy( bundle, true ).removeElement( listener ); 60 } 61 62 protected synchronized void removeAllListenersRegisteredBy( Bundle bundle ) { 64 listeners.remove( bundle ); 65 } 66 67 protected synchronized Enumeration getAllListeners() { 69 Enumeration enum = listeners.elements(); 70 Vector v; 71 Vector tmp = new Vector(); 73 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 protected synchronized Enumeration getBundles() { 85 return ( (Hashtable)listeners.clone() ).keys(); 86 } 87 88 protected synchronized Object[] getListenersRegisteredBy( Bundle bundle ) { 92 Vector v = getListenersRegisteredBy( bundle, false ); 94 if ( v == null ) { 96 v = new Vector(); 97 } 98 Object[] tmp = new Object[ v.size() ]; 100 v.copyInto( tmp ); 101 return tmp; 102 } 103 104 protected synchronized Vector getListenersRegisteredBy( Bundle bundle, boolean createIfNull ) { 115 if ( listeners.get( bundle ) == null && createIfNull ) { 118 listeners.put( bundle, new Vector() ); 119 } 120 return (Vector)listeners.get( bundle ); 122 } 123 124 } | Popular Tags |