1 /*
2 * Created on Oct 27, 2003
3 *
4 */
5 package portaview.model;
6
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Observable;
10 import java.util.Vector;
11
12 /***
13 * Generic collection class that can be observed. One needs to use this class
14 * if there is a collection of items that they want to observe when added or
15 * removed from this collection.
16 *
17 * @author William Lee
18 */
19 public class ObservableCollection extends Observable
20 {
21 private List list = new Vector();
22
23 /***
24 * Returns the iterator for this collection.
25 * @return iterator for the collection.
26 */
27 public Iterator iterator()
28 {
29 return list.iterator();
30 }
31
32 /***
33 * Clear the collection.
34 */
35 public void clear()
36 {
37 list.clear();
38 setChanged();
39 notifyObservers();
40 }
41
42 /***
43 * Can be called externally to change notify the observers of the observables.
44 */
45 public void notifyChange()
46 {
47 setChanged();
48 notifyObservers();
49 }
50
51 /***
52 * Notify the children's observers. If the instance inside this
53 * collection extends the Observable class, notify the change also.
54 */
55 public void notifyChildrenObservers()
56 {
57 for (Iterator i = list.iterator(); i.hasNext();)
58 {
59 Object obj = i.next();
60 if (obj instanceof Observable)
61 {
62 Observable obs = (Observable)obj;
63 obs.notifyObservers();
64 }
65
66 }
67 }
68
69 /***
70 * Add all the element in col to this collecion.
71 * @param col collection to add to this collection.
72 */
73 public void addAll(ObservableCollection col)
74 {
75 for (Iterator i = col.iterator(); i.hasNext();)
76 {
77 Object e = (Object) i.next();
78 list.add(e);
79 }
80 setChanged();
81 notifyObservers();
82 }
83
84 /***
85 * Returns the size of this collection.
86 * @return int representing the size.
87 */
88 public int size()
89 {
90 return list.size();
91 }
92
93 /***
94 * Removes an object and notify the observing observers. This will fire the event.
95 * @param obj object within this collection to remove.
96 */
97 public void remove(Object obj)
98 {
99 list.remove(obj);
100 // if we remove this, then we want to notify the observers
101 setChanged();
102 notifyObservers();
103 }
104
105 /***
106 * Adds an object to this collection. This will fire the event.
107 * @param obj object to add.
108 */
109 public void add(Object obj)
110 {
111 list.add(obj);
112 // if we add this, then we want to notify the observers
113 setChanged();
114 notifyObservers();
115 }
116 }
This page was automatically generated by Maven