KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > command > CreateCopyOverrideCommand


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: CreateCopyOverrideCommand.java,v 1.3 2005/06/08 06:21:43 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.command;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22
23 import org.eclipse.emf.common.command.AbstractCommand;
24 import org.eclipse.emf.ecore.EClass;
25 import org.eclipse.emf.ecore.EObject;
26 import org.eclipse.emf.edit.command.ChildrenToCopyProvider;
27 import org.eclipse.emf.edit.command.CopyCommand;
28 import org.eclipse.emf.edit.command.CreateCopyCommand;
29 import org.eclipse.emf.mapping.MappedObjectState;
30 import org.eclipse.emf.mapping.MappingRoot;
31 import org.eclipse.emf.mapping.domain.MappingDomain;
32
33
34 /**
35  * This command overrides the CreateCopyCommand for cross-domain copies.
36  */

37 public class CreateCopyOverrideCommand extends AbstractCommand implements ChildrenToCopyProvider
38 {
39   /**
40    * This keeps track of the mapping domain in which the command operates.
41    */

42   protected MappingDomain mappingDomain;
43
44   /**
45    * This is the object being copied.
46    */

47   protected EObject owner;
48
49   /**
50    * This is the copy.
51    */

52   protected EObject copy;
53
54   /**
55    * This is a map of objects to their copies
56    */

57   protected CopyCommand.Helper copyHelper;
58
59   /**
60    * This creates a command instance that creates a copy of owner.
61    */

62   public CreateCopyOverrideCommand(MappingDomain domain, CreateCopyCommand createCommand)
63   {
64     super(createCommand.doGetLabel(), createCommand.doGetDescription());
65
66     this.mappingDomain = domain;
67     this.owner = createCommand.getOwner();
68     this.copyHelper = createCommand.getCopyHelper();
69   }
70
71   protected boolean prepare()
72   {
73     return true;
74   }
75
76   public void execute()
77   {
78     // Get the corresponding type.
79
//
80

81     EClass outputType = (EClass)mappingDomain.getOutputMetaObject(owner.eClass());
82
83     if (outputType != null)
84     {
85       // Create the copy from this corresponding type.
86
//
87
copy = outputType.getEPackage().getEFactoryInstance().create(outputType);
88
89       copyHelper.put(owner, copy);
90
91       MappingRoot mappingRoot = mappingDomain.getMappingRoot();
92       if (mappingRoot.isInputObject(owner))
93       {
94         // This is done to ensure that this new copy is treated as an output by the domain.
95
// The CreateMappingCommand will rely on this setting.
96
//
97
MappedObjectState mappedObjectState = mappingRoot.getMappedObjectState(copy);
98         if (mappedObjectState != null)
99         {
100           mappedObjectState.setOriginatingInput(owner);
101         }
102       }
103     }
104   }
105
106   public void undo()
107   {
108     copyHelper.remove(owner);
109   }
110
111   public void redo()
112   {
113     copyHelper.put(owner, copy);
114   }
115
116   public Collection JavaDoc getResult()
117   {
118     return Collections.singleton(copy);
119   }
120
121   public Collection JavaDoc getChildrenToCopy()
122   {
123     return mappingDomain.getChildren(owner);
124   }
125
126   /**
127    * This gives an abbreviated name using this object's own class' name, without package qualification,
128    * followed by a space separated list of <tt>field:value</tt> pairs.
129    */

130   public String JavaDoc toString()
131   {
132     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
133     result.append(" (mappingDomain: " + mappingDomain + ")");
134     result.append(" (owner: " + owner + ")");
135     result.append(" (copy: " + copy + ")");
136     result.append(" (copyHelper: " + copyHelper + ")");
137
138     return result.toString();
139   }
140 }
141
Popular Tags