2 * Created on Sep 28, 2005
4 * TODO To change the template for this generated file go to
5 * Window - Preferences - Java - Code Style - Code Templates
7 package diku
.distlab
.motecontrolclientlib
.client
;
9 import java
.util
.Observable
;
10 import java
.util
.Hashtable
;
11 import java
.util
.Collection
;
12 import java
.util
.Collections
;
13 import java
.util
.Observer
;
18 * TODO To change the template for this generated type comment go to
19 * Window - Preferences - Java - Code Style - Code Templates
21 public class MoteList
{
23 private Hashtable motes
;
24 protected ExposedObservable eventDispatcher
= new ExposedObservable();
26 protected class MoteListEvent
28 public static final byte ADDED
= 0;
29 public static final byte REMOVED
= 1;
30 public static final byte CLEARED
= 2;
35 MoteListEvent(byte value
, Mote mote
)
43 protected class ObserverWrapper
implements Observer
45 protected MoteListListener listener
;
46 protected MoteList moteList
;
48 public ObserverWrapper(MoteList moteList
, MoteListListener listener
)
51 this.listener
= listener
;
52 this.moteList
= moteList
;
55 public void update(Observable o
, Object arg
) {
56 MoteListEvent event
= (MoteListEvent
)arg
;
59 case MoteListEvent
.ADDED
:
60 this.listener
.addedMote(moteList
,event
.mote
);
62 case MoteListEvent
.REMOVED
:
63 this.listener
.removedMote(moteList
,event
.mote
);
65 case MoteListEvent
.CLEARED
:
66 this.listener
.cleared(moteList
);
75 motes
= new Hashtable();
78 protected void add( Mote mote
)
80 Long keyobj
= new Long(mote
.getId());
81 motes
.put(keyobj
,mote
);
82 fireEvent(MoteListEvent
.ADDED
,mote
);
85 protected Mote
remove(long key
)
87 Long keyobj
= new Long(key
);
88 Mote mote
= (Mote
)motes
.remove(keyobj
);
91 fireEvent(MoteListEvent
.REMOVED
,mote
);
96 protected void clear()
99 fireEvent(MoteListEvent
.CLEARED
,null);
102 protected void fireEvent(byte eventValue
,Mote mote
)
104 eventDispatcher
.setChanged();
105 eventDispatcher
.notifyObservers(new MoteListEvent(eventValue
,mote
));
108 public Mote
get(long key
)
110 return (Mote
)motes
.get(new Long(key
));
118 public Collection
getCollectionView()
120 return Collections
.unmodifiableCollection(motes
.values());
123 protected Collection
getCollection()
125 return motes
.values();
128 public void addListener(MoteListListener listener
)
130 eventDispatcher
.addWrappedObserver(listener
,new ObserverWrapper(this,listener
));
133 public void removeListener(MoteListListener listener
)
135 eventDispatcher
.deleteWrappedObserver(listener
);