KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > RefactoringSaveHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 JavaDoc;
14 import java.util.Arrays JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20
21 import org.eclipse.core.resources.IWorkspace;
22 import org.eclipse.core.resources.IWorkspaceDescription;
23 import org.eclipse.core.resources.IncrementalProjectBuilder;
24 import org.eclipse.core.resources.ResourcesPlugin;
25
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Shell;
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
45 import org.eclipse.jdt.internal.ui.JavaPlugin;
46 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
47 import org.eclipse.jdt.internal.ui.refactoring.actions.ListDialog;
48 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
49
50 public class RefactoringSaveHelper {
51
52     private boolean fFilesSaved;
53     private final int fSaveMode;
54
55     /**
56      * Save mode to save all dirty editors (always ask).
57      */

58     public static final int SAVE_ALL_ALWAYS_ASK= 1;
59     
60     /**
61      * Save mode to save all dirty editors.
62      */

63     public static final int SAVE_ALL= 2;
64     
65     /**
66      * Save mode to save all unknown editors, i.e. those that don't work on
67      * resources, don't use file buffers, or are otherwise suspect.
68      *
69      * Used for refactorings with participants or qualified name updating.
70      */

71     public static final int SAVE_NON_JAVA_UPDATES= 3;
72     
73     /**
74      * Save mode to save only dirty editors on compilation units that are not in
75      * working copy mode.
76      *
77      * Used for refactorings without participants or qualified name updating.
78      */

79     public static final int SAVE_JAVA_ONLY_UPDATES= 4;
80     
81     /**
82      * Save mode to not save save any editors.
83      */

84     public static final int SAVE_NOTHING= 5;
85     
86     /**
87      * @param saveMode one of the SAVE_* constants
88      */

89     public RefactoringSaveHelper(int saveMode) {
90         Assert.isTrue(saveMode == SAVE_ALL_ALWAYS_ASK
91                 || saveMode == SAVE_ALL
92                 || saveMode == SAVE_NON_JAVA_UPDATES
93                 || saveMode == SAVE_JAVA_ONLY_UPDATES
94                 || saveMode == SAVE_NOTHING);
95         fSaveMode= saveMode;
96     }
97     
98     /**
99      * @param shell
100      * @return <code>true</code> if save was successful and refactoring can proceed;
101      * false if the refactoring must be cancelled
102      */

103     public boolean saveEditors(Shell shell) {
104         final IEditorPart[] dirtyEditors;
105         switch (fSaveMode) {
106             case SAVE_ALL_ALWAYS_ASK:
107             case SAVE_ALL:
108                 dirtyEditors= EditorUtility.getDirtyEditors(true);
109                 break;
110                 
111             case SAVE_NON_JAVA_UPDATES:
112                 dirtyEditors= EditorUtility.getDirtyEditorsToSave(false); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=175495
113
break;
114                 
115             case SAVE_JAVA_ONLY_UPDATES:
116                 dirtyEditors= EditorUtility.getDirtyEditorsToSave(false);
117                 break;
118                 
119             case SAVE_NOTHING:
120                 return true;
121                 
122             default:
123                 throw new IllegalStateException JavaDoc(Integer.toString(fSaveMode));
124         }
125         if (dirtyEditors.length == 0)
126             return true;
127         if (! askSaveAllDirtyEditors(shell, dirtyEditors))
128             return false;
129         try {
130             // Save isn't cancelable.
131
IWorkspace workspace= ResourcesPlugin.getWorkspace();
132             IWorkspaceDescription description= workspace.getDescription();
133             boolean autoBuild= description.isAutoBuilding();
134             description.setAutoBuilding(false);
135             workspace.setDescription(description);
136             try {
137                 if (fSaveMode == SAVE_ALL_ALWAYS_ASK || fSaveMode == SAVE_ALL
138                         || RefactoringSavePreferences.getSaveAllEditors()) {
139                     if (!JavaPlugin.getActiveWorkbenchWindow().getWorkbench().saveAllEditors(false))
140                         return false;
141                 } else {
142                     IRunnableWithProgress runnable= new IRunnableWithProgress() {
143                         public void run(IProgressMonitor pm) throws InterruptedException JavaDoc {
144                             int count= dirtyEditors.length;
145                             pm.beginTask("", count); //$NON-NLS-1$
146
for (int i= 0; i < count; i++) {
147                                 IEditorPart editor= dirtyEditors[i];
148                                 editor.doSave(new SubProgressMonitor(pm, 1));
149                                 if (pm.isCanceled())
150                                     throw new InterruptedException JavaDoc();
151                             }
152                             pm.done();
153                         }
154                     };
155                     try {
156                         PlatformUI.getWorkbench().getProgressService().runInUI(JavaPlugin.getActiveWorkbenchWindow(), runnable, null);
157                     } catch (InterruptedException JavaDoc e) {
158                         return false;
159                     } catch (InvocationTargetException JavaDoc e) {
160                         ExceptionHandler.handle(e, shell,
161                                 RefactoringMessages.RefactoringStarter_saving, RefactoringMessages.RefactoringStarter_unexpected_exception);
162                         return false;
163                     }
164                 }
165                 fFilesSaved= true;
166             } finally {
167                 description.setAutoBuilding(autoBuild);
168                 workspace.setDescription(description);
169             }
170             return true;
171         } catch (CoreException e) {
172             ExceptionHandler.handle(e, shell,
173                 RefactoringMessages.RefactoringStarter_saving, RefactoringMessages.RefactoringStarter_unexpected_exception);
174             return false;
175         }
176     }
177
178     public void triggerBuild() {
179         if (fFilesSaved && ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding()) {
180             new GlobalBuildAction(JavaPlugin.getActiveWorkbenchWindow(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run();
181         }
182     }
183     
184     private boolean askSaveAllDirtyEditors(Shell shell, IEditorPart[] dirtyEditors) {
185         final boolean canSaveAutomatically= fSaveMode != SAVE_ALL_ALWAYS_ASK;
186         if (canSaveAutomatically && RefactoringSavePreferences.getSaveAllEditors()) //must save everything
187
return true;
188         ListDialog dialog= new ListDialog(shell) {
189             {
190                 setShellStyle(getShellStyle() | SWT.APPLICATION_MODAL);
191             }
192             protected Control createDialogArea(Composite parent) {
193                 Composite result= (Composite) super.createDialogArea(parent);
194                 if (canSaveAutomatically) {
195                     final Button check= new Button(result, SWT.CHECK);
196                     check.setText(RefactoringMessages.RefactoringStarter_always_save);
197                     check.setSelection(RefactoringSavePreferences.getSaveAllEditors());
198                     check.addSelectionListener(new SelectionAdapter() {
199                         public void widgetSelected(SelectionEvent e) {
200                             RefactoringSavePreferences.setSaveAllEditors(check.getSelection());
201                         }
202                     });
203                     applyDialogFont(result);
204                 }
205                 return result;
206             }
207         };
208         dialog.setTitle(RefactoringMessages.RefactoringStarter_save_all_resources);
209         dialog.setAddCancelButton(true);
210         dialog.setLabelProvider(createDialogLabelProvider());
211         dialog.setMessage(RefactoringMessages.RefactoringStarter_must_save);
212         dialog.setContentProvider(new ArrayContentProvider());
213         dialog.setInput(Arrays.asList(dirtyEditors));
214         return dialog.open() == Window.OK;
215     }
216     
217     public boolean hasFilesSaved() {
218         return fFilesSaved;
219     }
220     
221     private ILabelProvider createDialogLabelProvider() {
222         return new LabelProvider() {
223             public Image getImage(Object JavaDoc element) {
224                 return ((IEditorPart) element).getTitleImage();
225             }
226             public String JavaDoc getText(Object JavaDoc element) {
227                 return ((IEditorPart) element).getTitle();
228             }
229         };
230     }
231 }
232
Popular Tags