1 11 package org.eclipse.ltk.ui.refactoring; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 18 import org.eclipse.swt.widgets.Shell; 19 20 import org.eclipse.jface.action.IAction; 21 import org.eclipse.jface.operation.IRunnableWithProgress; 22 import org.eclipse.jface.viewers.ISelection; 23 24 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 25 26 import org.eclipse.ltk.core.refactoring.IUndoManager; 27 import org.eclipse.ltk.core.refactoring.IValidationCheckResultQuery; 28 import org.eclipse.ltk.core.refactoring.RefactoringCore; 29 import org.eclipse.ltk.core.refactoring.UndoManagerAdapter; 30 import org.eclipse.ltk.internal.ui.refactoring.Messages; 31 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages; 32 import org.eclipse.ltk.internal.ui.refactoring.UndoManagerAction; 33 34 53 public class UndoRefactoringAction extends UndoManagerAction implements IWorkbenchWindowActionDelegate { 54 55 private int fPatternLength; 56 57 60 public UndoRefactoringAction() { 61 } 62 63 66 protected String getName() { 67 return RefactoringUIMessages.UndoRefactoringAction_name; 69 } 70 71 74 protected IRunnableWithProgress createOperation(Shell parent) { 75 final IValidationCheckResultQuery query= new Query(parent, RefactoringUIMessages.UndoRefactoringAction_error_title) { 76 protected String getFullMessage(String errorMessage) { 77 return Messages.format( 78 RefactoringUIMessages.UndoRefactoringAction_error_message, 79 errorMessage); 80 } 81 }; 82 return new IRunnableWithProgress(){ 83 public void run(IProgressMonitor pm) throws InvocationTargetException { 84 try { 85 RefactoringCore.getUndoManager().performUndo(query, pm); 86 } catch (CoreException e) { 87 throw new InvocationTargetException (e); 88 } 89 } 90 }; 91 } 92 93 96 protected UndoManagerAdapter createUndoManagerListener() { 97 return new UndoManagerAdapter() { 98 public void undoStackChanged(IUndoManager manager) { 99 IAction action= getAction(); 100 if (action == null) 101 return; 102 boolean enabled= false; 103 String text= null; 104 if (manager.anythingToUndo()) { 105 enabled= true; 106 text= getActionText(); 107 } else { 108 text= RefactoringUIMessages.UndoRefactoringAction_label; 109 } 110 action.setEnabled(enabled); 111 action.setText(text); 112 } 113 }; 114 } 115 116 119 public void selectionChanged(IAction action, ISelection s) { 120 if (!isHooked()) { 121 hookListener(action); 122 fPatternLength= RefactoringUIMessages.UndoRefactoringAction_extendedLabel.length(); 123 IUndoManager undoManager = RefactoringCore.getUndoManager(); 124 if (undoManager.anythingToUndo()) { 125 if (undoManager.peekUndoName() != null) 126 action.setText(getActionText()); 127 action.setEnabled(true); 128 } else { 129 action.setEnabled(false); 130 } 131 } 132 } 133 134 private String getActionText() { 135 return shortenText(Messages.format( 136 RefactoringUIMessages.UndoRefactoringAction_extendedLabel, 137 RefactoringCore.getUndoManager().peekUndoName()), fPatternLength); 138 } 139 } 140 | Popular Tags |