1 12 13 package org.eclipse.ui.views.navigator; 14 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.core.resources.IContainer; 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IMarker; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IWorkspace; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.IAdaptable; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.jface.action.IMenuListener; 29 import org.eclipse.jface.action.IMenuManager; 30 import org.eclipse.jface.action.MenuManager; 31 import org.eclipse.jface.dialogs.IDialogSettings; 32 import org.eclipse.jface.preference.IPreferenceStore; 33 import org.eclipse.jface.util.IPropertyChangeListener; 34 import org.eclipse.jface.util.PropertyChangeEvent; 35 import org.eclipse.jface.viewers.DecoratingLabelProvider; 36 import org.eclipse.jface.viewers.DoubleClickEvent; 37 import org.eclipse.jface.viewers.IDoubleClickListener; 38 import org.eclipse.jface.viewers.ILabelDecorator; 39 import org.eclipse.jface.viewers.ILabelProvider; 40 import org.eclipse.jface.viewers.IOpenListener; 41 import org.eclipse.jface.viewers.ISelection; 42 import org.eclipse.jface.viewers.ISelectionChangedListener; 43 import org.eclipse.jface.viewers.IStructuredSelection; 44 import org.eclipse.jface.viewers.OpenEvent; 45 import org.eclipse.jface.viewers.SelectionChangedEvent; 46 import org.eclipse.jface.viewers.StructuredSelection; 47 import org.eclipse.jface.viewers.TreeViewer; 48 import org.eclipse.jface.viewers.ViewerComparator; 49 import org.eclipse.jface.viewers.ViewerSorter; 50 import org.eclipse.osgi.util.NLS; 51 import org.eclipse.swt.SWT; 52 import org.eclipse.swt.dnd.DND; 53 import org.eclipse.swt.dnd.FileTransfer; 54 import org.eclipse.swt.dnd.Transfer; 55 import org.eclipse.swt.events.KeyEvent; 56 import org.eclipse.swt.events.KeyListener; 57 import org.eclipse.swt.widgets.Composite; 58 import org.eclipse.swt.widgets.Control; 59 import org.eclipse.swt.widgets.Event; 60 import org.eclipse.swt.widgets.Listener; 61 import org.eclipse.swt.widgets.Menu; 62 import org.eclipse.swt.widgets.Shell; 63 import org.eclipse.ui.IEditorPart; 64 import org.eclipse.ui.IMemento; 65 import org.eclipse.ui.IPartListener; 66 import org.eclipse.ui.IViewSite; 67 import org.eclipse.ui.IWorkbenchPage; 68 import org.eclipse.ui.IWorkbenchPart; 69 import org.eclipse.ui.IWorkbenchPreferenceConstants; 70 import org.eclipse.ui.IWorkingSet; 71 import org.eclipse.ui.IWorkingSetManager; 72 import org.eclipse.ui.PartInitException; 73 import org.eclipse.ui.PlatformUI; 74 import org.eclipse.ui.ResourceWorkingSetFilter; 75 import org.eclipse.ui.actions.ActionContext; 76 import org.eclipse.ui.actions.OpenResourceAction; 77 import org.eclipse.ui.ide.ResourceUtil; 78 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 79 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages; 80 import org.eclipse.ui.model.WorkbenchContentProvider; 81 import org.eclipse.ui.model.WorkbenchLabelProvider; 82 import org.eclipse.ui.part.ISetSelectionTarget; 83 import org.eclipse.ui.part.IShowInSource; 84 import org.eclipse.ui.part.IShowInTarget; 85 import org.eclipse.ui.part.PluginTransfer; 86 import org.eclipse.ui.part.ResourceTransfer; 87 import org.eclipse.ui.part.ShowInContext; 88 import org.eclipse.ui.part.ViewPart; 89 import org.eclipse.ui.plugin.AbstractUIPlugin; 90 import org.eclipse.ui.views.framelist.FrameList; 91 import org.eclipse.ui.views.framelist.TreeFrame; 92 93 96 public class ResourceNavigator extends ViewPart implements ISetSelectionTarget, 97 IResourceNavigator { 98 99 private TreeViewer viewer; 100 101 private IDialogSettings settings; 102 103 private IMemento memento; 104 105 private FrameList frameList; 106 107 private ResourceNavigatorActionGroup actionGroup; 108 109 private ResourcePatternFilter patternFilter = new ResourcePatternFilter(); 110 111 private ResourceWorkingSetFilter workingSetFilter = new ResourceWorkingSetFilter(); 112 113 private boolean linkingEnabled; 114 115 private boolean dragDetected; 116 117 private Listener dragDetectListener; 118 121 private IWorkingSet workingSet; 122 123 127 private boolean emptyWorkingSet = false; 128 129 132 private static final String STORE_SECTION = "ResourceNavigator"; 134 137 private static final String STORE_SORT_TYPE = "ResourceViewer.STORE_SORT_TYPE"; 139 142 private static final String STORE_WORKING_SET = "ResourceWorkingSetFilter.STORE_WORKING_SET"; 144 147 public static final String NAVIGATOR_VIEW_HELP_ID = INavigatorHelpContextIds.RESOURCE_VIEW; 148 149 152 private boolean linkScheduled = false; 153 154 private static final String TAG_SORTER = "sorter"; 157 private static final String TAG_FILTERS = "filters"; 159 private static final String TAG_FILTER = "filter"; 161 private static final String TAG_SELECTION = "selection"; 163 private static final String TAG_EXPANDED = "expanded"; 165 private static final String TAG_ELEMENT = "element"; 167 private static final String TAG_IS_ENABLED = "isEnabled"; 169 private static final String TAG_PATH = "path"; 171 private static final String TAG_CURRENT_FRAME = "currentFrame"; 173 private IPartListener partListener = new IPartListener() { 174 public void partActivated(IWorkbenchPart part) { 175 if (part instanceof IEditorPart) { 176 editorActivated((IEditorPart) part); 177 } 178 } 179 180 public void partBroughtToTop(IWorkbenchPart part) { 181 } 182 183 public void partClosed(IWorkbenchPart part) { 184 } 185 186 public void partDeactivated(IWorkbenchPart part) { 187 } 188 189 public void partOpened(IWorkbenchPart part) { 190 } 191 }; 192 193 private IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() { 194 public void propertyChange(PropertyChangeEvent event) { 195 String property = event.getProperty(); 196 Object newValue = event.getNewValue(); 197 Object oldValue = event.getOldValue(); 198 199 if (IWorkingSetManager.CHANGE_WORKING_SET_REMOVE.equals(property) 200 && oldValue == workingSet) { 201 setWorkingSet(null); 202 } else if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE 203 .equals(property) 204 && newValue == workingSet) { 205 updateTitle(); 206 } else if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE 207 .equals(property) 208 && newValue == workingSet) { 209 if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) { 210 if (!emptyWorkingSet) { 212 emptyWorkingSet = true; 213 workingSetFilter.setWorkingSet(null); 214 } 215 } else { 216 if (emptyWorkingSet) { 219 emptyWorkingSet = false; 220 workingSetFilter.setWorkingSet(workingSet); 221 } 222 } 223 getViewer().refresh(); 224 } 225 } 226 }; 227 228 231 public ResourceNavigator() { 232 IDialogSettings viewsSettings = getPlugin().getDialogSettings(); 233 234 settings = viewsSettings.getSection(STORE_SECTION); 235 if (settings == null) { 236 settings = viewsSettings.addNewSection(STORE_SECTION); 237 } 238 239 initLinkingEnabled(); 240 } 241 242 246 private StructuredSelection convertSelection(ISelection selection) { 247 ArrayList list = new ArrayList (); 248 if (selection instanceof IStructuredSelection) { 249 IStructuredSelection ssel = (IStructuredSelection) selection; 250 for (Iterator i = ssel.iterator(); i.hasNext();) { 251 Object o = i.next(); 252 IResource resource = null; 253 if (o instanceof IResource) { 254 resource = (IResource) o; 255 } else { 256 if (o instanceof IAdaptable) { 257 resource = (IResource) ((IAdaptable) o) 258 .getAdapter(IResource.class); 259 } 260 } 261 if (resource != null) { 262 list.add(resource); 263 } 264 } 265 } 266 return new StructuredSelection(list); 267 } 268 269 272 public void createPartControl(Composite parent) { 273 TreeViewer viewer = createViewer(parent); 274 this.viewer = viewer; 275 276 if (memento != null) { 277 restoreFilters(); 278 restoreLinkingEnabled(); 279 } 280 frameList = createFrameList(); 281 initDragAndDrop(); 282 updateTitle(); 283 284 initContextMenu(); 285 286 initResourceComparator(); 287 initWorkingSetFilter(); 288 289 viewer.setInput(getInitialInput()); 292 293 makeActions(); 296 297 getActionGroup().fillActionBars(getViewSite().getActionBars()); 300 updateActionBars((IStructuredSelection) viewer.getSelection()); 301 302 getSite().setSelectionProvider(viewer); 303 getSite().getPage().addPartListener(partListener); 304 IWorkingSetManager workingSetManager = getPlugin().getWorkbench() 305 .getWorkingSetManager(); 306 workingSetManager.addPropertyChangeListener(propertyChangeListener); 307 308 if (memento != null) { 309 restoreState(memento); 310 } 311 memento = null; 312 313 getSite().getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp( 315 viewer.getControl(), getHelpContextId()); 316 } 317 318 323 protected String getHelpContextId() { 324 return INavigatorHelpContextIds.RESOURCE_VIEW; 325 } 326 327 332 protected void initContextMenu() { 333 MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.setRemoveAllWhenShown(true); 335 menuMgr.addMenuListener(new IMenuListener() { 336 public void menuAboutToShow(IMenuManager manager) { 337 ResourceNavigator.this.fillContextMenu(manager); 338 } 339 }); 340 TreeViewer viewer = getTreeViewer(); 341 Menu menu = menuMgr.createContextMenu(viewer.getTree()); 342 viewer.getTree().setMenu(menu); 343 getSite().registerContextMenu(menuMgr, viewer); 344 } 345 346 352 protected TreeViewer createViewer(Composite parent) { 353 TreeViewer viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL 354 | SWT.V_SCROLL); 355 viewer.setUseHashlookup(true); 356 initContentProvider(viewer); 357 initLabelProvider(viewer); 358 initFilters(viewer); 359 initListeners(viewer); 360 361 return viewer; 362 } 363 364 370 protected void initContentProvider(TreeViewer viewer) { 371 viewer.setContentProvider(new WorkbenchContentProvider()); 372 } 373 374 380 protected void initLabelProvider(TreeViewer viewer) { 381 viewer.setLabelProvider(new DecoratingLabelProvider( 382 new WorkbenchLabelProvider(), getPlugin().getWorkbench() 383 .getDecoratorManager().getLabelDecorator())); 384 } 385 386 392 protected void initFilters(TreeViewer viewer) { 393 viewer.addFilter(patternFilter); 394 viewer.addFilter(workingSetFilter); 395 } 396 397 400 private void initLinkingEnabled() { 401 String setting = settings 403 .get(IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR); 404 if (setting != null) { 405 linkingEnabled = setting.equals("true"); return; 407 } 408 linkingEnabled = PlatformUI.getPreferenceStore().getBoolean( 411 IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR); 412 } 413 414 420 protected void initListeners(TreeViewer viewer) { 421 viewer.addSelectionChangedListener(new ISelectionChangedListener() { 422 public void selectionChanged(SelectionChangedEvent event) { 423 handleSelectionChanged(event); 424 } 425 }); 426 viewer.addDoubleClickListener(new IDoubleClickListener() { 427 public void doubleClick(DoubleClickEvent event) { 428 handleDoubleClick(event); 429 } 430 }); 431 viewer.addOpenListener(new IOpenListener() { 432 public void open(OpenEvent event) { 433 handleOpen(event); 434 } 435 }); 436 viewer.getControl().addKeyListener(new KeyListener() { 437 public void keyPressed(KeyEvent event) { 438 handleKeyPressed(event); 439 } 440 441 public void keyReleased(KeyEvent event) { 442 handleKeyReleased(event); 443 } 444 }); 445 } 446 447 450 public void dispose() { 451 getSite().getPage().removePartListener(partListener); 452 453 IWorkingSetManager workingSetManager = getPlugin().getWorkbench() 454 .getWorkingSetManager(); 455 workingSetManager.removePropertyChangeListener(propertyChangeListener); 456 457 if (getActionGroup() != null) { 458 getActionGroup().dispose(); 459 } 460 Control control = viewer.getControl(); 461 if (dragDetectListener != null && control != null 462 && control.isDisposed() == false) { 463 control.removeListener(SWT.DragDetect, dragDetectListener); 464 } 465 super.dispose(); 466 } 467 468 475 protected void editorActivated(IEditorPart editor) { 476 if (!isLinkingEnabled()) { 477 return; 478 } 479 480 IFile file = ResourceUtil.getFile(editor.getEditorInput()); 481 if (file != null) { 482 ISelection newSelection = new StructuredSelection(file); 483 if (getTreeViewer().getSelection().equals(newSelection)) { 484 getTreeViewer().getTree().showSelection(); 485 } else { 486 getTreeViewer().setSelection(newSelection, true); 487 } 488 } 489 } 490 491 496 protected void fillContextMenu(IMenuManager menu) { 497 IStructuredSelection selection = (IStructuredSelection) getViewer() 498 .getSelection(); 499 getActionGroup().setContext(new ActionContext(selection)); 500 getActionGroup().fillContextMenu(menu); 501 } 502 503 507 public FrameList getFrameList() { 508 return frameList; 509 } 510 511 520 protected IAdaptable getInitialInput() { 521 IAdaptable input = getSite().getPage().getInput(); 522 if (input != null) { 523 IResource resource = null; 524 if (input instanceof IResource) { 525 resource = (IResource) input; 526 } else { 527 resource = (IResource) input.getAdapter(IResource.class); 528 } 529 if (resource != null) { 530 switch (resource.getType()) { 531 case IResource.FILE: 532 return resource.getParent(); 533 case IResource.FOLDER: 534 case IResource.PROJECT: 535 case IResource.ROOT: 536 return resource; 537 default: 538 break; 540 } 541 } 542 } 543 return ResourcesPlugin.getWorkspace().getRoot(); 544 } 545 546 552 public ResourcePatternFilter getPatternFilter() { 553 return this.patternFilter; 554 } 555 556 562 public IWorkingSet getWorkingSet() { 563 return workingSetFilter.getWorkingSet(); 564 } 565 566 570 public AbstractUIPlugin getPlugin() { 571 return IDEWorkbenchPlugin.getDefault(); 572 } 573 574 580 public ResourceSorter getSorter() { 581 return (ResourceSorter) getTreeViewer().getSorter(); 582 } 583 584 590 591 public ResourceComparator getComparator(){ 592 return (ResourceComparator) getTreeViewer().getComparator(); 593 } 594 598 public TreeViewer getViewer() { 599 return viewer; 600 } 601 602 607 public TreeViewer getTreeViewer() { 608 return viewer; 609 } 610 611 618 public Shell getShell() { 619 return getViewSite().getShell(); 620 } 621 622 629 protected String getStatusLineMessage(IStructuredSelection selection) { 630 if (selection.size() == 1) { 631 Object o = selection.getFirstElement(); 632 if (o instanceof IResource) { 633 return ((IResource) o).getFullPath().makeRelative().toString(); 634 } 635 return ResourceNavigatorMessages.ResourceNavigator_oneItemSelected; 636 } 637 if (selection.size() > 1) { 638 return NLS.bind(ResourceNavigatorMessages.ResourceNavigator_statusLine, String.valueOf(selection.size())); 639 } 640 return ""; } 642 643 647 String getFrameName(Object element) { 648 if (element instanceof IResource) { 649 return ((IResource) element).getName(); 650 } 651 String text = ((ILabelProvider) getTreeViewer().getLabelProvider()) 652 .getText(element); 653 if(text == null) { 654 return ""; } 656 return text; 657 } 658 659 663 String getFrameToolTipText(Object element) { 664 if (element instanceof IResource) { 665 IPath path = ((IResource) element).getFullPath(); 666 if (path.isRoot()) { 667 return ResourceNavigatorMessages.ResourceManager_toolTip; 668 } 669 return path.makeRelative().toString(); 670 } 671 672 String text = ((ILabelProvider) getTreeViewer().getLabelProvider()) 673 .getText(element); 674 if(text == null) { 675 return ""; } 677 return text; 678 } 679 680 687 protected void handleOpen(OpenEvent event) { 688 IStructuredSelection selection = (IStructuredSelection) event 689 .getSelection(); 690 getActionGroup().runDefaultAction(selection); 691 } 692 693 700 protected void handleDoubleClick(DoubleClickEvent event) { 701 IStructuredSelection selection = (IStructuredSelection) event 702 .getSelection(); 703 Object element = selection.getFirstElement(); 704 705 TreeViewer viewer = getTreeViewer(); 707 if (viewer.isExpandable(element)) { 708 viewer.setExpandedState(element, !viewer.getExpandedState(element)); 709 } else if (selection.size() == 1 && (element instanceof IResource) 710 && ((IResource) element).getType() == IResource.PROJECT) { 711 OpenResourceAction ora = new OpenResourceAction(getSite() 712 .getShell()); 713 ora.selectionChanged((IStructuredSelection) viewer.getSelection()); 714 if (ora.isEnabled()) { 715 ora.run(); 716 } 717 } 718 719 } 720 721 728 protected void handleSelectionChanged(SelectionChangedEvent event) { 729 final IStructuredSelection sel = (IStructuredSelection) event 730 .getSelection(); 731 updateStatusLine(sel); 732 updateActionBars(sel); 733 dragDetected = false; 734 if (isLinkingEnabled() && !linkScheduled) { 735 linkScheduled = true; 738 getShell().getDisplay().asyncExec(new Runnable () { 739 public void run() { 740 linkScheduled = false; 743 744 if (viewer == null || viewer.getControl() == null || viewer.getControl().isDisposed()) { 745 return; 746 } 747 748 if (dragDetected == false) { 749 ISelection sel = viewer.getSelection(); 750 if (sel instanceof IStructuredSelection) { 751 IStructuredSelection selection = (IStructuredSelection)sel; 752 753 linkToEditor(selection); 756 } 757 } 758 } 759 }); 760 } 761 } 762 763 770 protected void handleKeyPressed(KeyEvent event) { 771 getActionGroup().handleKeyPressed(event); 772 } 773 774 780 protected void handleKeyReleased(KeyEvent event) { 781 } 782 783 786 public void init(IViewSite site, IMemento memento) throws PartInitException { 787 super.init(site, memento); 788 this.memento = memento; 789 } 790 791 796 protected void initDragAndDrop() { 797 int ops = DND.DROP_COPY | DND.DROP_MOVE; 798 Transfer[] transfers = new Transfer[] { 799 LocalSelectionTransfer.getInstance(), 800 ResourceTransfer.getInstance(), FileTransfer.getInstance(), 801 PluginTransfer.getInstance() }; 802 TreeViewer viewer = getTreeViewer(); 803 viewer.addDragSupport(ops, transfers, new NavigatorDragAdapter(viewer)); 804 NavigatorDropAdapter adapter = new NavigatorDropAdapter(viewer); 805 adapter.setFeedbackEnabled(false); 806 viewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, adapter); 807 dragDetectListener = new Listener() { 808 public void handleEvent(Event event) { 809 dragDetected = true; 810 } 811 }; 812 viewer.getControl().addListener(SWT.DragDetect, dragDetectListener); 813 } 814 815 820 protected FrameList createFrameList() { 821 NavigatorFrameSource frameSource = new NavigatorFrameSource(this); 822 FrameList frameList = new FrameList(frameSource); 823 frameSource.connectTo(frameList); 824 return frameList; 825 } 826 827 832 protected void initResourceSorter() { 833 int sortType = ResourceSorter.NAME; 834 try { 835 int sortInt = 0; 836 if (memento != null) { 837 String sortStr = memento.getString(TAG_SORTER); 838 if (sortStr != null) { 839 sortInt = new Integer (sortStr).intValue(); 840 } 841 } else { 842 sortInt = settings.getInt(STORE_SORT_TYPE); 843 } 844 if (sortInt == ResourceSorter.NAME 845 || sortInt == ResourceSorter.TYPE) { 846 sortType = sortInt; 847 } 848 } catch (NumberFormatException e) { 849 } 850 setSorter(new ResourceSorter(sortType)); 851 } 852 853 857 protected void initResourceComparator(){ 858 int sortType = ResourceComparator.NAME; 859 try { 860 int sortInt = 0; 861 if (memento != null) { 862 String sortStr = memento.getString(TAG_SORTER); 863 if (sortStr != null) { 864 sortInt = new Integer (sortStr).intValue(); 865 } 866 } else { 867 sortInt = settings.getInt(STORE_SORT_TYPE); 868 } 869 if (sortInt == ResourceComparator.NAME 870 || sortInt == ResourceComparator.TYPE) { 871 sortType = sortInt; 872 } 873 } catch (NumberFormatException e) { 874 } 875 setComparator(new ResourceComparator(sortType)); 876 } 877 878 881 protected void initWorkingSetFilter() { 882 String workingSetName = settings.get(STORE_WORKING_SET); 883 884 IWorkingSet workingSet = null; 885 886 if (workingSetName != null && workingSetName.equals("") == false) { IWorkingSetManager workingSetManager = getPlugin().getWorkbench() 888 .getWorkingSetManager(); 889 workingSet = workingSetManager.getWorkingSet(workingSetName); 890 } else if (PlatformUI 891 .getPreferenceStore() 892 .getBoolean( 893 IWorkbenchPreferenceConstants.USE_WINDOW_WORKING_SET_BY_DEFAULT)) { 894 workingSet = getSite().getPage().getAggregateWorkingSet(); 896 } 897 898 if (workingSet != null) { 899 workingSetFilter.setWorkingSet(workingSet); 903 internalSetWorkingSet(workingSet); 904 } 905 } 906 907 915 public boolean isLinkingEnabled() { 916 return linkingEnabled; 917 } 918 919 924 protected void linkToEditor(IStructuredSelection selection) { 925 926 if (this != this.getSite().getPage().getActivePart()) 927 return; 928 929 Object obj = selection.getFirstElement(); 930 if (obj instanceof IFile && selection.size() == 1) { 931 IFile file = (IFile) obj; 932 IWorkbenchPage page = getSite().getPage(); 933 IEditorPart editor = ResourceUtil.findEditor(page, file); 934 if (editor != null) { 935 page.bringToTop(editor); 936 return; 937 } 938 } 939 } 940 941 944 protected void makeActions() { 945 setActionGroup(new MainActionGroup(this)); 946 } 947 948 951 private void restoreFilters() { 952 IMemento filtersMem = memento.getChild(TAG_FILTERS); 953 954 if (filtersMem != null) { IMemento children[] = filtersMem.getChildren(TAG_FILTER); 956 957 if (children.length > 0 959 && children[0].getString(TAG_IS_ENABLED) != null) { 960 ArrayList selectedFilters = new ArrayList (); 961 ArrayList unSelectedFilters = new ArrayList (); 962 for (int i = 0; i < children.length; i++) { 963 if (children[i].getString(TAG_IS_ENABLED).equals( 964 String.valueOf(true))) { 965 selectedFilters.add(children[i].getString(TAG_ELEMENT)); 966 } else { 967 unSelectedFilters.add(children[i] 969 .getString(TAG_ELEMENT)); 970 } 971 } 972 973 975 List pluginFilters = FiltersContentProvider.getDefaultFilters(); 976 for (Iterator iter = pluginFilters.iterator(); iter.hasNext();) { 977 String element = (String ) iter.next(); 978 if (!selectedFilters.contains(element) 979 && !unSelectedFilters.contains(element)) { 980 selectedFilters.add(element); 981 } 982 } 983 984 String [] patternArray = new String [selectedFilters.size()]; 986 selectedFilters.toArray(patternArray); 987 getPatternFilter().setPatterns(patternArray); 988 989 } else { String filters[] = new String [children.length]; 991 for (int i = 0; i < children.length; i++) { 992 filters[i] = children[i].getString(TAG_ELEMENT); 993 } 994 getPatternFilter().setPatterns(filters); 995 } 996 } else { getPatternFilter().setPatterns(new String [0]); 998 } 999 } 1000 1001 1007 protected void restoreState(IMemento memento) { 1008 TreeViewer viewer = getTreeViewer(); 1009 IMemento frameMemento = memento.getChild(TAG_CURRENT_FRAME); 1010 1011 if (frameMemento != null) { 1012 TreeFrame frame = new TreeFrame(viewer); 1013 frame.restoreState(frameMemento); 1014 frame.setName(getFrameName(frame.getInput())); 1015 frame.setToolTipText(getFrameToolTipText(frame.getInput())); 1016 viewer.setSelection(new StructuredSelection(frame.getInput())); 1017 frameList.gotoFrame(frame); 1018 } else { 1019 IContainer container = ResourcesPlugin.getWorkspace().getRoot(); 1020 IMemento childMem = memento.getChild(TAG_EXPANDED); 1021 if (childMem != null) { 1022 ArrayList elements = new ArrayList (); 1023 IMemento[] elementMem = childMem.getChildren(TAG_ELEMENT); 1024 for (int i = 0; i < elementMem.length; i++) { 1025 Object element = container.findMember(elementMem[i] 1026 .getString(TAG_PATH)); 1027 if (element != null) { 1028 elements.add(element); 1029 } 1030 } 1031 viewer.setExpandedElements(elements.toArray()); 1032 } 1033 childMem = memento.getChild(TAG_SELECTION); 1034 if (childMem != null) { 1035 ArrayList list = new ArrayList (); 1036 IMemento[] elementMem = childMem.getChildren(TAG_ELEMENT); 1037 for (int i = 0; i < elementMem.length; i++) { 1038 Object element = container.findMember(elementMem[i] 1039 .getString(TAG_PATH)); 1040 if (element != null) { 1041 list.add(element); 1042 } 1043 } 1044 viewer.setSelection(new StructuredSelection(list)); 1045 } 1046 } 1047 } 1048 1049 1052 private void restoreLinkingEnabled() { 1053 Integer val = memento 1054 .getInteger(IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR); 1055 if (val != null) { 1056 linkingEnabled = val.intValue() != 0; 1057 } 1058 } 1059 1060 1063 public void saveState(IMemento memento) { 1064 TreeViewer viewer = getTreeViewer(); 1065 if (viewer == null) { 1066 if (this.memento != null) { 1067 memento.putMemento(this.memento); 1068 } 1069 return; 1070 } 1071 1072 memento.putInteger(TAG_SORTER, getComparator().getCriteria()); 1074 1075 String filters[] = getPatternFilter().getPatterns(); 1077 List selectedFilters = Arrays.asList(filters); 1078 List allFilters = FiltersContentProvider.getDefinedFilters(); 1079 IMemento filtersMem = memento.createChild(TAG_FILTERS); 1080 for (Iterator iter = allFilters.iterator(); iter.hasNext();) { 1081 String element = (String ) iter.next(); 1082 IMemento child = filtersMem.createChild(TAG_FILTER); 1083 child.putString(TAG_ELEMENT, element); 1084 child.putString(TAG_IS_ENABLED, String.valueOf(selectedFilters 1085 .contains(element))); 1086 } 1087 1088 if (frameList.getCurrentIndex() > 0) { 1089 TreeFrame currentFrame = (TreeFrame) frameList.getCurrentFrame(); 1091 IMemento frameMemento = memento.createChild(TAG_CURRENT_FRAME); 1092 currentFrame.saveState(frameMemento); 1093 } else { 1094 Object expandedElements[] = viewer.getVisibleExpandedElements(); 1096 if (expandedElements.length > 0) { 1097 IMemento expandedMem = memento.createChild(TAG_EXPANDED); 1098 for (int i = 0; i < expandedElements.length; i++) { 1099 if (expandedElements[i] instanceof IResource) { 1100 IMemento elementMem = expandedMem 1101 .createChild(TAG_ELEMENT); 1102 elementMem.putString(TAG_PATH, 1103 ((IResource) expandedElements[i]).getFullPath() 1104 .toString()); 1105 } 1106 } 1107 } 1108 Object elements[] = ((IStructuredSelection) viewer.getSelection()) 1110 .toArray(); 1111 if (elements.length > 0) { 1112 IMemento selectionMem = memento.createChild(TAG_SELECTION); 1113 for (int i = 0; i < elements.length; i++) { 1114 if (elements[i] instanceof IResource) { 1115 IMemento elementMem = selectionMem 1116 .createChild(TAG_ELEMENT); 1117 elementMem.putString(TAG_PATH, 1118 ((IResource) elements[i]).getFullPath() 1119 .toString()); 1120 } 1121 } 1122 } 1123 } 1124 1125 saveLinkingEnabled(memento); 1126 } 1127 1128 1131 private void saveLinkingEnabled(IMemento memento) { 1132 memento.putInteger( 1133 IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR, 1134 linkingEnabled ? 1 : 0); 1135 } 1136 1137 1140 public void selectReveal(ISelection selection) { 1141 StructuredSelection ssel = convertSelection(selection); 1142 if (!ssel.isEmpty()) { 1143 getViewer().getControl().setRedraw(false); 1144 getViewer().setSelection(ssel, true); 1145 getViewer().getControl().setRedraw(true); 1146 } 1147 } 1148 1149 1153 public void setFiltersPreference(String [] patterns) { 1154 1155 StringBuffer sb = new StringBuffer (); 1156 1157 for (int i = 0; i < patterns.length; i++) { 1158 if (i != 0) { 1159 sb.append(ResourcePatternFilter.COMMA_SEPARATOR); 1160 } 1161 sb.append(patterns[i]); 1162 } 1163 1164 getPlugin().getPreferenceStore().setValue( 1165 ResourcePatternFilter.FILTERS_TAG, sb.toString()); 1166 1167 IPreferenceStore preferenceStore = IDEWorkbenchPlugin.getDefault() 1169 .getPreferenceStore(); 1170 String storedPatterns = preferenceStore 1171 .getString(ResourcePatternFilter.FILTERS_TAG); 1172 if (storedPatterns.length() > 0) { 1173 preferenceStore.setValue(ResourcePatternFilter.FILTERS_TAG, ""); } 1175 } 1176 1177 1180 public void setFocus() { 1181 getTreeViewer().getTree().setFocus(); 1182 } 1183 1184 1194 public void setLabelDecorator(ILabelDecorator decorator) { 1195 } 1197 1198 1202 public void setLinkingEnabled(boolean enabled) { 1203 this.linkingEnabled = enabled; 1204 1205 settings.put(IWorkbenchPreferenceConstants.LINK_NAVIGATOR_TO_EDITOR, 1207 enabled); 1208 1209 if (enabled) { 1211 IEditorPart editor = getSite().getPage().getActiveEditor(); 1212 if (editor != null) { 1213 editorActivated(editor); 1214 } 1215 } 1216 } 1217 1218 1225 public void setSorter(ResourceSorter sorter) { 1226 TreeViewer viewer = getTreeViewer(); 1227 ViewerSorter viewerSorter = viewer.getSorter(); 1228 1229 viewer.getControl().setRedraw(false); 1230 if (viewerSorter == sorter) { 1231 viewer.refresh(); 1232 } else { 1233 viewer.setSorter(sorter); 1234 } 1235 viewer.getControl().setRedraw(true); 1236 settings.put(STORE_SORT_TYPE, sorter.getCriteria()); 1237 1238 updateActionBars((IStructuredSelection) viewer.getSelection()); 1240 } 1241 1242 1248 public void setComparator(ResourceComparator comparator){ 1249 TreeViewer viewer = getTreeViewer(); 1250 ViewerComparator viewerComparator = viewer.getComparator(); 1251 1252 viewer.getControl().setRedraw(false); 1253 if (viewerComparator == comparator) { 1254 viewer.refresh(); 1255 } else { 1256 viewer.setComparator(comparator); 1257 } 1258 viewer.getControl().setRedraw(true); 1259 settings.put(STORE_SORT_TYPE, comparator.getCriteria()); 1260 1261 updateActionBars((IStructuredSelection) viewer.getSelection()); 1263 } 1264 1265 1269 public void setWorkingSet(IWorkingSet workingSet) { 1270 TreeViewer treeViewer = getTreeViewer(); 1271 Object [] expanded = treeViewer.getExpandedElements(); 1272 ISelection selection = treeViewer.getSelection(); 1273 1274 internalSetWorkingSet(workingSet); 1275 1276 workingSetFilter.setWorkingSet(emptyWorkingSet ? null : workingSet); 1277 if (workingSet != null) { 1278 settings.put(STORE_WORKING_SET, workingSet.getName()); 1279 } else { 1280 settings.put(STORE_WORKING_SET, ""); } 1282 updateTitle(); 1283 treeViewer.refresh(); 1284 treeViewer.setExpandedElements(expanded); 1285 if (selection.isEmpty() == false 1286 && selection instanceof IStructuredSelection) { 1287 IStructuredSelection structuredSelection = (IStructuredSelection) selection; 1288 treeViewer.reveal(structuredSelection.getFirstElement()); 1289 } 1290 } 1291 1292 1299 private void internalSetWorkingSet(IWorkingSet workingSet) { 1300 this.workingSet = workingSet; 1301 emptyWorkingSet = workingSet != null && workingSet.isAggregateWorkingSet() 1302 && workingSet.isEmpty(); 1303 } 1304 1305 1311 protected void updateActionBars(IStructuredSelection selection) { 1312 ResourceNavigatorActionGroup group = getActionGroup(); 1313 if (group != null) { 1314 group.setContext(new ActionContext(selection)); 1315 group.updateActionBars(); 1316 } 1317 } 1318 1319 1324 protected void updateStatusLine(IStructuredSelection selection) { 1325 String msg = getStatusLineMessage(selection); 1326 getViewSite().getActionBars().getStatusLineManager().setMessage(msg); 1327 } 1328 1329 1336 public void updateTitle() { 1337 Object input = getViewer().getInput(); 1338 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 1339 IWorkingSet workingSet = workingSetFilter.getWorkingSet(); 1340 1341 if (input == null || input.equals(workspace) 1342 || input.equals(workspace.getRoot())) { 1343 setContentDescription(""); if (workingSet != null) { 1345 setTitleToolTip(NLS.bind(ResourceNavigatorMessages.ResourceNavigator_workingSetToolTip, workingSet.getLabel())); 1346 } else { 1347 setTitleToolTip(""); } 1349 } else { 1350 ILabelProvider labelProvider = (ILabelProvider) getTreeViewer() 1351 .getLabelProvider(); 1352 String inputToolTip = getFrameToolTipText(input); 1353 String text = labelProvider.getText(input); 1354 if(text != null) { 1355 setContentDescription(text); 1356 } 1357 if (workingSet != null) { 1358 setTitleToolTip(NLS.bind(ResourceNavigatorMessages.ResourceNavigator_workingSetInputToolTip, inputToolTip, workingSet.getLabel())); 1359 } else { 1360 setTitleToolTip(inputToolTip); 1361 } 1362 } 1363 } 1364 1365 1370 protected ResourceNavigatorActionGroup getActionGroup() { 1371 return actionGroup; 1372 } 1373 1374 1379 protected void setActionGroup(ResourceNavigatorActionGroup actionGroup) { 1380 this.actionGroup = actionGroup; 1381 } 1382 1383 1386 public Object getAdapter(Class adapter) { 1387 if (adapter == IShowInSource.class) { 1388 return getShowInSource(); 1389 } 1390 if (adapter == IShowInTarget.class) { 1391 return getShowInTarget(); 1392 } 1393 return null; 1394 } 1395 1396 1399 protected IShowInSource getShowInSource() { 1400 return new IShowInSource() { 1401 public ShowInContext getShowInContext() { 1402 return new ShowInContext(getViewer().getInput(), getViewer() 1403 .getSelection()); 1404 } 1405 }; 1406 } 1407 1408 1411 protected IShowInTarget getShowInTarget() { 1412 return new IShowInTarget() { 1413 public boolean show(ShowInContext context) { 1414 ArrayList toSelect = new ArrayList (); 1415 ISelection sel = context.getSelection(); 1416 if (sel instanceof IStructuredSelection) { 1417 IStructuredSelection ssel = (IStructuredSelection) sel; 1418 for (Iterator i = ssel.iterator(); i.hasNext();) { 1419 Object o = i.next(); 1420 if (o instanceof IResource) { 1421 toSelect.add(o); 1422 } else if (o instanceof IMarker) { 1423 IResource r = ((IMarker) o).getResource(); 1424 if (r.getType() != IResource.ROOT) { 1425 toSelect.add(r); 1426 } 1427 } else if (o instanceof IAdaptable) { 1428 IAdaptable adaptable = (IAdaptable) o; 1429 o = adaptable.getAdapter(IResource.class); 1430 if (o instanceof IResource) { 1431 toSelect.add(o); 1432 } else { 1433 o = adaptable.getAdapter(IMarker.class); 1434 if (o instanceof IMarker) { 1435 IResource r = ((IMarker) o).getResource(); 1436 if (r.getType() != IResource.ROOT) { 1437 toSelect.add(r); 1438 } 1439 } 1440 } 1441 } 1442 } 1443 } 1444 if (toSelect.isEmpty()) { 1445 Object input = context.getInput(); 1446 if (input instanceof IAdaptable) { 1447 IAdaptable adaptable = (IAdaptable) input; 1448 Object o = adaptable.getAdapter(IResource.class); 1449 if (o instanceof IResource) { 1450 toSelect.add(o); 1451 } 1452 } 1453 } 1454 if (!toSelect.isEmpty()) { 1455 selectReveal(new StructuredSelection(toSelect)); 1456 return true; 1457 } 1458 return false; 1459 } 1460 }; 1461 } 1462} 1463 | Popular Tags |