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