1 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 56 public class CleanDialog extends MessageDialog { 57 58 private static final String DIALOG_SETTINGS_SECTION = "CleanDialogSettings"; private static final String DIALOG_ORIGIN_X = "DIALOG_X_ORIGIN"; private static final String DIALOG_ORIGIN_Y = "DIALOG_Y_ORIGIN"; private static final String DIALOG_WIDTH = "DIALOG_WIDTH"; private static final String DIALOG_HEIGHT = "DIALOG_HEIGHT"; private static final String TOGGLE_SELECTED = "TOGGLE_SELECTED"; private static final String BUILD_NOW = "BUILD_NOW"; 66 private Button allButton, selectedButton, buildNowButton; 67 68 private CheckboxTableViewer projectNames; 69 70 private Object [] selection; 71 72 private IWorkbenchWindow window; 73 74 79 private static String getQuestion() { 80 boolean autoBuilding = ResourcesPlugin.getWorkspace().isAutoBuilding(); 81 if (autoBuilding) { 82 return IDEWorkbenchMessages.CleanDialog_buildCleanAuto; 83 } 84 return IDEWorkbenchMessages.CleanDialog_buildCleanManual; 85 } 86 87 93 public CleanDialog(IWorkbenchWindow window, IProject[] selection) { 94 super( 95 window.getShell(), 96 IDEWorkbenchMessages.CleanDialog_title, null, getQuestion(), QUESTION, new String [] { 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 [0]; 102 } 103 setShellStyle(SWT.RESIZE | getShellStyle()); 104 } 105 106 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 BuildUtilities.saveEditors(null); 121 WorkspaceJob cleanJob = new WorkspaceJob(IDEWorkbenchMessages.CleanDialog_taskName) { 123 public boolean belongsTo(Object family) { 124 return ResourcesPlugin.FAMILY_MANUAL_BUILD.equals(family); 125 } 126 public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException { 127 doClean(cleanAll, monitor); 128 if (buildAll) { 130 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 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 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 createProjectSelectionTable(area); 175 176 if (!ResourcesPlugin.getWorkspace().isAutoBuilding()) { 179 buildNowButton = new Button(parent, SWT.CHECK); 180 buildNowButton.setText(IDEWorkbenchMessages.CleanDialog_buildNowButton); 181 String 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 parentElement, Object 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 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 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 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 265 public boolean close() { 266 persistDialogSettings(getShell(), DIALOG_SETTINGS_SECTION); 267 return super.close(); 268 } 269 270 273 protected Point getInitialLocation(Point initialSize) { 274 Point p = getInitialLocation(DIALOG_SETTINGS_SECTION); 275 return p != null ? p : super.getInitialLocation(initialSize); 276 } 277 278 281 protected Point getInitialSize() { 282 Point p = super.getInitialSize(); 283 return getInitialSize(DIALOG_SETTINGS_SECTION, p); 284 } 285 286 294 public Point getInitialLocation(String 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 e) { 301 } 302 return null; 303 } 304 305 private IDialogSettings getDialogSettings(String 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 321 private void persistDialogSettings(Shell shell, String 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 345 private Point getInitialSize(String 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 e) { 353 } 354 return initialSize; 355 } 356 357 } 358 | Popular Tags |