KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.eclipse.emf.edit.ui.provider;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.eclipse.jface.viewers.ILabelProvider;
25 import org.eclipse.jface.viewers.ILabelProviderListener;
26 import org.eclipse.jface.viewers.ITableLabelProvider;
27 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
28 import org.eclipse.swt.graphics.Image;
29
30 import org.eclipse.emf.common.notify.AdapterFactory;
31 import org.eclipse.emf.common.notify.Notification;
32 import org.eclipse.emf.edit.EMFEditPlugin;
33 import org.eclipse.emf.edit.provider.IChangeNotifier;
34 import org.eclipse.emf.edit.provider.IItemLabelProvider;
35 import org.eclipse.emf.edit.provider.INotifyChangedListener;
36 import org.eclipse.emf.edit.provider.ITableItemLabelProvider;
37
38
39 /**
40  * This label provider wraps an AdapterFactory
41  * and it delegates its JFace provider interfaces to corresponding adapter-implemented item provider interfaces.
42  * All method calls to the various label provider interfaces
43  * are delegated to interfaces implemented by the adapters generated by the AdapterFactory.
44  * {@link ILabelProvider} is delegated to {@link IItemLabelProvider};
45  * and {@link ITableLabelProvider} is delegated to {@link ITableItemLabelProvider}.
46  * <p>
47  * The label provider has no mechanism for notifying the viewer of changes.
48  * As long as the AdapterFactory is also used in an AdapterFactoryContentProvider, this won't be a problem,
49  * because notifications will be forward as a result of that.
50  */

51 public class AdapterFactoryLabelProvider implements ILabelProvider, ITableLabelProvider, INotifyChangedListener
52 {
53   /**
54    * This keep track of the one factory we are using.
55    * Use a {@link org.eclipse.emf.edit.provider.ComposedAdapterFactory}
56    * if adapters from more the one factory are involved in the model.
57    */

58   protected AdapterFactory adapterFactory;
59
60   /**
61    * This keeps track of the label provider listeners.
62    */

63   protected Collection JavaDoc labelProviderListeners;
64
65   private static final Class JavaDoc IItemLabelProviderClass = IItemLabelProvider.class;
66   private static final Class JavaDoc ITableItemLabelProviderClass = ITableItemLabelProvider.class;
67
68   /**
69    * Construct an instance that wraps this factory.
70    * The AdapterFactory should yield adapters that implement the various item label provider interfaces.
71    */

72   public AdapterFactoryLabelProvider(AdapterFactory adapterFactory)
73   {
74     this.adapterFactory = adapterFactory;
75     if (adapterFactory instanceof IChangeNotifier)
76     {
77       ((IChangeNotifier)adapterFactory).addListener(this);
78     }
79
80     labelProviderListeners = new ArrayList JavaDoc();
81   }
82
83   /**
84    * Return the wrapped AdapterFactory.
85    */

86   public AdapterFactory getAdapterFactory()
87   {
88     return adapterFactory;
89   }
90
91   /**
92    * Set the wrapped AdapterFactory.
93    */

94   public void setAdapterFactory(AdapterFactory adapterFactory)
95   {
96     if (this.adapterFactory instanceof IChangeNotifier)
97     {
98       ((IChangeNotifier)this.adapterFactory).removeListener(this);
99     }
100
101     if (adapterFactory instanceof IChangeNotifier)
102     {
103       ((IChangeNotifier)adapterFactory).addListener(this);
104     }
105
106     this.adapterFactory = adapterFactory;
107   }
108
109   /**
110    * Since we won't ever generate these notifications, we can just ignore this.
111    */

112   public void addListener(ILabelProviderListener listener)
113   {
114     labelProviderListeners.add(listener);
115   }
116
117   /**
118    * Since we won't ever add listeners, we can just ignore this.
119    */

120   public void removeListener(ILabelProviderListener listener)
121   {
122     labelProviderListeners.remove(listener);
123   }
124
125   /**
126    * This discards the content provider and removes this as a listener to the {@link #adapterFactory}.
127    */

128   public void dispose()
129   {
130     if (this.adapterFactory instanceof IChangeNotifier)
131     {
132       ((IChangeNotifier)adapterFactory).removeListener(this);
133     }
134   }
135
136   /**
137    * This always returns true right now.
138    */

139   public boolean isLabelProperty(Object JavaDoc object, String JavaDoc id)
140   {
141     return true;
142   }
143
144   /**
145    * This implements {@link org.eclipse.jface.viewers.ILabelProvider}.getImage by forwarding it to an object that implements
146    * {@link org.eclipse.emf.edit.provider.IItemLabelProvider#getImage IItemLabelProvider.getImage}
147    */

148   public Image getImage(Object JavaDoc object)
149   {
150     // Get the adapter from the factory.
151
//
152
IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProviderClass);
153
154     return
155       itemLabelProvider != null ?
156         getImageFromObject(itemLabelProvider.getImage(object)) :
157         getDefaultImage(object);
158   }
159
160   protected Image getDefaultImage(Object JavaDoc object)
161   {
162     String JavaDoc image = "full/obj16/GenericValue";
163     if (object instanceof String JavaDoc)
164     {
165       image = "full/obj16/TextValue";
166     }
167     else if (object instanceof Boolean JavaDoc)
168     {
169       image = "full/obj16/BooleanValue";
170     }
171     else if (object instanceof Float JavaDoc || object instanceof Double JavaDoc)
172     {
173       image = "full/obj16/RealValue";
174     }
175     else if (object instanceof Integer JavaDoc || object instanceof Short JavaDoc || object instanceof Long JavaDoc || object instanceof Byte JavaDoc)
176     {
177       image = "full/obj16/RealValue";
178     }
179
180     return getImageFromObject(EMFEditPlugin.INSTANCE.getImage(image));
181   }
182
183   protected Image getImageFromObject(Object JavaDoc object)
184   {
185     return ExtendedImageRegistry.getInstance().getImage(object);
186   }
187
188   /**
189    * This implements {@link ILabelProvider}.getText by forwarding it to an object that implements
190    * {@link IItemLabelProvider#getText IItemLabelProvider.getText}
191    */

192   public String JavaDoc getText(Object JavaDoc object)
193   {
194     // Get the adapter from the factory.
195
//
196
IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProviderClass);
197
198     return
199       itemLabelProvider != null ?
200         itemLabelProvider.getText(object) :
201         object == null ?
202           "" :
203           object.toString();
204   }
205
206   /**
207    * This implements {@link ITableLabelProvider}.getColumnmage by forwarding it to an object that implements
208    * {@link ITableItemLabelProvider#getColumnImage ITableItemLabelProvider.getColumnImage}
209    * or failing that, an object that implements
210    * {@link IItemLabelProvider#getImage IItemLabelProvider.getImage}
211    * where the columnIndex is ignored.
212    */

213   public Image getColumnImage(Object JavaDoc object, int columnIndex)
214   {
215     // Get the adapter from the factory.
216
//
217
ITableItemLabelProvider tableItemLabelProvider = (ITableItemLabelProvider)adapterFactory.adapt(object, ITableItemLabelProviderClass);
218
219     // No image is a good default.
220
//
221
Image result = null;
222
223     // Now we could check that the adapter implements interface ITableItemLabelProvider.
224
//
225
if (tableItemLabelProvider != null)
226     {
227       // And delegate the call.
228
//
229
result = getImageFromObject(tableItemLabelProvider.getColumnImage(object, columnIndex));
230     }
231     // Otherwise, we could check that the adapter implements interface IItemLabelProvider.
232
//
233
else
234     {
235       IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProviderClass);
236       if (itemLabelProvider != null)
237       {
238         // And delegate the call.
239
//
240
result = getImageFromObject(itemLabelProvider.getImage(object));
241       }
242     }
243
244     return result;
245   }
246
247   /**
248    * This implements {@link ITableLabelProvider}.getColumnText by forwarding it to an object that implements
249    * {@link ITableItemLabelProvider#getColumnText ITableItemLabelProvider.getColumnText}
250    * or failing that, an object that implements
251    * {@link IItemLabelProvider#getText IItemLabelProvider.getText}
252    * where the columnIndex are is ignored.
253    */

254   public String JavaDoc getColumnText(Object JavaDoc object, int columnIndex)
255   {
256     // Get the adapter from the factory.
257
//
258
ITableItemLabelProvider tableItemLabelProvider = (ITableItemLabelProvider)adapterFactory.adapt(object, ITableItemLabelProviderClass);
259
260     // Now we could check that the adapter implements interface ITableItemLabelProvider.
261
//
262
if (tableItemLabelProvider != null)
263     {
264       // And delegate the call.
265
//
266
return tableItemLabelProvider.getColumnText(object, columnIndex);
267     }
268     // Otherwise, we could check that the adapter implements interface IItemLabelProvider.
269
//
270
else
271     {
272       IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProviderClass);
273       if (itemLabelProvider != null)
274       {
275         // And delegate the call.
276
//
277
return itemLabelProvider.getText(object);
278       }
279       // If there is a column object, just convert it to a string.
280
//
281
else if (object != null)
282       {
283         return object.toString();
284       }
285       else
286       {
287       return "";
288       }
289     }
290   }
291
292   public void fireLabelProviderChanged()
293   {
294     for (Iterator JavaDoc i = labelProviderListeners.iterator(); i.hasNext(); )
295     {
296       ILabelProviderListener labelProviderListener = (ILabelProviderListener)i.next();
297       labelProviderListener.labelProviderChanged(new LabelProviderChangedEvent(this));
298     }
299   }
300
301   public void notifyChanged(Notification notification)
302   {
303     // fireLabelProviderChanged();
304
}
305 }
306
Popular Tags