KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > provider > MappingItemProvider


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: MappingItemProvider.java,v 1.9 2005/06/12 13:38:46 emerks Exp $
16  */

17 package org.eclipse.emf.mapping.provider;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.eclipse.emf.common.command.Command;
28 import org.eclipse.emf.common.command.CommandWrapper;
29 import org.eclipse.emf.common.command.CompoundCommand;
30 import org.eclipse.emf.common.command.StrictCompoundCommand;
31 import org.eclipse.emf.common.command.UnexecutableCommand;
32 import org.eclipse.emf.common.notify.AdapterFactory;
33 import org.eclipse.emf.common.notify.Notification;
34 import org.eclipse.emf.common.util.ResourceLocator;
35 import org.eclipse.emf.common.util.TreeIterator;
36 import org.eclipse.emf.ecore.EObject;
37 import org.eclipse.emf.ecore.EStructuralFeature;
38 import org.eclipse.emf.ecore.InternalEObject;
39 import org.eclipse.emf.ecore.impl.ENotificationImpl;
40 import org.eclipse.emf.edit.command.CommandParameter;
41 import org.eclipse.emf.edit.command.CopyCommand;
42 import org.eclipse.emf.edit.command.RemoveCommand;
43 import org.eclipse.emf.edit.command.SetCommand;
44 import org.eclipse.emf.edit.domain.EditingDomain;
45 import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
46 import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
47 import org.eclipse.emf.edit.provider.IDisposable;
48 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
49 import org.eclipse.emf.edit.provider.IItemLabelProvider;
50 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
51 import org.eclipse.emf.edit.provider.IItemPropertySource;
52 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
53 import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
54 //import org.eclipse.emf.edit.provider.ViewerNotification;
55

56 import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
57 import org.eclipse.emf.edit.provider.ItemPropertyDescriptorDecorator;
58 import org.eclipse.emf.mapping.Mapping;
59 import org.eclipse.emf.mapping.MappingHelper;
60 import org.eclipse.emf.mapping.MappingPackage;
61 import org.eclipse.emf.mapping.MappingPlugin;
62 import org.eclipse.emf.mapping.MappingRoot;
63 import org.eclipse.emf.mapping.command.NameMatchMappingCommand;
64 import org.eclipse.emf.mapping.command.RemoveMappingCommand;
65 import org.eclipse.emf.mapping.command.TypeMatchMappingCommand;
66 import org.eclipse.emf.mapping.domain.MappingDomain;
67
68 // import org.eclipse.emf.edit.provider.ItemProviderAdapter;
69

70
71 /**
72  * This is the item provider adpater for a {@link org.eclipse.emf.mapping.Mapping} object.
73  */

74 public class MappingItemProvider
75   extends MappingItemProviderAdapter
76   implements
77     IEditingDomainItemProvider,
78     IStructuredItemContentProvider,
79     ITreeItemContentProvider,
80     IItemLabelProvider,
81     IItemPropertySource
82 {
83   /**
84    * This constructs an instance from a factory and a notifier.
85    */

86   public MappingItemProvider(AdapterFactory adapterFactory)
87   {
88     super(adapterFactory);
89   }
90
91   static public class TypeMappingHelperWrapper extends ItemPropertyDescriptor.PropertyValueWrapper
92   {
93     protected Mapping mapping;
94     protected MappingHelper typeMappingHelper;
95
96     public TypeMappingHelperWrapper(AdapterFactory adapterFactory, Mapping mapping, MappingHelper typeMappingHelper)
97     {
98       super(adapterFactory, mapping, typeMappingHelper, typeMappingHelper);
99       this.mapping = mapping;
100       this.typeMappingHelper = typeMappingHelper;
101     }
102
103     protected IItemPropertyDescriptor createPropertyDescriptorDecorator(Object JavaDoc object, IItemPropertyDescriptor decoratedDescriptor)
104     {
105       return
106         new ItemPropertyDescriptorDecorator(object, decoratedDescriptor)
107         {
108           public void setPropertyValue(Object JavaDoc thisObject, final Object JavaDoc value)
109           {
110             final MappingDomain domain = mapping.getMappingRoot().getDomain();
111             final Object JavaDoc feature = itemPropertyDescriptor.getFeature(value);
112             CompoundCommand compound = new StrictCompoundCommand();
113             final Command copyCommand = CopyCommand.create(domain, typeMappingHelper);
114             compound.append(copyCommand);
115             compound.append
116               (new CommandWrapper()
117                {
118                  protected Command createCommand()
119                  {
120                    return SetCommand.create(domain, copyCommand.getResult().iterator().next(), feature, value);
121                  }
122                });
123             compound.append
124               (new CommandWrapper()
125                {
126                  protected Command createCommand()
127                  {
128                    return SetCommand.create(domain, mapping, MappingPackage.eINSTANCE.getMapping_Helper(), copyCommand.getResult().iterator().next());
129                  }
130                });
131             domain.getCommandStack().execute(compound);
132           }
133
134           public void resetPropertyValue(Object JavaDoc thisObject)
135           {
136             //FB ?????
137
}
138         };
139     }
140   }
141
142   public void getPropertyDescriptorsForHelper(Mapping mapping)
143   {
144      // This is for the helper feature.
145
//
146
if (mapping.getEffectiveHelper() != null)
147      {
148        itemPropertyDescriptors.add
149          (new ItemPropertyDescriptor
150             (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
151              MappingPlugin.getPlugin().getString("_UI_Transformation_label"),
152              MappingPlugin.getPlugin().getString("_UI_Transformation_description"),
153              MappingPackage.eINSTANCE.getMapping_Helper(),
154              false)
155           {
156             public Object JavaDoc getPropertyValue(Object JavaDoc thisObject)
157             {
158               Mapping thisMapping = (Mapping)thisObject;
159               MappingHelper helper = thisMapping.getHelper();
160               if (helper != null)
161                 return helper;
162               else
163                 return new TypeMappingHelperWrapper(this.adapterFactory, thisMapping, thisMapping.getEffectiveHelper());
164             }
165           });
166      }
167   }
168
169   /**
170    * This returns the property descriptors for the adapted class.
171    */

172   public List JavaDoc getPropertyDescriptors(Object JavaDoc object)
173   {
174     if (itemPropertyDescriptors == null)
175     {
176       Mapping mapping = (Mapping)object;
177       MappingRoot mappingRoot = mapping.getMappingRoot();
178       boolean isTypeMapping = mappingRoot != null && mappingRoot.getDomain() == null;
179       MappingPackage ePackage = MappingPackage.eINSTANCE;
180
181       itemPropertyDescriptors = new ArrayList JavaDoc();
182       if (isTypeMapping)
183       {
184         int count = 1;
185         for (Iterator JavaDoc mappings = mapping.getNested().iterator(); mappings.hasNext(); ++count)
186         {
187           final Mapping childMapping = (Mapping)mappings.next();
188           IItemPropertyDescriptor childMappingItemPropertyDescriptor =
189             new ItemPropertyDescriptor
190                (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
191                 count + ". " + MappingPlugin.getPlugin().getString("_UI_Type_mapping_label") + " ",
192                 MappingPlugin.getPlugin().getString("_UI_Type_mapping_helpers_description"),
193                 ePackage.getMapping_TypeMapping());
194
195           itemPropertyDescriptors.add
196             (new ItemPropertyDescriptorDecorator(childMapping, childMappingItemPropertyDescriptor)
197              {
198                public Object JavaDoc getPropertyValue(Object JavaDoc o)
199                {
200                  super.getPropertyValue(o);
201                  return this.object;
202                }
203                public Collection JavaDoc getChoiceOfValues(Object JavaDoc o)
204                {
205                  return null;
206                }
207              });
208         }
209       }
210       else
211       {
212         getPropertyDescriptorsForHelper(mapping);
213
214         // This is for the typeMapping feature.
215
//
216
if (mapping.getTypeMapping() != null)
217         {
218           itemPropertyDescriptors.add
219             (new ItemPropertyDescriptor
220                (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
221                 MappingPlugin.getPlugin().getString("_UI_Type_mapping_label"),
222                 MappingPlugin.getPlugin().getString("_UI_Type_mapping_description"),
223                 ePackage.getMapping_TypeMapping())
224              {
225                public Object JavaDoc createPropertyValueWrapper(Object JavaDoc o, Object JavaDoc v)
226                {
227                  return v;
228                }
229                public Collection JavaDoc getChoiceOfValues(Object JavaDoc o)
230                {
231                  return null;
232                }
233              });
234         }
235       }
236     }
237     return itemPropertyDescriptors;
238   }
239
240   /**
241    * This specifies how to implement {@link #getChildren} and is used to deduce an appropriate feature for an
242    * {@link org.eclipse.emf.edit.command.AddCommand}, {@link org.eclipse.emf.edit.command.RemoveCommand} or
243    * {@link org.eclipse.emf.edit.command.MoveCommand} in {@link #createCommand}.
244    */

245   public Collection JavaDoc getChildrenFeatures(Object JavaDoc object)
246   {
247     Mapping mapping = (Mapping)object;
248     MappingRoot mappingRoot = mapping.getMappingRoot();
249     Collection JavaDoc result = new ArrayList JavaDoc();
250     if (mappingRoot == null || mappingRoot.isTopToBottom())
251     {
252       result.add(MappingPackage.eINSTANCE.getMapping_Inputs());
253       result.add(MappingPackage.eINSTANCE.getMapping_Outputs());
254     }
255     else
256     {
257       result.add(MappingPackage.eINSTANCE.getMapping_Outputs());
258       result.add(MappingPackage.eINSTANCE.getMapping_Inputs());
259     }
260
261     result.add(MappingPackage.eINSTANCE.getMapping_Nested());
262
263     return result;
264   }
265
266   protected EStructuralFeature getChildFeature(Object JavaDoc object, Object JavaDoc child)
267   {
268     Mapping mapping = (Mapping)object;
269     MappingRoot mappingRoot = mapping.getMappingRoot();
270     EObject refObject = (EObject)child;
271     if (refObject instanceof Mapping)
272     {
273       return MappingPackage.eINSTANCE.getMapping_Nested();
274     }
275     else if (mapping.getInputs().contains(child))
276     {
277       return MappingPackage.eINSTANCE.getMapping_Inputs();
278     }
279     else if (mapping.getOutputs().contains(child))
280     {
281       return MappingPackage.eINSTANCE.getMapping_Outputs();
282     }
283     else if (mappingRoot != null && mappingRoot.isInputObject(child))
284     {
285       return MappingPackage.eINSTANCE.getMapping_Inputs();
286     }
287     else if (mappingRoot != null && mappingRoot.isOutputObject(child))
288     {
289       if (mappingRoot.getDomain() != null && mappingRoot.getMappedObjects().contains(mappingRoot.getDomain().getRoot(child)))
290       {
291         return MappingPackage.eINSTANCE.getMapping_Outputs();
292       }
293     }
294
295     return super.getChildFeature(object, child);
296   }
297
298   public EStructuralFeature getSetFeature(Object JavaDoc object, Object JavaDoc value)
299   {
300     Mapping mapping = (Mapping)object;
301     MappingRoot mappingRoot = mapping.getMappingRoot();
302     if (mappingRoot != null && mapping != mappingRoot)
303     {
304       if (mappingRoot.isInputObject(value) && !mapping.getInputs().contains(value))
305       {
306         return MappingPackage.eINSTANCE.getMapping_Inputs();
307       }
308       else if (mappingRoot.isOutputObject(value) && !mapping.getOutputs().contains(value))
309       {
310         return MappingPackage.eINSTANCE.getMapping_Outputs();
311       }
312     }
313
314     return super.getSetFeature(object, value);
315   }
316
317
318   protected ArrayList JavaDoc mappedObjectItemProviderList = new ArrayList JavaDoc();
319
320   protected MappedObjectItemProvider getMappedObjectItemProvider(Mapping mapping, Object JavaDoc mappedObject)
321   {
322     for (Iterator JavaDoc mappedObjectItemProviders = mappedObjectItemProviderList.iterator(); mappedObjectItemProviders.hasNext(); )
323     {
324       MappedObjectItemProvider mappedObjectItemProvider = (MappedObjectItemProvider)mappedObjectItemProviders.next();
325       if (mappedObjectItemProvider.getMappedObject() == mappedObject)
326       {
327         return mappedObjectItemProvider;
328       }
329     }
330
331     MappedObjectItemProvider result = new MappedObjectItemProvider(adapterFactory, (EObject)mappedObject, mapping);
332     mappedObjectItemProviderList.add(result);
333     return result;
334   }
335
336   protected Object JavaDoc substituteMappedObjectItemProvider(Mapping mapping, Object JavaDoc object)
337   {
338     getChildren(mapping);
339     for (Iterator JavaDoc mappedObjectItemProviders = mappedObjectItemProviderList.iterator(); mappedObjectItemProviders.hasNext(); )
340     {
341       MappedObjectItemProvider mappedObjectItemProvider = (MappedObjectItemProvider)mappedObjectItemProviders.next();
342       if (mappedObjectItemProvider.getMappedObject() == object)
343       {
344         return mappedObjectItemProvider;
345       }
346     }
347
348     if (object instanceof Collection JavaDoc)
349     {
350       Collection JavaDoc result = new ArrayList JavaDoc();
351       LOOP: for (Iterator JavaDoc objects = ((Collection JavaDoc)object).iterator(); objects.hasNext(); )
352       {
353         Object JavaDoc o = objects.next();
354         for (Iterator JavaDoc mappedObjectItemProviders = mappedObjectItemProviderList.iterator(); mappedObjectItemProviders.hasNext(); )
355         {
356           MappedObjectItemProvider mappedObjectItemProvider = (MappedObjectItemProvider)mappedObjectItemProviders.next();
357           if (mappedObjectItemProvider.getMappedObject() == o)
358           {
359             result.add(mappedObjectItemProvider);
360             continue LOOP;
361           }
362         }
363         result.add(o);
364       }
365
366       return result;
367     }
368
369     return object;
370   }
371
372   protected Object JavaDoc substituteMappedObject(Mapping mapping, Object JavaDoc object)
373   {
374     if (object instanceof MappedObjectItemProvider)
375     {
376       return ((MappedObjectItemProvider)object).getMappedObject();
377     }
378     else if (object instanceof Collection JavaDoc)
379     {
380       Collection JavaDoc result = new ArrayList JavaDoc();
381       for (Iterator JavaDoc objects = ((Collection JavaDoc)object).iterator(); objects.hasNext(); )
382       {
383         Object JavaDoc o = objects.next();
384         if (o instanceof MappedObjectItemProvider)
385         {
386           result.add(((MappedObjectItemProvider)o).getMappedObject());
387         }
388         else
389         {
390           result.add(o);
391         }
392       }
393       return result;
394     }
395
396     return object;
397   }
398
399   public static Iterator JavaDoc createValueIterator(Object JavaDoc object)
400   {
401     if (object == null)
402     {
403       return Collections.EMPTY_LIST.iterator();
404     }
405     else if (object instanceof Collection JavaDoc)
406     {
407       return ((Collection JavaDoc)object).iterator();
408     }
409     else
410     {
411       return Collections.singleton(object).iterator();
412     }
413   }
414
415   public Collection JavaDoc getChildren(Object JavaDoc object)
416   {
417     final Mapping mapping = (Mapping)object;
418
419     Collection JavaDoc result = new ArrayList JavaDoc();
420
421    
422     for (Iterator JavaDoc inputs = mapping.getInputs().iterator(); inputs.hasNext(); )
423     {
424       Object JavaDoc input = inputs.next();
425       result.add(getMappedObjectItemProvider(mapping, input));
426     }
427
428     for (Iterator JavaDoc outputs = mapping.getOutputs().iterator(); outputs.hasNext(); )
429     {
430       Object JavaDoc output = outputs.next();
431       result.add(getMappedObjectItemProvider(mapping, output));
432     }
433
434     result.addAll(mapping.getNested());
435
436     return result;
437   }
438
439   public boolean hasChildren(Object JavaDoc object)
440   {
441     return true;
442   }
443
444   /**
445    * This returns the nestedIn of the Mapping.
446    */

447   public Object JavaDoc getParent(Object JavaDoc object)
448   {
449     return ((Mapping)object).getNestedIn();
450   }
451
452   public static Object JavaDoc getImage(MappingRoot mappingRoot, String JavaDoc prefix, Collection JavaDoc collection)
453   {
454     return getImage(mappingRoot, prefix, collection, false);
455   }
456
457   public static Object JavaDoc getImage(MappingRoot mappingRoot, String JavaDoc prefix, Collection JavaDoc collection, boolean supportNone)
458   {
459     int topsSize = 0;
460     int bottomsSize = 0;
461     for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
462     {
463       Object JavaDoc object = objects.next();
464       if (mappingRoot.isTopObject(object))
465       {
466         ++topsSize;
467       }
468       else if (mappingRoot.isBottomObject(object))
469       {
470         ++bottomsSize;
471       }
472     }
473
474     return getImage(prefix, topsSize, bottomsSize, supportNone);
475   }
476
477   public static Object JavaDoc getImage(String JavaDoc prefix, Collection JavaDoc tops, Collection JavaDoc bottoms)
478   {
479     return getImage(prefix, tops, bottoms, false);
480   }
481
482   public static Object JavaDoc getImage(String JavaDoc prefix, Collection JavaDoc tops, Collection JavaDoc bottoms, boolean supportNone)
483   {
484     return getImage(prefix, tops.size(), bottoms.size(), supportNone);
485   }
486
487   public static Object JavaDoc getImage(String JavaDoc prefix, int topsSize, int bottomsSize, boolean supportNone)
488   {
489     if (topsSize == 0)
490     {
491       if (bottomsSize == 0)
492       {
493         return MappingPlugin.getPlugin().getImage(prefix + (supportNone ? "NoneToNoneMapping" : "OneToOneMapping"));
494       }
495       else if (bottomsSize == 1)
496       {
497         return MappingPlugin.getPlugin().getImage(prefix + (supportNone ? "NoneToOneMapping" : "OneToOneMapping"));
498       }
499       else
500       {
501         return MappingPlugin.getPlugin().getImage(prefix + (supportNone ? "NoneToManyMapping" : "OneToManyMapping"));
502       }
503     }
504     else if (topsSize == 1)
505     {
506       if (bottomsSize == 0)
507       {
508         return MappingPlugin.getPlugin().getImage(prefix + (supportNone ? "OneToNoneMapping" : "OneToOneMapping"));
509       }
510       if (bottomsSize <= 1)
511       {
512         return MappingPlugin.getPlugin().getImage(prefix + "OneToOneMapping");
513       }
514       else
515       {
516         return MappingPlugin.getPlugin().getImage(prefix + "OneToManyMapping");
517       }
518     }
519     else
520     {
521       if (bottomsSize == 0)
522       {
523         return MappingPlugin.getPlugin().getImage(prefix + (supportNone ? "ManyToNoneMapping" : "ManyToOneMapping"));
524       }
525       else if (bottomsSize == 1)
526       {
527         return MappingPlugin.getPlugin().getImage(prefix + "ManyToOneMapping");
528       }
529       else
530       {
531         return MappingPlugin.getPlugin().getImage(prefix + "ManyToManyMapping");
532       }
533     }
534   }
535
536   /**
537    * This returns Mapping.gif.
538    */

539   public Object JavaDoc getImage(Object JavaDoc object)
540   {
541     Mapping mapping = (Mapping)object;
542     MappingRoot mappingRoot = mapping.getMappingRoot();
543     boolean isTypeMapping = mappingRoot != null && mappingRoot.getDomain() == null;
544
545     return
546       getImage
547         (isTypeMapping ? "full/obj16/Type" : "full/obj16/",
548          mapping.getTops(),
549          mapping.getBottoms(),
550          !isTypeMapping);
551   }
552
553   public static String JavaDoc getText(MappingRoot mappingRoot, AdapterFactory adapterFactory, Collection JavaDoc collection)
554   {
555     return getText(mappingRoot, adapterFactory, collection, (String JavaDoc)null);
556   }
557
558   protected static final String JavaDoc DIVIDER = " " + MappingPlugin.getPlugin().getString("_UI_Mapping_label_divider") + " ";
559   protected static final String JavaDoc SEPARATOR = MappingPlugin.getPlugin().getString("_UI_Mapping_label_separator") + " ";
560
561   public static String JavaDoc getText(MappingRoot mappingRoot, AdapterFactory adapterFactory, Collection JavaDoc collection, String JavaDoc pathSeparator)
562   {
563     return getText(mappingRoot, adapterFactory, collection, pathSeparator, SEPARATOR, DIVIDER);
564   }
565
566   public static String JavaDoc getText
567     (MappingRoot mappingRoot,
568      AdapterFactory adapterFactory,
569      Collection JavaDoc collection,
570      String JavaDoc pathSeparator,
571      String JavaDoc objectSeparator,
572      String JavaDoc divider)
573   {
574     AdapterFactoryItemDelegator labelProvider = new AdapterFactoryItemDelegator(adapterFactory);
575     boolean needsComma = false;
576     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
577
578     for (Iterator JavaDoc i = collection.iterator(); i.hasNext(); )
579     {
580       EObject input = (EObject)i.next();
581       if (mappingRoot.isTopObject(input))
582       {
583         if (needsComma)
584         {
585           result.append(objectSeparator);
586         }
587         else
588         {
589           needsComma = true;
590         }
591         int index = result.length();
592         result.append(labelProvider.getText(input));
593         if (pathSeparator != null)
594         {
595           for (Object JavaDoc parent = mappingRoot.getDomain().getParent(input);
596                parent instanceof EObject;
597                parent = mappingRoot.getDomain().getParent(parent))
598           {
599             result.insert(index, labelProvider.getText(parent) + pathSeparator);
600           }
601         }
602       }
603     }
604
605     result.append(divider);
606
607     needsComma = false;
608     for (Iterator JavaDoc o = collection.iterator(); o.hasNext(); )
609     {
610       EObject output = (EObject)o.next();
611       if (mappingRoot.isBottomObject(output))
612       {
613         if (needsComma)
614         {
615           result.append(objectSeparator);
616         }
617         else
618         {
619           needsComma = true;
620         }
621         int index = result.length();
622         result.append(labelProvider.getText(output));
623         if (pathSeparator != null)
624         {
625           for (Object JavaDoc parent = mappingRoot.getDomain().getParent(output);
626                parent != null;
627                parent = mappingRoot.getDomain().getParent(parent))
628           {
629             result.insert(index, labelProvider.getText(parent) + pathSeparator);
630           }
631         }
632       }
633     }
634
635     return result.toString();
636   }
637
638   public static String JavaDoc getText(MappingRoot mappingRoot, AdapterFactory adapterFactory, Collection JavaDoc inputs, Collection JavaDoc outputs)
639   {
640     return getText(mappingRoot, adapterFactory, inputs, outputs, SEPARATOR, DIVIDER);
641   }
642
643   public static String JavaDoc getText
644     (MappingRoot mappingRoot, AdapterFactory adapterFactory, Collection JavaDoc inputs, Collection JavaDoc outputs, String JavaDoc objectSeparator, String JavaDoc divider)
645   {
646     AdapterFactoryItemDelegator labelProvider = new AdapterFactoryItemDelegator(adapterFactory);
647     boolean needsComma = false;
648     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
649
650     for (Iterator JavaDoc i = inputs.iterator(); i.hasNext(); )
651     {
652       Object JavaDoc input = i.next();
653       if (needsComma)
654       {
655         result.append(objectSeparator);
656       }
657       else
658       {
659         needsComma = true;
660       }
661       result.append(labelProvider.getText(input));
662     }
663
664     result.append(divider);
665
666     needsComma = false;
667     for (Iterator JavaDoc o = outputs.iterator(); o.hasNext(); )
668     {
669       Object JavaDoc output = o.next();
670       if (needsComma)
671       {
672         result.append(objectSeparator);
673       }
674       else
675       {
676         needsComma = true;
677       }
678       result.append(labelProvider.getText(output));
679     }
680
681     return result.toString();
682   }
683
684   public String JavaDoc getText(Object JavaDoc object)
685   {
686     Mapping mapping = (Mapping)object;
687     return
688       getText
689         (mapping.getMappingRoot(),
690          ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
691          mapping.getTops(),
692          mapping.getBottoms());
693   }
694
695   /**
696    * This handles notification by delegating to {@link #fireNotifyChanged fireNotifyChanged}.
697    */

698   public void notifyChanged(Notification msg)
699   {
700     MappingPackage ePackage = MappingPackage.eINSTANCE;
701     if (msg.getFeature() == ePackage.getMapping_Inputs() || msg.getFeature() == ePackage.getMapping_Outputs())
702     {
703       fireNotifyChanged(msg);
704       //hgd::fireNotifyChanged(msg.getNotifier(), msg.getEventType(), msg.getFeature(), substituteOldValue, substituteNewValue, msg.getPosition());
705

706       ENotificationImpl note =
707         new ENotificationImpl
708          ((InternalEObject)msg.getNotifier(), //FIX:::
709
Notification.SET,
710           null,
711           null,
712           null,
713           -1);
714       fireNotifyChanged(note);
715     }
716     else if (msg.getFeature() == ePackage.getMapping_Helper() || msg.getFeature() == ePackage.getMapping_TypeMapping())
717     {
718       itemPropertyDescriptors = null;
719       fireNotifyChanged(msg);
720       //hgd::fireNotifyChanged(msg.getNotifier(), msg.getEventType(), msg.getFeature(), msg.getOldValue(), msg.getNewValue(), msg.getPosition());
721
}
722     else if (msg.getFeature() == ePackage.getMapping_Nested() || msg.getFeature() == ePackage.getMapping_NestedIn())
723     {
724       fireNotifyChanged(msg);
725       //fireNotifyChanged(msg.getNotifier(), msg.getEventType(), msg.getFeature(), msg.getOldValue(), msg.getNewValue(), msg.getPosition());
726
}
727     else
728     {
729       super.notifyChanged(msg);
730     }
731   }
732
733   public Command createCommand(Object JavaDoc object, EditingDomain editingDomain, Class JavaDoc commandClass, CommandParameter commandParameter)
734   {
735     if (editingDomain instanceof MappingDomain)
736     {
737       MappingDomain mappingDomain = (MappingDomain)editingDomain;
738       if (commandClass == NameMatchMappingCommand.class)
739       {
740         return createNameMatchMappingCommand(mappingDomain, (Mapping)commandParameter.getOwner());
741       }
742       else if (commandClass == TypeMatchMappingCommand.class)
743       {
744         return createTypeMatchMappingCommand(mappingDomain, (Mapping)commandParameter.getOwner());
745       }
746     }
747
748     // This ensures that we are dealing with actual MOF objects.
749
//
750
commandParameter.collection = (Collection JavaDoc)substituteMappedObject((Mapping)object, commandParameter.getCollection());
751     commandParameter.value = substituteMappedObject((Mapping)object, commandParameter.getValue());
752
753     return super.createCommand(object, editingDomain, commandClass, commandParameter);
754   }
755
756   /**
757    * This creates a primitive {@link org.eclipse.emf.mapping.command.NameMatchMappingCommand}.
758    */

759   protected Command createNameMatchMappingCommand(MappingDomain domain, Mapping mapping)
760   {
761     return new NameMatchMappingCommand(domain, mapping);
762   }
763
764   /**
765    * This creates a primitive {@link org.eclipse.emf.mapping.command.TypeMatchMappingCommand}.
766    */

767   protected Command createTypeMatchMappingCommand(MappingDomain domain, Mapping mapping)
768   {
769     return new TypeMatchMappingCommand(domain, mapping);
770   }
771
772   protected Command createRemoveCommand(final EditingDomain domain, EObject owner, final EStructuralFeature feature, Collection JavaDoc collection)
773   {
774     final Mapping mappingOwner = (Mapping)owner;
775     final MappingRoot mappingRoot = mappingOwner.getMappingRoot();
776
777     if (feature == MappingPackage.eINSTANCE.getMapping_Nested())
778     {
779       final Collection JavaDoc mappingCollection = collection;
780       return
781         new CommandWrapper(super.createRemoveCommand(domain, owner, feature, collection))
782         {
783           protected void register()
784           {
785             for (Iterator JavaDoc objects = mappingCollection.iterator(); objects.hasNext(); )
786             {
787               Mapping mapping = (Mapping)objects.next();
788               for (TreeIterator mappings = mapping.treeIterator(); mappings.hasNext(); )
789               {
790                 Mapping childMapping = (Mapping)mappings.next();
791                 mappingRoot.register(childMapping);
792               }
793             }
794           }
795
796           protected void deregister()
797           {
798             for (Iterator JavaDoc objects = mappingCollection.iterator(); objects.hasNext(); )
799             {
800               Mapping mapping = (Mapping)objects.next();
801               for (TreeIterator mappings = mapping.treeIterator(); mappings.hasNext(); )
802               {
803                 Mapping childMapping = (Mapping)mappings.next();
804                 mappingRoot.deregister(childMapping);
805               }
806             }
807           }
808
809           public void execute()
810           {
811             deregister();
812             super.execute();
813           }
814
815           public void undo()
816           {
817             super.undo();
818             register();
819           }
820
821           public void redo()
822           {
823             deregister();
824             super.redo();
825           }
826         };
827     }
828     else if (feature == MappingPackage.eINSTANCE.getMapping_Inputs() ||
829                feature == MappingPackage.eINSTANCE.getMapping_Outputs())
830     {
831       final Collection JavaDoc mappedObjectsCollection = collection;
832       final boolean removingInputs = (feature == MappingPackage.eINSTANCE.getMapping_Inputs());
833       return
834         new CommandWrapper(super.createRemoveCommand(domain, owner, feature, collection))
835         {
836           protected CompoundCommand commands;
837
838           protected boolean prepare()
839           {
840             boolean result = true;
841             Collection JavaDoc inputs = mappingOwner.getInputs();
842             Collection JavaDoc outputs = mappingOwner.getOutputs();
843             if (removingInputs)
844             {
845               inputs = new ArrayList JavaDoc(inputs);
846               inputs.removeAll(mappedObjectsCollection);
847             }
848             else
849             {
850               outputs = new ArrayList JavaDoc(outputs);
851               outputs.removeAll(mappedObjectsCollection);
852             }
853             if (!mappingRoot.canCreateMapping(inputs, outputs, mappingOwner))
854             {
855               result = false;
856             }
857             return result;
858           }
859
860           protected void register()
861           {
862             for (Iterator JavaDoc objects = mappedObjectsCollection.iterator(); objects.hasNext(); )
863             {
864               Object JavaDoc object = objects.next();
865               mappingRoot.getMappedObjectState(object).getMappings().add(mappingOwner);
866             }
867           }
868
869           protected void deregister()
870           {
871             for (Iterator JavaDoc objects = mappedObjectsCollection.iterator(); objects.hasNext(); )
872             {
873               Object JavaDoc object = objects.next();
874               mappingRoot.getMappedObjectState(object).getMappings().remove(mappingOwner);
875             }
876           }
877
878           public void execute()
879           {
880             if (mappingOwner == mappingRoot)
881             {
882               commands = new CompoundCommand();
883               Collection JavaDoc collectionOfDescendants = new HashSet JavaDoc();
884               for (Iterator JavaDoc objects = mappedObjectsCollection.iterator(); objects.hasNext(); )
885               {
886                 for (TreeIterator descendants = domain.treeIterator(objects.next()); descendants.hasNext(); )
887                 {
888                   Object JavaDoc descendant = descendants.next();
889                   collectionOfDescendants.add(descendant);
890                 }
891               }
892
893               for (TreeIterator mappings = mappingRoot.treeIterator(false); mappings.hasNext(); )
894               {
895                 Mapping mapping = (Mapping)mappings.next();
896                 if (!mapping.getInputs().isEmpty() && collectionOfDescendants.containsAll(mapping.getInputs()) ||
897                       !mapping.getOutputs().isEmpty() && collectionOfDescendants.containsAll(mapping.getOutputs()))
898                 {
899                   Command command = RemoveMappingCommand.create((MappingDomain)domain, mapping);
900                   commands.appendIfCanExecute(command);
901                 }
902                 else
903                 {
904                   if (feature == MappingPackage.eINSTANCE.getMapping_Inputs())
905                   {
906                     Collection JavaDoc inputsToRemove = new ArrayList JavaDoc(mapping.getInputs());
907                     inputsToRemove.retainAll(collectionOfDescendants);
908                     if (!inputsToRemove.isEmpty())
909                     {
910                       Command removeInputsCommand =
911                         RemoveCommand.create(domain, mapping, MappingPackage.eINSTANCE.getMapping_Inputs(), inputsToRemove);
912                       commands.appendIfCanExecute(removeInputsCommand);
913                     }
914                   }
915                   else
916                   {
917                     Collection JavaDoc outputsToRemove = new ArrayList JavaDoc(mapping.getOutputs());
918                     outputsToRemove.retainAll(collectionOfDescendants);
919                     if (!outputsToRemove.isEmpty())
920                     {
921                       Command removeOutputsCommand =
922                         RemoveCommand.create(domain, mapping, MappingPackage.eINSTANCE.getMapping_Outputs(), outputsToRemove);
923                       commands.appendIfCanExecute(removeOutputsCommand);
924                     }
925                   }
926                 }
927               }
928
929               commands.execute();
930             }
931             super.execute();
932             deregister();
933           }
934
935           public void undo()
936           {
937             super.undo();
938             if (commands != null)
939             {
940               commands.undo();
941             }
942             register();
943           }
944
945           public void redo()
946           {
947             if (commands != null)
948             {
949               commands.redo();
950             }
951             super.redo();
952             deregister();
953           }
954
955           public void dispose()
956           {
957             super.dispose();
958             if (commands != null)
959             {
960               command.dispose();
961             }
962           }
963         };
964     }
965     else
966     {
967       return super.createRemoveCommand(domain, owner, feature, collection);
968     }
969   }
970
971   protected Command factorAddCommand(EditingDomain domain, CommandParameter commandParameter)
972   {
973     return UnexecutableCommand.INSTANCE;
974   }
975
976   protected Command createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection JavaDoc collection, int index)
977   {
978     final Mapping mappingOwner = (Mapping)owner;
979     final MappingRoot mappingRoot = mappingOwner.getMappingRoot();
980
981     if (feature == MappingPackage.eINSTANCE.getMapping_Inputs() ||
982           feature == MappingPackage.eINSTANCE.getMapping_Outputs())
983     {
984       final Collection JavaDoc mappedObjectsCollection = collection;
985       final boolean addingInputs = (feature == MappingPackage.eINSTANCE.getMapping_Inputs());
986       return
987         new CommandWrapper(super.createAddCommand(domain, owner, feature, collection, index))
988         {
989           protected boolean prepare()
990           {
991             boolean result = true;
992             Collection JavaDoc inputs = mappingOwner.getInputs();
993             Collection JavaDoc outputs = mappingOwner.getOutputs();
994             if (addingInputs)
995             {
996               inputs = new ArrayList JavaDoc(inputs);
997               inputs.addAll(mappedObjectsCollection);
998             }
999             else
1000            {
1001              outputs = new ArrayList JavaDoc(outputs);
1002              outputs.addAll(mappedObjectsCollection);
1003            }
1004            if (!mappingRoot.canCreateMapping(inputs, outputs, mappingOwner))
1005            {
1006              result = false;
1007            }
1008            return result;
1009          }
1010          protected void register()
1011          {
1012            for (Iterator JavaDoc objects = mappedObjectsCollection.iterator(); objects.hasNext(); )
1013            {
1014              Object JavaDoc object = objects.next();
1015              mappingRoot.getMappedObjectState(object).getMappings().add(mappingOwner);
1016            }
1017          }
1018
1019          protected void deregister()
1020          {
1021            for (Iterator JavaDoc objects = mappedObjectsCollection.iterator(); objects.hasNext(); )
1022            {
1023              Object JavaDoc object = objects.next();
1024              mappingRoot.getMappedObjectState(object).getMappings().remove(mappingOwner);
1025            }
1026          }
1027
1028          public void execute()
1029          {
1030            super.execute();
1031            register();
1032          }
1033
1034          public void undo()
1035          {
1036            deregister();
1037            super.undo();
1038          }
1039
1040          public void redo()
1041          {
1042            super.redo();
1043            register();
1044          }
1045
1046          public Collection JavaDoc getAffectedObjects()
1047          {
1048            return (Collection JavaDoc)substituteMappedObjectItemProvider(mappingOwner, super.getAffectedObjects());
1049          }
1050
1051          public Collection JavaDoc getResult()
1052          {
1053            return (Collection JavaDoc)substituteMappedObjectItemProvider(mappingOwner, super.getResult());
1054          }
1055        };
1056    }
1057    else
1058    {
1059      return super.createAddCommand(domain, owner, feature, collection, index);
1060    }
1061  }
1062
1063  /**
1064   * This creates a primitive {@link org.eclipse.emf.edit.command.MoveCommand}.
1065   */

1066  protected Command createMoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, EObject value, int index)
1067  {
1068    final Mapping mappingOwner = (Mapping)owner;
1069    if (feature == MappingPackage.eINSTANCE.getMapping_Inputs() ||
1070          feature == MappingPackage.eINSTANCE.getMapping_Outputs())
1071    {
1072      return
1073        new CommandWrapper(super.createMoveCommand(domain, owner, feature, value, index))
1074        {
1075          public Collection JavaDoc getAffectedObjects()
1076          {
1077            return (Collection JavaDoc)substituteMappedObjectItemProvider(mappingOwner, super.getAffectedObjects());
1078          }
1079
1080          public Collection JavaDoc getResult()
1081          {
1082            return (Collection JavaDoc)substituteMappedObjectItemProvider(mappingOwner, super.getResult());
1083          }
1084        };
1085    }
1086    else
1087    {
1088      return super.createMoveCommand(domain, owner, feature, value, index);
1089    }
1090  }
1091
1092  /**
1093   * This creates a primitive {@link SetCommand}.
1094   */

1095  protected Command createSetCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object JavaDoc value)
1096  {
1097    if (feature == MappingPackage.eINSTANCE.getMapping_Inputs() ||
1098          feature == MappingPackage.eINSTANCE.getMapping_Outputs())
1099    {
1100      return createAddCommand(domain, owner, feature, Collections.singleton(value), Notification.NO_INDEX);
1101    }
1102    else
1103    {
1104      return super.createSetCommand(domain, owner, feature, value);
1105    }
1106  }
1107
1108  public void dispose()
1109  {
1110    for (Iterator JavaDoc providers = mappedObjectItemProviderList.iterator(); providers.hasNext(); )
1111    {
1112      Object JavaDoc provider = providers.next();
1113      ((IDisposable)provider).dispose();
1114    }
1115    super.dispose();
1116  }
1117
1118  /**
1119   * Return the resource locator for this item provider's resources.
1120   * <!-- begin-user-doc -->
1121   * <!-- end-user-doc -->
1122   * @generated
1123   */

1124  public ResourceLocator getResourceLocator()
1125  {
1126    return MappingPlugin.INSTANCE;
1127  }
1128
1129}
1130
Popular Tags