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.AbstractCommand; 24 import org.eclipse.emf.common.command.Command; 25 import org.eclipse.emf.common.util.EList; 26 import org.eclipse.emf.ecore.EObject; 27 import org.eclipse.emf.ecore.EStructuralFeature; 28 import org.eclipse.emf.edit.domain.EditingDomain; 29 30 31 37 public abstract class AbstractOverrideableCommand extends AbstractCommand implements OverrideableCommand 38 { 39 42 protected EditingDomain domain; 43 44 47 protected Command overrideCommand; 48 49 52 protected AbstractOverrideableCommand(EditingDomain domain) 53 { 54 this(domain, null, null); 55 } 56 57 60 protected AbstractOverrideableCommand(EditingDomain domain, String label) 61 { 62 this(domain, label, null); 63 } 64 65 68 protected AbstractOverrideableCommand(EditingDomain domain, String label, String description) 69 { 70 super(label, description); 71 72 this.domain = domain; 73 } 74 75 78 public EditingDomain getDomain() 79 { 80 return domain; 81 } 82 83 86 public Command getOverride() 87 { 88 return overrideCommand; 89 } 90 91 94 public void setOverride(Command overrideCommand) 95 { 96 this.overrideCommand = overrideCommand; 97 } 98 99 public final boolean canExecute() 100 { 101 if (domain != null && !isPrepared) 102 { 103 Command newOverrideCommand = domain.createOverrideCommand(this); 104 setOverride(newOverrideCommand); 105 } 106 107 boolean result = 108 overrideCommand != null ? 109 overrideCommand.canExecute() : 110 doCanExecute(); 111 112 return result; 113 } 114 115 public boolean doCanExecute() 116 { 117 return super.canExecute(); 118 } 119 120 public final void execute() 121 { 122 if (overrideCommand != null) 123 { 124 overrideCommand.execute(); 125 } 126 else 127 { 128 doExecute(); 129 } 130 } 131 132 public abstract void doExecute(); 133 134 public final boolean canUndo() 135 { 136 boolean result = 137 overrideCommand != null ? 138 overrideCommand.canUndo() : 139 doCanUndo(); 140 141 return result; 142 } 143 144 public boolean doCanUndo() 145 { 146 return super.canUndo(); 147 } 148 149 public final void undo() 150 { 151 if (overrideCommand != null) 152 { 153 overrideCommand.undo(); 154 } 155 else 156 { 157 doUndo(); 158 } 159 } 160 161 public abstract void doUndo(); 162 163 public final void redo() 164 { 165 if (overrideCommand != null) 166 { 167 overrideCommand.redo(); 168 } 169 else 170 { 171 doRedo(); 172 } 173 } 174 175 public abstract void doRedo(); 176 177 public final Collection getResult() 178 { 179 return 180 overrideCommand != null ? 181 overrideCommand.getResult() : 182 doGetResult(); 183 } 184 185 public Collection doGetResult() 186 { 187 return super.getResult(); 188 } 189 190 public final Collection getAffectedObjects() 191 { 192 return 193 overrideCommand != null ? 194 overrideCommand.getAffectedObjects() : 195 doGetAffectedObjects(); 196 } 197 198 public Collection doGetAffectedObjects() 199 { 200 return super.getAffectedObjects(); 201 } 202 203 public final String getLabel() 204 { 205 return 206 overrideCommand != null ? 207 overrideCommand.getLabel() : 208 doGetLabel(); 209 } 210 211 public String doGetLabel() 212 { 213 return super.getLabel(); 214 } 215 216 public final String getDescription() 217 { 218 return 219 overrideCommand != null ? 220 overrideCommand.getDescription() : 221 doGetDescription(); 222 } 223 224 public String doGetDescription() 225 { 226 return super.getDescription(); 227 } 228 229 public final void dispose() 230 { 231 if (overrideCommand != null) 232 { 233 overrideCommand.dispose(); 234 } 235 else 236 { 237 doDispose(); 238 } 239 } 240 241 public void doDispose() 242 { 243 super.dispose(); 244 } 245 246 public final Collection getChildrenToCopy() 247 { 248 Collection result = 249 overrideCommand instanceof ChildrenToCopyProvider ? 250 ((ChildrenToCopyProvider)overrideCommand).getChildrenToCopy() : 251 doGetChildrenToCopy(); 252 253 return result; 254 } 255 256 public Collection doGetChildrenToCopy() 257 { 258 return Collections.EMPTY_LIST; 259 } 260 261 public static EList getOwnerList(EObject owner, EStructuralFeature feature) 262 { 263 return 264 owner.eClass().getEAllStructuralFeatures().contains(feature) && feature.isMany() ? 265 (EList)owner.eGet(feature) : 266 null; 267 } 268 269 273 public String toString() 274 { 275 StringBuffer result = new StringBuffer (super.toString()); 276 result.append(" (domain: " + domain + ")"); 277 result.append(" (overrideCommand: " + overrideCommand + ")"); 278 return result.toString(); 279 } 280 } 281 | Popular Tags |