KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > ChangeExceptionHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.refactoring;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
47  * Copy of org.eclipse.ltk.internal.ui.refactoring.ChangeExceptionHandler
48  *
49  */

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             // ignore set cancel
58
}
59         public boolean isCanceled() {
60             return false;
61         }
62     }
63     
64     private Shell fParent;
65     private String JavaDoc fName;
66     
67     private static class RefactorErrorDialog extends ErrorDialog {
68         public RefactorErrorDialog(Shell parentShell, String JavaDoc dialogTitle, String JavaDoc 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); // filler
82
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 JavaDoc 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); //$NON-NLS-1$
140
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 JavaDoc e) {
158             ExceptionHandler.handle(e, fParent,
159                 RefactoringMessages.ChangeExceptionHandler_undo_dialog_title,
160                 RefactoringMessages.ChangeExceptionHandler_undo_dialog_message + fName);
161         } catch (InterruptedException JavaDoc e) {
162             // can't happen
163
}
164     }
165 }
166
Popular Tags