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