Remove TODO file
[remote/remote-gui.git] / src / motecontrol / client / MoteList.java
blob8460b057ce6e2e296b1ec37725d10feafb296674
1 /*
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
6 */
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;
15 /**
16 * @author zept
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;
32 public byte value;
33 public Mote mote;
35 MoteListEvent(byte value, Mote mote)
37 this.value = value;
38 this.mote = mote;
43 protected class ObserverWrapper implements Observer
45 protected MoteListListener listener;
46 protected MoteList moteList;
48 public ObserverWrapper(MoteList moteList, MoteListListener listener)
50 super();
51 this.listener = listener;
52 this.moteList = moteList;
55 public void update(Observable o, Object arg) {
56 MoteListEvent event = (MoteListEvent)arg;
57 switch (event.value)
59 case MoteListEvent.ADDED:
60 this.listener.addedMote(moteList,event.mote);
61 break;
62 case MoteListEvent.REMOVED:
63 this.listener.removedMote(moteList,event.mote);
64 break;
65 case MoteListEvent.CLEARED:
66 this.listener.cleared(moteList);
67 break;
73 protected 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);
89 if (mote!=null)
91 fireEvent(MoteListEvent.REMOVED,mote);
93 return mote;
96 protected void clear()
98 motes.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));
113 public int length()
115 return motes.size();
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);