1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.mortbay.component;
16 import java.util.EventListener;
17
18 import org.mortbay.log.Log;
19 import org.mortbay.util.LazyList;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class Container
43 {
44 private Object _listeners;
45
46 public synchronized void addEventListener(Container.Listener listener)
47 {
48 _listeners=LazyList.add(_listeners,listener);
49 }
50
51 public synchronized void removeEventListener(Container.Listener listener)
52 {
53 _listeners=LazyList.remove(_listeners,listener);
54 }
55
56
57
58
59
60
61
62
63 public synchronized void update(Object parent, Object oldChild, final Object child, String relationship)
64 {
65 if (oldChild!=null && !oldChild.equals(child))
66 remove(parent,oldChild,relationship);
67 if (child!=null && !child.equals(oldChild))
68 add(parent,child,relationship);
69 }
70
71
72
73
74
75
76
77
78
79 public synchronized void update(Object parent, Object oldChild, final Object child, String relationship,boolean addRemove)
80 {
81 if (oldChild!=null && !oldChild.equals(child))
82 {
83 remove(parent,oldChild,relationship);
84 if (addRemove)
85 removeBean(oldChild);
86 }
87
88 if (child!=null && !child.equals(oldChild))
89 {
90 if (addRemove)
91 addBean(child);
92 add(parent,child,relationship);
93 }
94 }
95
96
97
98
99
100
101
102
103
104 public synchronized void update(Object parent, Object[] oldChildren, final Object[] children, String relationship)
105 {
106 update(parent,oldChildren,children,relationship,false);
107 }
108
109
110
111
112
113
114
115
116
117
118 public synchronized void update(Object parent, Object[] oldChildren, final Object[] children, String relationship, boolean addRemove)
119 {
120 Object[] newChildren = null;
121 if (children!=null)
122 {
123 newChildren = new Object[children.length];
124
125 for (int i=children.length;i-->0;)
126 {
127 boolean new_child=true;
128 if (oldChildren!=null)
129 {
130 for (int j=oldChildren.length;j-->0;)
131 {
132 if (children[i]!=null && children[i].equals(oldChildren[j]))
133 {
134 oldChildren[j]=null;
135 new_child=false;
136 }
137 }
138 }
139 if (new_child)
140 newChildren[i]=children[i];
141 }
142 }
143
144 if (oldChildren!=null)
145 {
146 for (int i=oldChildren.length;i-->0;)
147 {
148 if (oldChildren[i]!=null)
149 {
150 remove(parent,oldChildren[i],relationship);
151 if (addRemove)
152 removeBean(oldChildren[i]);
153 }
154 }
155 }
156
157 if (newChildren!=null)
158 {
159 for (int i=0;i<newChildren.length;i++)
160 if (newChildren[i]!=null)
161 {
162 if (addRemove)
163 addBean(newChildren[i]);
164 add(parent,newChildren[i],relationship);
165 }
166 }
167 }
168
169
170 public void addBean(Object obj)
171 {
172 if (_listeners!=null)
173 {
174 for (int i=0; i<LazyList.size(_listeners); i++)
175 {
176 Listener listener=(Listener)LazyList.get(_listeners, i);
177 listener.addBean(obj);
178 }
179 }
180 }
181
182
183 public void removeBean(Object obj)
184 {
185 if (_listeners!=null)
186 {
187 for (int i=0; i<LazyList.size(_listeners); i++)
188 ((Listener)LazyList.get(_listeners, i)).removeBean(obj);
189 }
190 }
191
192
193
194
195
196
197
198 private void add(Object parent, Object child, String relationship)
199 {
200 if (Log.isDebugEnabled())
201 Log.debug("Container "+parent+" + "+child+" as "+relationship);
202 if (_listeners!=null)
203 {
204 Relationship event=new Relationship(this,parent,child,relationship);
205 for (int i=0; i<LazyList.size(_listeners); i++)
206 ((Listener)LazyList.get(_listeners, i)).add(event);
207 }
208 }
209
210
211
212
213
214
215
216 private void remove(Object parent, Object child, String relationship)
217 {
218 if (Log.isDebugEnabled())
219 Log.debug("Container "+parent+" - "+child+" as "+relationship);
220 if (_listeners!=null)
221 {
222 Relationship event=new Relationship(this,parent,child,relationship);
223 for (int i=0; i<LazyList.size(_listeners); i++)
224 ((Listener)LazyList.get(_listeners, i)).remove(event);
225 }
226 }
227
228
229
230
231
232
233 public static class Relationship
234 {
235 private Object _parent;
236 private Object _child;
237 private String _relationship;
238 private Container _container;
239
240 private Relationship(Container container, Object parent,Object child, String relationship)
241 {
242 _container=container;
243 _parent=parent;
244 _child=child;
245 _relationship=relationship;
246 }
247
248 public Container getContainer()
249 {
250 return _container;
251 }
252
253 public Object getChild()
254 {
255 return _child;
256 }
257
258 public Object getParent()
259 {
260 return _parent;
261 }
262
263 public String getRelationship()
264 {
265 return _relationship;
266 }
267
268 public String toString()
269 {
270 return _parent+"---"+_relationship+"-->"+_child;
271 }
272
273 public int hashCode()
274 {
275 return _parent.hashCode()+_child.hashCode()+_relationship.hashCode();
276 }
277
278 public boolean equals(Object o)
279 {
280 if (o==null || !(o instanceof Relationship))
281 return false;
282 Relationship r = (Relationship)o;
283 return r._parent==_parent && r._child==_child && r._relationship.equals(_relationship);
284 }
285 }
286
287
288
289
290
291 public interface Listener extends EventListener
292 {
293 public void addBean(Object bean);
294 public void removeBean(Object bean);
295 public void add(Container.Relationship relationship);
296 public void remove(Container.Relationship relationship);
297
298 }
299 }