KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > CleanDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.dialogs;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IncrementalProjectBuilder;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.resources.WorkspaceJob;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22 import org.eclipse.jface.dialogs.IDialogConstants;
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.CheckStateChangedEvent;
26 import org.eclipse.jface.viewers.CheckboxTableViewer;
27 import org.eclipse.jface.viewers.ICheckStateListener;
28 import org.eclipse.jface.viewers.Viewer;
29 import org.eclipse.jface.viewers.ViewerFilter;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionListener;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.actions.GlobalBuildAction;
43 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
44 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
45 import org.eclipse.ui.internal.ide.actions.BuildUtilities;
46 import org.eclipse.ui.model.WorkbenchContentProvider;
47 import org.eclipse.ui.model.WorkbenchLabelProvider;
48
49 /**
50  * Dialog that asks the user to confirm a clean operation, and to configure
51  * settings in relation to the clean. Clicking ok in the dialog will perform the
52  * clean operation.
53  *
54  * @since 3.0
55  */

56 public class CleanDialog extends MessageDialog {
57     
58     private static final String JavaDoc DIALOG_SETTINGS_SECTION = "CleanDialogSettings"; //$NON-NLS-1$
59
private static final String JavaDoc DIALOG_ORIGIN_X = "DIALOG_X_ORIGIN"; //$NON-NLS-1$
60
private static final String JavaDoc DIALOG_ORIGIN_Y = "DIALOG_Y_ORIGIN"; //$NON-NLS-1$
61
private static final String JavaDoc DIALOG_WIDTH = "DIALOG_WIDTH"; //$NON-NLS-1$
62
private static final String JavaDoc DIALOG_HEIGHT = "DIALOG_HEIGHT"; //$NON-NLS-1$
63
private static final String JavaDoc TOGGLE_SELECTED = "TOGGLE_SELECTED"; //$NON-NLS-1$
64
private static final String JavaDoc BUILD_NOW = "BUILD_NOW"; //$NON-NLS-1$
65

66     private Button allButton, selectedButton, buildNowButton;
67
68     private CheckboxTableViewer projectNames;
69
70     private Object JavaDoc[] selection;
71
72     private IWorkbenchWindow window;
73
74     /**
75      * Gets the text of the clean dialog, depending on whether the
76      * workspace is currently in autobuild mode.
77      * @return String the question the user will be asked.
78      */

79     private static String JavaDoc getQuestion() {
80         boolean autoBuilding = ResourcesPlugin.getWorkspace().isAutoBuilding();
81         if (autoBuilding) {
82             return IDEWorkbenchMessages.CleanDialog_buildCleanAuto;
83         }
84         return IDEWorkbenchMessages.CleanDialog_buildCleanManual;
85     }
86
87     /**
88      * Creates a new clean dialog.
89      *
90      * @param window the window to create it in
91      * @param selection the currently selected projects (may be empty)
92      */

93     public CleanDialog(IWorkbenchWindow window, IProject[] selection) {
94         super(
95                 window.getShell(),
96                 IDEWorkbenchMessages.CleanDialog_title, null, getQuestion(), QUESTION, new String JavaDoc[] {
97                 IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL }, 0);
98         this.window = window;
99         this.selection = selection;
100         if (this.selection == null) {
101             this.selection = new Object JavaDoc[0];
102         }
103         setShellStyle(SWT.RESIZE | getShellStyle());
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
110      */

111     protected void buttonPressed(int buttonId) {
112         final boolean cleanAll = allButton.getSelection();
113         final boolean buildAll = buildNowButton != null
114                 && buildNowButton.getSelection();
115         super.buttonPressed(buttonId);
116         if (buttonId != IDialogConstants.OK_ID) {
117             return;
118         }
119         //save all dirty editors
120
BuildUtilities.saveEditors(null);
121         //batching changes ensures that autobuild runs after cleaning
122
WorkspaceJob cleanJob = new WorkspaceJob(IDEWorkbenchMessages.CleanDialog_taskName) {
123             public boolean belongsTo(Object JavaDoc family) {
124                 return ResourcesPlugin.FAMILY_MANUAL_BUILD.equals(family);
125             }
126             public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
127                 doClean(cleanAll, monitor);
128                 //see if a build was requested
129
if (buildAll) {
130                     //start an immediate workspace build
131
GlobalBuildAction build = new GlobalBuildAction(window,
132                             IncrementalProjectBuilder.INCREMENTAL_BUILD);
133                     build.doBuild();
134                 }
135                 return Status.OK_STATUS;
136             }
137         };
138         cleanJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory()
139                 .buildRule());
140         cleanJob.setUser(true);
141         cleanJob.schedule();
142     }
143
144     /* (non-Javadoc)
145      * @see org.eclipse.jface.dialogs.MessageDialog#createCustomArea(org.eclipse.swt.widgets.Composite)
146      */

147     protected Control createCustomArea(Composite parent) {
148         Composite area = new Composite(parent, SWT.NONE);
149         GridLayout layout = new GridLayout();
150         layout.marginWidth = layout.marginHeight = 0;
151         layout.numColumns = 2;
152         layout.makeColumnsEqualWidth = true;
153         area.setLayout(layout);
154         area.setLayoutData(new GridData(GridData.FILL_BOTH));
155         SelectionListener updateEnablement = new SelectionAdapter() {
156             public void widgetSelected(SelectionEvent e) {
157                 updateEnablement();
158             }
159         };
160
161         IDialogSettings settings = getDialogSettings(DIALOG_SETTINGS_SECTION);
162         boolean selectSelectedButton= settings.getBoolean(TOGGLE_SELECTED);
163         //first row
164
allButton = new Button(area, SWT.RADIO);
165         allButton.setText(IDEWorkbenchMessages.CleanDialog_cleanAllButton);
166         allButton.setSelection(!selectSelectedButton);
167         allButton.addSelectionListener(updateEnablement);
168         selectedButton = new Button(area, SWT.RADIO);
169         selectedButton.setText(IDEWorkbenchMessages.CleanDialog_cleanSelectedButton);
170         selectedButton.setSelection(selectSelectedButton);
171         selectedButton.addSelectionListener(updateEnablement);
172
173         //second row
174
createProjectSelectionTable(area);
175         
176         //third row
177
//only prompt for immediate build if autobuild is off
178
if (!ResourcesPlugin.getWorkspace().isAutoBuilding()) {
179             buildNowButton = new Button(parent, SWT.CHECK);
180             buildNowButton.setText(IDEWorkbenchMessages.CleanDialog_buildNowButton);
181             String JavaDoc buildNow = settings.get(BUILD_NOW);
182             buildNowButton.setSelection(buildNow == null || Boolean.valueOf(buildNow).booleanValue());
183             buildNowButton.setLayoutData(new GridData(
184                     GridData.HORIZONTAL_ALIGN_BEGINNING));
185         }
186         projectNames.getTable().setEnabled(selectSelectedButton);
187         return area;
188     }
189
190     private void createProjectSelectionTable(Composite radioGroup) {
191         projectNames = CheckboxTableViewer.newCheckList(radioGroup, SWT.BORDER);
192         projectNames.setContentProvider(new WorkbenchContentProvider());
193         projectNames.setLabelProvider(new WorkbenchLabelProvider());
194         projectNames.setComparator(new ResourceComparator(ResourceComparator.NAME));
195         projectNames.addFilter(new ViewerFilter() {
196             private final IProject[] projectHolder = new IProject[1];
197             public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
198                 if (!(element instanceof IProject)) {
199                     return false;
200                 }
201                 IProject project = (IProject) element;
202                 if (!project.isAccessible()) {
203                     return false;
204                 }
205                 projectHolder[0] = project;
206                 return BuildUtilities.isEnabled(projectHolder, IncrementalProjectBuilder.CLEAN_BUILD);
207             }
208         });
209         projectNames.setInput(ResourcesPlugin.getWorkspace().getRoot());
210         GridData data = new GridData(GridData.FILL_BOTH);
211         data.horizontalSpan = 2;
212         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
213         data.heightHint = IDialogConstants.ENTRY_FIELD_WIDTH;
214         projectNames.getTable().setLayoutData(data);
215         projectNames.setCheckedElements(selection);
216         //table is disabled to start because all button is selected
217
projectNames.getTable().setEnabled(selectedButton.getSelection());
218         projectNames.addCheckStateListener(new ICheckStateListener() {
219             public void checkStateChanged(CheckStateChangedEvent event) {
220                 selection = projectNames.getCheckedElements();
221                 updateEnablement();
222             }
223         });
224     }
225
226     /**
227      * Performs the actual clean operation.
228      * @param cleanAll if <code>true</true> clean all projects
229      * @param monitor The monitor that the build will report to
230      * @throws CoreException thrown if there is a problem from the
231      * core builder.
232      */

233     protected void doClean(boolean cleanAll, IProgressMonitor monitor)
234             throws CoreException {
235         if (cleanAll) {
236             ResourcesPlugin.getWorkspace().build(
237                     IncrementalProjectBuilder.CLEAN_BUILD, monitor);
238         } else {
239             try {
240                 monitor.beginTask(IDEWorkbenchMessages.CleanDialog_taskName, selection.length);
241                 for (int i = 0; i < selection.length; i++) {
242                     ((IProject) selection[i]).build(
243                             IncrementalProjectBuilder.CLEAN_BUILD,
244                             new SubProgressMonitor(monitor, 1));
245                 }
246             } finally {
247                 monitor.done();
248             }
249         }
250     }
251
252     /**
253      * Updates the enablement of the dialog's ok button based
254      * on the current choices in the dialog.
255      */

256     protected void updateEnablement() {
257         projectNames.getTable().setEnabled(selectedButton.getSelection());
258         boolean enabled = allButton.getSelection() || selection.length > 0;
259         getButton(OK).setEnabled(enabled);
260     }
261     
262     /* (non-Javadoc)
263      * @see org.eclipse.jface.window.Window#close()
264      */

265     public boolean close() {
266         persistDialogSettings(getShell(), DIALOG_SETTINGS_SECTION);
267         return super.close();
268     }
269     
270     /* (non-Javadoc)
271      * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
272      */

273     protected Point getInitialLocation(Point initialSize) {
274         Point p = getInitialLocation(DIALOG_SETTINGS_SECTION);
275         return p != null ? p : super.getInitialLocation(initialSize);
276     }
277     
278     /* (non-Javadoc)
279      * @see org.eclipse.jface.window.Window#getInitialSize()
280      */

281     protected Point getInitialSize() {
282         Point p = super.getInitialSize();
283         return getInitialSize(DIALOG_SETTINGS_SECTION, p);
284     }
285     
286     /**
287      * Returns the initial location which is persisted in the Ant UI Plugin dialog settings
288      * under the provided dialog setttings section name.
289      * If location is not persisted in the settings, the <code>null</code> is returned.
290      *
291      * @param dialogSettingsSectionName The name of the dialog settings section
292      * @return The initial location or <code>null</code>
293      */

294     public Point getInitialLocation(String JavaDoc dialogSettingsSectionName) {
295         IDialogSettings settings = getDialogSettings(dialogSettingsSectionName);
296         try {
297             int x= settings.getInt(DIALOG_ORIGIN_X);
298             int y= settings.getInt(DIALOG_ORIGIN_Y);
299             return new Point(x,y);
300         } catch (NumberFormatException JavaDoc e) {
301         }
302         return null;
303     }
304     
305     private IDialogSettings getDialogSettings(String JavaDoc dialogSettingsSectionName) {
306         IDialogSettings settings = IDEWorkbenchPlugin.getDefault().getDialogSettings();
307         IDialogSettings section = settings.getSection(dialogSettingsSectionName);
308         if (section == null) {
309             section = settings.addNewSection(dialogSettingsSectionName);
310         }
311         return section;
312     }
313     
314     /**
315      * Persists the location and dimensions of the shell and other user settings in the
316      * plugin's dialog settings under the provided dialog settings section name
317      *
318      * @param shell The shell whose geometry is to be stored
319      * @param dialogSettingsSectionName The name of the dialog settings section
320      */

321     private void persistDialogSettings(Shell shell, String JavaDoc dialogSettingsSectionName) {
322         Point shellLocation = shell.getLocation();
323         Point shellSize = shell.getSize();
324         IDialogSettings settings = getDialogSettings(dialogSettingsSectionName);
325         settings.put(DIALOG_ORIGIN_X, shellLocation.x);
326         settings.put(DIALOG_ORIGIN_Y, shellLocation.y);
327         settings.put(DIALOG_WIDTH, shellSize.x);
328         settings.put(DIALOG_HEIGHT, shellSize.y);
329         
330         if (buildNowButton != null) {
331             settings.put(BUILD_NOW, buildNowButton.getSelection());
332         }
333         settings.put(TOGGLE_SELECTED, selectedButton.getSelection());
334     }
335     
336     /**
337      * Returns the initial size which is the larger of the <code>initialSize</code> or
338      * the size persisted in the Ant UI Plugin dialog settings under the provided dialog setttings section name.
339      * If no size is persisted in the settings, the <code>initialSize</code> is returned.
340      *
341      * @param initialSize The initialSize to compare against
342      * @param dialogSettingsSectionName The name of the dialog settings section
343      * @return the initial size
344      */

345     private Point getInitialSize(String JavaDoc dialogSettingsSectionName, Point initialSize) {
346         IDialogSettings settings = getDialogSettings(dialogSettingsSectionName);
347         try {
348             int x, y;
349             x = settings.getInt(DIALOG_WIDTH);
350             y = settings.getInt(DIALOG_HEIGHT);
351             return new Point(Math.max(x, initialSize.x), Math.max(y, initialSize.y));
352         } catch (NumberFormatException JavaDoc e) {
353         }
354         return initialSize;
355     }
356     
357 }
358
Popular Tags