KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > notify > impl > BasicNotifierImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: BasicNotifierImpl.java,v 1.3 2005/06/08 06:19:08 nickb Exp $
16  */

17 package org.eclipse.emf.common.notify.impl;
18
19
20 import java.util.Collection JavaDoc;
21
22 import org.eclipse.emf.common.notify.Adapter;
23 import org.eclipse.emf.common.notify.Notification;
24 import org.eclipse.emf.common.notify.Notifier;
25 import org.eclipse.emf.common.util.BasicEList;
26 import org.eclipse.emf.common.util.ECollections;
27 import org.eclipse.emf.common.util.EList;
28
29
30 /**
31  * An extensible notifier implementation.
32  */

33 public class BasicNotifierImpl implements Notifier
34 {
35   /**
36    * Creates a blank new instance.
37    */

38   public BasicNotifierImpl()
39   {
40   }
41
42   public static class EAdapterList extends BasicEList
43   {
44     protected Notifier notifier;
45
46     public EAdapterList(Notifier notifier)
47     {
48       this.notifier = notifier;
49     }
50
51     protected boolean safe;
52
53     protected boolean canContainNull()
54     {
55       return false;
56     }
57
58     protected boolean useEquals()
59     {
60       return false;
61     }
62
63     protected Object JavaDoc [] newData(int capacity)
64     {
65       return new Adapter [capacity];
66     }
67
68     protected void didAdd(int index, Object JavaDoc newObject)
69     {
70       Adapter adapter = (Adapter)newObject;
71       adapter.setTarget(notifier);
72     }
73
74     protected void didRemove(int index, Object JavaDoc oldObject)
75     {
76       Adapter adapter = (Adapter)oldObject;
77       if (notifier.eDeliver())
78       {
79         Notification notification =
80           new NotificationImpl(Notification.REMOVING_ADAPTER, oldObject, null, index)
81           {
82             public Object JavaDoc getNotifier()
83             {
84               return notifier;
85             }
86           };
87         adapter.notifyChanged(notification);
88       }
89       if (adapter instanceof Adapter.Internal)
90       {
91         ((Adapter.Internal)adapter).unsetTarget(notifier);
92       }
93       else if (adapter.getTarget() == notifier)
94       {
95         adapter.setTarget(null);
96       }
97     }
98
99     public Object JavaDoc [] data()
100     {
101       safe = true;
102       return (Adapter [])data;
103     }
104
105     protected void ensureSafety()
106     {
107       if (safe && data != null)
108       {
109         Object JavaDoc [] oldData = data;
110         data = newData(data.length);
111         System.arraycopy(oldData, 0, data, 0, size);
112         safe = false;
113       }
114     }
115
116     public boolean add(Object JavaDoc object)
117     {
118       ensureSafety();
119       return super.add(object);
120     }
121
122     public void add(int index, Object JavaDoc object)
123     {
124       ensureSafety();
125       super.add(index, object);
126     }
127
128     public boolean addAll(Collection JavaDoc collection)
129     {
130       ensureSafety();
131       return super.addAll(collection);
132     }
133
134     public boolean remove(Object JavaDoc object)
135     {
136       ensureSafety();
137       return super.remove(object);
138     }
139
140     public Object JavaDoc remove(int index)
141     {
142       ensureSafety();
143       return super.remove(index);
144     }
145
146     public boolean removeAll(Collection JavaDoc collection)
147     {
148       ensureSafety();
149       return super.removeAll(collection);
150     }
151
152     public void clear()
153     {
154       ensureSafety();
155       super.clear();
156     }
157
158     public boolean retainAll(Collection JavaDoc collection)
159     {
160       ensureSafety();
161       return super.retainAll(collection);
162     }
163
164     public Object JavaDoc set(int index, Object JavaDoc object)
165     {
166       ensureSafety();
167       return super.set(index, object);
168     }
169
170     public void move(int newPosition, Object JavaDoc object)
171     {
172       ensureSafety();
173       super.move(newPosition, object);
174     }
175
176     public Object JavaDoc move(int newPosition, int oldPosition)
177     {
178       ensureSafety();
179       return super.move(newPosition, oldPosition);
180     }
181   }
182
183   /*
184    * Javadoc copied from interface.
185    */

186   public EList eAdapters()
187   {
188     return ECollections.EMPTY_ELIST;
189   }
190
191   /**
192    * Returns the adapter list, even if it is <code>null</code>.
193    * @return the adapter list, even if it is <code>null</code>.
194    */

195   protected BasicEList eBasicAdapters()
196   {
197     return null;
198   }
199
200   /*
201    * Javadoc copied from interface.
202    */

203   public boolean eDeliver()
204   {
205     return false;
206   }
207
208   /*
209    * Javadoc copied from interface.
210    */

211   public void eSetDeliver(boolean deliver)
212   {
213     throw new UnsupportedOperationException JavaDoc();
214   }
215
216   /*
217    * Javadoc copied from interface.
218    */

219   public void eNotify(Notification notification)
220   {
221     if (eDeliver() && eBasicAdapters() != null)
222     {
223       int size = eBasicAdapters().size();
224       if (size > 0)
225       {
226         Adapter [] adapters = (Adapter [])eBasicAdapters().data();
227         for (int i = 0; i < size; ++i)
228         {
229           adapters[i].notifyChanged(notification);
230         }
231       }
232     }
233   }
234
235   /**
236    * Returns whether {@link #eNotify eNotify} needs to be called.
237    * This may return <code>true</code> even when {@link #eDeliver eDeliver} is <code>false</code>
238    * or when {@link #eAdapters eAdapters} is empty.
239    * @return whether {@link #eNotify eNotify} needs to be called.
240    */

241   public boolean eNotificationRequired()
242   {
243     BasicEList eAdapters = eBasicAdapters();
244     return eAdapters != null && eDeliver() && !eAdapters.isEmpty();
245   }
246 }
247
Popular Tags