1 11 package org.eclipse.core.commands.operations; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.commands.ExecutionException; 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 22 36 public abstract class AbstractOperation implements IUndoableOperation { 37 List contexts = new ArrayList (); 38 39 private String label = ""; 41 47 public AbstractOperation(String label) { 48 this.label = label; 49 } 50 51 58 public void addContext(IUndoContext context) { 59 if (!contexts.contains(context)) { 60 contexts.add(context); 61 } 62 } 63 64 72 public boolean canExecute() { 73 return true; 74 } 75 76 83 public boolean canRedo() { 84 return true; 85 } 86 87 94 public boolean canUndo() { 95 return true; 96 } 97 98 105 public void dispose() { 106 } 108 109 115 public abstract IStatus execute(IProgressMonitor monitor, IAdaptable info) 116 throws ExecutionException; 117 118 public final IUndoContext[] getContexts() { 119 return (IUndoContext[]) contexts.toArray(new IUndoContext[contexts 120 .size()]); 121 } 122 123 130 public String getLabel() { 131 return label; 132 } 133 134 140 public void setLabel(String name) { 141 label = name; 142 } 143 144 149 public final boolean hasContext(IUndoContext context) { 150 Assert.isNotNull(context); 151 for (int i = 0; i < contexts.size(); i++) { 152 IUndoContext otherContext = (IUndoContext) contexts.get(i); 153 if (context.matches(otherContext) || otherContext.matches(context)) { 157 return true; 158 } 159 } 160 return false; 161 } 162 163 169 public abstract IStatus redo(IProgressMonitor monitor, IAdaptable info) 170 throws ExecutionException; 171 172 179 180 public void removeContext(IUndoContext context) { 181 contexts.remove(context); 182 } 183 184 190 public abstract IStatus undo(IProgressMonitor monitor, IAdaptable info) 191 throws ExecutionException; 192 193 199 public String toString() { 200 final StringBuffer stringBuffer = new StringBuffer (); 201 stringBuffer.append(getLabel()); 202 stringBuffer.append("("); IUndoContext[] contexts = getContexts(); 204 for (int i = 0; i < contexts.length; i++) { 205 stringBuffer.append(contexts[i].toString()); 206 if (i != contexts.length - 1) { 207 stringBuffer.append(','); 208 } 209 } 210 stringBuffer.append(')'); 211 return stringBuffer.toString(); 212 } 213 } 214 | Popular Tags |