KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > util > EContentAdapter


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: EContentAdapter.java,v 1.3 2005/06/08 06:20:10 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.util;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.eclipse.emf.common.notify.Notification;
24 import org.eclipse.emf.common.notify.Notifier;
25 import org.eclipse.emf.common.notify.impl.AdapterImpl;
26 import org.eclipse.emf.ecore.EObject;
27 import org.eclipse.emf.ecore.EReference;
28 import org.eclipse.emf.ecore.resource.Resource;
29 import org.eclipse.emf.ecore.resource.ResourceSet;
30
31
32 /**
33  * An adapter that maintains itself as an adapter for all contained objects
34  * as they come and go.
35  * It can be installed for an {@link EObject}, a {@link Resource}, or a {@link ResourceSet}.
36  */

37 public class EContentAdapter extends AdapterImpl
38 {
39   /**
40    * Handles a notification by calling {@link #selfAdapt selfAdapter}.
41    */

42   public void notifyChanged(Notification notification)
43   {
44     selfAdapt(notification);
45
46     super.notifyChanged(notification);
47   }
48
49   /**
50    * Handles a notification by calling {@link #handleContainment handleContainment}
51    * for any containment-based notification.
52    */

53   protected void selfAdapt(Notification notification)
54   {
55     Object JavaDoc notifier = notification.getNotifier();
56     if (notification.getEventType() == Notification.REMOVING_ADAPTER)
57     {
58       unsetTarget(notifier);
59     }
60     else if (notifier instanceof ResourceSet)
61     {
62       if (notification.getFeatureID(ResourceSet.class) == ResourceSet.RESOURCE_SET__RESOURCES)
63       {
64         handleContainment(notification);
65       }
66     }
67     else if (notifier instanceof Resource)
68     {
69       if (notification.getFeatureID(Resource.class) == Resource.RESOURCE__CONTENTS)
70       {
71         handleContainment(notification);
72       }
73     }
74     else if (notifier instanceof EObject)
75     {
76       Object JavaDoc feature = notification.getFeature();
77       if (feature instanceof EReference && ((EReference)feature).isContainment())
78       {
79         handleContainment(notification);
80       }
81     }
82   }
83
84   /**
85    * Handles a containment change by adding and removing the adapter as appropriate.
86    */

87   protected void handleContainment(Notification notification)
88   {
89     switch (notification.getEventType())
90     {
91       case Notification.SET:
92       case Notification.UNSET:
93       {
94         Notifier oldValue = (Notifier)notification.getOldValue();
95         if (oldValue != null)
96         {
97           removeAdapter(oldValue);
98         }
99         Notifier newValue = (Notifier)notification.getNewValue();
100         if (newValue != null)
101         {
102           addAdapter(newValue);
103         }
104         break;
105       }
106       case Notification.ADD:
107       {
108         Notifier newValue = (Notifier)notification.getNewValue();
109         if (newValue != null)
110         {
111           addAdapter(newValue);
112         }
113         break;
114       }
115       case Notification.ADD_MANY:
116       {
117         Collection JavaDoc newValues = (Collection JavaDoc)notification.getNewValue();
118         for (Iterator JavaDoc i = newValues.iterator(); i.hasNext(); )
119         {
120           Notifier newValue = (Notifier)i.next();
121           addAdapter(newValue);
122         }
123         break;
124       }
125       case Notification.REMOVE:
126       {
127         Notifier oldValue = (Notifier)notification.getOldValue();
128         if (oldValue != null)
129         {
130           removeAdapter(oldValue);
131         }
132         break;
133       }
134       case Notification.REMOVE_MANY:
135       {
136         Collection JavaDoc oldValues = (Collection JavaDoc)notification.getOldValue();
137         for (Iterator JavaDoc i = oldValues.iterator(); i.hasNext(); )
138         {
139           Notifier oldContentValue = (Notifier)i.next();
140           removeAdapter(oldContentValue);
141         }
142         break;
143       }
144     }
145   }
146
147   /**
148    * Handles installation of the adapter
149    * by adding the adapter to each of the directly contained objects.
150    */

151   public void setTarget(Notifier target)
152   {
153     super.setTarget(target);
154
155     Collection JavaDoc contents =
156       target instanceof EObject ?
157         ((EObject)target).eContents() :
158         target instanceof ResourceSet ?
159           ((ResourceSet)target).getResources() :
160           target instanceof Resource ?
161             ((Resource)target).getContents() :
162             null;
163     if (contents != null)
164     {
165       for (Iterator JavaDoc i = contents.iterator(); i.hasNext(); )
166       {
167         Notifier notifier = (Notifier)i.next();
168         addAdapter(notifier);
169       }
170     }
171   }
172
173   /**
174    * Handles installation of the adapter
175    * by adding the adapter to each of the directly contained objects.
176    */

177   protected void unsetTarget(Object JavaDoc target)
178   {
179     super.setTarget(null);
180
181     Collection JavaDoc contents =
182       target instanceof EObject ?
183         ((EObject)target).eContents() :
184         target instanceof ResourceSet ?
185           ((ResourceSet)target).getResources() :
186           target instanceof Resource ?
187             ((Resource)target).getContents() :
188             null;
189     if (contents != null)
190     {
191       for (Iterator JavaDoc i = contents.iterator(); i.hasNext(); )
192       {
193         Notifier notifier = (Notifier)i.next();
194         removeAdapter(notifier);
195       }
196     }
197   }
198   
199   protected void addAdapter(Notifier notifier)
200   {
201     notifier.eAdapters().add(this);
202   }
203   
204   protected void removeAdapter(Notifier notifier)
205   {
206     notifier.eAdapters().remove(this);
207   }
208 }
209
Popular Tags