1 11 package org.eclipse.ui.operations; 12 13 import org.eclipse.core.commands.ExecutionException; 14 import org.eclipse.core.commands.operations.IOperationHistory; 15 import org.eclipse.core.commands.operations.IUndoContext; 16 import org.eclipse.core.commands.operations.IUndoableOperation; 17 import org.eclipse.core.commands.operations.LinearUndoViolationDetector; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.jface.dialogs.MessageDialog; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.ui.IWorkbenchPart; 24 import org.eclipse.ui.IWorkbenchPart2; 25 import org.eclipse.ui.internal.Workbench; 26 import org.eclipse.ui.internal.WorkbenchMessages; 27 28 41 public final class LinearUndoViolationUserApprover extends 42 LinearUndoViolationDetector { 43 44 private IWorkbenchPart part; 45 46 private IUndoContext context; 47 48 57 public LinearUndoViolationUserApprover(IUndoContext context, 58 IWorkbenchPart part) { 59 super(); 60 this.part = part; 61 this.context = context; 62 } 63 64 72 protected IStatus allowLinearRedoViolation(IUndoableOperation operation, 73 IUndoContext context, IOperationHistory history, IAdaptable uiInfo) { 74 75 if (this.context != context) { 76 return Status.OK_STATUS; 77 } 78 79 final String message = NLS.bind( 80 WorkbenchMessages.Operations_linearRedoViolation, 81 getTitle(part), operation.getLabel()); 82 final boolean [] proceed = new boolean[1]; 83 Workbench.getInstance().getDisplay().syncExec(new Runnable () { 84 public void run() { 85 part.setFocus(); 87 proceed[0] = MessageDialog.openQuestion(part.getSite() 88 .getShell(), getTitle(part), message); 89 } 90 }); 91 92 if (proceed[0]) { 93 while (operation != history.getRedoOperation(context)) { 95 try { 96 IStatus status = history.redo(context, null, uiInfo); 97 if (!status.isOK()) { 98 history.dispose(context, false, true, false); 101 return Status.CANCEL_STATUS; 102 } 103 } catch (ExecutionException e) { 104 history.dispose(context, false, true, false); 106 return Status.CANCEL_STATUS; 107 } 108 } 109 return Status.OK_STATUS; 110 } 111 112 return Status.CANCEL_STATUS; 113 } 114 115 123 protected IStatus allowLinearUndoViolation(IUndoableOperation operation, 124 IUndoContext context, IOperationHistory history, IAdaptable uiInfo) { 125 126 if (this.context != context) { 127 return Status.OK_STATUS; 128 } 129 130 final String message = NLS.bind( 131 WorkbenchMessages.Operations_linearUndoViolation, 132 getTitle(part), operation.getLabel()); 133 final boolean [] proceed = new boolean[1]; 134 Workbench.getInstance().getDisplay().syncExec(new Runnable () { 135 public void run() { 136 part.setFocus(); 138 proceed[0] = MessageDialog.openQuestion(part.getSite() 139 .getShell(), getTitle(part), message); 140 } 141 }); 142 143 if (proceed[0]) { 144 while (operation != history.getUndoOperation(context)) { 146 try { 147 IStatus status = history.undo(context, null, uiInfo); 148 if (!status.isOK()) { 149 history.dispose(context, true, false, false); 152 return Status.CANCEL_STATUS; 153 } 154 } catch (ExecutionException e) { 155 history.dispose(context, true, false, false); 157 return Status.CANCEL_STATUS; 158 } 159 } 160 return Status.OK_STATUS; 161 } 162 return Status.CANCEL_STATUS; 163 } 164 165 169 private String getTitle(IWorkbenchPart part) { 170 String title; 171 if (part instanceof IWorkbenchPart2) { 172 title = ((IWorkbenchPart2) part).getPartName(); 173 } else { 174 title = part.getTitle(); 175 } 176 if (title == null) { 178 title = ""; } 180 return title; 181 } 182 } 183 | Popular Tags |