KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > command > InitializeCopyCommand


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

17 package org.eclipse.emf.edit.command;
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.util.EList;
27 import org.eclipse.emf.ecore.EAttribute;
28 import org.eclipse.emf.ecore.EObject;
29 import org.eclipse.emf.ecore.EReference;
30 import org.eclipse.emf.ecore.EStructuralFeature;
31 import org.eclipse.emf.ecore.util.FeatureMap;
32 import org.eclipse.emf.ecore.util.FeatureMapUtil;
33 import org.eclipse.emf.edit.EMFEditPlugin;
34 import org.eclipse.emf.edit.domain.EditingDomain;
35
36
37 /**
38  * The initialize copy command is implementated to set the values of an object copy based on those
39  * of the original (copied) object. It is a helper command used by the CopyCommand.
40  *
41  * <p>
42  * An initialize copy command is an {@link OverrideableCommand}.
43  */

44 public class InitializeCopyCommand extends AbstractOverrideableCommand
45 {
46   public static Command create(EditingDomain domain, Object JavaDoc owner, CopyCommand.Helper copyHelper)
47   {
48     return domain.createCommand(InitializeCopyCommand.class, new CommandParameter(owner, null, copyHelper));
49   }
50
51   /**
52    * This caches the label.
53    */

54   protected static final String JavaDoc LABEL = EMFEditPlugin.INSTANCE.getString("_UI_InitializeCopyCommand_label");
55
56   /**
57    * This caches the description.
58    */

59   protected static final String JavaDoc DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_InitializeCopyCommand_description");
60
61   /**
62    * This is the object being copied.
63    */

64   protected EObject owner;
65
66   /**
67    * This is the object (copy) being initialized.
68    */

69   protected EObject copy;
70
71   /**
72    * This is a map of objects to their copies
73    */

74   protected CopyCommand.Helper copyHelper;
75
76   /**
77    * This constructs an instance that will copy the attribute values of value to those of owner.
78    */

79   public InitializeCopyCommand(EditingDomain domain, EObject owner, CopyCommand.Helper copyHelper)
80   {
81     super(domain, LABEL, DESCRIPTION);
82
83     this.owner = owner;
84     this.copy = copyHelper.getCopy(owner);
85     this.copyHelper = copyHelper;
86   }
87
88   /**
89    * This is the object being copied.
90    */

91   public EObject getOwner()
92   {
93     return owner;
94   }
95
96   /**
97    * This is the object (copy) being initialized.
98    */

99   public EObject getCopy()
100   {
101     return copy;
102   }
103
104   /**
105    * This is the map of objects to their copies.
106    */

107   public CopyCommand.Helper getCopyHelper()
108   {
109     return copyHelper;
110   }
111
112   protected boolean prepare()
113   {
114     return owner.eClass().isInstance(copy);
115   }
116
117   public void doExecute()
118   {
119     copyAttributes();
120     copyReferences();
121   }
122
123   protected Collection JavaDoc getAttributesToCopy()
124   {
125     return owner.eClass().getEAllAttributes();
126   }
127
128   /**
129    * This method will iterate over the attributes of the owner object and set them
130    * accordingly in the copy.
131    */

132   protected void copyAttributes()
133     {
134     for (Iterator JavaDoc attributes = getAttributesToCopy().iterator(); attributes.hasNext(); )
135     {
136       EAttribute attribute = (EAttribute) attributes.next();
137       if (attribute.isChangeable() && !attribute.isDerived() && (attribute.isMany() || owner.eIsSet(attribute)))
138       {
139         Object JavaDoc value = owner.eGet(attribute);
140         if (!attribute.isMany())
141         {
142           copy.eSet(attribute, value);
143         }
144         else
145         {
146           List JavaDoc list = (List JavaDoc)copy.eGet(attribute);
147           if (FeatureMapUtil.isFeatureMap(attribute))
148           {
149             FeatureMap featureMap = (FeatureMap)list;
150             LOOP:
151             for (Iterator JavaDoc i = ((List JavaDoc)value).iterator(); i.hasNext(); )
152             {
153               FeatureMap.Entry entry = (FeatureMap.Entry)i.next();
154               EStructuralFeature entryFeature = entry.getEStructuralFeature();
155               if (entryFeature instanceof EAttribute)
156               {
157                 featureMap.add(entry);
158               }
159               else
160               {
161                 EReference reference = (EReference)entryFeature;
162                 EReference reverseReference = reference.getEOpposite();
163                 Object JavaDoc entryValue = entry.getValue();
164                 boolean copiedTargetRequired = reverseReference != null || reference.isContainment();
165                 EObject target = copyHelper.getCopyTarget((EObject)entryValue, copiedTargetRequired);
166                 if (target != null)
167                 {
168                   if (reverseReference != null)
169                   {
170                     for (Iterator JavaDoc j = featureMap.iterator(); j.hasNext(); )
171                     {
172                       FeatureMap.Entry copyEntry = (FeatureMap.Entry)j.next();
173                       if (copyEntry.getEStructuralFeature() == reference && copyEntry.getValue() == target)
174                       {
175                         featureMap.move(featureMap.size() - 1, copyEntry);
176                         continue LOOP;
177                       }
178                     }
179                   }
180                   featureMap.add(reference, target);
181                 }
182               }
183             }
184           }
185           else
186           {
187             list.addAll((List JavaDoc)value);
188           }
189         }
190       }
191     }
192   }
193
194
195   protected Collection JavaDoc getReferencesToCopy()
196   {
197     return owner.eClass().getEAllReferences();
198   }
199
200   /**
201    * This method will iterate over the references of the owner object and sets them.
202    * accordingly in the copy.
203    */

204   protected void copyReferences()
205   {
206     for (Iterator JavaDoc references = getReferencesToCopy().iterator(); references.hasNext(); )
207     {
208       EReference reference = (EReference) references.next();
209       if (!reference.isChangeable() || reference.isDerived())
210       {
211         continue;
212       }
213
214       EReference reverseReference = reference.getEOpposite();
215
216       Object JavaDoc value = owner.eGet(reference);
217       if (value == null)
218       {
219 // copy.eSet(reference, null); // is it possible to not be null already?
220
continue;
221       }
222
223       boolean copiedTargetRequired = reverseReference != null || reference.isContainment();
224       if (reference.isMany())
225       {
226         List JavaDoc valueList = (List JavaDoc) value;
227         if (!valueList.isEmpty())
228         {
229           EList copyList = (EList) copy.eGet(reference);
230           int index = 0;
231           for (Iterator JavaDoc valueIter = valueList.iterator(); valueIter.hasNext(); ++index)
232           {
233             EObject target = copyHelper.getCopyTarget((EObject) valueIter.next(), copiedTargetRequired);
234             if (target == null) break; // if one is null, they'll all be null
235
if (reverseReference != null)
236             {
237               int position = copyList.indexOf(target);
238               if (position == -1)
239               {
240                 copyList.add(index, target);
241               }
242               else
243               {
244                 copyList.move(index, target);
245               }
246             }
247             else
248             {
249               copyList.add(target);
250             }
251           }
252         }
253       }
254       else
255       {
256         EObject target = copyHelper.getCopyTarget((EObject) value, copiedTargetRequired);
257         if (target != null)
258         {
259           copy.eSet(reference, target);
260         }
261       }
262     }
263   }
264
265   public void doUndo()
266   {
267     // no-op
268
}
269
270   public void doRedo()
271   {
272     // no-op
273
}
274
275   public Collection JavaDoc doGetResult()
276   {
277     return Collections.singleton(copy);
278   }
279
280   public Collection JavaDoc doGetAffectedObjects()
281   {
282     return Collections.singleton(copy);
283   }
284
285   /**
286    * This gives an abbreviated name using this object's own class' name, without package qualification,
287    * followed by a space separated list of <tt>field:value</tt> pairs.
288    */

289   public String JavaDoc toString()
290   {
291     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString() + ")");
292     result.append(" (domain: " + domain + ")");
293     result.append(" (owner: " + owner + ")");
294     result.append(" (copy: " + copy + ")");
295     result.append(" (copyHelper: " + copyHelper + ")");
296
297     return result.toString();
298   }
299 }
300
Popular Tags