1 11 package org.eclipse.ui.internal.navigator; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.ISafeRunnable; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.SafeRunner; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jface.action.IMenuListener; 19 import org.eclipse.jface.action.IMenuManager; 20 import org.eclipse.jface.action.IStatusLineManager; 21 import org.eclipse.jface.action.MenuManager; 22 import org.eclipse.jface.action.Separator; 23 import org.eclipse.jface.viewers.ILabelProvider; 24 import org.eclipse.jface.viewers.IOpenListener; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.ISelectionChangedListener; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.OpenEvent; 29 import org.eclipse.jface.viewers.SelectionChangedEvent; 30 import org.eclipse.jface.viewers.StructuredSelection; 31 import org.eclipse.jface.viewers.TreeViewer; 32 import org.eclipse.swt.graphics.Image; 33 import org.eclipse.swt.widgets.Menu; 34 import org.eclipse.ui.IMemento; 35 import org.eclipse.ui.IWorkbenchActionConstants; 36 import org.eclipse.ui.actions.ActionContext; 37 import org.eclipse.ui.actions.RetargetAction; 38 import org.eclipse.ui.navigator.CommonNavigator; 39 import org.eclipse.ui.navigator.CommonViewer; 40 import org.eclipse.ui.navigator.CommonViewerSiteFactory; 41 import org.eclipse.ui.navigator.ICommonActionConstants; 42 import org.eclipse.ui.navigator.ICommonViewerSite; 43 import org.eclipse.ui.navigator.IDescriptionProvider; 44 import org.eclipse.ui.navigator.INavigatorContentService; 45 import org.eclipse.ui.navigator.NavigatorActionService; 46 import org.eclipse.ui.progress.UIJob; 47 48 60 public final class CommonNavigatorManager implements ISelectionChangedListener { 61 62 private static final long DELAY = 200; 64 65 private final CommonNavigator commonNavigator; 66 67 private final INavigatorContentService contentService; 68 69 private NavigatorActionService actionService; 70 71 private final IDescriptionProvider commonDescriptionProvider; 72 73 private final IStatusLineManager statusLineManager; 74 75 private final ILabelProvider labelProvider; 76 77 private UpdateActionBarsJob updateActionBars; 78 79 private ISelectionChangedListener statusBarListener = new ISelectionChangedListener() { 80 81 public void selectionChanged(SelectionChangedEvent anEvent) { 82 updateStatusBar(anEvent.getSelection()); 83 } 84 85 }; 86 87 private class UpdateActionBarsJob extends UIJob { 88 public UpdateActionBarsJob(String label) { 89 super(label); 90 } 91 92 public IStatus runInUIThread(IProgressMonitor monitor) { 93 94 SafeRunner.run(new ISafeRunnable() { 95 100 public void run() throws Exception { 101 IStructuredSelection selection = new StructuredSelection(commonNavigator.getCommonViewer().getInput()); 102 actionService.setContext(new ActionContext(selection)); 103 actionService.fillActionBars(commonNavigator.getViewSite().getActionBars()); 104 } 105 110 public void handleException(Throwable exception) { 111 NavigatorPlugin.logError(0, exception.getMessage(), exception); 112 } 113 }); 114 return Status.OK_STATUS; 115 } 116 } 117 118 128 public CommonNavigatorManager(CommonNavigator aNavigator) { 129 this(aNavigator, null); 130 } 131 132 142 public CommonNavigatorManager(CommonNavigator aNavigator, IMemento aMemento) { 143 super(); 144 commonNavigator = aNavigator; 145 contentService = commonNavigator.getNavigatorContentService(); 146 statusLineManager = commonNavigator.getViewSite().getActionBars() 147 .getStatusLineManager(); 148 commonDescriptionProvider = contentService 149 .createCommonDescriptionProvider(); 150 labelProvider = (ILabelProvider) commonNavigator.getCommonViewer() 151 .getLabelProvider(); 152 153 init(aMemento); 154 } 155 156 157 private void init(IMemento memento) { 158 159 updateActionBars = new UpdateActionBarsJob(commonNavigator.getTitle()); 160 161 CommonViewer commonViewer = commonNavigator.getCommonViewer(); 162 commonViewer.addSelectionChangedListener(this); 163 commonViewer.addPostSelectionChangedListener(statusBarListener); 164 updateStatusBar(commonViewer.getSelection()); 165 166 ICommonViewerSite commonViewerSite = CommonViewerSiteFactory 167 .createCommonViewerSite(commonNavigator.getViewSite()); 168 actionService = new NavigatorActionService(commonViewerSite, 169 commonViewer, commonViewer.getNavigatorContentService()); 170 171 final RetargetAction openAction = new RetargetAction( 172 ICommonActionConstants.OPEN, 173 CommonNavigatorMessages.Open_action_label); 174 commonNavigator.getViewSite().getPage().addPartListener(openAction); 175 openAction.setActionDefinitionId(ICommonActionConstants.OPEN); 176 177 commonNavigator.getCommonViewer().addOpenListener(new IOpenListener() { 178 public void open(OpenEvent event) { 179 actionService.setContext(new ActionContext(commonNavigator.getCommonViewer().getSelection())); 180 actionService.fillActionBars(commonNavigator.getViewSite().getActionBars()); 181 openAction.run(); 182 } 183 }); 184 185 if(memento != null) 186 restoreState(memento); 187 188 initContextMenu(); 189 initViewMenu(); 190 191 } 192 193 198 public void dispose() { 199 commonNavigator.getCommonViewer().removeSelectionChangedListener(this); 200 commonNavigator.getCommonViewer().removeSelectionChangedListener(statusBarListener); 201 actionService.dispose(); 202 } 203 204 212 public void selectionChanged(SelectionChangedEvent anEvent) { 213 if (anEvent.getSelection() instanceof IStructuredSelection) { 214 IStructuredSelection structuredSelection = (IStructuredSelection) anEvent 215 .getSelection(); 216 actionService.setContext(new ActionContext(structuredSelection)); 217 actionService.fillActionBars(commonNavigator.getViewSite() 218 .getActionBars()); 219 } 220 } 221 222 227 public void restoreState(IMemento aMemento) { 228 actionService.restoreState(aMemento); 229 230 } 231 232 237 public void saveState(IMemento aMemento) { 238 actionService.saveState(aMemento); 239 } 240 241 252 protected void fillContextMenu(IMenuManager aMenuManager) { 253 ISelection selection = commonNavigator.getCommonViewer().getSelection(); 254 actionService.setContext(new ActionContext(selection)); 255 actionService.fillContextMenu(aMenuManager); 256 } 257 258 263 protected void initContextMenu() { 264 MenuManager menuMgr = new MenuManager(contentService 265 .getViewerDescriptor().getPopupMenuId()); 266 menuMgr.setRemoveAllWhenShown(true); 267 menuMgr.addMenuListener(new IMenuListener() { 268 269 public void menuAboutToShow(IMenuManager manager) { 270 fillContextMenu(manager); 271 } 272 }); 273 TreeViewer commonViewer = commonNavigator.getCommonViewer(); 274 Menu menu = menuMgr.createContextMenu(commonViewer.getTree()); 275 276 commonViewer.getTree().setMenu(menu); 277 278 actionService.prepareMenuForPlatformContributions(menuMgr, 279 commonViewer, false); 280 281 } 282 283 protected void initViewMenu() { 284 IMenuManager viewMenu = commonNavigator.getViewSite().getActionBars() 285 .getMenuManager(); 286 viewMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 287 viewMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS 288 + "-end")); 290 updateActionBars.schedule(DELAY); 291 292 } 293 294 298 protected void updateStatusBar(ISelection aSelection) { 299 300 Image img = null; 301 if (aSelection != null && !aSelection.isEmpty() 302 && aSelection instanceof IStructuredSelection) { 303 img = labelProvider.getImage(((IStructuredSelection) aSelection) 304 .getFirstElement()); 305 } 306 307 statusLineManager.setMessage(img, commonDescriptionProvider 308 .getDescription(aSelection)); 309 } 310 311 315 public NavigatorActionService getNavigatorActionService() { 316 return actionService; 317 } 318 319 } 320 | Popular Tags |