]>
Commit | Line | Data |
---|---|---|
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 | *******************************************************************************/ | |
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.jface.dialogs.ErrorDialog; | |
34 | import org.eclipse.jface.dialogs.IDialogConstants; | |
35 | import org.eclipse.jface.dialogs.ProgressMonitorDialog; | |
36 | ||
37 | import org.eclipse.ltk.core.refactoring.Change; | |
38 | import org.eclipse.ltk.core.refactoring.CompositeChange; | |
39 | import org.eclipse.ltk.core.refactoring.Refactoring; | |
40 | ||
41 | import org.eclipse.jdt.internal.corext.util.Messages; | |
42 | ||
43 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
44 | import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; | |
45 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
46 | ||
47 | /** | |
48 | * Copy of org.eclipse.ltk.internal.ui.refactoring.ChangeExceptionHandler | |
49 | * | |
50 | */ | |
51 | public 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 | } |