KickJava   Java API By Example, From Geeks To Geeks.

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


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: CopyToClipboardCommand.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
23 import org.eclipse.emf.common.command.Command;
24 import org.eclipse.emf.common.command.AbstractCommand;
25 import org.eclipse.emf.edit.EMFEditPlugin;
26 import org.eclipse.emf.edit.domain.EditingDomain;
27
28
29 /**
30  * This works exactly like a {@link CopyCommand} but set the copy result to the {@link EditingDomain}.
31  * In fact, the implementation is just a proxy for copy command.
32  */

33 public class CopyToClipboardCommand extends AbstractOverrideableCommand implements AbstractCommand.NonDirtying
34 {
35   /**
36    * This creates a command that copies the given collection of objects to the clipboard.
37    */

38   public static Command create(EditingDomain domain, final Collection JavaDoc collection)
39   {
40     if (domain == null)
41     {
42       CopyToClipboardCommand command = new CopyToClipboardCommand(domain, collection);
43       return command;
44     }
45     else
46     {
47       Command command = domain.createCommand(CopyToClipboardCommand.class, new CommandParameter(null, null, collection));
48       return command;
49     }
50   }
51
52   /**
53    * This caches the label.
54    */

55   protected static final String JavaDoc LABEL = EMFEditPlugin.INSTANCE.getString("_UI_CopyToClipboardCommand_label");
56
57   /**
58    * This caches the description.
59    */

60   protected static final String JavaDoc DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_CopyToClipboardCommand_description");
61
62   /**
63    * This constructs a command that copies the given collection of objects to the clipboard.
64    */

65   public CopyToClipboardCommand(EditingDomain domain, Collection JavaDoc collection)
66   {
67     super (domain, LABEL, DESCRIPTION);
68
69     this.sourceObjects = collection;
70   }
71
72   /**
73    * This is the collection of objects to be copied to the clipboard.
74    */

75   protected Collection JavaDoc sourceObjects;
76
77   /**
78    * This is the original clipboard value before execute.
79    */

80   protected Collection JavaDoc oldClipboard;
81
82   /**
83    * This is the command that does the actual copying.
84    */

85   protected Command copyCommand;
86
87   /**
88    * This creates a command that copies the given object to the clipboard.
89    */

90   public static Command create(EditingDomain domain, Object JavaDoc owner)
91   {
92     return create(domain, Collections.singleton(owner));
93   }
94
95   /**
96    * This returns the collection of objects to be copied to the clipboard.
97    */

98   public Collection JavaDoc getSourceObjects()
99   {
100     return sourceObjects;
101   }
102
103   protected boolean prepare()
104   {
105     copyCommand = CopyCommand.create(domain, sourceObjects);
106     return copyCommand.canExecute();
107   }
108
109   public void doExecute()
110   {
111     copyCommand.execute();
112
113     oldClipboard = domain.getClipboard();
114     domain.setClipboard(copyCommand.getResult());
115   }
116
117   public void doUndo()
118   {
119     copyCommand.undo();
120
121     domain.setClipboard(oldClipboard);
122   }
123
124   public void doRedo()
125   {
126     copyCommand.redo();
127
128     oldClipboard = domain.getClipboard();
129     domain.setClipboard(copyCommand.getResult());
130   }
131
132   public Collection JavaDoc doGetResult()
133   {
134     return copyCommand.getResult();
135   }
136
137   public Collection JavaDoc doGetAffectedObjects()
138   {
139     return copyCommand.getAffectedObjects();
140   }
141
142   public void doDispose()
143   {
144     if (copyCommand != null) copyCommand.dispose();
145   }
146
147   /**
148    * This gives an abbreviated name using this object's own class' name, without package qualification,
149    * followed by a space separated list of <tt>field:value</tt> pairs.
150    */

151   public String JavaDoc toString()
152   {
153     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
154     result.append(" (domain: " + domain + ")");
155     result.append(" (sourceObjects: " + sourceObjects + ")");
156     result.append(" (oldClipboard: " + oldClipboard + ")");
157
158     return result.toString();
159   }
160 }
161
Popular Tags