KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ltk.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.Status;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28
29 import org.eclipse.core.resources.IWorkspaceRunnable;
30 import org.eclipse.core.resources.ResourcesPlugin;
31
32 import org.eclipse.ltk.core.refactoring.Change;
33 import org.eclipse.ltk.core.refactoring.CompositeChange;
34 import org.eclipse.ltk.core.refactoring.Refactoring;
35 import org.eclipse.jface.dialogs.ErrorDialog;
36 import org.eclipse.jface.dialogs.IDialogConstants;
37 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
38
39 public class ChangeExceptionHandler {
40     
41     private Shell fParent;
42     private String JavaDoc fName;
43     
44     private static class RefactorErrorDialog extends ErrorDialog {
45         public RefactorErrorDialog(Shell parentShell, String JavaDoc dialogTitle, String JavaDoc dialogMessage, IStatus status, int displayMask) {
46             super(parentShell, dialogTitle, dialogMessage, status, displayMask);
47         }
48         protected void createButtonsForButtonBar(Composite parent) {
49             super.createButtonsForButtonBar(parent);
50             Button ok= getButton(IDialogConstants.OK_ID);
51             ok.setText( RefactoringUIMessages.ChangeExceptionHandler_undo);
52             Button abort= createButton(parent, IDialogConstants.CANCEL_ID, RefactoringUIMessages.ChangeExceptionHandler_abort, true);
53             abort.moveBelow(ok);
54             abort.setFocus();
55         }
56         protected Control createMessageArea (Composite parent) {
57             Control result= super.createMessageArea(parent);
58             new Label(parent, SWT.NONE); // filler
59
Label label= new Label(parent, SWT.NONE);
60             label.setText(RefactoringUIMessages.ChangeExceptionHandler_button_explanation);
61             label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
62             applyDialogFont(result);
63             return result;
64         }
65     }
66     
67     public ChangeExceptionHandler(Shell parent, Refactoring refactoring) {
68         fParent= parent;
69         fName= refactoring.getName();
70     }
71     
72     public void handle(Change change, RuntimeException JavaDoc exception) {
73         RefactoringUIPlugin.log(exception);
74         IStatus status= null;
75         if (exception.getMessage() == null) {
76             status= new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), IStatus.ERROR,
77                 RefactoringUIMessages.ChangeExceptionHandler_no_details, exception);
78         } else {
79             status= new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(), IStatus.ERROR,
80                 exception.getMessage(), exception);
81         }
82         handle(change, status);
83     }
84     
85     public void handle(Change change, CoreException exception) {
86         RefactoringUIPlugin.log(exception);
87         handle(change, exception.getStatus());
88     }
89     
90     private void handle(Change change, IStatus status) {
91         if (change instanceof CompositeChange) {
92             Change undo= ((CompositeChange)change).getUndoUntilException();
93             if (undo != null) {
94                 RefactoringUIPlugin.log(status);
95                 final ErrorDialog dialog= new RefactorErrorDialog(fParent,
96                     RefactoringUIMessages.ChangeExceptionHandler_refactoring,
97                     Messages.format(RefactoringUIMessages.ChangeExceptionHandler_unexpected_exception, fName),
98                     status, IStatus.OK | IStatus.INFO | IStatus.WARNING | IStatus.ERROR);
99                 int result= dialog.open();
100                 if (result == IDialogConstants.OK_ID) {
101                     performUndo(undo);
102                 }
103                 return;
104             }
105         }
106         ErrorDialog dialog= new ErrorDialog(fParent,
107             RefactoringUIMessages.ChangeExceptionHandler_refactoring,
108             Messages.format(RefactoringUIMessages.ChangeExceptionHandler_unexpected_exception, fName),
109             status, IStatus.OK | IStatus.INFO | IStatus.WARNING | IStatus.ERROR);
110         dialog.open();
111     }
112     
113     private void performUndo(final Change undo) {
114         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
115             public void run(IProgressMonitor monitor) throws CoreException {
116                 monitor.beginTask("", 11); //$NON-NLS-1$
117
try {
118                     undo.initializeValidationData(new NotCancelableProgressMonitor(new SubProgressMonitor(monitor, 1)));
119                     if (undo.isValid(new SubProgressMonitor(monitor,1)).hasFatalError()) {
120                         monitor.done();
121                         return;
122                     }
123                     undo.perform(new SubProgressMonitor(monitor, 9));
124                 } finally {
125                     undo.dispose();
126                 }
127             }
128         };
129         WorkbenchRunnableAdapter adapter= new WorkbenchRunnableAdapter(runnable,
130             ResourcesPlugin.getWorkspace().getRoot());
131         ProgressMonitorDialog dialog= new ProgressMonitorDialog(fParent);
132         try {
133             dialog.run(false, false, adapter);
134         } catch (InvocationTargetException JavaDoc e) {
135             ExceptionHandler.handle(e, fParent,
136                 RefactoringUIMessages.ChangeExceptionHandler_rollback_title,
137                 RefactoringUIMessages.ChangeExceptionHandler_rollback_message + fName);
138         } catch (InterruptedException JavaDoc e) {
139             // can't happen
140
}
141     }
142 }
143
Popular Tags