KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 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: WrapperItemProvider.java,v 1.7 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.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.eclipse.emf.common.command.Command;
26 import org.eclipse.emf.common.command.CommandWrapper;
27 import org.eclipse.emf.common.command.UnexecutableCommand;
28 import org.eclipse.emf.common.notify.AdapterFactory;
29 import org.eclipse.emf.common.util.ResourceLocator;
30 import org.eclipse.emf.ecore.EObject;
31 import org.eclipse.emf.ecore.EStructuralFeature;
32 import org.eclipse.emf.edit.EMFEditPlugin;
33 import org.eclipse.emf.edit.command.AbstractOverrideableCommand;
34 import org.eclipse.emf.edit.command.CommandParameter;
35 import org.eclipse.emf.edit.command.CopyCommand;
36 import org.eclipse.emf.edit.command.DragAndDropCommand;
37 import org.eclipse.emf.edit.command.SetCommand;
38 import org.eclipse.emf.edit.domain.EditingDomain;
39
40
41 /**
42  * A basic implementation of <code>IWrapperProvider</code> from which others can extend. This class provides
43  * all the methods required to implement the following item provider iterfaces:
44  * <ul>
45  * <li>{@link IStructuredItemContentProvider}
46  * <li>{@link ITreeItemContentProvider}
47  * <li>{@link IItemLabelProvider}
48  * <li>{@link IItemPropertySource}
49  * <li>{@link IEditingDomainItemProvider}
50  * </ul>
51  * <p>Subclasses should declare which of these interfaces they are meant to implement, and override methods as needed.
52  * In addition, a partial implementation for {@link IUpdateableItemText} is provided, along with additional methods and
53  * classes that are useful in implementing multiple subclasses.
54  */

55 public class WrapperItemProvider implements IWrapperItemProvider
56 {
57   /**
58    * The wrapped value.
59    */

60   protected Object JavaDoc value;
61
62   /**
63    * The object that owns the value.
64    */

65   protected Object JavaDoc owner;
66
67   /**
68    * The structural feature, if applicable, through which the value can be set and retrieved.
69    */

70   protected EStructuralFeature feature;
71
72   /**
73    * The index at which the value is located. If {@link #feature} is non-null, this index is within that feature.
74    */

75   protected int index;
76
77   /**
78    * The adapter factory for the owner's item provider.
79    */

80   protected AdapterFactory adapterFactory;
81
82   /**
83    * Creates an instance. The adapter factory of the owner's item provider may be needed for echoing notifications and
84    * providing property descriptors.
85    */

86   public WrapperItemProvider(Object JavaDoc value, Object JavaDoc owner, EStructuralFeature feature, int index, AdapterFactory adapterFactory)
87   {
88     this.value = value;
89     this.owner = owner;
90     this.feature = feature;
91     this.index = index;
92     this.adapterFactory = adapterFactory;
93   }
94
95   /**
96    * Disposes the wrapper by deactivating any notification that this wrapper may provide. Since this implementation
97    * does not provide any notification, this method does nothing.
98    */

99   public void dispose()
100   {
101   }
102
103   /**
104    * Returns the wrapped value.
105    */

106   public Object JavaDoc getValue()
107   {
108     return value;
109   }
110
111   /**
112    * Returns the object that owns the value.
113    */

114   public Object JavaDoc getOwner()
115   {
116     return owner;
117   }
118
119   /**
120    * Returns the structural feature through which the value can be set and retrieved, or null if the feature is
121    * unknown or not applicable.
122    */

123   public EStructuralFeature getFeature()
124   {
125     return feature;
126   }
127
128   /**
129    * The index at which the value is located, or {@link org.eclipse.emf.edit.command.CommandParameter#NO_INDEX} if the
130    * index isn't known to the wrapper. If {@link #feature} is non-null, this index is within that feature.
131    */

132   public int getIndex()
133   {
134     return index;
135   }
136
137   /**
138    * Sets the index. Has no effect if the index isn't known to the wrapper.
139    */

140   public void setIndex(int index)
141   {
142     this.index = index;
143   }
144
145   /**
146    * {@link IStructuredItemContentProvider#getElements IStructuredItemContentProvider.getElements} is implemented by
147    * forwarding the call to {@link #getChildren getChildren}.
148    */

149   public Collection JavaDoc getElements(Object JavaDoc object)
150   {
151     return getChildren(object);
152   }
153  
154   /**
155    * {@link ITreeItemContentProvider#getChildren ITreeItemContentProvider.getChildren} is implemented to return an
156    * empty list. Subclasses may override it to return something else.
157    */

158   public Collection JavaDoc getChildren(Object JavaDoc object)
159   {
160     return Collections.EMPTY_LIST;
161   }
162
163   /**
164    * {@link ITreeItemContentProvider#hasChildren ITreeItemContentProvider.hasChildren} is implemented by testing
165    * whether the collection returned by {@link #getChildren getChildren} is non-empty.
166    */

167   public boolean hasChildren(Object JavaDoc object)
168   {
169     return !getChildren(object).isEmpty();
170   }
171
172   /**
173    * {@link ITreeItemContentProvider#getParent ITreeItemContentProvider.getParent} is implemented by returning the
174    * {@link #owner}.
175    */

176   public Object JavaDoc getParent(Object JavaDoc object)
177   {
178     return owner;
179   }
180
181   /**
182    * {@link org.eclipse.emf.edit.provider.IItemLabelProvider#getText IItemLabelProvider.getText} is implemented by returning a non-null value, as a
183    * string, or "null".
184    */

185   public String JavaDoc getText(Object JavaDoc object)
186   {
187     return value != null ? value.toString() : "null";
188   }
189
190   /**
191    * {@link org.eclipse.emf.edit.provider.IItemLabelProvider#getImage IItemLabelProvider.getImage} is implemented by returning the default icon for
192    * an EMF.Edit item.
193    */

194   public Object JavaDoc getImage(Object JavaDoc object)
195   {
196     return EMFEditPlugin.INSTANCE.getImage("full/obj16/Item");
197   }
198
199   /**
200    * {@link IUpdateableItemText#getUpdateableText IUpdateableItemText.getUpdateableText} is implemented by forwarding
201    * the call to {@link #getText getText}.
202    */

203   public String JavaDoc getUpdateableText(Object JavaDoc object)
204   {
205     return getText(object);
206   }
207
208   /**
209    * {@link IItemPropertySource#getPropertyDescriptors IItemPropertySource.getPropertyDescriptors} is implemented to
210    * return an empty list. Subclasses may override it to return something else.
211    */

212   public List JavaDoc getPropertyDescriptors(Object JavaDoc object)
213   {
214     return Collections.EMPTY_LIST;
215   }
216
217   /**
218    * {@link IItemPropertySource#getPropertyDescriptor IItemPropertySource.getPropertyDescriptor} is implemented by
219    * iterating over the descriptors returned by {@link #getPropertyDescriptors getPropertyDescriptors}, and returning
220    * the first descriptor whose ID matches the specified ID, or null if none match.
221    */

222   public IItemPropertyDescriptor getPropertyDescriptor(Object JavaDoc object, Object JavaDoc propertyId)
223   {
224     for (Iterator JavaDoc i = getPropertyDescriptors(object).iterator(); i.hasNext(); )
225     {
226       IItemPropertyDescriptor descriptor = (IItemPropertyDescriptor)i.next();
227       if (propertyId.equals(descriptor.getId(object)))
228       {
229         return descriptor;
230       }
231     }
232     return null;
233   }
234
235   /**
236    * {@link IItemPropertySource#getEditableValue IItemPropertySource.getEditableValue} is implemented to return the
237    * value, itself.
238    */

239   public Object JavaDoc getEditableValue(Object JavaDoc object)
240   {
241     return value;
242   }
243
244   /**
245    * Returns a name for a value's single property. Subclasses may use this in creating a property descriptor, and user
246    * subclasses may override it to provide a specific name.
247    */

248   protected String JavaDoc getPropertyName()
249   {
250     return EMFEditPlugin.INSTANCE.getString("_UI_ValueProperty_name");
251   }
252
253   /**
254    * Returns a description for a value's single property. Subclasses may use this in creating a property descriptor,
255    * and user subclasses may override it to provide a specific name.
256    */

257   protected String JavaDoc getPropertyDescription()
258   {
259     return EMFEditPlugin.INSTANCE.getString("_UI_ValueProperty_description");
260   }
261
262   /**
263    * Returns whether a value's single property is settable. By default, this returns whether the structural feature is
264    * {@link org.eclipse.emf.ecore.EStructuralFeature#isChangeable changeable}. Subclasses may use this in creating a
265    * property descriptor, and user subclasses may override it to restrict or allow setting of the property.
266    */

267   protected boolean isPropertySettable()
268   {
269     return feature.isChangeable();
270   }
271
272   /**
273    * Returns an image for a value's single property. By default, a standard property icon is selected based on the type
274    * of the structural feature. Subclasses may use this in creating a property descriptor, and user subclasses may
275    * override it to select a different icon.
276    */

277   protected Object JavaDoc getPropertyImage()
278   {
279     return getPropertyImage(feature.getEType().getInstanceClass());
280   }
281
282   /**
283    * Returns the property image for the specified type. Implementations of {@link #getPropertyImage getPropertyImage}
284    * typically call this method.
285    */

286   protected Object JavaDoc getPropertyImage(Class JavaDoc typeClass)
287   {
288     if (typeClass == Boolean.TYPE || typeClass == Boolean JavaDoc.class)
289     {
290       return ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE;
291     }
292     else if (typeClass == Byte.TYPE || typeClass == Byte JavaDoc.class ||
293              typeClass == Integer.TYPE || typeClass == Integer JavaDoc.class ||
294              typeClass == Long.TYPE || typeClass == Long JavaDoc.class ||
295              typeClass == Short.TYPE || typeClass == Short JavaDoc.class)
296     {
297       return ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE;
298     }
299     else if (typeClass == Character.TYPE || typeClass == Character JavaDoc.class ||
300              typeClass == String JavaDoc.class)
301     {
302       return ItemPropertyDescriptor.TEXT_VALUE_IMAGE;
303     }
304     else if (typeClass == Double.TYPE || typeClass == Double JavaDoc.class ||
305              typeClass == Float.TYPE || typeClass == Float JavaDoc.class)
306     {
307       return ItemPropertyDescriptor.REAL_VALUE_IMAGE;
308     }
309
310     return ItemPropertyDescriptor.GENERIC_VALUE_IMAGE;
311   }
312
313   /**
314    * Returns a category for a value's single property. By default, null is returned. Subclasses may use this in
315    * creating a property descriptor, and user subclasses may override it to actually provide a category.
316    */

317   protected String JavaDoc getPropertyCategory()
318   {
319     return null;
320   }
321
322   /**
323    * Returns filter flags for a value's single property. By default, null is returned. Subclasses may use this in
324    * createing a property descriptor, and user subclasses may override it to actually provide a category.
325    */

326   protected String JavaDoc[] getPropertyFilterFlags()
327   {
328     return null;
329   }
330
331   /**
332    * {@link IEditingDomainItemProvider#getNewChildDescriptors IEditingDomainItemProvider.getNewChildDescriptors} is
333    * implemented to return an empty list. Subclasses may override it to return something else.
334    */

335   public Collection JavaDoc getNewChildDescriptors(Object JavaDoc object, EditingDomain editingDomain, Object JavaDoc sibling)
336   {
337     return Collections.EMPTY_LIST;
338   }
339
340   /**
341    * {IEditingDomainItemProvider#createCommand IEditingDomainItemProvider.createCommand} is implemented via {@link
342    * #baseCreateCommand baseCreateCommand} to create set, copy, and drag-and-drop commands, only.
343    */

344   public Command createCommand(Object JavaDoc object, EditingDomain domain, Class JavaDoc commandClass, CommandParameter commandParameter)
345   {
346     return baseCreateCommand(object, domain, commandClass, commandParameter);
347   }
348
349   /**
350    * Implements creation of a set, copy, or drag-and-drop command by calling out to {@link #createSetCommand
351    * createSetCommand}, {@link #createCopyCommand createCopyCommand}, or {@link #createDragAndDropCommand
352    * createDragAndDropCommand}.
353    */

354   public Command baseCreateCommand(Object JavaDoc object, EditingDomain domain, Class JavaDoc commandClass, CommandParameter commandParameter)
355   {
356     if (commandClass == SetCommand.class)
357     {
358       return createSetCommand(domain, commandParameter.getOwner(), commandParameter.getFeature(), commandParameter.getValue(), commandParameter.getIndex());
359     }
360     else if (commandClass == CopyCommand.class)
361     {
362       return createCopyCommand(domain, commandParameter.getOwner(), (CopyCommand.Helper)commandParameter.getValue());
363     }
364     else if (commandClass == DragAndDropCommand.class)
365     {
366       DragAndDropCommand.Detail detail = (DragAndDropCommand.Detail)commandParameter.getFeature();
367       return createDragAndDropCommand(domain, commandParameter.getOwner(), detail.location, detail.operations, detail.operation, commandParameter.getCollection());
368     }
369     else
370     {
371       return UnexecutableCommand.INSTANCE;
372     }
373   }
374
375   /**
376    * Return an {@link org.eclipse.emf.common.command.UnexecutableCommand}. Subclasses should override this to map this
377    * into a real set on a model object.
378    */

379   protected Command createSetCommand(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value, int index)
380   {
381     return UnexecutableCommand.INSTANCE;
382   }
383
384   /**
385    * Returns an {@link org.eclipse.emf.common.command.UnexecutableCommand}. An ordinary {@link
386    * org.eclipse.emf.edit.command.CopyCommand} is only useful for copying model objects, so it would be inappropriate
387    * here. Subclasses should override it to return something more useful, like a concrete subclass of a {@link
388    * SimpleCopyCommand} or {@link WrappingCopyCommand}.
389    */

390   protected Command createCopyCommand(EditingDomain domain, Object JavaDoc owner, CopyCommand.Helper helper)
391   {
392     return UnexecutableCommand.INSTANCE;
393   }
394
395   /**
396    * Creates a {@link org.eclipse.emf.edit.command.DragAndDropCommand}.
397    */

398   protected Command createDragAndDropCommand(EditingDomain domain, Object JavaDoc owner, float location, int operations, int operation, Collection JavaDoc collection)
399   {
400     return new DragAndDropCommand(domain, owner, location, operations, operation, collection);
401   }
402
403   /**
404    * A label for copy command inner classes, the same one used by {@link org.eclipse.emf.edit.command.CopyCommand}.
405    */

406   protected static final String JavaDoc COPY_COMMAND_LABEL = EMFEditPlugin.INSTANCE.getString("_UI_CopyCommand_label");
407
408   /**
409    * A description for copy command inner classes, the same as in {@link org.eclipse.emf.edit.command.CopyCommand}.
410    */

411   protected static final String JavaDoc COPY_COMMAND_DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_CopyCommand_description");
412
413   /**
414    * A command base class for copying a simple value and the wrapper. This is useful when the value isn't able provide
415    * an adapter to return a copy command, itself. This class just provides the scaffolding; concrete subclasses must
416    * implement {@link #copy copy} to do the copying.
417    */

418   protected abstract class SimpleCopyCommand extends AbstractOverrideableCommand
419   {
420     protected Collection JavaDoc result = Collections.EMPTY_LIST;
421     protected Collection JavaDoc affectedObjects;
422
423     /**
424      * Creates an instance for the given domain.
425      */

426     public SimpleCopyCommand(EditingDomain domain)
427     {
428       super(domain, COPY_COMMAND_LABEL, COPY_COMMAND_DESCRIPTION);
429     }
430
431     /**
432      * Returns true; this command can requires now preparation and can always be executed.
433      */

434     protected boolean prepare()
435     {
436       return true;
437     }
438
439     /**
440      * Calls {@link #copy} to do the copying, {@link IDisposable#dispose disposes} the copy, and sets it to
441      * be the result of the command. Since the copy has not been created within the viewed model, it should never do
442      * any kind of notification, which is why it is immediately disposed.
443      */

444     public void doExecute()
445     {
446       IWrapperItemProvider copy = copy();
447       copy.dispose();
448       result = Collections.singletonList(copy);
449     }
450
451     /**
452      * Concrete subclasses must implement this to copy and return the value and wrapper.
453      */

454     public abstract IWrapperItemProvider copy();
455
456     /**
457      * Does nothing.
458      */

459     public void doUndo()
460     {
461       // no-op
462
}
463
464     /**
465      * Does nothing.
466      */

467     public void doRedo()
468     {
469       // no-op
470
}
471
472     /**
473      * If the command has executed, returns a list containing only the copy of the wrapper.
474      */

475     public Collection JavaDoc doGetResult()
476     {
477       return result;
478     }
479
480     /**
481      * Returns a list containing only the original wrapper itself.
482      */

483     public Collection JavaDoc doGetAffectedObjects()
484     {
485       if (affectedObjects == null)
486       {
487         affectedObjects = Collections.singletonList(WrapperItemProvider.this);
488       }
489       return affectedObjects;
490     }
491   }
492
493   /**
494    * A command base class for copying the wrapper for a value that is partly copied by another command. This is useful
495    * when the value includes a model object that is able provide an adapter to return a copy command, but also includes
496    * an element that is not adaptable, such as a feature map entry. This command copies the non-adapter element and the
497    * wrapper, which ensures the copy can be copied again.
498    */

499   protected abstract class WrappingCopyCommand extends CommandWrapper
500   {
501     protected Collection JavaDoc result = Collections.EMPTY_LIST;
502     protected Collection JavaDoc affectedObjects;
503
504     /**
505      * Creates an instance where some adaptable value is copied by the given command.
506      */

507     public WrappingCopyCommand(Command command)
508     {
509       super(command);
510     }
511
512     /**
513      * Executes the adaptable-value-copying command, then calls {@link #copy copy} to copy the rest of the value and
514      * the wrapper, {@link IDisposable#dispose disposes} the copy, and sets it to be the result of the
515      * command. Since the copy has not been created within the viewed model, it should never do any kind of
516      * notification, which is why it is immediately disposed.
517      */

518     public void execute()
519     {
520       super.execute();
521       IWrapperItemProvider copy = copy();
522       copy.dispose();
523       result = Collections.singletonList(copy);
524     }
525
526     /**
527      * Concrete subclasses must implement this to copy and return the value and wrapper. The result of the
528      * adaptable-value-copying command is available from <code>getCommand().getResult()</code>.
529      */

530     public abstract IWrapperItemProvider copy();
531
532     /**
533      * If the command has executed, returns a list containing only the copy of the wrapper.
534      */

535     public Collection JavaDoc getResult()
536     {
537       return result;
538     }
539
540     /**
541      * Returns a list containing only the original wrapper itself.
542      */

543     public Collection JavaDoc getAffectedObjects()
544     {
545       if (affectedObjects == null)
546       {
547         affectedObjects = Collections.singletonList(WrapperItemProvider.this);
548       }
549       return affectedObjects;
550     }
551   }
552
553   /**
554    * Returns the {@link #adapterFactory}, if non-composeable, otherwise, returns its root adapter factory.
555    */

556   protected AdapterFactory getRootAdapterFactory()
557   {
558     return adapterFactory instanceof ComposeableAdapterFactory ?
559       ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory() :
560       adapterFactory;
561   }
562
563   /**
564    * An item property descriptor for the single property of a wrapper for a simple value. This extends the base
565    * implentation and substitutes the wrapper's owner for the selected object (the wrapper itself) in the call to {@link
566    * #getPropertyValue getPropertyValue}. Thus, the owner must be an EObject to use this class. The property's name,
567    * description, settable flag, static image, category, and filter flags are obtained by calling out to various
568    * template methods, so can be easily changed by subclassing.
569    */

570   protected class WrapperItemPropertyDescriptor extends ItemPropertyDescriptor
571   {
572     public WrapperItemPropertyDescriptor(ResourceLocator resourceLocator, EStructuralFeature feature)
573     {
574       super(
575         WrapperItemProvider.this.adapterFactory,
576         resourceLocator,
577         getPropertyName(),
578         getPropertyDescription(),
579         feature,
580         isPropertySettable(),
581         getPropertyImage(),
582         getPropertyCategory(),
583         getPropertyFilterFlags());
584     }
585
586     /**
587      * Substitutes the wrapper owner for the selected object and invokes the base implementation. The actual value
588      * returned depends on the implementation of {@link #getValue getValue}.
589      */

590     public Object JavaDoc getPropertyValue(Object JavaDoc object)
591     {
592       return super.getPropertyValue(owner);
593     }
594
595     /**
596      * Returns <code>true</code>, as the property of a value wrapper is always considered to be set.
597      */

598     public boolean isPropertySet(Object JavaDoc object)
599     {
600       return true;
601     }
602
603     /**
604      * Does nothing, as resetting the property of a value wrapper is not meaningful.
605      */

606     public void resetPropertyValue(Object JavaDoc object)
607     {
608     }
609
610     /**
611      * Sets the property value. If an editing domain can be obtained, the command returned by {@link #createSetCommand
612      * createSetcommand} is executed; otherwise, {@link #setValue setValue} is called to set the value.
613      */

614     public void setPropertyValue(Object JavaDoc object, Object JavaDoc value)
615     {
616       EObject eObject = (EObject)owner;
617       EditingDomain editingDomain = getEditingDomain(owner);
618
619       if (editingDomain == null)
620       {
621         setValue(eObject, feature, value);
622       }
623       else
624       {
625         editingDomain.getCommandStack().execute(createSetCommand(editingDomain, eObject, feature, value));
626       }
627     }
628
629     /**
630      * Returns a value from a model object. If the feature is multi-valued, only the single value that the wrapper
631      * represents is returned.
632      */

633     protected Object JavaDoc getValue(EObject object, EStructuralFeature feature)
634     {
635       // When the value is changed, the property sheet page doesn't update the property sheet viewer input
636
// before refreshing, and this gets called on the obsolete wrapper. So, we need to read directly from the
637
// model object.
638
//
639
//return value;
640

641       Object JavaDoc result = ((EObject)owner).eGet(feature);
642       if (feature.isMany())
643       {
644         // If the last object was deleted and the selection was in the property sheet view, the obsolete wrapper will
645
// reference past the end of the list.
646
//
647
List JavaDoc list = (List JavaDoc)result;
648         result = index >= 0 && index < list.size() ? list.get(index) : value;
649       }
650       return result;
651     }
652
653     /**
654      * Sets a value on a model object. If the feature is multi-valued, only the single value that the wrapper
655      * represents is set.
656      */

657     protected void setValue(EObject object, EStructuralFeature feature, Object JavaDoc value)
658     {
659       if (feature.isMany())
660       {
661         ((List JavaDoc)object.eGet(feature)).set(index, value);
662       }
663       else
664       {
665         object.eSet(feature, value);
666       }
667     }
668
669     /**
670      * Returns a command that will set the value on the model object. The wrapper is used as the owner of the command,
671      * unless overridden, so that it can specialize the command that eventually gets created.
672      */

673     protected Command createSetCommand(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value)
674     {
675       return SetCommand.create(domain, getCommandOwner(WrapperItemProvider.this), null, value);
676     }
677
678     /**
679      * Returns <code>false</code>, as the property only represents a single value, even if the feature is multi-valued.
680      */

681     public boolean isMany(Object JavaDoc object)
682     {
683       return false;
684     }
685
686     /**
687      * Substitutes the wrapper owner for the selected object and invokes the base implementation.
688      */

689     public Collection JavaDoc getChoiceOfValues(Object JavaDoc object)
690     {
691       return super.getChoiceOfValues(owner);
692     }
693   }
694
695   /**
696    * A <code>ReplacementAffectedObjectCommand</code> wraps another command to return as its affected objects the single
697    * wrapper that replaces this wrapper. That is, it obtains the children of the wrapper's owner, and returns a
698    * collection containing the first wrapper whose feature and index match this one's.
699    */

700   protected class ReplacementAffectedObjectCommand extends CommandWrapper
701   {
702     public ReplacementAffectedObjectCommand(Command command)
703     {
704       super(command);
705     }
706
707     /**
708      * Obtains the children of the wrapper's owner, and returns a collection containing the first wrapper whose feature
709      * and index match this one's.
710      */

711     public Collection JavaDoc getAffectedObjects()
712     {
713       Collection JavaDoc children = Collections.EMPTY_LIST;
714
715       // Either the IEditingDomainItemProvider or ITreeItemContentProvider item provider interface can give us
716
// the children.
717
//
718
Object JavaDoc adapter = adapterFactory.adapt(owner, IEditingDomainItemProvider.class);
719       if (adapter instanceof IEditingDomainItemProvider)
720       {
721         children = ((IEditingDomainItemProvider)adapter).getChildren(owner);
722       }
723       else
724       {
725         adapter = adapterFactory.adapt(owner, ITreeItemContentProvider.class);
726         if (adapter instanceof ITreeItemContentProvider)
727         {
728           children = ((ITreeItemContentProvider)adapter).getChildren(owner);
729         }
730       }
731
732       for (Iterator JavaDoc i = children.iterator(); i.hasNext(); )
733       {
734         Object JavaDoc child = i.next();
735         if (child instanceof IWrapperItemProvider)
736         {
737           IWrapperItemProvider wrapper = (IWrapperItemProvider)child;
738           if (wrapper.getFeature() == feature && wrapper.getIndex() == index)
739           {
740             return Collections.singletonList(child);
741           }
742         }
743       }
744       return Collections.EMPTY_LIST;
745     }
746   }
747 }
748
Popular Tags