KickJava   Java API By Example, From Geeks To Geeks.

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


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: CutToClipboardCommand.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.edit.EMFEditPlugin;
26 import org.eclipse.emf.edit.domain.EditingDomain;
27
28
29 /**
30  * This works just like {@link RemoveCommand} but also sets the removed objects to the {@link EditingDomain}.
31  * In fact, the implementation is just a proxy for remove command.
32  */

33 public class CutToClipboardCommand extends CommandWrapper
34 {
35   /**
36    * This creates a command to remove an object
37    * and set it to the clipboard.
38    */

39   public static Command create(EditingDomain domain, Object JavaDoc value)
40   {
41     if (domain == null)
42     {
43       return new CutToClipboardCommand(domain, RemoveCommand.create(domain, value));
44     }
45     else
46     {
47       return domain.createCommand(CutToClipboardCommand.class, new CommandParameter(null, null, Collections.singleton(value)));
48     }
49   }
50
51   /**
52    * This creates a command to remove a particular value from the specified feature of the owner
53    * and set it to the clipboard.
54    */

55   public static Command create(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value)
56   {
57     if (domain == null)
58     {
59       return new CutToClipboardCommand(domain, RemoveCommand.create(domain, owner, feature, value));
60     }
61     else
62     {
63       return domain.createCommand(CutToClipboardCommand.class, new CommandParameter(owner, feature, Collections.singleton(value)));
64     }
65   }
66
67   /**
68    * This creates a command to remove multiple objects
69    * and set it to the clipboard.
70    */

71   public static Command create(EditingDomain domain, Collection JavaDoc collection)
72   {
73     if (domain == null)
74     {
75       return new CutToClipboardCommand(domain, RemoveCommand.create(domain, collection));
76     }
77     else
78     {
79       return domain.createCommand(CutToClipboardCommand.class, new CommandParameter(null, null, collection));
80     }
81   }
82
83   /**
84    * This creates a command to remove a collection of values from the specified feature of the owner
85    * and set it to the clipboard.
86    */

87   public static Command create(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, Collection JavaDoc collection)
88   {
89     if (domain == null)
90     {
91       return new CutToClipboardCommand(domain, RemoveCommand.create(domain, owner, feature, collection));
92     }
93     else
94     {
95       return domain.createCommand(CutToClipboardCommand.class, new CommandParameter(owner, feature, collection));
96     }
97   }
98
99   /**
100    * This caches the label.
101    */

102   protected static final String JavaDoc LABEL = EMFEditPlugin.INSTANCE.getString("_UI_CutToClipboardCommand_label");
103
104   /**
105    * This caches the description.
106    */

107   protected static final String JavaDoc DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_CutToClipboardCommand_description");
108
109   /**
110    * This is the editing doman in which this command operates.
111    */

112   protected EditingDomain domain;
113
114   /**
115    * This is the original clipboard value before execute.
116    */

117   protected Collection JavaDoc oldClipboard;
118
119   /**
120    * This constructs an instance that ields the result of the given command as its clipboard.
121    */

122   public CutToClipboardCommand(EditingDomain domain, Command command)
123   {
124     super(LABEL, DESCRIPTION, command);
125
126     this.domain = domain;
127   }
128
129   public void execute()
130   {
131     super.execute();
132
133     if (domain != null)
134     {
135       oldClipboard = domain.getClipboard();
136       domain.setClipboard(command.getResult());
137     }
138   }
139
140   public void undo()
141   {
142     super.undo();
143
144     if (domain != null)
145     {
146       domain.setClipboard(oldClipboard);
147     }
148   }
149
150   public void redo()
151   {
152     super.redo();
153
154     if (domain != null)
155     {
156       oldClipboard = domain.getClipboard();
157       domain.setClipboard(command.getResult());
158     }
159   }
160
161   /**
162    * This gives an abbreviated name using this object's own class' name, without package qualification,
163    * followed by a space separated list of <tt>field:value</tt> pairs.
164    */

165   public String JavaDoc toString()
166   {
167     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
168     result.append(" (domain: " + domain + ")");
169     result.append(" (oldClipboard: " + oldClipboard + ")");
170
171     return result.toString();
172   }
173 }
174
Popular Tags