]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.ui.refactoring; | |
12 | ||
13 | import java.lang.reflect.InvocationTargetException; | |
14 | import java.util.Arrays; | |
15 | ||
16 | import org.eclipse.swt.SWT; | |
17 | import org.eclipse.swt.events.SelectionAdapter; | |
18 | import org.eclipse.swt.events.SelectionEvent; | |
19 | import org.eclipse.swt.graphics.Image; | |
20 | import org.eclipse.swt.widgets.Button; | |
21 | import org.eclipse.swt.widgets.Composite; | |
22 | import org.eclipse.swt.widgets.Control; | |
23 | import org.eclipse.swt.widgets.Shell; | |
24 | ||
25 | import org.eclipse.core.runtime.Assert; | |
26 | import org.eclipse.core.runtime.CoreException; | |
27 | import org.eclipse.core.runtime.IProgressMonitor; | |
28 | import org.eclipse.core.runtime.SubProgressMonitor; | |
29 | ||
30 | import org.eclipse.core.resources.IncrementalProjectBuilder; | |
31 | import org.eclipse.core.resources.ResourcesPlugin; | |
32 | ||
33 | import org.eclipse.jface.dialogs.IDialogConstants; | |
34 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
35 | import org.eclipse.jface.viewers.ArrayContentProvider; | |
36 | import org.eclipse.jface.viewers.ILabelProvider; | |
37 | import org.eclipse.jface.viewers.LabelProvider; | |
38 | import org.eclipse.jface.window.Window; | |
39 | ||
40 | import org.eclipse.ui.IEditorPart; | |
41 | import org.eclipse.ui.PlatformUI; | |
42 | import org.eclipse.ui.actions.GlobalBuildAction; | |
43 | import org.eclipse.ui.dialogs.ListDialog; | |
44 | ||
45 | import org.eclipse.ltk.ui.refactoring.RefactoringWizard; | |
46 | import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation; | |
47 | ||
48 | import org.eclipse.jdt.ui.PreferenceConstants; | |
49 | ||
50 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
51 | import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; | |
52 | import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; | |
53 | import org.eclipse.jdt.internal.ui.refactoring.RefactoringSavePreferences; | |
54 | import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter; | |
55 | import org.eclipse.jdt.internal.ui.util.CoreUtility; | |
56 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
57 | ||
58 | ||
59 | /** | |
60 | * Helper to save dirty editors prior to starting a refactoring. | |
61 | * | |
62 | * @see PreferenceConstants#REFACTOR_SAVE_ALL_EDITORS | |
63 | * @since 3.5 | |
64 | * | |
65 | * @noextend This class is not intended to be subclassed by clients. | |
66 | */ | |
67 | public class RefactoringSaveHelper { | |
68 | ||
69 | private boolean fFilesSaved; | |
70 | private final int fSaveMode; | |
71 | ||
72 | /** | |
73 | * Save mode to save all dirty editors (always ask). | |
74 | */ | |
75 | public static final int SAVE_ALL_ALWAYS_ASK= 1; | |
76 | ||
77 | /** | |
78 | * Save mode to save all dirty editors. | |
79 | */ | |
80 | public static final int SAVE_ALL= 2; | |
81 | ||
82 | /** | |
83 | * Save mode to not save any editors. | |
84 | */ | |
85 | public static final int SAVE_NOTHING= 3; | |
86 | ||
87 | /** | |
88 | * Save mode to save all editors that are known to cause trouble for Java refactorings, e.g. | |
89 | * editors on compilation units that are not in working copy mode. | |
90 | */ | |
91 | public static final int SAVE_REFACTORING= 4; | |
92 | ||
93 | /** | |
94 | * Creates a refactoring save helper with the given save mode. | |
95 | * | |
96 | * @param saveMode one of the SAVE_* constants | |
97 | */ | |
98 | public RefactoringSaveHelper(int saveMode) { | |
99 | Assert.isLegal(saveMode == SAVE_ALL_ALWAYS_ASK | |
100 | || saveMode == SAVE_ALL | |
101 | || saveMode == SAVE_NOTHING | |
102 | || saveMode == SAVE_REFACTORING); | |
103 | fSaveMode= saveMode; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Saves all editors. Depending on the {@link PreferenceConstants#REFACTOR_SAVE_ALL_EDITORS} | |
108 | * preference, the user is asked to save affected dirty editors. | |
109 | * | |
110 | * @param shell the parent shell for the confirmation dialog | |
111 | * @return <code>true</code> if save was successful and refactoring can proceed; | |
112 | * false if the refactoring must be cancelled | |
113 | */ | |
114 | public boolean saveEditors(Shell shell) { | |
115 | final IEditorPart[] dirtyEditors; | |
116 | switch (fSaveMode) { | |
117 | case SAVE_ALL_ALWAYS_ASK: | |
118 | case SAVE_ALL: | |
119 | dirtyEditors= EditorUtility.getDirtyEditors(true); | |
120 | break; | |
121 | ||
122 | case SAVE_REFACTORING: | |
123 | dirtyEditors= EditorUtility.getDirtyEditorsToSave(false); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=175495 | |
124 | break; | |
125 | ||
126 | case SAVE_NOTHING: | |
127 | return true; | |
128 | ||
129 | default: | |
130 | throw new IllegalStateException(Integer.toString(fSaveMode)); | |
131 | } | |
132 | if (dirtyEditors.length == 0) | |
133 | return true; | |
134 | if (! askSaveAllDirtyEditors(shell, dirtyEditors)) | |
135 | return false; | |
136 | try { | |
137 | // Save isn't cancelable. | |
138 | boolean autoBuild= CoreUtility.setAutoBuilding(false); | |
139 | try { | |
140 | if (fSaveMode == SAVE_ALL_ALWAYS_ASK || fSaveMode == SAVE_ALL | |
141 | || RefactoringSavePreferences.getSaveAllEditors()) { | |
142 | if (!JavaPlugin.getActiveWorkbenchWindow().getWorkbench().saveAllEditors(false)) | |
143 | return false; | |
144 | } else { | |
145 | IRunnableWithProgress runnable= new IRunnableWithProgress() { | |
146 | public void run(IProgressMonitor pm) throws InterruptedException { | |
147 | int count= dirtyEditors.length; | |
148 | pm.beginTask("", count); //$NON-NLS-1$ | |
149 | for (int i= 0; i < count; i++) { | |
150 | IEditorPart editor= dirtyEditors[i]; | |
151 | editor.doSave(new SubProgressMonitor(pm, 1)); | |
152 | if (pm.isCanceled()) | |
153 | throw new InterruptedException(); | |
154 | } | |
155 | pm.done(); | |
156 | } | |
157 | }; | |
158 | try { | |
159 | PlatformUI.getWorkbench().getProgressService().runInUI(JavaPlugin.getActiveWorkbenchWindow(), runnable, null); | |
160 | } catch (InterruptedException e) { | |
161 | return false; | |
162 | } catch (InvocationTargetException e) { | |
163 | ExceptionHandler.handle(e, shell, | |
164 | RefactoringMessages.RefactoringStarter_saving, RefactoringMessages.RefactoringStarter_unexpected_exception); | |
165 | return false; | |
166 | } | |
167 | } | |
168 | fFilesSaved= true; | |
169 | } finally { | |
170 | CoreUtility.setAutoBuilding(autoBuild); | |
171 | } | |
172 | return true; | |
173 | } catch (CoreException e) { | |
174 | ExceptionHandler.handle(e, shell, | |
175 | RefactoringMessages.RefactoringStarter_saving, RefactoringMessages.RefactoringStarter_unexpected_exception); | |
176 | return false; | |
177 | } | |
178 | } | |
179 | ||
180 | /** | |
181 | * Triggers an incremental build if this save helper did save files before. | |
182 | */ | |
183 | public void triggerIncrementalBuild() { | |
184 | if (fFilesSaved && ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding()) { | |
185 | new GlobalBuildAction(JavaPlugin.getActiveWorkbenchWindow(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run(); | |
186 | } | |
187 | } | |
188 | ||
189 | /** | |
190 | * Returns whether this save helper did actually save any files. | |
191 | * | |
192 | * @return <code>true</code> iff files have been saved | |
193 | */ | |
194 | public boolean didSaveFiles() { | |
195 | return fFilesSaved; | |
196 | } | |
197 | ||
198 | private boolean askSaveAllDirtyEditors(Shell shell, IEditorPart[] dirtyEditors) { | |
199 | final boolean canSaveAutomatically= fSaveMode != SAVE_ALL_ALWAYS_ASK; | |
200 | if (canSaveAutomatically && RefactoringSavePreferences.getSaveAllEditors()) //must save everything | |
201 | return true; | |
202 | ListDialog dialog= new ListDialog(shell) { | |
203 | { | |
204 | setShellStyle(getShellStyle() | SWT.APPLICATION_MODAL); | |
205 | } | |
206 | @Override | |
207 | protected Control createDialogArea(Composite parent) { | |
208 | Composite result= (Composite) super.createDialogArea(parent); | |
209 | if (canSaveAutomatically) { | |
210 | final Button check= new Button(result, SWT.CHECK); | |
211 | check.setText(RefactoringMessages.RefactoringStarter_always_save); | |
212 | check.setSelection(RefactoringSavePreferences.getSaveAllEditors()); | |
213 | check.addSelectionListener(new SelectionAdapter() { | |
214 | @Override | |
215 | public void widgetSelected(SelectionEvent e) { | |
216 | RefactoringSavePreferences.setSaveAllEditors(check.getSelection()); | |
217 | } | |
218 | }); | |
219 | applyDialogFont(result); | |
220 | } | |
221 | return result; | |
222 | } | |
223 | }; | |
224 | dialog.setTitle(RefactoringMessages.RefactoringStarter_save_all_resources); | |
225 | dialog.setLabelProvider(createDialogLabelProvider()); | |
226 | dialog.setMessage(RefactoringMessages.RefactoringStarter_must_save); | |
227 | dialog.setContentProvider(new ArrayContentProvider()); | |
228 | dialog.setInput(Arrays.asList(dirtyEditors)); | |
229 | return dialog.open() == Window.OK; | |
230 | } | |
231 | ||
232 | private ILabelProvider createDialogLabelProvider() { | |
233 | return new LabelProvider() { | |
234 | @Override | |
235 | public Image getImage(Object element) { | |
236 | return ((IEditorPart) element).getTitleImage(); | |
237 | } | |
238 | @Override | |
239 | public String getText(Object element) { | |
240 | return ((IEditorPart) element).getTitle(); | |
241 | } | |
242 | }; | |
243 | } | |
244 | ||
245 | public boolean generated_6448363496418548998(RefactoringWizard wizard, Shell parent, String dialogTitle, RefactoringStarter refactoringstarter) { | |
246 | if (! refactoringstarter.canActivate(this, parent)) | |
247 | return false; | |
248 | ||
249 | try { | |
250 | RefactoringWizardOpenOperation op= new RefactoringWizardOpenOperation(wizard); | |
251 | int result= op.run(parent, dialogTitle); | |
252 | refactoringstarter.fStatus= op.getInitialConditionCheckingStatus(); | |
253 | if (result == IDialogConstants.CANCEL_ID || result == RefactoringWizardOpenOperation.INITIAL_CONDITION_CHECKING_FAILED) { | |
254 | triggerIncrementalBuild(); | |
255 | return false; | |
256 | } else { | |
257 | return true; | |
258 | } | |
259 | } catch (InterruptedException e) { | |
260 | return false; // User action got cancelled | |
261 | } | |
262 | } | |
263 | } |