]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/internal compatibility/org/eclipse/jdt/internal/ui/refactoring/RefactoringSaveHelper.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / internal compatibility / org / eclipse / jdt / internal / ui / refactoring / RefactoringSaveHelper.java
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.internal.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.IWorkspace;
31 import org.eclipse.core.resources.IWorkspaceDescription;
32 import org.eclipse.core.resources.IncrementalProjectBuilder;
33 import org.eclipse.core.resources.ResourcesPlugin;
34
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;
40
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;
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.util.ExceptionHandler;
49
50
51 /**
52  * DO NOT REMOVE, used in a product.
53  * @deprecated As of 3.5, replaced by {@link org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper}
54  */
55 public class RefactoringSaveHelper {
56
57         private boolean fFilesSaved;
58         private final int fSaveMode;
59
60         /**
61          * Save mode to save all dirty editors (always ask).
62          */
63         public static final int SAVE_ALL_ALWAYS_ASK= 1;
64
65         /**
66          * Save mode to save all dirty editors.
67          */
68         public static final int SAVE_ALL= 2;
69
70         /**
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.
73          *
74          * Used for refactorings with participants or qualified name updating.
75          */
76         public static final int SAVE_NON_JAVA_UPDATES= 3;
77
78         /**
79          * Save mode to save only dirty editors on compilation units that are not in
80          * working copy mode.
81          *
82          * Used for refactorings without participants or qualified name updating.
83          */
84         public static final int SAVE_JAVA_ONLY_UPDATES= 4;
85
86         /**
87          * Save mode to not save save any editors.
88          */
89         public static final int SAVE_NOTHING= 5;
90
91         /**
92          * @param saveMode one of the SAVE_* constants
93          */
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);
100                 fSaveMode= saveMode;
101         }
102
103         /**
104          * @param shell
105          * @return <code>true</code> if save was successful and refactoring can proceed;
106          *              false if the refactoring must be cancelled
107          */
108         public boolean saveEditors(Shell shell) {
109                 final IEditorPart[] dirtyEditors;
110                 switch (fSaveMode) {
111                         case SAVE_ALL_ALWAYS_ASK:
112                         case SAVE_ALL:
113                                 dirtyEditors= EditorUtility.getDirtyEditors(true);
114                                 break;
115
116                         case SAVE_NON_JAVA_UPDATES:
117                                 dirtyEditors= EditorUtility.getDirtyEditorsToSave(false); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=175495
118                                 break;
119
120                         case SAVE_JAVA_ONLY_UPDATES:
121                                 dirtyEditors= EditorUtility.getDirtyEditorsToSave(false);
122                                 break;
123
124                         case SAVE_NOTHING:
125                                 return true;
126
127                         default:
128                                 throw new IllegalStateException(Integer.toString(fSaveMode));
129                 }
130                 if (dirtyEditors.length == 0)
131                         return true;
132                 if (! askSaveAllDirtyEditors(shell, dirtyEditors))
133                         return false;
134                 try {
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);
141                         try {
142                                 if (fSaveMode == SAVE_ALL_ALWAYS_ASK || fSaveMode == SAVE_ALL
143                                                 || RefactoringSavePreferences.getSaveAllEditors()) {
144                                         if (!JavaPlugin.getActiveWorkbenchWindow().getWorkbench().saveAllEditors(false))
145                                                 return false;
146                                 } else {
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));
154                                                                 if (pm.isCanceled())
155                                                                         throw new InterruptedException();
156                                                         }
157                                                         pm.done();
158                                                 }
159                                         };
160                                         try {
161                                                 PlatformUI.getWorkbench().getProgressService().runInUI(JavaPlugin.getActiveWorkbenchWindow(), runnable, null);
162                                         } catch (InterruptedException e) {
163                                                 return false;
164                                         } catch (InvocationTargetException e) {
165                                                 ExceptionHandler.handle(e, shell,
166                                                                 RefactoringMessages.RefactoringStarter_saving, RefactoringMessages.RefactoringStarter_unexpected_exception);
167                                                 return false;
168                                         }
169                                 }
170                                 fFilesSaved= true;
171                         } finally {
172                                 description.setAutoBuilding(autoBuild);
173                                 workspace.setDescription(description);
174                         }
175                         return true;
176                 } catch (CoreException e) {
177                         ExceptionHandler.handle(e, shell,
178                                 RefactoringMessages.RefactoringStarter_saving, RefactoringMessages.RefactoringStarter_unexpected_exception);
179                         return false;
180                 }
181         }
182
183         public void triggerBuild() {
184                 if (fFilesSaved && ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding()) {
185                         new GlobalBuildAction(JavaPlugin.getActiveWorkbenchWindow(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run();
186                 }
187         }
188
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
192                         return true;
193                 ListDialog dialog= new ListDialog(shell) {
194                         {
195                                 setShellStyle(getShellStyle() | SWT.APPLICATION_MODAL);
196                         }
197                         @Override
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() {
205                                                 @Override
206                                                 public void widgetSelected(SelectionEvent e) {
207                                                         RefactoringSavePreferences.setSaveAllEditors(check.getSelection());
208                                                 }
209                                         });
210                                         applyDialogFont(result);
211                                 }
212                                 return result;
213                         }
214                 };
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;
222         }
223
224         public boolean hasFilesSaved() {
225                 return fFilesSaved;
226         }
227
228         private ILabelProvider createDialogLabelProvider() {
229                 return new LabelProvider() {
230                         @Override
231                         public Image getImage(Object element) {
232                                 return ((IEditorPart) element).getTitleImage();
233                         }
234                         @Override
235                         public String getText(Object element) {
236                                 return ((IEditorPart) element).getTitle();
237                         }
238                 };
239         }
240 }