1 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.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 58 public static final int SAVE_ALL_ALWAYS_ASK= 1; 59 60 63 public static final int SAVE_ALL= 2; 64 65 71 public static final int SAVE_NON_JAVA_UPDATES= 3; 72 73 79 public static final int SAVE_JAVA_ONLY_UPDATES= 4; 80 81 84 public static final int SAVE_NOTHING= 5; 85 86 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 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); 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 (Integer.toString(fSaveMode)); 124 } 125 if (dirtyEditors.length == 0) 126 return true; 127 if (! askSaveAllDirtyEditors(shell, dirtyEditors)) 128 return false; 129 try { 130 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 { 144 int count= dirtyEditors.length; 145 pm.beginTask("", count); 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 (); 151 } 152 pm.done(); 153 } 154 }; 155 try { 156 PlatformUI.getWorkbench().getProgressService().runInUI(JavaPlugin.getActiveWorkbenchWindow(), runnable, null); 157 } catch (InterruptedException e) { 158 return false; 159 } catch (InvocationTargetException 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()) 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 element) { 224 return ((IEditorPart) element).getTitleImage(); 225 } 226 public String getText(Object element) { 227 return ((IEditorPart) element).getTitle(); 228 } 229 }; 230 } 231 } 232 | Popular Tags |