001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.widgets.Button;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.Label;
020: import org.eclipse.swt.widgets.Shell;
021:
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.core.runtime.IProgressMonitor;
024: import org.eclipse.core.runtime.IStatus;
025: import org.eclipse.core.runtime.ProgressMonitorWrapper;
026: import org.eclipse.core.runtime.Status;
027: import org.eclipse.core.runtime.SubProgressMonitor;
028:
029: import org.eclipse.core.resources.IWorkspaceRunnable;
030: import org.eclipse.core.resources.ResourcesPlugin;
031:
032: import org.eclipse.ltk.core.refactoring.Change;
033: import org.eclipse.ltk.core.refactoring.CompositeChange;
034: import org.eclipse.ltk.core.refactoring.Refactoring;
035: import org.eclipse.jface.dialogs.ErrorDialog;
036: import org.eclipse.jface.dialogs.IDialogConstants;
037: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
038:
039: import org.eclipse.jdt.internal.corext.util.Messages;
040:
041: import org.eclipse.jdt.internal.ui.JavaPlugin;
042: import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
043: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
044:
045: /**
046: * Copy of org.eclipse.ltk.internal.ui.refactoring.ChangeExceptionHandler
047: *
048: */
049: public class ChangeExceptionHandler {
050:
051: public static class NotCancelableProgressMonitor extends
052: ProgressMonitorWrapper {
053: public NotCancelableProgressMonitor(IProgressMonitor monitor) {
054: super (monitor);
055: }
056:
057: public void setCanceled(boolean b) {
058: // ignore set cancel
059: }
060:
061: public boolean isCanceled() {
062: return false;
063: }
064: }
065:
066: private Shell fParent;
067: private String fName;
068:
069: private static class RefactorErrorDialog extends ErrorDialog {
070: public RefactorErrorDialog(Shell parentShell,
071: String dialogTitle, String dialogMessage,
072: IStatus status, int displayMask) {
073: super (parentShell, dialogTitle, dialogMessage, status,
074: displayMask);
075: }
076:
077: protected void createButtonsForButtonBar(Composite parent) {
078: super .createButtonsForButtonBar(parent);
079: Button ok = getButton(IDialogConstants.OK_ID);
080: ok
081: .setText(RefactoringMessages.ChangeExceptionHandler_undo_button);
082: Button abort = createButton(
083: parent,
084: IDialogConstants.CANCEL_ID,
085: RefactoringMessages.ChangeExceptionHandler_abort_button,
086: true);
087: abort.moveBelow(ok);
088: abort.setFocus();
089: }
090:
091: protected Control createMessageArea(Composite parent) {
092: Control result = super .createMessageArea(parent);
093: new Label(parent, SWT.NONE); // filler
094: Label label = new Label(parent, SWT.NONE);
095: label
096: .setText(RefactoringMessages.ChangeExceptionHandler_message);
097: label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
098: applyDialogFont(result);
099: return result;
100: }
101: }
102:
103: public ChangeExceptionHandler(Shell parent, Refactoring refactoring) {
104: fParent = parent;
105: fName = refactoring.getName();
106: }
107:
108: public void handle(Change change, RuntimeException exception) {
109: JavaPlugin.log(exception);
110: IStatus status = null;
111: if (exception.getMessage() == null) {
112: status = new Status(
113: IStatus.ERROR,
114: JavaPlugin.getPluginId(),
115: IStatus.ERROR,
116: RefactoringMessages.ChangeExceptionHandler_status_without_detail,
117: exception);
118: } else {
119: status = new Status(IStatus.ERROR,
120: JavaPlugin.getPluginId(), IStatus.ERROR, exception
121: .getMessage(), exception);
122: }
123: handle(change, status);
124: }
125:
126: public void handle(Change change, CoreException exception) {
127: JavaPlugin.log(exception);
128: handle(change, exception.getStatus());
129: }
130:
131: private void handle(Change change, IStatus status) {
132: if (change instanceof CompositeChange) {
133: Change undo = ((CompositeChange) change)
134: .getUndoUntilException();
135: if (undo != null) {
136: JavaPlugin.log(status);
137: final ErrorDialog dialog = new RefactorErrorDialog(
138: fParent,
139: RefactoringMessages.ChangeExceptionHandler_dialog_title,
140: Messages
141: .format(
142: RefactoringMessages.ChangeExceptionHandler_dialog_message,
143: fName), status, IStatus.OK
144: | IStatus.INFO | IStatus.WARNING
145: | IStatus.ERROR);
146: int result = dialog.open();
147: if (result == IDialogConstants.OK_ID) {
148: performUndo(undo);
149: }
150: return;
151: }
152: }
153: ErrorDialog dialog = new ErrorDialog(
154: fParent,
155: RefactoringMessages.ChangeExceptionHandler_dialog_title,
156: Messages
157: .format(
158: RefactoringMessages.ChangeExceptionHandler_dialog_message,
159: fName), status, IStatus.OK
160: | IStatus.INFO | IStatus.WARNING
161: | IStatus.ERROR);
162: dialog.open();
163: }
164:
165: private void performUndo(final Change undo) {
166: IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
167: public void run(IProgressMonitor monitor)
168: throws CoreException {
169: monitor.beginTask("", 11); //$NON-NLS-1$
170: try {
171: undo
172: .initializeValidationData(new NotCancelableProgressMonitor(
173: new SubProgressMonitor(monitor, 1)));
174: if (undo
175: .isValid(new SubProgressMonitor(monitor, 1))
176: .hasFatalError()) {
177: monitor.done();
178: return;
179: }
180: undo.perform(new SubProgressMonitor(monitor, 9));
181: } finally {
182: undo.dispose();
183: }
184: }
185: };
186: WorkbenchRunnableAdapter adapter = new WorkbenchRunnableAdapter(
187: runnable, ResourcesPlugin.getWorkspace().getRoot());
188: ProgressMonitorDialog dialog = new ProgressMonitorDialog(
189: fParent);
190: try {
191: dialog.run(false, false, adapter);
192: } catch (InvocationTargetException e) {
193: ExceptionHandler
194: .handle(
195: e,
196: fParent,
197: RefactoringMessages.ChangeExceptionHandler_undo_dialog_title,
198: RefactoringMessages.ChangeExceptionHandler_undo_dialog_message
199: + fName);
200: } catch (InterruptedException e) {
201: // can't happen
202: }
203: }
204: }
|