1 13 package org.eclipse.ui.internal.ide.dialogs; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IContainer; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IAdaptable; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.jface.dialogs.Dialog; 26 import org.eclipse.jface.dialogs.ErrorDialog; 27 import org.eclipse.jface.dialogs.IDialogConstants; 28 import org.eclipse.jface.dialogs.MessageDialog; 29 import org.eclipse.jface.viewers.CheckStateChangedEvent; 30 import org.eclipse.jface.viewers.CheckboxTreeViewer; 31 import org.eclipse.jface.viewers.DecoratingLabelProvider; 32 import org.eclipse.jface.viewers.ICheckStateListener; 33 import org.eclipse.jface.viewers.ISelection; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.viewers.ITreeContentProvider; 36 import org.eclipse.jface.viewers.ITreeViewerListener; 37 import org.eclipse.jface.viewers.TreeExpansionEvent; 38 import org.eclipse.jface.wizard.WizardPage; 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.custom.BusyIndicator; 41 import org.eclipse.swt.events.ModifyEvent; 42 import org.eclipse.swt.events.ModifyListener; 43 import org.eclipse.swt.events.SelectionAdapter; 44 import org.eclipse.swt.events.SelectionEvent; 45 import org.eclipse.swt.layout.GridData; 46 import org.eclipse.swt.layout.GridLayout; 47 import org.eclipse.swt.widgets.Button; 48 import org.eclipse.swt.widgets.Composite; 49 import org.eclipse.swt.widgets.Label; 50 import org.eclipse.swt.widgets.Shell; 51 import org.eclipse.swt.widgets.Text; 52 import org.eclipse.ui.IWorkbenchPage; 53 import org.eclipse.ui.IWorkbenchPart; 54 import org.eclipse.ui.IWorkingSet; 55 import org.eclipse.ui.IWorkingSetManager; 56 import org.eclipse.ui.PlatformUI; 57 import org.eclipse.ui.dialogs.IWorkingSetPage; 58 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages; 59 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 60 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 61 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 62 import org.eclipse.ui.model.WorkbenchContentProvider; 63 import org.eclipse.ui.model.WorkbenchLabelProvider; 64 65 74 public class ResourceWorkingSetPage extends WizardPage implements 75 IWorkingSetPage { 76 private final static int SIZING_SELECTION_WIDGET_WIDTH = 50; 77 78 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 200; 79 80 private Text text; 81 82 private CheckboxTreeViewer tree; 83 84 private IWorkingSet workingSet; 85 86 private boolean firstCheck = false; 88 91 public ResourceWorkingSetPage() { 92 super( 93 "resourceWorkingSetPage", IDEWorkbenchMessages.ResourceWorkingSetPage_title, 95 IDEInternalWorkbenchImages 96 .getImageDescriptor(IDEInternalWorkbenchImages.IMG_WIZBAN_RESOURCEWORKINGSET_WIZ)); 97 setDescription(IDEWorkbenchMessages.ResourceWorkingSetPage_description); 98 } 99 100 107 private void addWorkingSetElements(List collectedResources, 108 IContainer container) { 109 IAdaptable[] elements = workingSet.getElements(); 110 IPath containerPath = container.getFullPath(); 111 112 for (int i = 0; i < elements.length; i++) { 113 IResource resource = null; 114 115 if (elements[i] instanceof IResource) { 116 resource = (IResource) elements[i]; 117 } else { 118 resource = (IResource) elements[i].getAdapter(IResource.class); 119 } 120 121 if (resource != null) { 122 IPath resourcePath = resource.getFullPath(); 123 if (containerPath.isPrefixOf(resourcePath)) { 124 collectedResources.add(elements[i]); 125 } 126 } 127 } 128 } 129 130 135 public void createControl(Composite parent) { 136 initializeDialogUnits(parent); 137 138 Composite composite = new Composite(parent, SWT.NULL); 139 GridLayout layout = new GridLayout(); 140 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 141 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 142 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 143 composite.setLayout(layout); 144 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 145 setControl(composite); 146 147 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, 148 IIDEHelpContextIds.WORKING_SET_RESOURCE_PAGE); 149 Label label = new Label(composite, SWT.WRAP); 150 label.setText(IDEWorkbenchMessages.ResourceWorkingSetPage_message); 151 GridData data = new GridData(GridData.GRAB_HORIZONTAL 152 | GridData.HORIZONTAL_ALIGN_FILL 153 | GridData.VERTICAL_ALIGN_CENTER); 154 label.setLayoutData(data); 155 156 text = new Text(composite, SWT.SINGLE | SWT.BORDER); 157 text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 158 | GridData.HORIZONTAL_ALIGN_FILL)); 159 text.addModifyListener(new ModifyListener() { 160 public void modifyText(ModifyEvent e) { 161 validateInput(); 162 } 163 }); 164 text.setFocus(); 165 166 label = new Label(composite, SWT.WRAP); 167 label.setText(IDEWorkbenchMessages.ResourceWorkingSetPage_label_tree); 168 data = new GridData(GridData.GRAB_HORIZONTAL 169 | GridData.HORIZONTAL_ALIGN_FILL 170 | GridData.VERTICAL_ALIGN_CENTER); 171 label.setLayoutData(data); 172 173 tree = new CheckboxTreeViewer(composite); 174 tree.setUseHashlookup(true); 175 final ITreeContentProvider treeContentProvider = new WorkbenchContentProvider(); 176 tree.setContentProvider(treeContentProvider); 177 tree.setLabelProvider(new DecoratingLabelProvider( 178 new WorkbenchLabelProvider(), IDEWorkbenchPlugin.getDefault() 179 .getWorkbench().getDecoratorManager() 180 .getLabelDecorator())); 181 tree.setInput(IDEWorkbenchPlugin.getPluginWorkspace().getRoot()); 182 tree.setComparator(new ResourceComparator(ResourceComparator.NAME)); 183 184 data = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL); 185 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; 186 data.widthHint = SIZING_SELECTION_WIDGET_WIDTH; 187 tree.getControl().setLayoutData(data); 188 189 tree.addCheckStateListener(new ICheckStateListener() { 190 public void checkStateChanged(CheckStateChangedEvent event) { 191 handleCheckStateChange(event); 192 } 193 }); 194 195 tree.addTreeListener(new ITreeViewerListener() { 196 public void treeCollapsed(TreeExpansionEvent event) { 197 } 198 199 public void treeExpanded(TreeExpansionEvent event) { 200 final Object element = event.getElement(); 201 if (tree.getGrayed(element) == false) { 202 BusyIndicator.showWhile(getShell().getDisplay(), 203 new Runnable () { 204 public void run() { 205 setSubtreeChecked((IContainer) element, 206 tree.getChecked(element), false); 207 } 208 }); 209 } 210 } 211 }); 212 213 Composite buttonComposite = new Composite(composite, SWT.NONE); 215 layout = new GridLayout(2, false); 216 layout.marginWidth = 0; 217 layout.marginHeight = 0; 218 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 219 buttonComposite.setLayout(layout); 220 buttonComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 221 222 Button selectAllButton = new Button(buttonComposite, SWT.PUSH); 223 selectAllButton.setText(IDEWorkbenchMessages.ResourceWorkingSetPage_selectAll_label); 224 selectAllButton.setToolTipText(IDEWorkbenchMessages.ResourceWorkingSetPage_selectAll_toolTip); 225 selectAllButton.addSelectionListener(new SelectionAdapter() { 226 public void widgetSelected(SelectionEvent selectionEvent) { 227 tree.setCheckedElements(treeContentProvider.getElements(tree.getInput())); 228 validateInput(); 229 } 230 }); 231 setButtonLayoutData(selectAllButton); 232 233 Button deselectAllButton = new Button(buttonComposite, SWT.PUSH); 234 deselectAllButton.setText(IDEWorkbenchMessages.ResourceWorkingSetPage_deselectAll_label); 235 deselectAllButton.setToolTipText(IDEWorkbenchMessages.ResourceWorkingSetPage_deselectAll_toolTip); 236 deselectAllButton.addSelectionListener(new SelectionAdapter() { 237 public void widgetSelected(SelectionEvent selectionEvent) { 238 tree.setCheckedElements(new Object [0]); 239 validateInput(); 240 } 241 }); 242 setButtonLayoutData(deselectAllButton); 243 244 initializeCheckedState(); 245 if (workingSet != null) { 246 text.setText(workingSet.getName()); 247 } 248 setPageComplete(false); 249 250 Dialog.applyDialogFont(composite); 251 } 252 253 259 private void findCheckedResources(List checkedResources, 260 IContainer container) { 261 IResource[] resources = null; 262 try { 263 resources = container.members(); 264 } catch (CoreException ex) { 265 handleCoreException( 266 ex, 267 getShell(), 268 IDEWorkbenchMessages.ResourceWorkingSetPage_error, 269 IDEWorkbenchMessages.ResourceWorkingSetPage_error_updateCheckedState); 270 } 271 for (int i = 0; i < resources.length; i++) { 272 if (tree.getGrayed(resources[i])) { 273 if (resources[i].isAccessible()) { 274 findCheckedResources(checkedResources, 275 (IContainer) resources[i]); 276 } else { 277 addWorkingSetElements(checkedResources, 278 (IContainer) resources[i]); 279 } 280 } else if (tree.getChecked(resources[i])) { 281 checkedResources.add(resources[i]); 282 } 283 } 284 } 285 286 291 public void finish() { 292 ArrayList resources = new ArrayList (10); 293 findCheckedResources(resources, (IContainer) tree.getInput()); 294 if (workingSet == null) { 295 IWorkingSetManager workingSetManager = PlatformUI.getWorkbench() 296 .getWorkingSetManager(); 297 workingSet = workingSetManager.createWorkingSet( 298 getWorkingSetName(), (IAdaptable[]) resources 299 .toArray(new IAdaptable[resources.size()])); 300 } else { 301 workingSet.setName(getWorkingSetName()); 302 workingSet.setElements((IAdaptable[]) resources 303 .toArray(new IAdaptable[resources.size()])); 304 } 305 } 306 307 312 public IWorkingSet getSelection() { 313 return workingSet; 314 } 315 316 321 private String getWorkingSetName() { 322 return text.getText(); 323 } 324 325 330 private void handleCheckStateChange(final CheckStateChangedEvent event) { 331 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable () { 332 public void run() { 333 IResource resource = (IResource) event.getElement(); 334 boolean state = event.getChecked(); 335 336 tree.setGrayed(resource, false); 337 if (resource instanceof IContainer) { 338 setSubtreeChecked((IContainer) resource, state, true); 339 } 340 updateParentState(resource); 341 validateInput(); 342 } 343 }); 344 } 345 346 354 private void handleCoreException(CoreException exception, Shell shell, 355 String title, String message) { 356 IStatus status = exception.getStatus(); 357 if (status != null) { 358 ErrorDialog.openError(shell, title, message, status); 359 } else { 360 MessageDialog 361 .openError( 362 shell, 363 IDEWorkbenchMessages.InternalError, exception.getLocalizedMessage()); 364 } 365 } 366 367 371 private void initializeCheckedState() { 372 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable () { 373 public void run() { 374 Object [] items = null; 375 if (workingSet == null) { 376 377 IWorkbenchPage page = IDEWorkbenchPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage(); 378 if(page == null) { 379 return; 380 } 381 IWorkbenchPart part = page.getActivePart(); 382 if(part == null) { 383 return; 384 } 385 ISelection selection = page.getSelection(); 386 if(selection instanceof IStructuredSelection) { 387 items = ((IStructuredSelection)selection).toArray(); 388 } 389 390 } else { 391 items = workingSet.getElements(); 392 } 393 if(items == null) { 394 return; 395 } 396 tree.setCheckedElements(items); 397 for (int i = 0; i < items.length; i++) { 398 IAdaptable item = null; 399 if(!(items[i] instanceof IAdaptable)) { 400 continue; 401 } 402 item = (IAdaptable)items[i]; 403 IContainer container = null; 404 IResource resource = null; 405 406 if (item instanceof IContainer) { 407 container = (IContainer) item; 408 } else { 409 container = (IContainer) item 410 .getAdapter(IContainer.class); 411 } 412 if (container != null) { 413 setSubtreeChecked(container, true, true); 414 } 415 if (item instanceof IResource) { 416 resource = (IResource) item; 417 } else { 418 resource = (IResource) item.getAdapter(IResource.class); 419 } 420 if (resource != null && resource.isAccessible() == false) { 421 IProject project = resource.getProject(); 422 if (tree.getChecked(project) == false) { 423 tree.setGrayChecked(project, true); 424 } 425 } else { 426 updateParentState(resource); 427 } 428 } 429 } 430 }); 431 } 432 433 438 public void setSelection(IWorkingSet workingSet) { 439 if (workingSet == null) { 440 throw new IllegalArgumentException ("Working set must not be null"); } 442 this.workingSet = workingSet; 443 if (getShell() != null && text != null) { 444 firstCheck = true; 445 initializeCheckedState(); 446 text.setText(workingSet.getName()); 447 } 448 } 449 450 459 private void setSubtreeChecked(IContainer container, boolean state, 460 boolean checkExpandedState) { 461 if (container.isAccessible() == false 463 || (tree.getExpandedState(container) == false && state && checkExpandedState)) { 464 return; 465 } 466 IResource[] members = null; 467 try { 468 members = container.members(); 469 } catch (CoreException ex) { 470 handleCoreException( 471 ex, 472 getShell(), 473 IDEWorkbenchMessages.ResourceWorkingSetPage_error, 474 IDEWorkbenchMessages.ResourceWorkingSetPage_error_updateCheckedState); 475 } 476 for (int i = members.length - 1; i >= 0; i--) { 477 IResource element = members[i]; 478 boolean elementGrayChecked = tree.getGrayed(element) 479 || tree.getChecked(element); 480 481 if (state) { 482 tree.setChecked(element, true); 483 tree.setGrayed(element, false); 484 } else { 485 tree.setGrayChecked(element, false); 486 } 487 if (element instanceof IContainer && (state || elementGrayChecked)) { 490 setSubtreeChecked((IContainer) element, state, true); 491 } 492 } 493 } 494 495 502 private void updateParentState(IResource child) { 503 if (child == null || child.getParent() == null) { 504 return; 505 } 506 507 IContainer parent = child.getParent(); 508 boolean childChecked = false; 509 IResource[] members = null; 510 try { 511 members = parent.members(); 512 } catch (CoreException ex) { 513 handleCoreException( 514 ex, 515 getShell(), 516 IDEWorkbenchMessages.ResourceWorkingSetPage_error, 517 IDEWorkbenchMessages.ResourceWorkingSetPage_error_updateCheckedState); 518 } 519 for (int i = members.length - 1; i >= 0; i--) { 520 if (tree.getChecked(members[i]) || tree.getGrayed(members[i])) { 521 childChecked = true; 522 break; 523 } 524 } 525 tree.setGrayChecked(parent, childChecked); 526 updateParentState(parent); 527 } 528 529 533 private void validateInput() { 534 String errorMessage = null; 535 String infoMessage= null; 536 String newText = text.getText(); 537 538 if (newText.equals(newText.trim()) == false) { 539 errorMessage = IDEWorkbenchMessages.ResourceWorkingSetPage_warning_nameWhitespace; 540 } else if (firstCheck) { 541 firstCheck = false; 542 return; 543 } 544 if (newText.equals("")) { errorMessage = IDEWorkbenchMessages.ResourceWorkingSetPage_warning_nameMustNotBeEmpty; 546 } 547 if (errorMessage == null 548 && (workingSet == null || newText.equals(workingSet.getName()) == false)) { 549 IWorkingSet[] workingSets = PlatformUI.getWorkbench() 550 .getWorkingSetManager().getWorkingSets(); 551 for (int i = 0; i < workingSets.length; i++) { 552 if (newText.equals(workingSets[i].getName())) { 553 errorMessage = IDEWorkbenchMessages.ResourceWorkingSetPage_warning_workingSetExists; 554 } 555 } 556 } 557 if (infoMessage == null && tree.getCheckedElements().length == 0) { 558 infoMessage = IDEWorkbenchMessages.ResourceWorkingSetPage_warning_resourceMustBeChecked; 559 } 560 setMessage(infoMessage, INFORMATION); 561 setErrorMessage(errorMessage); 562 setPageComplete(errorMessage == null); 563 } 564 } 565 | Popular Tags |