KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > tree > provider > TreeItemProviderAdapterFactory


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: TreeItemProviderAdapterFactory.java,v 1.2 2005/06/08 06:17:06 nickb Exp $
16  */

17 package org.eclipse.emf.edit.tree.provider;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22
23 import org.eclipse.emf.common.notify.Adapter;
24 import org.eclipse.emf.common.notify.Notification;
25 import org.eclipse.emf.common.notify.Notifier;
26 import org.eclipse.emf.ecore.EObject;
27 import org.eclipse.emf.edit.provider.ChangeNotifier;
28 import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
29 import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
30 import org.eclipse.emf.edit.provider.Disposable;
31 import org.eclipse.emf.edit.provider.IChangeNotifier;
32 import org.eclipse.emf.edit.provider.IDisposable;
33 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
34 import org.eclipse.emf.edit.provider.IItemLabelProvider;
35 import org.eclipse.emf.edit.provider.IItemPropertySource;
36 import org.eclipse.emf.edit.provider.INotifyChangedListener;
37 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
38 import org.eclipse.emf.edit.provider.ITableItemLabelProvider;
39 import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
40 import org.eclipse.emf.edit.tree.util.TreeAdapterFactory;
41
42
43 /**
44  * This is the factory that is used to provide the interfaces needed to support Viewers.
45  * The adapters generated by this factory convert EMF adapter notifications into calls to {@link #fireNotifyChanged fireNotifyChanged}.
46  * The adapters also support property sheets.
47  * Note that most of the adapters are shared among multiple instances.
48  *
49  * @generated modifiable
50  */

51 public class TreeItemProviderAdapterFactory
52   extends
53     TreeAdapterFactory
54   implements
55     ComposeableAdapterFactory,
56     IChangeNotifier,
57     IDisposable
58 {
59   /**
60    * This keeps track of the root adapter factory that delegates to this adapter factory.
61    */

62   protected ComposedAdapterFactory parentAdapterFactory;
63
64   /**
65    * This is used to implement {@link org.eclipse.emf.edit.provider.IChangeNotifier}.
66    */

67   protected IChangeNotifier changeNotifier = new ChangeNotifier();
68
69   /**
70    * This is used to implement {@link org.eclipse.emf.edit.provider.IDisposable}.
71    */

72   protected Disposable disposable = new Disposable();
73
74   /**
75    * This keeps track of all the supported types checked by {@link #isFactoryForType isFactoryForType}.
76    */

77   protected Collection JavaDoc supportedTypes = new ArrayList JavaDoc();
78
79   /**
80    * This constructs an instance.
81    */

82   public TreeItemProviderAdapterFactory()
83   {
84     supportedTypes.add(IStructuredItemContentProvider.class);
85     supportedTypes.add(ITreeItemContentProvider.class);
86     supportedTypes.add(IItemPropertySource.class);
87     supportedTypes.add(IEditingDomainItemProvider.class);
88     supportedTypes.add(IItemLabelProvider.class);
89     supportedTypes.add(ITableItemLabelProvider.class);
90   }
91
92   /**
93    * This creates an adapter for a {@link org.eclipse.emf.edit.tree.TreeNode}.
94    */

95   public Adapter createTreeNodeAdapter()
96   {
97     return new TreeNodeItemProvider(this);
98   }
99
100   /**
101    * This returns the root adapter factory that contains this factory.
102    */

103   public ComposeableAdapterFactory getRootAdapterFactory()
104   {
105     return parentAdapterFactory == null ? this : parentAdapterFactory.getRootAdapterFactory();
106   }
107
108   /**
109    * This sets the composed adapter factory that contains this factory.
110    */

111   public void setParentAdapterFactory(ComposedAdapterFactory parentAdapterFactory)
112   {
113     this.parentAdapterFactory = parentAdapterFactory;
114   }
115
116   /**
117    * This also check if the type is one of the supported types.
118    */

119   public boolean isFactoryForType(Object JavaDoc type)
120   {
121     return super.isFactoryForType(type) || supportedTypes.contains(type);
122   }
123
124   /**
125    * This implementation substitutes the factory itself as the key for the adapter.
126    */

127   public Adapter adapt(Notifier notifier, Object JavaDoc type)
128   {
129     return super.adapt(notifier, this);
130   }
131
132   public Object JavaDoc adapt(Object JavaDoc object, Object JavaDoc type)
133   {
134     // This is a kludge to deal with enumerators, which crash the doSwitch.
135
//
136
if (object instanceof EObject && ((EObject)object).eClass() == null)
137     {
138       return null;
139     }
140
141     if (isFactoryForType(type))
142     {
143       Object JavaDoc adapter = super.adapt(object, type);
144       if (!(type instanceof Class JavaDoc) || (((Class JavaDoc)type).isInstance(adapter)))
145       {
146         return adapter;
147       }
148     }
149
150     return null;
151   }
152
153   /**
154    * This records adapters for subsequent disposable.
155    */

156   public Adapter adaptNew(Notifier object, Object JavaDoc type)
157   {
158     Adapter result = super.adaptNew(object, type);
159     disposable.add(result);
160     return result;
161   }
162
163   /**
164    * This adds a listener.
165    */

166   public void addListener(INotifyChangedListener notifyChangedListener)
167   {
168     changeNotifier.addListener(notifyChangedListener);
169   }
170
171   /**
172    * This removes a listener.
173    */

174   public void removeListener(INotifyChangedListener notifyChangedListener)
175   {
176     changeNotifier.removeListener(notifyChangedListener);
177   }
178
179   /**
180    * This delegates to {@link #changeNotifier} and to {@link #parentAdapterFactory}.
181    */

182   public void fireNotifyChanged(Notification notification)
183   {
184     changeNotifier.fireNotifyChanged(notification);
185
186     if (parentAdapterFactory != null)
187     {
188       parentAdapterFactory.fireNotifyChanged(notification);
189     }
190   }
191
192   /**
193    * This disposes all the disposables.
194    */

195   public void dispose()
196   {
197     disposable.dispose();
198   }
199 }
200
Popular Tags