1 11 package org.eclipse.ui.navigator; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.jface.action.IAction; 16 import org.eclipse.jface.viewers.DoubleClickEvent; 17 import org.eclipse.jface.viewers.IDoubleClickListener; 18 import org.eclipse.jface.viewers.ILabelProvider; 19 import org.eclipse.jface.viewers.ISelection; 20 import org.eclipse.jface.viewers.ISelectionChangedListener; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.SelectionChangedEvent; 23 import org.eclipse.jface.viewers.StructuredSelection; 24 import org.eclipse.jface.viewers.TreeViewer; 25 import org.eclipse.jface.viewers.ViewerFilter; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.widgets.Composite; 28 import org.eclipse.ui.IEditorInput; 29 import org.eclipse.ui.IMemento; 30 import org.eclipse.ui.ISaveablePart; 31 import org.eclipse.ui.ISaveablesLifecycleListener; 32 import org.eclipse.ui.ISaveablesSource; 33 import org.eclipse.ui.IViewSite; 34 import org.eclipse.ui.PartInitException; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.Saveable; 37 import org.eclipse.ui.SaveablesLifecycleEvent; 38 import org.eclipse.ui.actions.ActionGroup; 39 import org.eclipse.ui.internal.navigator.CommonNavigatorActionGroup; 40 import org.eclipse.ui.internal.navigator.CommonNavigatorManager; 41 import org.eclipse.ui.internal.navigator.NavigatorContentService; 42 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 43 import org.eclipse.ui.internal.navigator.extensions.LinkHelperService; 44 import org.eclipse.ui.part.ISetSelectionTarget; 45 import org.eclipse.ui.part.IShowInTarget; 46 import org.eclipse.ui.part.ShowInContext; 47 import org.eclipse.ui.part.ViewPart; 48 49 110 public class CommonNavigator extends ViewPart implements ISetSelectionTarget, ISaveablePart, ISaveablesSource, IShowInTarget { 111 112 private static final Class INAVIGATOR_CONTENT_SERVICE = INavigatorContentService.class; 113 private static final Class COMMON_VIEWER_CLASS = CommonViewer.class; 114 private static final Class ISHOW_IN_TARGET_CLASS = IShowInTarget.class; 115 116 private static final String HELP_CONTEXT = NavigatorPlugin.PLUGIN_ID + ".common_navigator"; 118 123 public static final int IS_LINKING_ENABLED_PROPERTY = 1; 124 125 private CommonViewer commonViewer; 126 127 private CommonNavigatorManager commonManager; 128 129 private ActionGroup commonActionGroup; 130 131 private IMemento memento; 132 133 private boolean isLinkingEnabled = false; 134 135 private String LINKING_ENABLED = "CommonNavigator.LINKING_ENABLED"; 137 private LinkHelperService linkService; 138 139 142 public CommonNavigator() { 143 super(); 144 } 145 146 155 public void createPartControl(Composite aParent) { 156 157 commonViewer = createCommonViewer(aParent); 158 159 try { 160 commonViewer.getControl().setRedraw(false); 161 162 INavigatorFilterService filterService = commonViewer 163 .getNavigatorContentService().getFilterService(); 164 ViewerFilter[] visibleFilters = filterService.getVisibleFilters(true); 165 for (int i = 0; i < visibleFilters.length; i++) { 166 commonViewer.addFilter(visibleFilters[i]); 167 } 168 169 commonViewer.setSorter(new CommonViewerSorter()); 170 171 175 commonViewer.setInput(getInitialInput()); 176 177 getSite().setSelectionProvider(commonViewer); 178 179 updateTitle(); 180 } finally { 181 commonViewer.getControl().setRedraw(true); 182 } 183 184 189 commonManager = createCommonManager(); 190 if (memento != null) { 191 commonViewer.getNavigatorContentService().restoreState(memento); 192 } 193 194 commonActionGroup = createCommonActionGroup(); 195 commonActionGroup.fillActionBars(getViewSite().getActionBars()); 196 197 ISaveablesLifecycleListener saveablesLifecycleListener = new ISaveablesLifecycleListener() { 198 ISaveablesLifecycleListener siteSaveablesLifecycleListener = (ISaveablesLifecycleListener) getSite() 199 .getService(ISaveablesLifecycleListener.class); 200 201 public void handleLifecycleEvent(SaveablesLifecycleEvent event) { 202 if (event.getEventType() == SaveablesLifecycleEvent.DIRTY_CHANGED) { 203 firePropertyChange(PROP_DIRTY); 204 } 205 siteSaveablesLifecycleListener.handleLifecycleEvent(event); 206 } 207 }; 208 commonViewer.getNavigatorContentService() 209 .getSaveablesService().init(this, getCommonViewer(), 210 saveablesLifecycleListener); 211 212 commonViewer.addSelectionChangedListener(new ISelectionChangedListener() { 213 214 public void selectionChanged(SelectionChangedEvent event) { 215 firePropertyChange(PROP_DIRTY); 216 }}); 217 218 PlatformUI.getWorkbench().getHelpSystem().setHelp(commonViewer.getControl(), HELP_CONTEXT); 219 } 220 221 233 public void dispose() { 234 if (commonManager != null) { 235 commonManager.dispose(); 236 } 237 if(commonActionGroup != null) { 238 commonActionGroup.dispose(); 239 } 240 super.dispose(); 241 } 242 243 252 public void init(IViewSite aSite, IMemento aMemento) 253 throws PartInitException { 254 super.init(aSite, aMemento); 255 memento = aMemento; 256 if (memento != null) { 257 Integer linkingEnabledInteger = memento.getInteger(LINKING_ENABLED); 258 setLinkingEnabled(((linkingEnabledInteger != null) ? linkingEnabledInteger 259 .intValue() == 1 260 : false)); 261 } 262 263 } 264 265 274 public void saveState(IMemento aMemento) { 275 aMemento.putInteger(LINKING_ENABLED, (isLinkingEnabled) ? 1 : 0); 276 super.saveState(aMemento); 277 commonManager.saveState(aMemento); 278 commonViewer.getNavigatorContentService().saveState(aMemento); 279 } 280 281 288 public void setFocus() { 289 if (commonViewer != null) { 290 commonViewer.getTree().setFocus(); 291 } 292 } 293 294 303 public void selectReveal(ISelection selection) { 304 if (commonViewer != null) { 305 if(selection instanceof IStructuredSelection) { 306 Object [] newSelection = ((IStructuredSelection)selection).toArray(); 307 Object [] expandedElements = commonViewer.getExpandedElements(); 308 Object [] newExpandedElements = new Object [newSelection.length + expandedElements.length]; 309 System.arraycopy(expandedElements, 0, newExpandedElements, 0, expandedElements.length); 310 System.arraycopy(newSelection, 0, newExpandedElements, expandedElements.length, newSelection.length); 311 commonViewer.setExpandedElements(newExpandedElements); 312 } 313 commonViewer.setSelection(selection, true); 314 } 315 } 316 317 328 public final void setLinkingEnabled(boolean toEnableLinking) { 329 isLinkingEnabled = toEnableLinking; 330 firePropertyChange(IS_LINKING_ENABLED_PROPERTY); 331 } 332 333 337 public final boolean isLinkingEnabled() { 338 return isLinkingEnabled; 339 } 340 341 351 public CommonViewer getCommonViewer() { 352 return commonViewer; 353 } 354 355 359 public INavigatorContentService getNavigatorContentService() { 360 return getCommonViewer().getNavigatorContentService(); 361 } 362 363 373 public Object getAdapter(Class adapter) { 374 if (adapter == COMMON_VIEWER_CLASS) { 375 return getCommonViewer(); 376 } else if (adapter == INAVIGATOR_CONTENT_SERVICE) { 377 return getCommonViewer().getNavigatorContentService(); 378 } else if ( adapter == ISHOW_IN_TARGET_CLASS) { 379 return this; 380 } 381 return super.getAdapter(adapter); 382 } 383 384 388 public NavigatorActionService getNavigatorActionService() { 389 return commonManager.getNavigatorActionService(); 390 } 391 392 404 protected CommonViewer createCommonViewer(Composite aParent) { 405 CommonViewer aViewer = new CommonViewer(getViewSite().getId(), aParent, 406 SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); 407 initListeners(aViewer); 408 aViewer.getNavigatorContentService().restoreState(memento); 409 return aViewer; 410 } 411 412 421 protected void initListeners(TreeViewer viewer) { 422 423 viewer.addDoubleClickListener(new IDoubleClickListener() { 424 425 public void doubleClick(DoubleClickEvent event) { 426 try { 427 handleDoubleClick(event); 428 } catch (RuntimeException re) { 429 re.printStackTrace(); 430 } 431 } 432 }); 433 } 434 435 444 protected void handleDoubleClick(DoubleClickEvent anEvent) { 445 446 IAction openHandler = getViewSite().getActionBars().getGlobalActionHandler(ICommonActionConstants.OPEN); 447 448 if(openHandler == null) { 449 IStructuredSelection selection = (IStructuredSelection) anEvent 450 .getSelection(); 451 Object element = selection.getFirstElement(); 452 453 TreeViewer viewer = getCommonViewer(); 454 if (viewer.isExpandable(element)) { 455 viewer.setExpandedState(element, !viewer.getExpandedState(element)); 456 } 457 } 458 } 459 460 470 protected CommonNavigatorManager createCommonManager() { 471 return new CommonNavigatorManager(this, memento); 472 } 473 474 504 protected ActionGroup createCommonActionGroup() { 505 return new CommonNavigatorActionGroup(this, commonViewer, getLinkHelperService()); 506 } 507 508 512 protected IAdaptable getInitialInput() { 513 return getSite().getPage().getInput(); 514 } 515 516 522 protected void updateTitle() { 523 524 if (commonViewer == null) { 525 return; 526 } 527 528 Object input = commonViewer.getInput(); 529 String viewName = getConfigurationElement().getAttribute("name"); 532 if (input == null) { 533 setPartName(viewName); 534 setTitleToolTip(""); } else { 536 String inputToolTip = getFrameToolTipText(input); 537 538 setPartName(viewName); 539 setTitleToolTip(inputToolTip); 540 } 541 } 542 543 549 protected String getFrameToolTipText(Object anElement) { 550 if (commonViewer != null) { 551 return ((ILabelProvider) commonViewer.getLabelProvider()) 552 .getText(anElement); 553 } 554 return ""; } 556 557 560 public Saveable[] getSaveables() { 561 return getNavigatorContentService().getSaveablesService().getSaveables(); 562 } 563 564 567 public Saveable[] getActiveSaveables() { 568 return getNavigatorContentService().getSaveablesService().getActiveSaveables(); 569 } 570 571 574 public void doSave(IProgressMonitor monitor) { 575 } 579 580 583 public void doSaveAs() { 584 } 586 587 590 public boolean isDirty() { 591 Saveable[] saveables = getSaveables(); 592 for (int i = 0; i < saveables.length; i++) { 593 if(saveables[i].isDirty()) { 594 return true; 595 } 596 } 597 return false; 598 } 599 600 603 public boolean isSaveAsAllowed() { 604 return false; 605 } 606 607 610 public boolean isSaveOnCloseNeeded() { 611 return isDirty(); 612 } 613 614 617 public boolean show(ShowInContext context) { 618 IStructuredSelection selection = getSelection(context); 619 if (selection != null && !selection.isEmpty()) { 620 selectReveal(selection); 621 return true; 622 } 623 return false; 624 } 625 626 private IStructuredSelection getSelection(ShowInContext context) { 627 if (context == null) 628 return StructuredSelection.EMPTY; 629 ISelection selection = context.getSelection(); 630 if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) 631 return (IStructuredSelection)selection; 632 Object input = context.getInput(); 633 if (input instanceof IEditorInput) { 634 LinkHelperService lhs = getLinkHelperService(); 635 return lhs.getSelectionFor((IEditorInput) input); 636 } 637 if (input != null) { 638 return new StructuredSelection(input); 639 } 640 return StructuredSelection.EMPTY; 641 } 642 643 private synchronized LinkHelperService getLinkHelperService() { 644 if (linkService == null) 645 linkService = new LinkHelperService((NavigatorContentService)getCommonViewer().getNavigatorContentService()); 646 return linkService; 647 } 648 649 } 650 | Popular Tags |