KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > provider > AdapterFactoryContentProvider


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: AdapterFactoryContentProvider.java,v 1.6 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.provider;
18
19
20 import java.util.Collections JavaDoc;
21
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.jface.viewers.StructuredViewer;
24 import org.eclipse.jface.viewers.Viewer;
25 import org.eclipse.ui.views.properties.IPropertySource;
26 import org.eclipse.ui.views.properties.IPropertySourceProvider;
27 import org.eclipse.swt.widgets.Display;
28
29 import org.eclipse.emf.common.notify.AdapterFactory;
30 import org.eclipse.emf.common.notify.Notification;
31 import org.eclipse.emf.ecore.EObject;
32 import org.eclipse.emf.edit.provider.IChangeNotifier;
33 import org.eclipse.emf.edit.provider.IItemPropertySource;
34 import org.eclipse.emf.edit.provider.INotifyChangedListener;
35 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
36 import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
37 import org.eclipse.emf.edit.provider.IViewerNotification;
38
39
40 /**
41  * This content provider wraps an AdapterFactory
42  * and it delegates its JFace provider interfaces to corresponding adapter-implemented item provider interfaces.
43  * All method calls to the various structured content provider interfaces
44  * are delegated to interfaces implemented by the adapters generated by the AdapterFactory.
45  * {@link org.eclipse.jface.viewers.IStructuredContentProvider} is delegated to
46  * {@link IStructuredItemContentProvider}; {@link ITreeContentProvider} is delegated
47  * to {@link ITreeItemContentProvider};
48  * and {@link IPropertySourceProvider} to {@link IItemPropertySource}.
49  */

50 public class AdapterFactoryContentProvider
51   implements
52     ITreeContentProvider,
53     IPropertySourceProvider,
54     INotifyChangedListener
55 {
56   /**
57    * This keeps track of the one factory we are using.
58    * Use a {@link org.eclipse.emf.edit.provider.ComposedAdapterFactory} if adapters
59    * from more the one factory are involved in the model.
60    */

61   protected AdapterFactory adapterFactory;
62
63   /**
64    * This keeps track of the one viewer using this content provider.
65    */

66   protected Viewer viewer;
67
68   private static final Class JavaDoc IStructuredItemContentProviderClass = IStructuredItemContentProvider.class;
69   private static final Class JavaDoc ITreeItemContentProviderClass = ITreeItemContentProvider.class;
70   private static final Class JavaDoc IItemPropertySourceClass = IItemPropertySource.class;
71
72   /**
73    * This constructs an instance that wraps this factory.
74    * The factory should yield adapters that implement the various IItemContentProvider interfaces.
75    */

76   public AdapterFactoryContentProvider(AdapterFactory adapterFactory)
77   {
78     this.adapterFactory = adapterFactory;
79
80     if (adapterFactory instanceof IChangeNotifier)
81     {
82       ((IChangeNotifier)adapterFactory).addListener(this);
83     }
84   }
85
86   /**
87    * This sets the wrapped factory.
88    */

89   public void setAdapterFactory(AdapterFactory adapterFactory)
90   {
91     if (this.adapterFactory instanceof IChangeNotifier)
92     {
93       ((IChangeNotifier)this.adapterFactory).removeListener(this);
94     }
95
96     if (adapterFactory instanceof IChangeNotifier)
97     {
98       ((IChangeNotifier)adapterFactory).addListener(this);
99     }
100
101     this.adapterFactory = adapterFactory;
102   }
103
104   /**
105    * This returns the wrapped factory.
106    */

107   public AdapterFactory getAdapterFactory()
108   {
109     return adapterFactory;
110   }
111
112   /**
113    * The given Viewer will start (oldInput == null) or stop (newInput == null) listening for domain events.
114    */

115   public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput)
116   {
117     // If there was no old input, then we must be providing content for this part for the first time...
118
//
119
this.viewer = viewer;
120   }
121
122   /**
123    * This implements {@link org.eclipse.jface.viewers.IStructuredContentProvider}.getElements to
124    * forward the call to an object that implements
125    * (@link org.eclipse.emf.edit.provider.IStructuredItemContentProvider#getElements IStructuredItemContentProvider.getElements}.
126    */

127   public Object JavaDoc [] getElements(Object JavaDoc object)
128   {
129     // Get the adapter from the factory.
130
//
131
IStructuredItemContentProvider structuredItemContentProvider =
132       (IStructuredItemContentProvider)adapterFactory.adapt(object, IStructuredItemContentProviderClass);
133
134     // Either delegate the call or return nothing.
135
//
136
return
137       (structuredItemContentProvider != null ?
138         structuredItemContentProvider.getElements(object) :
139         Collections.EMPTY_LIST).toArray();
140   }
141
142   /**
143    * This implements {@link org.eclipse.jface.viewers.ITreeContentProvider}.getChildren to forward the call to an object that implements
144    * {@link org.eclipse.emf.edit.provider.ITreeItemContentProvider#getChildren ITreeItemContentProvider.getChildren}.
145    */

146   public Object JavaDoc [] getChildren(Object JavaDoc object)
147   {
148     // Get the adapter from the factory.
149
//
150
ITreeItemContentProvider treeItemContentProvider =
151       (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProviderClass);
152
153     // Either delegate the call or return nothing.
154
//
155
return
156       (treeItemContentProvider != null ?
157         treeItemContentProvider.getChildren(object) :
158         Collections.EMPTY_LIST).toArray();
159   }
160
161   /**
162    * This implements {@link org.eclipse.jface.viewers.ITreeContentProvider}.hasChildren to forward the call to an object that implements
163    * {@link org.eclipse.emf.edit.provider.ITreeItemContentProvider#hasChildren ITreeItemContentProvider.hasChildren}.
164    */

165   public boolean hasChildren(Object JavaDoc object)
166   {
167     // Get the adapter from the factory.
168
//
169
ITreeItemContentProvider treeItemContentProvider =
170       (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProviderClass);
171
172     // Either delegate the call or return nothing.
173
//
174
return
175       treeItemContentProvider != null &&
176         treeItemContentProvider.hasChildren(object);
177   }
178
179   /**
180    * This implements {@link org.eclipse.jface.viewers.ITreeContentProvider}.getParent to forward the call to an object that implements
181    * {@link org.eclipse.emf.edit.provider.ITreeItemContentProvider#getParent ITreeItemContentProvider.getParent}.
182    */

183   public Object JavaDoc getParent(Object JavaDoc object)
184   {
185     // Get the adapter from the factory.
186
//
187
ITreeItemContentProvider treeItemContentProvider =
188       (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProviderClass);
189
190     // Either delegate the call or return nothing.
191
//
192
return
193       treeItemContentProvider != null ?
194         treeItemContentProvider.getParent(object) :
195         null;
196   }
197
198   /**
199    * This discards the content provider and removes this as a listener to the {@link #adapterFactory}.
200    */

201   public void dispose()
202   {
203     if (adapterFactory instanceof IChangeNotifier)
204     {
205       ((IChangeNotifier)adapterFactory).removeListener(this);
206     }
207     viewer = null;
208   }
209
210   /**
211    * This implements {@link org.eclipse.ui.views.properties.IPropertySourceProvider}.getPropertySource to forward the call to an object that implements
212    * {@link org.eclipse.emf.edit.provider.IItemPropertySource}.
213    */

214   public IPropertySource getPropertySource(Object JavaDoc object)
215   {
216     if (object instanceof IPropertySource)
217     {
218       return (IPropertySource)object;
219     }
220     else
221     {
222       IItemPropertySource itemPropertySource =
223         (IItemPropertySource)
224           (object instanceof EObject && ((EObject)object).eClass() == null ?
225             null :
226             adapterFactory.adapt(object, IItemPropertySourceClass));
227   
228       return
229         itemPropertySource != null ? createPropertySource(object, itemPropertySource) : null;
230     }
231   }
232
233   protected IPropertySource createPropertySource(Object JavaDoc object, IItemPropertySource itemPropertySource)
234   {
235     return new PropertySource(object, itemPropertySource);
236   }
237
238   public void notifyChanged(Notification notification)
239   {
240     if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed())
241     {
242       // If the notification is an IViewerNotification, it specifies how ViewerRefresh should behave. Otherwise fall
243
// back to NotifyChangedToViewerRefresh, which determines how to refresh the viewer directly from the model
244
// notification.
245
//
246
if (notification instanceof IViewerNotification)
247       {
248         ViewerRefresh viewerRefresh = new ViewerRefresh(viewer, (IViewerNotification)notification);
249
250         Display d = viewer.getControl().getDisplay();
251         if (d != Display.getCurrent())
252         {
253           d.asyncExec(viewerRefresh);
254         }
255         else
256         {
257           viewerRefresh.run();
258         }
259       }
260       else
261       {
262         NotifyChangedToViewerRefresh.handleNotifyChanged(
263           viewer,
264           notification.getNotifier(),
265           notification.getEventType(),
266           notification.getFeature(),
267           notification.getOldValue(),
268           notification.getNewValue(),
269           notification.getPosition());
270       }
271     }
272   }
273
274   /**
275    * A runnable class that efficiently updates a {@link org.eclipse.jface.viewers.Viewer} via standard APIs, based on
276    * an {@link org.eclipse.emf.edit.provider.IViewerNotification} from the model's item providers.
277    */

278   public static class ViewerRefresh implements Runnable JavaDoc
279   {
280     Viewer viewer;
281     IViewerNotification notification;
282
283     public ViewerRefresh(Viewer viewer, IViewerNotification notification)
284     {
285       this.viewer = viewer;
286       this.notification = notification;
287     }
288
289     public void run()
290     {
291       // Never update the viewer on a resolve.
292
//
293
if (notification.getEventType() == Notification.RESOLVE) return;
294
295       Object JavaDoc element = notification.getElement();
296
297       if (viewer instanceof StructuredViewer)
298       {
299         StructuredViewer structuredViewer = (StructuredViewer)viewer;
300
301         if (element != null)
302         {
303           if (notification.isContentRefresh())
304           {
305             structuredViewer.refresh(element, notification.isLabelUpdate());
306           }
307           else if (notification.isLabelUpdate())
308           {
309             structuredViewer.update(element, null);
310           }
311         }
312         else
313         {
314           structuredViewer.refresh(notification.isLabelUpdate());
315         }
316       }
317       else
318       {
319         viewer.refresh();
320       }
321     }
322   }
323 }
324
Popular Tags