1 11 package org.eclipse.ui.operations; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.commands.operations.IAdvancedUndoableOperation; 16 import org.eclipse.core.commands.operations.IOperationApprover; 17 import org.eclipse.core.commands.operations.IOperationHistory; 18 import org.eclipse.core.commands.operations.IUndoContext; 19 import org.eclipse.core.commands.operations.IUndoableOperation; 20 import org.eclipse.core.runtime.IAdaptable; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.ui.IEditorPart; 27 import org.eclipse.ui.internal.Workbench; 28 import org.eclipse.ui.internal.WorkbenchMessages; 29 import org.eclipse.ui.internal.util.Util; 30 31 56 public final class NonLocalUndoUserApprover implements IOperationApprover { 57 58 private IUndoContext context; 59 60 private IEditorPart part; 61 62 private Object [] elements; 63 64 private Class affectedObjectsClass; 65 66 private ArrayList elementsAndAdapters; 67 68 94 public NonLocalUndoUserApprover(IUndoContext context, IEditorPart part, 95 Object [] affectedObjects, Class preferredComparisonClass) { 96 super(); 97 this.context = context; 98 this.part = part; 99 this.affectedObjectsClass = preferredComparisonClass; 100 this.elements = affectedObjects; 101 } 102 103 110 public IStatus proceedRedoing(IUndoableOperation operation, 111 IOperationHistory history, IAdaptable uiInfo) { 112 113 if (!requiresApproval(operation, uiInfo)) { 115 return Status.OK_STATUS; 116 } 117 118 String message = NLS.bind( 119 WorkbenchMessages.Operations_nonLocalRedoWarning, operation 120 .getLabel(), part.getEditorInput().getName()); 121 return proceedWithOperation(operation, message, WorkbenchMessages.Operations_discardRedo); 122 } 123 124 131 public IStatus proceedUndoing(IUndoableOperation operation, 132 IOperationHistory history, IAdaptable uiInfo) { 133 134 if (!requiresApproval(operation, uiInfo)) { 136 return Status.OK_STATUS; 137 } 138 139 String message = NLS.bind( 140 WorkbenchMessages.Operations_nonLocalUndoWarning, operation 141 .getLabel(), part.getEditorInput().getName()); 142 return proceedWithOperation(operation, message, WorkbenchMessages.Operations_discardUndo); 143 144 } 145 146 151 private IStatus proceedWithOperation(IUndoableOperation operation, 152 final String message, final String discardButton) { 153 154 if (!(operation instanceof IAdvancedUndoableOperation)) { 157 return Status.OK_STATUS; 158 } 159 160 Object [] modifiedElements = ((IAdvancedUndoableOperation) operation) 162 .getAffectedObjects(); 163 164 169 boolean local; 170 if (modifiedElements == null) { 171 local = false; 174 } else { 175 local = true; 180 for (int i = 0; i < modifiedElements.length; i++) { 181 Object modifiedElement = modifiedElements[i]; 182 if (!elementsContains(modifiedElement)) { 183 local = false; 185 if (affectedObjectsClass != null) { 189 Object adapter = Util.getAdapter(modifiedElement, 190 affectedObjectsClass); 191 if (adapter != null && elementsContains(adapter)) { 192 local = true; 193 } 194 } 195 if (!local) { 198 break; 199 } 200 } 201 } 202 } 203 if (local) { 204 return Status.OK_STATUS; 205 } 206 207 final int[] answer = new int[1]; 212 Workbench.getInstance().getDisplay().syncExec(new Runnable () { 213 public void run() { 214 MessageDialog dialog = new MessageDialog(part.getSite().getShell(), part.getEditorInput().getName(), 215 null, message, MessageDialog.QUESTION, new String [] { IDialogConstants.OK_LABEL, 216 discardButton, IDialogConstants.CANCEL_LABEL }, 0); answer[0] = dialog.open(); 218 }}); 219 switch (answer[0]) { 220 case 0: 221 return Status.OK_STATUS; 222 case 1: 223 return IOperationHistory.OPERATION_INVALID_STATUS; 224 default: 225 return Status.CANCEL_STATUS; 228 } 229 } 230 231 235 private boolean requiresApproval(IUndoableOperation operation, 236 IAdaptable uiInfo) { 237 if (!(operation.hasContext(context))) { 240 return false; 241 } 242 243 if (operation.getContexts().length == 1) { 245 return false; 246 } 247 248 if (uiInfo != null) { 252 IUndoContext originatingContext = (IUndoContext) Util.getAdapter(uiInfo, 253 IUndoContext.class); 254 if (originatingContext != null 255 && !(originatingContext.matches(context))) { 256 return false; 257 } 258 } 259 260 return true; 261 } 262 263 267 private boolean elementsContains(Object someObject) { 268 if (elements == null) { 269 return false; 270 } 271 if (elementsAndAdapters == null) { 272 elementsAndAdapters = new ArrayList (elements.length); 276 for (int i = 0; i < elements.length; i++) { 277 Object element = elements[i]; 278 elementsAndAdapters.add(element); 279 if (affectedObjectsClass != null 280 && !affectedObjectsClass.isInstance(element)) { 281 Object adapter = Util.getAdapter(element, affectedObjectsClass); 282 if (adapter != null) { 283 elementsAndAdapters.add(adapter); 284 } 285 } 286 } 287 } 288 for (int i = 0; i < elementsAndAdapters.size(); i++) { 289 if (elementsAndAdapters.get(i).equals(someObject)) { 290 return true; 291 } 292 } 293 return false; 294 } 295 } 296 | Popular Tags |