1 11 package org.eclipse.jdt.internal.ui.refactoring; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.widgets.Button; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Control; 20 import org.eclipse.swt.widgets.Label; 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.ProgressMonitorWrapper; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 30 import org.eclipse.core.resources.IWorkspaceRunnable; 31 import org.eclipse.core.resources.ResourcesPlugin; 32 33 import org.eclipse.ltk.core.refactoring.Change; 34 import org.eclipse.ltk.core.refactoring.CompositeChange; 35 import org.eclipse.ltk.core.refactoring.Refactoring; 36 import org.eclipse.jface.dialogs.ErrorDialog; 37 import org.eclipse.jface.dialogs.IDialogConstants; 38 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 39 40 import org.eclipse.jdt.internal.corext.util.Messages; 41 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 45 46 50 public class ChangeExceptionHandler { 51 52 public static class NotCancelableProgressMonitor extends ProgressMonitorWrapper { 53 public NotCancelableProgressMonitor(IProgressMonitor monitor) { 54 super(monitor); 55 } 56 public void setCanceled(boolean b) { 57 } 59 public boolean isCanceled() { 60 return false; 61 } 62 } 63 64 private Shell fParent; 65 private String fName; 66 67 private static class RefactorErrorDialog extends ErrorDialog { 68 public RefactorErrorDialog(Shell parentShell, String dialogTitle, String dialogMessage, IStatus status, int displayMask) { 69 super(parentShell, dialogTitle, dialogMessage, status, displayMask); 70 } 71 protected void createButtonsForButtonBar(Composite parent) { 72 super.createButtonsForButtonBar(parent); 73 Button ok= getButton(IDialogConstants.OK_ID); 74 ok.setText( RefactoringMessages.ChangeExceptionHandler_undo_button); 75 Button abort= createButton(parent, IDialogConstants.CANCEL_ID, RefactoringMessages.ChangeExceptionHandler_abort_button, true); 76 abort.moveBelow(ok); 77 abort.setFocus(); 78 } 79 protected Control createMessageArea (Composite parent) { 80 Control result= super.createMessageArea(parent); 81 new Label(parent, SWT.NONE); Label label= new Label(parent, SWT.NONE); 83 label.setText(RefactoringMessages.ChangeExceptionHandler_message); 84 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 85 applyDialogFont(result); 86 return result; 87 } 88 } 89 90 public ChangeExceptionHandler(Shell parent, Refactoring refactoring) { 91 fParent= parent; 92 fName= refactoring.getName(); 93 } 94 95 public void handle(Change change, RuntimeException exception) { 96 JavaPlugin.log(exception); 97 IStatus status= null; 98 if (exception.getMessage() == null) { 99 status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR, 100 RefactoringMessages.ChangeExceptionHandler_status_without_detail, exception); 101 } else { 102 status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR, 103 exception.getMessage(), exception); 104 } 105 handle(change, status); 106 } 107 108 public void handle(Change change, CoreException exception) { 109 JavaPlugin.log(exception); 110 handle(change, exception.getStatus()); 111 } 112 113 private void handle(Change change, IStatus status) { 114 if (change instanceof CompositeChange) { 115 Change undo= ((CompositeChange)change).getUndoUntilException(); 116 if (undo != null) { 117 JavaPlugin.log(status); 118 final ErrorDialog dialog= new RefactorErrorDialog(fParent, 119 RefactoringMessages.ChangeExceptionHandler_dialog_title, 120 Messages.format(RefactoringMessages.ChangeExceptionHandler_dialog_message, fName), 121 status, IStatus.OK | IStatus.INFO | IStatus.WARNING | IStatus.ERROR); 122 int result= dialog.open(); 123 if (result == IDialogConstants.OK_ID) { 124 performUndo(undo); 125 } 126 return; 127 } 128 } 129 ErrorDialog dialog= new ErrorDialog(fParent, 130 RefactoringMessages.ChangeExceptionHandler_dialog_title, 131 Messages.format(RefactoringMessages.ChangeExceptionHandler_dialog_message, fName), 132 status, IStatus.OK | IStatus.INFO | IStatus.WARNING | IStatus.ERROR); 133 dialog.open(); 134 } 135 136 private void performUndo(final Change undo) { 137 IWorkspaceRunnable runnable= new IWorkspaceRunnable() { 138 public void run(IProgressMonitor monitor) throws CoreException { 139 monitor.beginTask("", 11); try { 141 undo.initializeValidationData(new NotCancelableProgressMonitor(new SubProgressMonitor(monitor, 1))); 142 if (undo.isValid(new SubProgressMonitor(monitor,1)).hasFatalError()) { 143 monitor.done(); 144 return; 145 } 146 undo.perform(new SubProgressMonitor(monitor, 9)); 147 } finally { 148 undo.dispose(); 149 } 150 } 151 }; 152 WorkbenchRunnableAdapter adapter= new WorkbenchRunnableAdapter(runnable, 153 ResourcesPlugin.getWorkspace().getRoot()); 154 ProgressMonitorDialog dialog= new ProgressMonitorDialog(fParent); 155 try { 156 dialog.run(false, false, adapter); 157 } catch (InvocationTargetException e) { 158 ExceptionHandler.handle(e, fParent, 159 RefactoringMessages.ChangeExceptionHandler_undo_dialog_title, 160 RefactoringMessages.ChangeExceptionHandler_undo_dialog_message + fName); 161 } catch (InterruptedException e) { 162 } 164 } 165 } 166 | Popular Tags |