KickJava   Java API By Example, From Geeks To Geeks.

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


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: ItemProviderDecorator.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.Collection JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.eclipse.emf.common.command.Command;
24 import org.eclipse.emf.common.notify.AdapterFactory;
25 import org.eclipse.emf.common.notify.Notification;
26 import org.eclipse.emf.edit.command.CommandParameter;
27 import org.eclipse.emf.edit.domain.EditingDomain;
28
29
30 /**
31  * This implementation provides a convenient reusable base for item providers that will be used as decorators of other item providers.
32  * Default implementations for the following interfaces are provided:
33  * {@link IEditingDomainItemProvider}, {@link IItemLabelProvider}, {@link IItemPropertySource}, {@link IStructuredItemContentProvider},
34  * {@link ITableItemLabelProvider}, and {@link ITreeItemContentProvider}, and {@link IUpdateableItemText#getUpdateableText}.
35  */

36 public class ItemProviderDecorator
37   implements
38     INotifyChangedListener,
39     IItemProviderDecorator,
40     IChangeNotifier,
41     IDisposable
42 {
43   /**
44    * This keeps track of the adapter factory that created this adaptor.
45    * It is also used as the key/type for this adapter.
46    */

47   protected AdapterFactory adapterFactory;
48
49   /**
50    * This keeps track of the item provider being decorated.
51    */

52   protected IChangeNotifier decoratedItemProvider;
53
54   /**
55    * This is used to implement {@link IChangeNotifier}.
56    */

57   protected IChangeNotifier changeNotifier;
58
59   /**
60    * An instance is created from an adapter factory.
61    * The factory is used as a key so that we always know which factory created this adapter.
62    */

63   public ItemProviderDecorator(AdapterFactory adapterFactory)
64   {
65     this.adapterFactory = adapterFactory;
66   }
67
68   /**
69    * This returns true, only if this adapter was created by the given factory; the adapter factory is used as the type key.
70    */

71   public boolean isAdapterForType(Object JavaDoc type)
72   {
73     return type == adapterFactory;
74   }
75
76   /**
77    * This provides access to the factory.
78    */

79   public AdapterFactory getAdapterFactory()
80   {
81     return adapterFactory;
82   }
83
84   /**
85    * This returns the item provider that this decorates.
86    */

87   public IChangeNotifier getDecoratedItemProvider()
88   {
89     return decoratedItemProvider;
90   }
91
92   /**
93    * This sets the item provider that this decorates;
94    * it also hooks up the {@link org.eclipse.emf.edit.provider.INotifyChangedListener}.
95    */

96   public void setDecoratedItemProvider(IChangeNotifier decoratedItemProvider)
97   {
98     if (this.decoratedItemProvider != null)
99     {
100       this.decoratedItemProvider.removeListener(this);
101     }
102
103     this.decoratedItemProvider = decoratedItemProvider;
104
105     if (decoratedItemProvider != null)
106     {
107       decoratedItemProvider.addListener(this);
108     }
109   }
110
111   public void addListener(INotifyChangedListener listener)
112   {
113     if (changeNotifier == null)
114     {
115       changeNotifier = new ChangeNotifier();
116     }
117     changeNotifier.addListener(listener);
118   }
119
120   public void removeListener(INotifyChangedListener listener)
121   {
122     if (changeNotifier != null)
123     {
124       changeNotifier.removeListener(listener);
125     }
126   }
127
128   /**
129    * This delegates to the {@link #adapterFactory}.
130    * The event type is a value from the static constants in {@link org.eclipse.emf.common.notify.Notifier}.
131    */

132   public void fireNotifyChanged(Notification notification)
133   {
134     if (adapterFactory instanceof IChangeNotifier)
135     {
136       IChangeNotifier adapterFactoryChangeNotifier = (IChangeNotifier)adapterFactory;
137       adapterFactoryChangeNotifier.fireNotifyChanged(notification);
138     }
139     if (changeNotifier != null)
140     {
141       changeNotifier.fireNotifyChanged(notification);
142     }
143   }
144
145   /**
146    * This implements {@link IItemPropertySource#getPropertyDescriptors IItemPropertySource.getPropertyDescriptors}
147    * by delegating to <code>(IItemPropertySource)</code>{@link #decoratedItemProvider}.
148    */

149   public List JavaDoc getPropertyDescriptors(Object JavaDoc object)
150   {
151     return ((IItemPropertySource)decoratedItemProvider).getPropertyDescriptors(object);
152   }
153
154   /**
155    * This implements {@link IItemPropertySource#getPropertyDescriptors IItemPropertySource.getPropertyDescriptors}
156    * by delegating to <code>(IItemPropertySource)</code>{@link #decoratedItemProvider}.
157    */

158   public IItemPropertyDescriptor getPropertyDescriptor(Object JavaDoc object, Object JavaDoc propertyId)
159   {
160     return ((IItemPropertySource)decoratedItemProvider).getPropertyDescriptor(object, propertyId);
161   }
162
163   /**
164    * This implements {@link IItemPropertySource#getEditableValue IItemPropertySource.getEditableValue}
165    * by delegating to <code>(IItemPropertySource)</code>{@link #decoratedItemProvider}.
166    */

167   public Object JavaDoc getEditableValue(Object JavaDoc object)
168   {
169     return ((IItemPropertySource)decoratedItemProvider).getEditableValue(object);
170   }
171
172   /**
173    * This implements {@link IStructuredItemContentProvider#getElements IStructuredItemContentProvider.getElements}
174    * by delegating to <code>(IStructuredItemContentProvider)</code>{@link #decoratedItemProvider}.
175    */

176   public Collection JavaDoc getElements(Object JavaDoc object)
177   {
178     return ((IStructuredItemContentProvider)decoratedItemProvider).getElements(object);
179   }
180
181   /**
182    * This implements {@link ITreeItemContentProvider#getChildren ITreeItemContentProvider.getChildren}
183    * by delegating to <code>(ITreeItemContentProvider)</code>{@link #decoratedItemProvider}.
184    */

185   public Collection JavaDoc getChildren(Object JavaDoc object)
186   {
187     return ((ITreeItemContentProvider)decoratedItemProvider).getChildren(object);
188   }
189
190   /**
191    * This implements {@link ITreeItemContentProvider#hasChildren ITreeItemContentProvider.hasChildren}
192    * by delegating to <code>(ITreeItemContentProvider)</code>{@link #decoratedItemProvider}.
193    */

194   public boolean hasChildren(Object JavaDoc object)
195   {
196     return ((ITreeItemContentProvider)decoratedItemProvider).hasChildren(object);
197   }
198
199   /**
200    * This implements {@link ITreeItemContentProvider#getParent ITreeItemContentProvider.getParent}
201    * by delegating to <code>(ITreeItemContentProvider)</code>{@link #decoratedItemProvider}.
202    */

203   public Object JavaDoc getParent(Object JavaDoc object)
204   {
205     return ((ITreeItemContentProvider)decoratedItemProvider).getParent(object);
206   }
207
208   /**
209    * This implements {@link IItemLabelProvider#getImage IItemLabelProvider.getImage}
210    * by delegating to <code>(IItemLabelProvider)</code>{@link #decoratedItemProvider}.
211    */

212   public Object JavaDoc getImage(Object JavaDoc object)
213   {
214     return ((IItemLabelProvider)decoratedItemProvider).getImage(object);
215   }
216
217   /**
218    * This implements {@link ITableItemLabelProvider#getColumnImage ITableItemLabelProvider.getColumnImage}
219    * by delegating to <code>(ITableItemLabelProvider)</code>{@link #decoratedItemProvider}.
220    */

221   public Object JavaDoc getColumnImage(Object JavaDoc object, int columnIndex)
222   {
223     return ((ITableItemLabelProvider)decoratedItemProvider).getColumnImage(object, columnIndex);
224   }
225
226   /**
227    * This implements {@link IItemLabelProvider#getText IItemLabelProvider.getText}
228    * by delegating to <code>(IItemLabelProvider)</code>{@link #decoratedItemProvider}.
229    */

230   public String JavaDoc getText(Object JavaDoc object)
231   {
232     return ((IItemLabelProvider)decoratedItemProvider).getText(object);
233   }
234
235   /**
236    * This implements {@link ITableItemLabelProvider#getColumnText ITableItemLabelProvider.getColumnText}
237    * by delegating to <code>(ITableItemLabelProvider)</code>{@link #decoratedItemProvider}.
238    */

239   public String JavaDoc getColumnText(Object JavaDoc object, int columnIndex)
240   {
241     return ((ITableItemLabelProvider)decoratedItemProvider).getColumnText(object, columnIndex);
242   }
243
244   /**
245    * This implements {@link IUpdateableItemText#getUpdateableText IUpdateableItemText.getUpdateableText}
246    * by delegating to <code>(IUpdateableItemText)</code>{@link #decoratedItemProvider}.
247    */

248   public String JavaDoc getUpdateableText(Object JavaDoc object)
249   {
250     return ((IUpdateableItemText)decoratedItemProvider).getUpdateableText(object);
251   }
252
253   /**
254    * This implements {@link IEditingDomainItemProvider#getNewChildDescriptors
255    * IEditingDomainItemProvider.getNewChildDescriptors} by delegating to
256    * <code>(IEditingDomainItemProvider)</code>{@link #decoratedItemProvider}.
257    */

258   public Collection JavaDoc getNewChildDescriptors(Object JavaDoc object, EditingDomain editingDomain, Object JavaDoc sibling)
259   {
260     return ((IEditingDomainItemProvider)decoratedItemProvider).getNewChildDescriptors(object, editingDomain, sibling);
261   }
262
263   /**
264    * This implements {@link IEditingDomainItemProvider#createCommand IEditingDomainItemProvider.createCommand}
265    * by delegating to <code>(IEditingDomainItemProvider)</code>{@link #decoratedItemProvider}.
266    */

267   public Command createCommand(Object JavaDoc object, EditingDomain domain, Class JavaDoc commandClass, CommandParameter commandParameter)
268   {
269     return ((IEditingDomainItemProvider)decoratedItemProvider).createCommand(object, domain, commandClass, commandParameter);
270 /*
271     // For convenience of overrides, this class delegates to the various createXyzCommand methods first.
272     if (commandClass == SetCommand.class)
273     {
274       return
275         createSetCommand(domain, commandParameter.getOwner(), commandParameter.getFeature(), commandParameter.getValue());
276     }
277     else if (commandClass == CopyCommand.class)
278     {
279       return
280         createCopyCommand(domain, commandParameter.getOwner(), (CopyCommand.Helper)commandParameter.getValue());
281     }
282     else if (commandClass == CreateCopyCommand.class)
283     {
284       return
285         createCreateCopyCommand(domain, commandParameter.getOwner(), (CopyCommand.Helper)commandParameter.getValue());
286     }
287     else if (commandClass == InitializeCopyCommand.class)
288     {
289       return
290         createInitializeCopyCommand(domain, commandParameter.getOwner(), (CopyCommand.Helper)commandParameter.getValue());
291     }
292     else if (commandClass == RemoveCommand.class)
293     {
294       return
295         createRemoveCommand(domain, commandParameter.getOwner(), commandParameter.getFeature(), commandParameter.getCollection());
296     }
297     else if (commandClass == AddCommand.class)
298     {
299       return
300         createAddCommand
301           (domain,
302            commandParameter.getOwner(),
303            commandParameter.getFeature(),
304            commandParameter.getCollection(),
305            commandParameter.getIndex());
306     }
307     else if (commandClass == MoveCommand.class)
308     {
309       return
310         createMoveCommand
311           (domain,
312            commandParameter.getOwner(),
313            commandParameter.getFeature(),
314            commandParameter.getValue(),
315            commandParameter.getIndex());
316     }
317     else if (commandClass == ReplaceCommand.class)
318     {
319       return
320         createReplaceCommand
321           (domain,
322            commandParameter.getOwner(),
323            commandParameter.getFeature(),
324            commandParameter.getValue(),
325            commandParameter.getCollection());
326     }
327     else if (commandClass == DragAndDropCommand.class)
328     {
329       DragAndDropCommand.Detail detail = (DragAndDropCommand.Detail)commandParameter.getFeature();
330       return
331         createDragAndDropCommand
332           (domain,
333            commandParameter.getOwner(),
334            detail.location,
335            detail.operations,
336            detail.operation,
337            commandParameter.getCollection());
338     }
339     else
340     {
341       return ((IEditingDomainItemProvider)decoratedItemProvider).createCommand(object, domain, commandClass, commandParameter);
342     }
343 */

344   }
345
346 /*
347   protected Command createSetCommand(EditingDomain domain, Object owner, Object feature, Object value)
348   {
349     return
350       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
351         (owner, domain, SetCommand.class, new CommandParameter(owner, feature, value));
352   }
353
354   protected Command createCopyCommand(EditingDomain domain, Object owner, CopyCommand.Helper helper)
355   {
356     return
357       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
358         (owner, domain, CopyCommand.class, new CommandParameter(owner, null, helper));
359   }
360
361   protected Command createCreateCopyCommand(EditingDomain domain, Object owner, CopyCommand.Helper helper)
362   {
363     return
364       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
365         (owner, domain, CreateCopyCommand.class, new CommandParameter(owner, null, helper));
366   }
367
368   protected Command createInitializeCopyCommand(EditingDomain domain, Object owner, CopyCommand.Helper helper)
369   {
370     return
371       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
372         (owner, domain, InitializeCopyCommand.class, new CommandParameter(owner, null, helper));
373   }
374
375   protected Command createRemoveCommand(EditingDomain domain, Object owner, Object feature, Collection collection)
376   {
377     return
378       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
379         (owner, domain, RemoveCommand.class, new CommandParameter(owner, feature, collection));
380   }
381
382   protected Command createReplaceCommand(EditingDomain domain, Object owner, Object feature, Object value, Collection collection)
383   {
384     return
385       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
386         (owner, domain, ReplaceCommand.class, new CommandParameter(owner, feature, value, collection));
387   }
388
389   protected Command createAddCommand(EditingDomain domain, Object owner, Object feature, Collection collection, int index)
390   {
391     return
392       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
393         (owner, domain, ReplaceCommand.class, new CommandParameter(owner, feature, collection, index));
394   }
395
396   protected Command createMoveCommand(EditingDomain domain, Object owner, Object feature, Object value, int index)
397   {
398     return
399       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
400         (owner, domain, ReplaceCommand.class, new CommandParameter(owner, feature, value, index));
401   }
402
403   protected Command createDragAndDropCommand
404     (EditingDomain domain, Object owner, float location, int operations, int operation, Collection collection)
405   {
406     DragAndDropCommand.Detail detail = new DragAndDropCommand.Detail(location, operations, operation);
407     return
408       ((IEditingDomainItemProvider)decoratedItemProvider).createCommand
409         (owner, domain, DragAndDropCommand.class, new CommandParameter(owner, detail, collection));
410   }
411 */

412
413   /**
414    * This will be called by the {@link #decoratedItemProvider} when it normally fires a notification to it's adapter factory.
415    * This listener method is hooked up in {@link #setDecoratedItemProvider}.
416    */

417   public void notifyChanged(Notification notification)
418   {
419     fireNotifyChanged(notification);
420   }
421
422   public void dispose()
423   {
424     if (decoratedItemProvider != null)
425     {
426       decoratedItemProvider.removeListener(this);
427     }
428   }
429
430   public String JavaDoc toString()
431   {
432     return getClass().getName() + '@' + Integer.toHexString(hashCode());
433   }
434 }
435
Popular Tags