1 17 package org.eclipse.emf.edit.command; 18 19 20 import java.util.Collection ; 21 import java.util.Collections ; 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 33 public class PasteFromClipboardCommand extends AbstractOverrideableCommand 34 { 35 38 public static Command create(EditingDomain domain, Object owner, Object feature) 39 { 40 return create(domain, owner, feature, CommandParameter.NO_INDEX); 41 } 42 43 46 public static Command create(EditingDomain domain, Object owner, Object 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 63 protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_PasteFromClipboardCommand_label"); 64 65 68 protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_PasteFromClipboardCommand_description"); 69 70 73 protected StrictCompoundCommand command; 74 75 78 protected Object owner; 79 80 83 protected Object feature; 84 85 88 protected int index; 89 90 93 protected boolean optimize; 94 95 99 public PasteFromClipboardCommand(EditingDomain domain, Object owner, Object feature, int index) 100 { 101 this(domain, owner, feature, index, true); 102 } 103 104 public PasteFromClipboardCommand(EditingDomain domain, Object owner, Object 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 getOwner() 115 { 116 return owner; 117 } 118 119 public Object getFeature() 120 { 121 return feature; 122 } 123 124 public int getIndex() 125 { 126 return index; 127 } 128 129 protected boolean prepare() 130 { 131 command = new StrictCompoundCommand(); 134 135 final Command copyCommand = CopyCommand.create(domain, domain.getClipboard()); 138 command.append(copyCommand); 139 140 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 result = optimizedCanExecute(); 158 } 159 else 160 { 161 result = command.canExecute(); 164 } 165 166 return result; 167 } 168 169 protected boolean optimizedCanExecute() 170 { 171 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 if (command.canExecute()) 185 { 186 command.execute(); 187 } 188 else 189 { 190 } 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 doGetResult() 205 { 206 return command.getResult(); 207 } 208 209 public Collection doGetAffectedObjects() 210 { 211 return command.getAffectedObjects(); 212 } 213 214 public void doDispose() 215 { 216 if (command != null) command.dispose(); 217 } 218 219 223 public String toString() 224 { 225 StringBuffer result = new StringBuffer (super.toString()); 226 result.append(" (domain: " + domain + ")"); 227 228 return result.toString(); 229 } 230 } 231 | Popular Tags |