1 11 package org.eclipse.ltk.internal.ui.refactoring; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.OperationCanceledException; 16 17 import org.eclipse.core.resources.ResourcesPlugin; 18 19 import org.eclipse.swt.widgets.Shell; 20 21 import org.eclipse.jface.action.IAction; 22 import org.eclipse.jface.dialogs.Dialog; 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 26 import org.eclipse.jface.operation.IRunnableWithProgress; 27 28 import org.eclipse.ui.IWorkbenchWindow; 29 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 30 import org.eclipse.ui.PlatformUI; 31 32 import org.eclipse.ltk.core.refactoring.IValidationCheckResultQuery; 33 import org.eclipse.ltk.core.refactoring.RefactoringCore; 34 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 35 import org.eclipse.ltk.core.refactoring.UndoManagerAdapter; 36 import org.eclipse.ltk.ui.refactoring.RefactoringUI; 37 38 public abstract class UndoManagerAction implements IWorkbenchWindowActionDelegate { 39 40 private static final int MAX_LENGTH= 30; 41 42 private IAction fAction; 43 private IWorkbenchWindow fWorkbenchWindow; 44 private UndoManagerAdapter fUndoManagerListener; 45 46 protected static abstract class Query implements IValidationCheckResultQuery { 47 private Shell fParent; 48 private String fTitle; 49 public Query(Shell parent, String title) { 50 fParent= parent; 51 fTitle= title; 52 } 53 public boolean proceed(RefactoringStatus status) { 54 final Dialog dialog= RefactoringUI.createRefactoringStatusDialog(status, fParent, fTitle, false); 55 final int[] result= new int[1]; 56 Runnable r= new Runnable () { 57 public void run() { 58 result[0]= dialog.open(); 59 } 60 }; 61 fParent.getDisplay().syncExec(r); 62 return result[0] == IDialogConstants.OK_ID; 63 } 64 public void stopped(final RefactoringStatus status) { 65 Runnable r= new Runnable () { 66 public void run() { 67 String message= status.getMessageMatchingSeverity(RefactoringStatus.FATAL); 68 MessageDialog.openWarning(fParent, fTitle, getFullMessage(message)); 69 } 70 }; 71 fParent.getDisplay().syncExec(r); 72 } 73 protected abstract String getFullMessage(String errorMessage); 74 } 75 76 protected UndoManagerAction() { 77 } 78 79 protected abstract IRunnableWithProgress createOperation(Shell parent); 80 81 protected abstract UndoManagerAdapter createUndoManagerListener(); 82 83 protected abstract String getName(); 84 85 protected IWorkbenchWindow getWorkbenchWindow() { 86 return fWorkbenchWindow; 87 } 88 89 protected IAction getAction() { 90 return fAction; 91 } 92 93 protected boolean isHooked() { 94 return fAction != null; 95 } 96 97 protected void hookListener(IAction action) { 98 if (isHooked()) 99 return; 100 fAction= action; 101 fUndoManagerListener= createUndoManagerListener(); 102 RefactoringCore.getUndoManager().addListener(fUndoManagerListener); 103 } 104 105 protected String shortenText(String text, int patternLength) { 106 int length= text.length(); 107 final int finalLength = MAX_LENGTH + patternLength; 108 if (text.length() <= finalLength) 109 return text; 110 StringBuffer result= new StringBuffer (); 111 int mid= finalLength / 2; 112 result.append(text.substring(0, mid)); 113 result.append("..."); result.append(text.substring(length - mid)); 115 return result.toString(); 116 } 117 118 121 public void dispose() { 122 if (fUndoManagerListener != null) 123 RefactoringCore.getUndoManager().removeListener(fUndoManagerListener); 124 fWorkbenchWindow= null; 125 fAction= null; 126 fUndoManagerListener= null; 127 } 128 129 132 public void init(IWorkbenchWindow window) { 133 fWorkbenchWindow= window; 134 } 135 136 139 public void run(IAction action) { 140 Shell parent= fWorkbenchWindow.getShell(); 141 IRunnableWithProgress op= createOperation(parent); 142 try { 143 PlatformUI.getWorkbench().getProgressService().runInUI( 144 new ProgressMonitorDialog(fWorkbenchWindow.getShell()), 145 op, ResourcesPlugin.getWorkspace().getRoot()); 146 } catch (InvocationTargetException e) { 147 RefactoringCore.getUndoManager().flush(); 148 ExceptionHandler.handle(e, 149 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 150 RefactoringUIMessages.UndoManagerAction_internal_error_title, 151 RefactoringUIMessages.UndoManagerAction_internal_error_message); 152 } catch (InterruptedException e) { 153 } catch (OperationCanceledException e) { 155 } 157 } 158 } 159 | Popular Tags |