1 11 12 package org.eclipse.debug.internal.ui.launchConfigurations; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.NullProgressMonitor; 22 import org.eclipse.debug.core.ILaunchConfiguration; 23 import org.eclipse.debug.core.IStatusHandler; 24 import org.eclipse.debug.internal.ui.DebugUIPlugin; 25 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 26 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 27 import org.eclipse.debug.ui.DebugUITools; 28 import org.eclipse.debug.ui.IDebugUIConstants; 29 import org.eclipse.jface.dialogs.IDialogConstants; 30 import org.eclipse.jface.dialogs.IDialogSettings; 31 import org.eclipse.jface.dialogs.MessageDialogWithToggle; 32 import org.eclipse.jface.preference.IPreferenceStore; 33 import org.eclipse.jface.viewers.ILabelProvider; 34 import org.eclipse.jface.viewers.IStructuredContentProvider; 35 import org.eclipse.swt.SWT; 36 import org.eclipse.swt.widgets.Button; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Control; 39 import org.eclipse.swt.widgets.Shell; 40 import org.eclipse.ui.IEditorPart; 41 import org.eclipse.ui.IWorkbenchPage; 42 import org.eclipse.ui.IWorkbenchWindow; 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.dialogs.ListSelectionDialog; 45 import org.eclipse.ui.model.AdaptableList; 46 import org.eclipse.ui.model.WorkbenchContentProvider; 47 import org.eclipse.ui.model.WorkbenchPartLabelProvider; 48 49 61 public class SaveScopeResourcesHandler implements IStatusHandler { 62 63 67 class ScopedResourcesSelectionDialog extends ListSelectionDialog { 68 69 private final String SETTINGS_ID = IDebugUIConstants.PLUGIN_ID + ".SCOPED_SAVE_SELECTION_DIALOG"; Button fSavePref; 71 72 public ScopedResourcesSelectionDialog(Shell parentShell, Object input, IStructuredContentProvider contentProvider, ILabelProvider labelProvider, String message) { 73 super(parentShell, input, contentProvider, labelProvider, message); 74 setShellStyle(getShellStyle() | SWT.RESIZE); 75 } 76 77 protected Control createDialogArea(Composite parent) { 78 Composite ctrl = (Composite) super.createDialogArea(parent); 79 fSavePref = new Button(ctrl, SWT.CHECK); 80 fSavePref.setText(LaunchConfigurationsMessages.SaveScopeResourcesHandler_1); 81 PlatformUI.getWorkbench().getHelpSystem().setHelp(ctrl, IDebugHelpContextIds.SELECT_RESOURCES_TO_SAVE_DIALOG); 82 return ctrl; 83 } 84 85 protected void okPressed() { 86 IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore(); 87 String val = (fSavePref.getSelection() ? MessageDialogWithToggle.ALWAYS : MessageDialogWithToggle.PROMPT); 88 store.setValue(IInternalDebugUIConstants.PREF_SAVE_DIRTY_EDITORS_BEFORE_LAUNCH, val); 89 super.okPressed(); 90 } 91 92 protected IDialogSettings getDialogBoundsSettings() { 93 IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings(); 94 IDialogSettings section = settings.getSection(SETTINGS_ID); 95 if (section == null) { 96 section = settings.addNewSection(SETTINGS_ID); 97 } 98 return section; 99 } 100 } 101 102 105 Object [] fSaves = null; 106 107 113 public Object handleStatus(IStatus status, Object source) throws CoreException { 114 ILaunchConfiguration config = null; 116 IProject[] projects = null; 117 if (source instanceof Object []) { 118 Object [] objects = (Object []) source; 119 if (objects.length == 2) { 120 config = (ILaunchConfiguration) objects[0]; 121 projects = (IProject[]) objects[1]; 122 } 123 } 124 if (config != null) { 125 if (DebugUITools.isPrivate(config)) { 126 return Boolean.TRUE; 127 } 128 } 129 if (projects != null) { 130 IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore(); 131 String save = store.getString(IInternalDebugUIConstants.PREF_SAVE_DIRTY_EDITORS_BEFORE_LAUNCH); 132 int ret = showSaveDialog(projects, !save.equals(MessageDialogWithToggle.NEVER), save.equals(MessageDialogWithToggle.PROMPT)); 133 if(ret == IDialogConstants.OK_ID) { 134 doSave(); 135 return Boolean.TRUE; 136 } 137 return Boolean.FALSE; 138 } 139 else { 140 boolean cancel = DebugUIPlugin.preLaunchSave(); 141 return Boolean.valueOf(cancel); 142 } 143 } 144 145 152 protected IEditorPart[] getScopedDirtyEditors(IProject[] projects) { 153 List dirtyparts = new ArrayList (); 154 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); 155 for(int l = 0; l < windows.length; l++) { 156 IWorkbenchPage[] pages = windows[l].getPages(); 157 for(int i = 0; i < pages.length; i++) { 158 IEditorPart[] eparts = pages[i].getDirtyEditors(); 159 for(int j = 0; j < eparts.length; j++) { 160 IResource resource = (IResource)eparts[j].getEditorInput().getAdapter(IResource.class); 161 if(resource != null) { 162 for(int k = 0; k < projects.length; k++) { 163 if(projects[k].equals(resource.getProject()) & !dirtyparts.contains(eparts[j])) { 164 dirtyparts.add(eparts[j]); 165 } 166 } 167 } 168 } 169 } 170 } 171 return (IEditorPart[])dirtyparts.toArray(new IEditorPart[dirtyparts.size()]); 172 } 173 174 177 protected void doSave() { 178 if(fSaves != null) { 179 for (int i = 0; i < fSaves.length; i++) { 180 ((IEditorPart)fSaves[i]).doSave(new NullProgressMonitor()); 181 } 182 } 183 } 184 185 193 protected int showSaveDialog(IProject[] projects, boolean save, boolean prompt) { 194 if(save) { 195 IEditorPart[] editors = getScopedDirtyEditors(projects); 196 if(prompt && (editors.length > 0)) { 197 ScopedResourcesSelectionDialog lsd = new ScopedResourcesSelectionDialog(DebugUIPlugin.getShell(), 198 new AdaptableList(editors), 199 new WorkbenchContentProvider(), 200 new WorkbenchPartLabelProvider(), 201 LaunchConfigurationsMessages.SaveScopeResourcesHandler_2); 202 lsd.setInitialSelections(editors); 203 lsd.setTitle(LaunchConfigurationsMessages.SaveScopeResourcesHandler_3); 204 if(lsd.open() == IDialogConstants.CANCEL_ID) { 205 return IDialogConstants.CANCEL_ID; 206 } 207 fSaves = lsd.getResult(); 208 } 209 else { 210 fSaves = editors; 211 } 212 } 213 return IDialogConstants.OK_ID; 214 } 215 } 216 | Popular Tags |