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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.Arrays;
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;
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;
30 import org.eclipse.core.resources.IWorkspace;
31 import org.eclipse.core.resources.IWorkspaceDescription;
32 import org.eclipse.core.resources.IncrementalProjectBuilder;
33 import org.eclipse.core.resources.ResourcesPlugin;
35 import org.eclipse.jface.operation.IRunnableWithProgress;
36 import org.eclipse.jface.viewers.ArrayContentProvider;
37 import org.eclipse.jface.viewers.ILabelProvider;
38 import org.eclipse.jface.viewers.LabelProvider;
39 import org.eclipse.jface.window.Window;
41 import org.eclipse.ui.IEditorPart;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.actions.GlobalBuildAction;
44 import org.eclipse.ui.dialogs.ListDialog;
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
48 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
52 * DO NOT REMOVE, used in a product.
53 * @deprecated As of 3.5, replaced by {@link org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper}
55 public class RefactoringSaveHelper {
57 private boolean fFilesSaved;
58 private final int fSaveMode;
61 * Save mode to save all dirty editors (always ask).
63 public static final int SAVE_ALL_ALWAYS_ASK= 1;
66 * Save mode to save all dirty editors.
68 public static final int SAVE_ALL= 2;
71 * Save mode to save all unknown editors, i.e. those that don't work on
72 * resources, don't use file buffers, or are otherwise suspect.
74 * Used for refactorings with participants or qualified name updating.
76 public static final int SAVE_NON_JAVA_UPDATES= 3;
79 * Save mode to save only dirty editors on compilation units that are not in
82 * Used for refactorings without participants or qualified name updating.
84 public static final int SAVE_JAVA_ONLY_UPDATES= 4;
87 * Save mode to not save save any editors.
89 public static final int SAVE_NOTHING= 5;
92 * @param saveMode one of the SAVE_* constants
94 public RefactoringSaveHelper(int saveMode) {
95 Assert.isTrue(saveMode == SAVE_ALL_ALWAYS_ASK
96 || saveMode == SAVE_ALL
97 || saveMode == SAVE_NON_JAVA_UPDATES
98 || saveMode == SAVE_JAVA_ONLY_UPDATES
99 || saveMode == SAVE_NOTHING);
105 * @return <code>true</code> if save was successful and refactoring can proceed;
106 * false if the refactoring must be cancelled
108 public boolean saveEditors(Shell shell) {
109 final IEditorPart[] dirtyEditors;
111 case SAVE_ALL_ALWAYS_ASK:
113 dirtyEditors= EditorUtility.getDirtyEditors(true);
116 case SAVE_NON_JAVA_UPDATES:
117 dirtyEditors= EditorUtility.getDirtyEditorsToSave(false); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=175495
120 case SAVE_JAVA_ONLY_UPDATES:
121 dirtyEditors= EditorUtility.getDirtyEditorsToSave(false);
128 throw new IllegalStateException(Integer.toString(fSaveMode));
130 if (dirtyEditors.length == 0)
132 if (! askSaveAllDirtyEditors(shell, dirtyEditors))
135 // Save isn't cancelable.
136 IWorkspace workspace= ResourcesPlugin.getWorkspace();
137 IWorkspaceDescription description= workspace.getDescription();
138 boolean autoBuild= description.isAutoBuilding();
139 description.setAutoBuilding(false);
140 workspace.setDescription(description);
142 if (fSaveMode == SAVE_ALL_ALWAYS_ASK || fSaveMode == SAVE_ALL
143 || RefactoringSavePreferences.getSaveAllEditors()) {
144 if (!JavaPlugin.getActiveWorkbenchWindow().getWorkbench().saveAllEditors(false))
147 IRunnableWithProgress runnable= new IRunnableWithProgress() {
148 public void run(IProgressMonitor pm) throws InterruptedException {
149 int count= dirtyEditors.length;
150 pm.beginTask("", count); //$NON-NLS-1$
151 for (int i= 0; i < count; i++) {
152 IEditorPart editor= dirtyEditors[i];
153 editor.doSave(new SubProgressMonitor(pm, 1));
155 throw new InterruptedException();
161 PlatformUI.getWorkbench().getProgressService().runInUI(JavaPlugin.getActiveWorkbenchWindow(), runnable, null);
162 } catch (InterruptedException e) {
164 } catch (InvocationTargetException e) {
165 ExceptionHandler.handle(e, shell,
166 RefactoringMessages.RefactoringStarter_saving, RefactoringMessages.RefactoringStarter_unexpected_exception);
172 description.setAutoBuilding(autoBuild);
173 workspace.setDescription(description);
176 } catch (CoreException e) {
177 ExceptionHandler.handle(e, shell,
178 RefactoringMessages.RefactoringStarter_saving, RefactoringMessages.RefactoringStarter_unexpected_exception);
183 public void triggerBuild() {
184 if (fFilesSaved && ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding()) {
185 new GlobalBuildAction(JavaPlugin.getActiveWorkbenchWindow(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run();
189 private boolean askSaveAllDirtyEditors(Shell shell, IEditorPart[] dirtyEditors) {
190 final boolean canSaveAutomatically= fSaveMode != SAVE_ALL_ALWAYS_ASK;
191 if (canSaveAutomatically && RefactoringSavePreferences.getSaveAllEditors()) //must save everything
193 ListDialog dialog= new ListDialog(shell) {
195 setShellStyle(getShellStyle() | SWT.APPLICATION_MODAL);
198 protected Control createDialogArea(Composite parent) {
199 Composite result= (Composite) super.createDialogArea(parent);
200 if (canSaveAutomatically) {
201 final Button check= new Button(result, SWT.CHECK);
202 check.setText(RefactoringMessages.RefactoringStarter_always_save);
203 check.setSelection(RefactoringSavePreferences.getSaveAllEditors());
204 check.addSelectionListener(new SelectionAdapter() {
206 public void widgetSelected(SelectionEvent e) {
207 RefactoringSavePreferences.setSaveAllEditors(check.getSelection());
210 applyDialogFont(result);
215 dialog.setTitle(RefactoringMessages.RefactoringStarter_save_all_resources);
216 dialog.setAddCancelButton(true);
217 dialog.setLabelProvider(createDialogLabelProvider());
218 dialog.setMessage(RefactoringMessages.RefactoringStarter_must_save);
219 dialog.setContentProvider(new ArrayContentProvider());
220 dialog.setInput(Arrays.asList(dirtyEditors));
221 return dialog.open() == Window.OK;
224 public boolean hasFilesSaved() {
228 private ILabelProvider createDialogLabelProvider() {
229 return new LabelProvider() {
231 public Image getImage(Object element) {
232 return ((IEditorPart) element).getTitleImage();
235 public String getText(Object element) {
236 return ((IEditorPart) element).getTitle();