KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > SaveScopeResourcesHandler


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.debug.internal.ui.launchConfigurations;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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 /**
50  * Status handler to prompt for saving of resources prior to launching.
51  * <p>
52  * This class provides a behavior breaking function from 3.1. We now perform pre-launch saving for resources
53  * scoped to the affected projects of the launch instead of all unsaved resources from within the current workspace.
54  * </p>
55  * <p>
56  * The 'breaking' occurs as the saving is moved from <code>DebugUIPlugin</code> to the launch configuration delegate, which will require
57  * implementors of <code>LaunchConfigurationDelegate</code> to incorporate the use of this status handler to perform any pre-launch saving.
58  * </p>
59  * @since 3.2
60  */

61 public class SaveScopeResourcesHandler implements IStatusHandler {
62
63     /**
64      * Provides a custom class for a resizable selection dialog with a don't ask again button on it
65      * @since 3.2
66      */

67     class ScopedResourcesSelectionDialog extends ListSelectionDialog {
68
69         private final String JavaDoc SETTINGS_ID = IDebugUIConstants.PLUGIN_ID + ".SCOPED_SAVE_SELECTION_DIALOG"; //$NON-NLS-1$
70
Button fSavePref;
71         
72         public ScopedResourcesSelectionDialog(Shell parentShell, Object JavaDoc input, IStructuredContentProvider contentProvider, ILabelProvider labelProvider, String JavaDoc 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 JavaDoc 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     /**
103      * The objects to save (if any)
104      */

105     Object JavaDoc[] fSaves = null;
106     
107     /* (non-Javadoc)
108      *
109      * Source object is an array - a launch configuration and an array of projects to save resources for.
110      *
111      * @see org.eclipse.debug.core.IStatusHandler#handleStatus(org.eclipse.core.runtime.IStatus, java.lang.Object)
112      */

113     public Object JavaDoc handleStatus(IStatus status, Object JavaDoc source) throws CoreException {
114         // retrieve config and projects
115
ILaunchConfiguration config = null;
116         IProject[] projects = null;
117         if (source instanceof Object JavaDoc[]) {
118             Object JavaDoc[] objects = (Object JavaDoc[]) 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 JavaDoc 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     /**
146      *
147      * Builds the list of editors that apply to this build that need to be saved
148      *
149      * @param projects the projects involved in this build, used to scope the searching process
150      * @return the list of dirty editors for this launch to save, never null
151      */

152     protected IEditorPart[] getScopedDirtyEditors(IProject[] projects) {
153         List JavaDoc dirtyparts = new ArrayList JavaDoc();
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     /**
175      * Performs the save of the editor parts returned by getScopedResources
176      */

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     /**
186      * show the save dialog with a list of editors to save (if any)
187      * The dialog is also not shown if the the preference for automatically saving dirty before launch is set to always
188      * @param projects the projects to consider for the save
189      * @param save if we should save
190      * @param prompt if we should prompt to save or do it automatically
191      * @return the dialog status, to be propagated back to the <code>handleStatus</code> method
192      */

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