KickJava   Java API By Example, From Geeks To Geeks.

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


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: PasteFromClipboardCommand.java,v 1.2 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
23 import org.eclipse.emf.common.command.Command;
24 import org.eclipse.emf.common.command.CommandWrapper;
25 import org.eclipse.emf.common.command.StrictCompoundCommand;
26 import org.eclipse.emf.edit.EMFEditPlugin;
27 import org.eclipse.emf.edit.domain.EditingDomain;
28
29
30 /**
31  * This works exactly like an {@link AddCommand} but the things to be added are copied from the {@link EditingDomain} clipboard.
32  */

33 public class PasteFromClipboardCommand extends AbstractOverrideableCommand
34 {
35   /**
36    * This creates a command to add copies from the clipboard to the specified feature of the owner.
37    */

38   public static Command create(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature)
39   {
40     return create(domain, owner, feature, CommandParameter.NO_INDEX);
41   }
42
43   /**
44    * This creates a command to add copies from the clipboard to the specified feature of the owner and at the given index.
45    */

46   public static Command create(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, int index)
47   {
48     if (domain == null)
49     {
50       return new PasteFromClipboardCommand(domain, owner, feature, index, true);
51     }
52     else
53     {
54       Command command =
55         domain.createCommand(PasteFromClipboardCommand.class, new CommandParameter(owner, feature, Collections.EMPTY_LIST, index));
56       return command;
57     }
58   }
59     
60   /**
61    * This caches the label.
62    */

63   protected static final String JavaDoc LABEL = EMFEditPlugin.INSTANCE.getString("_UI_PasteFromClipboardCommand_label");
64
65   /**
66    * This caches the description.
67    */

68   protected static final String JavaDoc DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_PasteFromClipboardCommand_description");
69
70   /**
71    * This is the command that does the actual pasting.
72    */

73   protected StrictCompoundCommand command;
74
75   /**
76    * This is object where the clipboard copy is pasted.
77    */

78   protected Object JavaDoc owner;
79
80   /**
81    * This is feature of the owner where the clipboard copy is pasted.
82    */

83   protected Object JavaDoc feature;
84
85   /**
86    * This is index in the feature of the owner where the clipboard copy is pasted.
87    */

88   protected int index;
89
90   /**
91    * This controls whether or not to optimize the canExecute (prepare)
92    */

93   protected boolean optimize;
94
95   /**
96    * This constructs an instance from the domain, which provides access the clipboard collection
97    * via {@link EditingDomain#getCommandStack}.
98    */

99   public PasteFromClipboardCommand(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, int index)
100   {
101     this(domain, owner, feature, index, true);
102   }
103
104   public PasteFromClipboardCommand(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, int index, boolean optimize)
105   {
106     super(domain, LABEL, DESCRIPTION);
107
108     this.owner = owner;
109     this.feature = feature;
110     this.index = index;
111     this.optimize = optimize;
112   }
113
114   public Object JavaDoc getOwner()
115   {
116     return owner;
117   }
118
119   public Object JavaDoc getFeature()
120   {
121     return feature;
122   }
123
124   public int getIndex()
125   {
126     return index;
127   }
128
129   protected boolean prepare()
130   {
131     // Create a strict compound command to do a copy and then add the result
132
//
133
command = new StrictCompoundCommand();
134
135     // Create a command to copy the clipboard.
136
//
137
final Command copyCommand = CopyCommand.create(domain, domain.getClipboard());
138     command.append(copyCommand);
139
140     // Create a proxy that will create an add command.
141
//
142
command.append
143       (new CommandWrapper()
144        {
145          protected Command createCommand()
146          {
147            Command addCommand = AddCommand.create(domain, owner, feature, copyCommand.getResult(), index);
148            return addCommand;
149          }
150        });
151
152     boolean result;
153     if (optimize)
154     {
155       // This will determine canExecute as efficiently as possible.
156
//
157
result = optimizedCanExecute();
158     }
159     else
160     {
161       // This will actually execute the copy command in order to check if the add can execute.
162
//
163
result = command.canExecute();
164     }
165
166     return result;
167   }
168
169   protected boolean optimizedCanExecute()
170   {
171     // We'll assume that the copy command can execute and that adding a copy of the clipboard
172
// is the same test as adding the clipboard contents itself.
173
//
174
Command addCommand = AddCommand.create(domain, owner, feature, domain.getClipboard());
175     boolean result = addCommand.canExecute();
176     addCommand.dispose();
177     return result;
178   }
179
180   public void doExecute()
181   {
182     // We need to check canExecute() here in case prepare() went down the "optimize" path.
183
//
184
if (command.canExecute())
185     {
186       command.execute();
187     }
188     else
189     {
190       // Thread.dumpStack();
191
}
192   }
193
194   public void doUndo()
195   {
196     command.undo();
197   }
198
199   public void doRedo()
200   {
201     command.redo();
202   }
203
204   public Collection JavaDoc doGetResult()
205   {
206     return command.getResult();
207   }
208
209   public Collection JavaDoc doGetAffectedObjects()
210   {
211     return command.getAffectedObjects();
212   }
213
214   public void doDispose()
215   {
216     if (command != null) command.dispose();
217   }
218
219   /**
220    * This gives an abbreviated name using this object's own class' name, without package qualification,
221    * followed by a space separated list of <tt>field:value</tt> pairs.
222    */

223   public String JavaDoc toString()
224   {
225     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
226     result.append(" (domain: " + domain + ")");
227
228     return result.toString();
229   }
230 }
231
Popular Tags