KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.eclipse.emf.edit.provider;
18
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.eclipse.emf.common.notify.Adapter;
24 import org.eclipse.emf.common.notify.AdapterFactory;
25 import org.eclipse.emf.common.notify.Notification;
26 import org.eclipse.emf.common.notify.Notifier;
27 import org.eclipse.emf.edit.EMFEditPlugin;
28
29
30 /**
31  * This abstract class provides support for creating {@link IItemProviderDecorator}s
32  * for the adapters created by another {@link AdapterFactory}.
33  */

34 public abstract class DecoratorAdapterFactory
35   implements
36     AdapterFactory,
37     ComposeableAdapterFactory,
38     IChangeNotifier,
39     IDisposable
40 {
41   protected HashMap JavaDoc itemProviderDecorators = new HashMap JavaDoc();
42
43   /**
44    * This keeps the {@link org.eclipse.emf.common.notify.AdapterFactory} being decorated.
45    */

46   protected AdapterFactory decoratedAdapterFactory;
47
48   /**
49    * This is used to implement the {@link ComposeableAdapterFactory} interface.
50    */

51   protected ComposedAdapterFactory parentAdapterFactory;
52
53   /**
54    * This keeps track of all the Viewers.
55    */

56   protected ChangeNotifier changeNotifier = new ChangeNotifier();
57
58   /**
59    * This creates an instance that decorates the adapters from the given adapter factory.
60    */

61   public DecoratorAdapterFactory(AdapterFactory decoratedAdapterFactory)
62   {
63     this.decoratedAdapterFactory = decoratedAdapterFactory;
64   }
65
66   /**
67    * This just delegates to the {@link #decoratedAdapterFactory}.
68    */

69   public boolean isFactoryForType(Object JavaDoc type)
70   {
71     return decoratedAdapterFactory.isFactoryForType(type);
72   }
73
74   /**
75    * This returns the adapter factory whose adapters are being decorated.
76    */

77   public AdapterFactory getDecoratedAdapterFactory()
78   {
79     return decoratedAdapterFactory;
80   }
81
82   /**
83    * This sets the adapter factory whose adapters will be decorated.
84    */

85   public void setDecoratedAdapterFactory(AdapterFactory decoratedAdapterFactory)
86   {
87     this.decoratedAdapterFactory = decoratedAdapterFactory;
88   }
89
90   /**
91    * This is called when a new decorator is needed by {@link #adapt(Object,Object)}.
92    */

93   protected abstract IItemProviderDecorator createItemProviderDecorator(Object JavaDoc target, Object JavaDoc Type);
94
95   /**
96    * All adapter creation is delegated to this method, which yields decorated item providers.
97    * It hooks up the decorators created by {@link #createItemProviderDecorator}
98    * to the adapters returned by {@link #decoratedAdapterFactory}.
99    */

100   public Object JavaDoc adapt(Object JavaDoc target, Object JavaDoc type)
101   {
102     Object JavaDoc adapter = decoratedAdapterFactory.adapt(target, type);
103     if (adapter instanceof IChangeNotifier)
104     {
105       IItemProviderDecorator itemProviderDecorator = (IItemProviderDecorator)itemProviderDecorators.get(adapter);
106       if (itemProviderDecorator == null)
107       {
108         itemProviderDecorator = createItemProviderDecorator(target, type);
109         itemProviderDecorators.put(adapter, itemProviderDecorator);
110         itemProviderDecorator.setDecoratedItemProvider((IChangeNotifier)adapter);
111       }
112
113       if (itemProviderDecorator != null)
114       {
115         return itemProviderDecorator;
116       }
117     }
118
119     return adapter;
120   }
121
122   /**
123    * This delegates to {@link #adapt(Object,Object)}
124    */

125   public Adapter adapt(Notifier target, Object JavaDoc type)
126   {
127     return (Adapter)adapt((Object JavaDoc)target, type);
128   }
129
130   /**
131    * This interface is not support; an exception will be thrown.
132    */

133   public Adapter adaptNew(Notifier target, Object JavaDoc type)
134   {
135     throw
136       new RuntimeException JavaDoc
137         (EMFEditPlugin.INSTANCE.getString
138           ("_EXC_Method_not_implemented", new Object JavaDoc [] { this.getClass() + "adaptNew(Notifier target, Object type)" }));
139   }
140
141   public void adaptAllNew(Notifier target)
142   {
143     decoratedAdapterFactory.adaptAllNew(target);
144   }
145
146   /**
147    * This returns the root adapter factory that delegates to this factory.
148    */

149   public ComposeableAdapterFactory getRootAdapterFactory()
150   {
151     return parentAdapterFactory == null ? this : parentAdapterFactory.getRootAdapterFactory();
152   }
153
154   /**
155    * This sets the direct parent adapter factory into which this factory is composed.
156    */

157   public void setParentAdapterFactory(ComposedAdapterFactory parentAdapterFactory)
158   {
159     this.parentAdapterFactory = parentAdapterFactory;
160   }
161
162   public void addListener(INotifyChangedListener notifyChangedListener)
163   {
164     changeNotifier.addListener(notifyChangedListener);
165   }
166
167   public void removeListener(INotifyChangedListener notifyChangedListener)
168   {
169     changeNotifier.removeListener(notifyChangedListener);
170   }
171
172   public void fireNotifyChanged(Notification notification)
173   {
174     changeNotifier.fireNotifyChanged(notification);
175
176     if (parentAdapterFactory != null)
177     {
178       parentAdapterFactory.fireNotifyChanged(notification);
179     }
180   }
181
182   public void dispose()
183   {
184     for (Iterator JavaDoc objects = itemProviderDecorators.values().iterator(); objects.hasNext(); )
185     {
186       Object JavaDoc object = objects.next();
187       if (object instanceof IDisposable)
188       {
189         ((IDisposable)object).dispose();
190       }
191     }
192   }
193 }
194
Popular Tags