1 11 package org.eclipse.ui.dialogs; 12 13 import java.io.IOException ; 14 import java.io.StringReader ; 15 import java.io.StringWriter ; 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.Comparator ; 19 import java.util.List ; 20 21 import org.eclipse.core.resources.IContainer; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IResourceProxy; 24 import org.eclipse.core.resources.IResourceProxyVisitor; 25 import org.eclipse.core.resources.IWorkspace; 26 import org.eclipse.core.resources.ResourcesPlugin; 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.IProgressMonitor; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.ListenerList; 31 import org.eclipse.core.runtime.Status; 32 import org.eclipse.jface.action.Action; 33 import org.eclipse.jface.action.IAction; 34 import org.eclipse.jface.action.IMenuManager; 35 import org.eclipse.jface.action.Separator; 36 import org.eclipse.jface.dialogs.IDialogSettings; 37 import org.eclipse.jface.text.ITextSelection; 38 import org.eclipse.jface.util.IPropertyChangeListener; 39 import org.eclipse.jface.util.PropertyChangeEvent; 40 import org.eclipse.jface.viewers.ILabelDecorator; 41 import org.eclipse.jface.viewers.ILabelProviderListener; 42 import org.eclipse.jface.viewers.ISelection; 43 import org.eclipse.jface.viewers.LabelProvider; 44 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 45 import org.eclipse.jface.viewers.Viewer; 46 import org.eclipse.jface.viewers.ViewerFilter; 47 import org.eclipse.swt.graphics.Image; 48 import org.eclipse.swt.widgets.Composite; 49 import org.eclipse.swt.widgets.Control; 50 import org.eclipse.swt.widgets.Shell; 51 import org.eclipse.ui.IMemento; 52 import org.eclipse.ui.IWorkbenchPage; 53 import org.eclipse.ui.IWorkbenchWindow; 54 import org.eclipse.ui.IWorkingSet; 55 import org.eclipse.ui.PlatformUI; 56 import org.eclipse.ui.ResourceWorkingSetFilter; 57 import org.eclipse.ui.WorkbenchException; 58 import org.eclipse.ui.XMLMemento; 59 import org.eclipse.ui.actions.WorkingSetFilterActionGroup; 60 import org.eclipse.ui.internal.WorkbenchMessages; 61 import org.eclipse.ui.internal.WorkbenchPlugin; 62 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 63 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 64 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 65 import org.eclipse.ui.internal.ide.model.ResourceFactory; 66 import org.eclipse.ui.model.WorkbenchLabelProvider; 67 import org.eclipse.ui.statushandlers.StatusManager; 68 69 import com.ibm.icu.text.Collator; 70 71 77 public class FilteredResourcesSelectionDialog extends 78 FilteredItemsSelectionDialog { 79 80 private static final String DIALOG_SETTINGS = "org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog"; 82 private static final String WORKINGS_SET_SETTINGS = "WorkingSet"; 84 private static final String SHOW_DERIVED = "ShowDerived"; 86 private ShowDerivedResourcesAction showDerivedResourcesAction; 87 88 private ResourceItemLabelProvider resourceItemLabelProvider; 89 90 private ResourceItemDetailsLabelProvider resourceItemDetailsLabelProvider; 91 92 private WorkingSetFilterActionGroup workingSetFilterActionGroup; 93 94 private CustomWorkingSetFilter workingSetFilter = new CustomWorkingSetFilter(); 95 96 private String title; 97 98 private IContainer container; 99 100 private int typeMask; 101 102 private boolean isDerived; 103 104 116 public FilteredResourcesSelectionDialog(Shell shell, boolean multi, 117 IContainer container, int typesMask) { 118 super(shell, multi); 119 120 setSelectionHistory(new ResourceSelectionHistory()); 121 122 setTitle(IDEWorkbenchMessages.OpenResourceDialog_title); 123 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 124 IIDEHelpContextIds.OPEN_RESOURCE_DIALOG); 125 126 this.container = container; 127 this.typeMask = typesMask; 128 129 resourceItemLabelProvider = new ResourceItemLabelProvider(); 130 131 resourceItemDetailsLabelProvider = new ResourceItemDetailsLabelProvider(); 132 133 setListLabelProvider(resourceItemLabelProvider); 134 setDetailsLabelProvider(resourceItemDetailsLabelProvider); 135 } 136 137 142 public void setTitle(String title) { 143 super.setTitle(title); 144 this.title = title; 145 } 146 147 153 private void setSubtitle(String text) { 154 if (text == null || text.length() == 0) { 155 getShell().setText(title); 156 } else { 157 getShell().setText(title + " - " + text); } 159 } 160 161 166 protected IDialogSettings getDialogSettings() { 167 IDialogSettings settings = IDEWorkbenchPlugin.getDefault() 168 .getDialogSettings().getSection(DIALOG_SETTINGS); 169 170 if (settings == null) { 171 settings = IDEWorkbenchPlugin.getDefault().getDialogSettings() 172 .addNewSection(DIALOG_SETTINGS); 173 } 174 175 return settings; 176 } 177 178 183 protected void storeDialog(IDialogSettings settings) { 184 super.storeDialog(settings); 185 186 settings.put(SHOW_DERIVED, showDerivedResourcesAction.isChecked()); 187 188 XMLMemento memento = XMLMemento.createWriteRoot("workingSet"); workingSetFilterActionGroup.saveState(memento); 190 workingSetFilterActionGroup.dispose(); 191 StringWriter writer = new StringWriter (); 192 try { 193 memento.save(writer); 194 settings.put(WORKINGS_SET_SETTINGS, writer.getBuffer().toString()); 195 } catch (IOException e) { 196 StatusManager.getManager().handle( 197 new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, 198 IStatus.ERROR, "", e)); } 201 } 202 203 208 protected void restoreDialog(IDialogSettings settings) { 209 super.restoreDialog(settings); 210 211 boolean showDerived = settings.getBoolean(SHOW_DERIVED); 212 showDerivedResourcesAction.setChecked(showDerived); 213 this.isDerived = showDerived; 214 215 String setting = settings.get(WORKINGS_SET_SETTINGS); 216 if (setting != null) { 217 try { 218 IMemento memento = XMLMemento.createReadRoot(new StringReader ( 219 setting)); 220 workingSetFilterActionGroup.restoreState(memento); 221 } catch (WorkbenchException e) { 222 StatusManager.getManager().handle( 223 new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, 224 IStatus.ERROR, "", e)); } 227 } 228 229 addListFilter(workingSetFilter); 230 231 applyFilter(); 232 } 233 234 239 protected void fillViewMenu(IMenuManager menuManager) { 240 super.fillViewMenu(menuManager); 241 242 showDerivedResourcesAction = new ShowDerivedResourcesAction(); 243 menuManager.add(showDerivedResourcesAction); 244 245 workingSetFilterActionGroup = new WorkingSetFilterActionGroup( 246 getShell(), new IPropertyChangeListener() { 247 public void propertyChange(PropertyChangeEvent event) { 248 String property = event.getProperty(); 249 250 if (WorkingSetFilterActionGroup.CHANGE_WORKING_SET 251 .equals(property)) { 252 253 IWorkingSet workingSet = (IWorkingSet) event 254 .getNewValue(); 255 256 if (workingSet != null 257 && !(workingSet.isAggregateWorkingSet() && workingSet 258 .isEmpty())) { 259 workingSetFilter.setWorkingSet(workingSet); 260 setSubtitle(workingSet.getLabel()); 261 } else { 262 IWorkbenchWindow window = PlatformUI 263 .getWorkbench() 264 .getActiveWorkbenchWindow(); 265 266 if (window != null) { 267 IWorkbenchPage page = window 268 .getActivePage(); 269 workingSet = page.getAggregateWorkingSet(); 270 271 if (workingSet.isAggregateWorkingSet() 272 && workingSet.isEmpty()) { 273 workingSet = null; 274 } 275 } 276 277 workingSetFilter.setWorkingSet(workingSet); 278 setSubtitle(null); 279 } 280 281 scheduleRefresh(); 282 } 283 } 284 }); 285 286 menuManager.add(new Separator()); 287 workingSetFilterActionGroup.fillContextMenu(menuManager); 288 } 289 290 295 protected Control createExtendedContentArea(Composite parent) { 296 return null; 297 } 298 299 304 public Object [] getResult() { 305 Object [] result = super.getResult(); 306 307 if (result == null) 308 return null; 309 310 List resultToReturn = new ArrayList (); 311 312 for (int i = 0; i < result.length; i++) { 313 if (result[i] instanceof IResource) { 314 resultToReturn.add((result[i])); 315 } 316 } 317 318 return resultToReturn.toArray(); 319 } 320 321 326 public int open() { 327 if (getInitialPattern() == null) { 328 IWorkbenchWindow window = PlatformUI.getWorkbench() 329 .getActiveWorkbenchWindow(); 330 if (window != null) { 331 ISelection selection = window.getSelectionService() 332 .getSelection(); 333 if (selection instanceof ITextSelection) { 334 String text = ((ITextSelection) selection).getText(); 335 if (text != null) { 336 text = text.trim(); 337 if (text.length() > 0) { 338 IWorkspace workspace = ResourcesPlugin 339 .getWorkspace(); 340 IStatus result = workspace.validateName(text, 341 IResource.FILE); 342 if (result.isOK()) { 343 setInitialPattern(text); 344 } 345 } 346 } 347 } 348 } 349 } 350 return super.open(); 351 } 352 353 358 public String getElementName(Object item) { 359 IResource resource = (IResource) item; 360 return resource.getName(); 361 } 362 363 368 protected IStatus validateItem(Object item) { 369 return new Status(IStatus.OK, WorkbenchPlugin.PI_WORKBENCH, 0, "", null); } 371 372 377 protected ItemsFilter createFilter() { 378 return new ResourceFilter(container, isDerived, typeMask); 379 } 380 381 384 protected void applyFilter() { 385 super.applyFilter(); 386 } 387 388 393 protected Comparator getItemsComparator() { 394 return new Comparator () { 395 396 402 public int compare(Object o1, Object o2) { 403 Collator collator = Collator.getInstance(); 404 IResource resource1 = (IResource) o1; 405 IResource resource2 = (IResource) o2; 406 String s1 = resource1.getName(); 407 String s2 = resource2.getName(); 408 int comparability = collator.compare(s1, s2); 409 if (comparability == 0) { 410 s1 = resource1.getFullPath().toString(); 411 s2 = resource2.getFullPath().toString(); 412 comparability = collator.compare(s1, s2); 413 } 414 415 return comparability; 416 } 417 }; 418 } 419 420 427 protected void fillContentProvider(AbstractContentProvider contentProvider, 428 ItemsFilter itemsFilter, IProgressMonitor progressMonitor) 429 throws CoreException { 430 if (itemsFilter instanceof ResourceFilter) 431 container.accept(new ResourceProxyVisitor(contentProvider, 432 (ResourceFilter) itemsFilter, progressMonitor), 433 IResource.NONE); 434 if (progressMonitor != null) 435 progressMonitor.done(); 436 437 } 438 439 442 private class ShowDerivedResourcesAction extends Action { 443 444 447 public ShowDerivedResourcesAction() { 448 super( 449 IDEWorkbenchMessages.FilteredResourcesSelectionDialog_showDerivedResourcesAction, 450 IAction.AS_CHECK_BOX); 451 } 452 453 public void run() { 454 FilteredResourcesSelectionDialog.this.isDerived = isChecked(); 455 applyFilter(); 456 } 457 } 458 459 464 private class ResourceItemLabelProvider extends LabelProvider implements 465 ILabelProviderListener { 466 467 private ListenerList listeners = new ListenerList(); 469 470 WorkbenchLabelProvider provider = new WorkbenchLabelProvider(); 471 472 ILabelDecorator decorator = PlatformUI.getWorkbench() 473 .getDecoratorManager().getLabelDecorator(); 474 475 478 public ResourceItemLabelProvider() { 479 super(); 480 provider.addListener(this); 481 decorator.addListener(this); 482 } 483 484 489 public Image getImage(Object element) { 490 if (!(element instanceof IResource)) { 491 return super.getImage(element); 492 } 493 494 IResource res = (IResource) element; 495 496 Image img = provider.getImage(res); 497 498 return decorator.decorateImage(img, res); 499 } 500 501 506 public String getText(Object element) { 507 if (!(element instanceof IResource)) { 508 return super.getText(element); 509 } 510 511 IResource res = (IResource) element; 512 513 String str = res.getName(); 514 515 if (isDuplicateElement(element)) 517 str = str 518 + " - " + res.getParent().getFullPath().makeRelative().toString(); 520 return decorator.decorateText(str, res); 521 } 522 523 528 public void dispose() { 529 provider.removeListener(this); 530 provider.dispose(); 531 532 decorator.removeListener(this); 533 decorator.dispose(); 534 535 super.dispose(); 536 } 537 538 543 public void addListener(ILabelProviderListener listener) { 544 listeners.add(listener); 545 } 546 547 552 public void removeListener(ILabelProviderListener listener) { 553 listeners.remove(listener); 554 } 555 556 561 public void labelProviderChanged(LabelProviderChangedEvent event) { 562 Object [] l = listeners.getListeners(); 563 for (int i = 0; i < listeners.size(); i++) { 564 ((ILabelProviderListener) l[i]).labelProviderChanged(event); 565 } 566 } 567 568 } 569 570 573 private class ResourceItemDetailsLabelProvider extends 574 ResourceItemLabelProvider { 575 580 public Image getImage(Object element) { 581 if (!(element instanceof IResource)) { 582 return super.getImage(element); 583 } 584 585 IResource parent = ((IResource) element).getParent(); 586 Image img = provider.getImage(parent); 587 588 return decorator.decorateImage(img, parent); 589 } 590 591 596 public String getText(Object element) { 597 if (!(element instanceof IResource)) { 598 return super.getText(element); 599 } 600 601 IResource parent = ((IResource) element).getParent(); 602 603 if (parent.getType() == IResource.ROOT) { 604 return super.decorator.decorateText(null, parent); 607 } 608 609 return super.decorator.decorateText(parent.getFullPath() 610 .makeRelative().toString(), parent); 611 } 612 613 618 public void labelProviderChanged(LabelProviderChangedEvent event) { 619 Object [] l = super.listeners.getListeners(); 620 for (int i = 0; i < super.listeners.size(); i++) { 621 ((ILabelProviderListener) l[i]).labelProviderChanged(event); 622 } 623 } 624 } 625 626 629 private class CustomWorkingSetFilter extends ViewerFilter { 630 private ResourceWorkingSetFilter resourceWorkingSetFilter = new ResourceWorkingSetFilter(); 631 632 637 public IWorkingSet getWorkingSet() { 638 return resourceWorkingSetFilter.getWorkingSet(); 639 } 640 641 647 public void setWorkingSet(IWorkingSet workingSet) { 648 resourceWorkingSetFilter.setWorkingSet(workingSet); 649 } 650 651 657 public boolean select(Viewer viewer, Object parentElement, 658 Object element) { 659 return resourceWorkingSetFilter.select(viewer, parentElement, 660 element); 661 } 662 } 663 664 669 private class ResourceProxyVisitor implements IResourceProxyVisitor { 670 671 private AbstractContentProvider proxyContentProvider; 672 673 private ResourceFilter resourceFilter; 674 675 private IProgressMonitor progressMonitor; 676 677 private List projects; 678 679 687 public ResourceProxyVisitor(AbstractContentProvider contentProvider, 688 ResourceFilter resourceFilter, IProgressMonitor progressMonitor) 689 throws CoreException { 690 super(); 691 this.proxyContentProvider = contentProvider; 692 this.resourceFilter = resourceFilter; 693 this.progressMonitor = progressMonitor; 694 IResource[] resources = container.members(); 695 this.projects = new ArrayList (Arrays.asList(resources)); 696 697 if (progressMonitor != null) 698 progressMonitor 699 .beginTask( 700 WorkbenchMessages.FilteredItemsSelectionDialog_searchJob_taskName, 701 projects.size()); 702 } 703 704 709 public boolean visit(IResourceProxy proxy) { 710 711 if (progressMonitor.isCanceled()) 712 return false; 713 714 IResource resource = proxy.requestResource(); 715 716 if (this.projects.remove((resource.getProject())) 717 || this.projects.remove((resource))) { 718 progressMonitor.worked(1); 719 } 720 721 proxyContentProvider.add(resource, resourceFilter); 722 723 if (resource.getType() == IResource.FOLDER && resource.isDerived() 724 && !resourceFilter.isShowDerived()) { 725 726 return false; 727 } 728 729 if (resource.getType() == IResource.FILE) { 730 return false; 731 } 732 733 return true; 734 } 735 } 736 737 741 protected class ResourceFilter extends ItemsFilter { 742 743 private boolean showDerived = false; 744 745 private IContainer filterContainer; 746 747 private int filterTypeMask; 748 749 757 public ResourceFilter(IContainer container, boolean showDerived, 758 int typeMask) { 759 super(); 760 this.filterContainer = container; 761 this.showDerived = showDerived; 762 this.filterTypeMask = typeMask; 763 } 764 765 768 public ResourceFilter() { 769 super(); 770 this.filterContainer = container; 771 this.showDerived = isDerived; 772 this.filterTypeMask = typeMask; 773 } 774 775 781 public boolean isConsistentItem(Object item) { 782 if (!(item instanceof IResource)) { 783 return false; 784 } 785 IResource resource = (IResource) item; 786 if (this.filterContainer.findMember(resource.getFullPath()) != null) 787 return true; 788 return false; 789 } 790 791 797 public boolean matchItem(Object item) { 798 if (!(item instanceof IResource)) { 799 return false; 800 } 801 IResource resource = (IResource) item; 802 if ((!this.showDerived && resource.isDerived()) 803 || ((this.filterTypeMask & resource.getType()) == 0)) 804 return false; 805 return matches(resource.getName()); 806 } 807 808 813 public boolean isSubFilter(ItemsFilter filter) { 814 if (!super.isSubFilter(filter)) 815 return false; 816 if (filter instanceof ResourceFilter) 817 if (this.showDerived == ((ResourceFilter) filter).showDerived) 818 return true; 819 return false; 820 } 821 822 827 public boolean equalsFilter(ItemsFilter iFilter) { 828 if (!super.equalsFilter(iFilter)) 829 return false; 830 if (iFilter instanceof ResourceFilter) 831 if (this.showDerived == ((ResourceFilter) iFilter).showDerived) 832 return true; 833 return false; 834 } 835 836 841 public boolean isShowDerived() { 842 return showDerived; 843 } 844 845 } 846 847 852 private class ResourceSelectionHistory extends SelectionHistory { 853 854 859 protected Object restoreItemFromMemento(IMemento element) { 860 ResourceFactory resourceFactory = new ResourceFactory(); 861 IResource resource = (IResource) resourceFactory 862 .createElement(element); 863 return resource; 864 } 865 866 872 protected void storeItemToMemento(Object item, IMemento element) { 873 IResource resource = (IResource) item; 874 ResourceFactory resourceFactory = new ResourceFactory(resource); 875 resourceFactory.saveState(element); 876 } 877 878 } 879 880 } 881 | Popular Tags |