KickJava   Java API By Example, From Geeks To Geeks.

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


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: CreateCopyCommand.java,v 1.3 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.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.eclipse.emf.common.command.Command;
26 import org.eclipse.emf.ecore.EClass;
27 import org.eclipse.emf.ecore.EFactory;
28 import org.eclipse.emf.ecore.EObject;
29 import org.eclipse.emf.ecore.EPackage;
30 import org.eclipse.emf.edit.EMFEditPlugin;
31 import org.eclipse.emf.edit.domain.EditingDomain;
32
33
34 /**
35  * The create copy command is used to create an uninitialized object of the same type
36  * as owner which will later be initialized using {@link InitializeCopyCommand}.
37  *
38  * <p>
39  * A create copy command is an {@link OverrideableCommand}.
40  */

41 public class CreateCopyCommand extends AbstractOverrideableCommand implements ChildrenToCopyProvider
42 {
43   /**
44    * This creates a command that will create and object for copying the given object
45    */

46   public static Command create(EditingDomain domain, Object JavaDoc owner, CopyCommand.Helper copyHelper)
47   {
48     return domain.createCommand(CreateCopyCommand.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_CreateCopyCommand_label");
55
56   /**
57    * This caches the description.
58    */

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

64   protected EObject owner;
65
66   /**
67    * This is the copy.
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 a command that will create an object that is a copy of the given object.
78    */

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

90   public EObject getOwner()
91   {
92     return owner;
93   }
94
95   /**
96    * This is the map of objects to their copies.
97    */

98   public CopyCommand.Helper getCopyHelper()
99   {
100     return copyHelper;
101   }
102
103   protected boolean prepare()
104   {
105     return true;
106   }
107
108   public void doExecute()
109   {
110     // Create the copy
111
//
112
EClass metaObject = owner.eClass();
113     EPackage ePackage = metaObject.getEPackage();
114     EFactory eFactory = ePackage.getEFactoryInstance();
115     copy = eFactory.create(metaObject);
116     copyHelper.put(owner, copy);
117   }
118
119   public void doUndo()
120   {
121     copyHelper.remove(owner);
122   }
123
124   public void doRedo()
125   {
126     copyHelper.put(owner, copy);
127   }
128
129   public Collection JavaDoc doGetResult()
130   {
131     return Collections.singleton(copy);
132   }
133
134   public Collection JavaDoc doGetChildrenToCopy()
135   {
136     // Create commands to create copies of the children.
137
//
138
HashSet JavaDoc result = new HashSet JavaDoc();
139     for (Iterator JavaDoc i = owner.eContents().iterator(); i.hasNext(); )
140     {
141       result.add(i.next());
142     }
143     return result;
144   }
145
146   /**
147    * This gives an abbreviated name using this object's own class' name, without package qualification,
148    * followed by a space separated list of <tt>field:value</tt> pairs.
149    */

150   public String JavaDoc toString()
151   {
152     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
153     result.append(" (owner: " + owner + ")");
154     result.append(" (copyHelper: " + copyHelper + ")");
155
156     return result.toString();
157   }
158 }
159
Popular Tags