KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > provider > ReflectiveItemProviderAdapterFactory


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

17 package org.eclipse.emf.edit.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.common.notify.impl.AdapterFactoryImpl;
27 import org.eclipse.emf.ecore.EObject;
28
29 /**
30  * This is the factory that is used to provide the interfaces needed to support Viewers reflectively.
31  */

32 public class ReflectiveItemProviderAdapterFactory
33   extends AdapterFactoryImpl
34   implements ComposeableAdapterFactory, IChangeNotifier, IDisposable
35 {
36   /**
37    * This keeps track of the root adapter factory that delegates to this adapter factory.
38    */

39   protected ComposedAdapterFactory parentAdapterFactory;
40
41   /**
42    * This is used to implement {@link org.eclipse.emf.edit.provider.IChangeNotifier}.
43    */

44   protected IChangeNotifier changeNotifier = new ChangeNotifier();
45
46   /**
47    * This keeps track of all the supported types checked by {@link #isFactoryForType isFactoryForType}.
48    */

49   protected Collection JavaDoc supportedTypes = new ArrayList JavaDoc();
50
51   /**
52    * The singleton reflective instance.
53    */

54   protected ReflectiveItemProvider reflectiveItemProviderAdapter;
55
56   /**
57    * This constructs an instance.
58    */

59   public ReflectiveItemProviderAdapterFactory()
60   {
61     reflectiveItemProviderAdapter = new ReflectiveItemProvider(this);
62
63     supportedTypes.add(IStructuredItemContentProvider.class);
64     supportedTypes.add(ITreeItemContentProvider.class);
65     supportedTypes.add(IItemPropertySource.class);
66     supportedTypes.add(IEditingDomainItemProvider.class);
67     supportedTypes.add(IItemLabelProvider.class);
68     supportedTypes.add(ITableItemLabelProvider.class);
69   }
70
71   public Adapter createAdapter(Notifier target)
72   {
73     return reflectiveItemProviderAdapter;
74   }
75
76   /**
77    * This returns the root adapter factory that contains this factory.
78    */

79   public ComposeableAdapterFactory getRootAdapterFactory()
80   {
81     return parentAdapterFactory == null ? this : parentAdapterFactory.getRootAdapterFactory();
82   }
83
84   /**
85    * This sets the composed adapter factory that contains this factory.
86    */

87   public void setParentAdapterFactory(ComposedAdapterFactory parentAdapterFactory)
88   {
89     this.parentAdapterFactory = parentAdapterFactory;
90   }
91
92   /**
93    * Returns whether this factory is applicable for the type of the object.
94    * @return whether this factory is applicable for the type of the object.
95    */

96   public boolean isFactoryForType(Object JavaDoc type)
97   {
98     return type instanceof EObject || supportedTypes.contains(type);
99   }
100
101   /**
102    * This implementation substitutes the factory itself as the key for the adapter.
103    */

104   public Adapter adapt(Notifier notifier, Object JavaDoc type)
105   {
106     return super.adapt(notifier, this);
107   }
108
109   /**
110    */

111   public Object JavaDoc adapt(Object JavaDoc object, Object JavaDoc type)
112   {
113     if (isFactoryForType(type))
114     {
115       Object JavaDoc adapter = super.adapt(object, type);
116       if (!(type instanceof Class JavaDoc) || (((Class JavaDoc)type).isInstance(adapter)))
117       {
118         return adapter;
119       }
120     }
121
122     return null;
123   }
124
125   /**
126    * This adds a listener.
127    */

128   public void addListener(INotifyChangedListener notifyChangedListener)
129   {
130     changeNotifier.addListener(notifyChangedListener);
131   }
132
133   /**
134    * This removes a listener.
135    */

136   public void removeListener(INotifyChangedListener notifyChangedListener)
137   {
138     changeNotifier.removeListener(notifyChangedListener);
139   }
140
141   /**
142    * This delegates to {@link #changeNotifier} and to {@link #parentAdapterFactory}.
143    */

144   public void fireNotifyChanged(Notification notification)
145   {
146     changeNotifier.fireNotifyChanged(notification);
147
148     if (parentAdapterFactory != null)
149     {
150       parentAdapterFactory.fireNotifyChanged(notification);
151     }
152   }
153
154   public void dispose()
155   {
156     if (reflectiveItemProviderAdapter != null)
157     {
158       reflectiveItemProviderAdapter.dispose();
159     }
160   }
161 }
162
Popular Tags