KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.eclipse.emf.mapping.command;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.eclipse.emf.common.command.AbstractCommand;
26 import org.eclipse.emf.common.command.Command;
27 import org.eclipse.emf.ecore.EObject;
28 import org.eclipse.emf.edit.command.AddCommand;
29 import org.eclipse.emf.edit.command.CopyCommand;
30 import org.eclipse.emf.edit.command.InitializeCopyCommand;
31 import org.eclipse.emf.mapping.domain.MappingDomain;
32
33
34 /**
35  * This command overrides the InitializeCopyCommand for cross-domain copies.
36  * This implementation is a no-op unless the objects are suitable ECore classes,
37  * in which case it copies the name and possibly the eType.
38  * Application specific subclasses should override execute() to complete the initialization, if necessary.
39  */

40 public class InitializeCopyOverrideCommand extends AbstractCommand
41 {
42   /**
43    * This keeps track of the mapping domain in which the command operates.
44    */

45   protected MappingDomain mappingDomain;
46
47   /**
48    * This is command we're overriding
49    */

50   protected InitializeCopyCommand initializeCommand;
51
52   /**
53    * This creates a command instance that initializes a copied object
54    */

55   public InitializeCopyOverrideCommand(MappingDomain domain, InitializeCopyCommand initializeCommand)
56   {
57     super(initializeCommand.doGetLabel(), initializeCommand.doGetDescription());
58
59     this.mappingDomain = domain;
60     this.initializeCommand = initializeCommand;
61   }
62
63   protected boolean prepare()
64   {
65     return true;
66   }
67
68   public void execute()
69   {
70     EObject owner = initializeCommand.getOwner();
71     EObject copy = initializeCommand.getCopy();
72     CopyCommand.Helper copyHelper = initializeCommand.getCopyHelper();
73
74     /*
75     if (owner.refMetaObject() == copy.refMetaObject())
76     {
77       initializeCommand.doExecute();
78       return;
79     }
80     */

81
82     // Copy the children references
83
//
84
Collection JavaDoc copyChildren = new ArrayList JavaDoc();
85     for (Iterator JavaDoc ownerChildren = mappingDomain.getChildren(owner).iterator(); ownerChildren.hasNext(); )
86     {
87       Object JavaDoc copyChildObject = ownerChildren.next();
88       if (copyChildObject instanceof EObject)
89       {
90         EObject copyChild = copyHelper.getCopy((EObject)copyChildObject);
91         if (copyChild != null)
92         {
93           copyChildren.add(copyChild);
94         }
95       }
96     }
97     if (!copyChildren.isEmpty()) {
98       Command addCommand = AddCommand.create(mappingDomain, copy, null, copyChildren);
99       if (addCommand.canExecute())
100       {
101         addCommand.execute(); // this will create the mapping as well
102
}
103       else
104       {
105         addCommand.dispose();
106         return;
107       }
108     }
109
110     // Copy the name
111
//
112
String JavaDoc ownerName = mappingDomain.getName(owner);
113     if (ownerName != null)
114     {
115       mappingDomain.setName(copy, mappingDomain.getOutputName(ownerName));
116     }
117
118     // Copy the type
119
//
120
Object JavaDoc ownerType = mappingDomain.getTypeClassifier(owner);
121     if (ownerType != null)
122     {
123       Object JavaDoc copyType = mappingDomain.getOutputTypeClassifier(ownerType);
124       if (copyType != null)
125       {
126         mappingDomain.setTypeClassifier(copy, copyType);
127       }
128     }
129   }
130
131   public void undo()
132   {
133     // no-op
134
}
135
136   public void redo()
137   {
138     // no-op
139
}
140
141   public Collection JavaDoc getResult()
142   {
143     return Collections.singleton(initializeCommand.getCopy());
144   }
145
146   public Collection JavaDoc getAffectedObjects()
147   {
148     return Collections.singleton(initializeCommand.getCopy());
149   }
150
151   /**
152    * This gives an abbreviated name using this object's own class' name, without package qualification,
153    * followed by a space separated list of <tt>field:value</tt> pairs.
154    */

155   public String JavaDoc toString()
156   {
157     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
158     result.append(" (mappingDomain: " + mappingDomain + ")");
159     result.append(" (owner: " + initializeCommand.getOwner() + ")");
160     result.append(" (copy: " + initializeCommand.getCopy() + ")");
161
162     return result.toString();
163   }
164 }
165
Popular Tags