001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.navigator;
011:
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.core.runtime.ISafeRunnable;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.SafeRunner;
016: import org.eclipse.core.runtime.Status;
017: import org.eclipse.jface.action.IMenuListener;
018: import org.eclipse.jface.action.IMenuManager;
019: import org.eclipse.jface.action.IStatusLineManager;
020: import org.eclipse.jface.action.MenuManager;
021: import org.eclipse.jface.action.Separator;
022: import org.eclipse.jface.viewers.ILabelProvider;
023: import org.eclipse.jface.viewers.IOpenListener;
024: import org.eclipse.jface.viewers.ISelection;
025: import org.eclipse.jface.viewers.ISelectionChangedListener;
026: import org.eclipse.jface.viewers.IStructuredSelection;
027: import org.eclipse.jface.viewers.OpenEvent;
028: import org.eclipse.jface.viewers.SelectionChangedEvent;
029: import org.eclipse.jface.viewers.StructuredSelection;
030: import org.eclipse.jface.viewers.TreeViewer;
031: import org.eclipse.swt.graphics.Image;
032: import org.eclipse.swt.widgets.Menu;
033: import org.eclipse.ui.IMemento;
034: import org.eclipse.ui.IWorkbenchActionConstants;
035: import org.eclipse.ui.actions.ActionContext;
036: import org.eclipse.ui.actions.RetargetAction;
037: import org.eclipse.ui.navigator.CommonNavigator;
038: import org.eclipse.ui.navigator.CommonViewer;
039: import org.eclipse.ui.navigator.CommonViewerSiteFactory;
040: import org.eclipse.ui.navigator.ICommonActionConstants;
041: import org.eclipse.ui.navigator.ICommonViewerSite;
042: import org.eclipse.ui.navigator.IDescriptionProvider;
043: import org.eclipse.ui.navigator.INavigatorContentService;
044: import org.eclipse.ui.navigator.NavigatorActionService;
045: import org.eclipse.ui.progress.UIJob;
046:
047: /**
048: * <p>
049: * Manages the non-viewer responsibilities of the Common Navigator View Part,
050: * including the display and population of the context menu and the registration
051: * of extensions for opening content.
052: * </p>
053: * <p>
054: * This class is not intended to be instantiated or subclassed by clients
055: * </p>
056: *
057: * @since 3.2
058: */
059: public final class CommonNavigatorManager implements
060: ISelectionChangedListener {
061:
062: // delay for updating the action bars (in ms)
063: private static final long DELAY = 200;
064:
065: private final CommonNavigator commonNavigator;
066:
067: private final INavigatorContentService contentService;
068:
069: private NavigatorActionService actionService;
070:
071: private final IDescriptionProvider commonDescriptionProvider;
072:
073: private final IStatusLineManager statusLineManager;
074:
075: private final ILabelProvider labelProvider;
076:
077: private UpdateActionBarsJob updateActionBars;
078:
079: private ISelectionChangedListener statusBarListener = new ISelectionChangedListener() {
080:
081: public void selectionChanged(SelectionChangedEvent anEvent) {
082: updateStatusBar(anEvent.getSelection());
083: }
084:
085: };
086:
087: private class UpdateActionBarsJob extends UIJob {
088: public UpdateActionBarsJob(String label) {
089: super (label);
090: }
091:
092: public IStatus runInUIThread(IProgressMonitor monitor) {
093:
094: SafeRunner.run(new ISafeRunnable() {
095: /*
096: * (non-Javadoc)
097: *
098: * @see org.eclipse.core.runtime.ISafeRunnable#run()
099: */
100: public void run() throws Exception {
101: if (commonNavigator.getCommonViewer().getInput() != null) {
102: IStructuredSelection selection = new StructuredSelection(
103: commonNavigator.getCommonViewer()
104: .getInput());
105: actionService.setContext(new ActionContext(
106: selection));
107: actionService.fillActionBars(commonNavigator
108: .getViewSite().getActionBars());
109: }
110: }
111:
112: /*
113: * (non-Javadoc)
114: *
115: * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
116: */
117: public void handleException(Throwable exception) {
118: NavigatorPlugin.logError(0, exception.getMessage(),
119: exception);
120: }
121: });
122: return Status.OK_STATUS;
123: }
124: }
125:
126: /**
127: * <p>
128: * Adds listeners to aNavigator to listen for selection changes and respond
129: * to mouse events.
130: * </p>
131: *
132: * @param aNavigator
133: * The CommonNavigator managed by this class. Requires a non-null
134: * value.
135: */
136: public CommonNavigatorManager(CommonNavigator aNavigator) {
137: this (aNavigator, null);
138: }
139:
140: /**
141: * <p>
142: * Adds listeners to aNavigator to listen for selection changes and respond
143: * to mouse events.
144: * </p>
145: *
146: * @param aNavigator
147: * The CommonNavigator managed by this class. Requires a non-null
148: * value.
149: */
150: public CommonNavigatorManager(CommonNavigator aNavigator,
151: IMemento aMemento) {
152: super ();
153: commonNavigator = aNavigator;
154: contentService = commonNavigator.getNavigatorContentService();
155: statusLineManager = commonNavigator.getViewSite()
156: .getActionBars().getStatusLineManager();
157: commonDescriptionProvider = contentService
158: .createCommonDescriptionProvider();
159: labelProvider = (ILabelProvider) commonNavigator
160: .getCommonViewer().getLabelProvider();
161:
162: init(aMemento);
163: }
164:
165: private void init(IMemento memento) {
166:
167: updateActionBars = new UpdateActionBarsJob(commonNavigator
168: .getTitle());
169:
170: CommonViewer commonViewer = commonNavigator.getCommonViewer();
171: commonViewer.addSelectionChangedListener(this );
172: commonViewer.addPostSelectionChangedListener(statusBarListener);
173: updateStatusBar(commonViewer.getSelection());
174:
175: ICommonViewerSite commonViewerSite = CommonViewerSiteFactory
176: .createCommonViewerSite(commonNavigator.getViewSite());
177: actionService = new NavigatorActionService(commonViewerSite,
178: commonViewer, commonViewer.getNavigatorContentService());
179:
180: final RetargetAction openAction = new RetargetAction(
181: ICommonActionConstants.OPEN,
182: CommonNavigatorMessages.Open_action_label);
183: commonNavigator.getViewSite().getPage().addPartListener(
184: openAction);
185: openAction.setActionDefinitionId(ICommonActionConstants.OPEN);
186:
187: commonNavigator.getCommonViewer().addOpenListener(
188: new IOpenListener() {
189: public void open(OpenEvent event) {
190: actionService.setContext(new ActionContext(
191: commonNavigator.getCommonViewer()
192: .getSelection()));
193: actionService.fillActionBars(commonNavigator
194: .getViewSite().getActionBars());
195: openAction.run();
196: }
197: });
198:
199: if (memento != null)
200: restoreState(memento);
201:
202: initContextMenu();
203: initViewMenu();
204:
205: }
206:
207: /**
208: * <p>
209: * Called by {@link CommonNavigator} when the View Part is disposed.
210: *
211: */
212: public void dispose() {
213: commonNavigator.getCommonViewer()
214: .removeSelectionChangedListener(this );
215: commonNavigator.getCommonViewer()
216: .removeSelectionChangedListener(statusBarListener);
217: actionService.dispose();
218: }
219:
220: /**
221: *
222: * @param anEvent
223: * An event indicating the current selection of the
224: * {@link CommonViewer}
225: *
226: * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
227: */
228: public void selectionChanged(SelectionChangedEvent anEvent) {
229: if (anEvent.getSelection() instanceof IStructuredSelection) {
230: IStructuredSelection structuredSelection = (IStructuredSelection) anEvent
231: .getSelection();
232: actionService.setContext(new ActionContext(
233: structuredSelection));
234: actionService.fillActionBars(commonNavigator.getViewSite()
235: .getActionBars());
236: }
237: }
238:
239: /**
240: * @param aMemento
241: * Used to restore state of action extensions via the
242: * {@link NavigatorActionService}.
243: */
244: public void restoreState(IMemento aMemento) {
245: actionService.restoreState(aMemento);
246:
247: }
248:
249: /**
250: * @param aMemento
251: * Used to save state of action extensions via the
252: * {@link NavigatorActionService}.
253: */
254: public void saveState(IMemento aMemento) {
255: actionService.saveState(aMemento);
256: }
257:
258: /**
259: * <p>
260: * Fills aMenuManager with menu contributions from the
261: * {@link NavigatorActionService}.
262: * </p>
263: *
264: * @param aMenuManager
265: * A popup menu
266: * @see NavigatorActionService#fillContextMenu(IMenuManager)
267: *
268: */
269: protected void fillContextMenu(IMenuManager aMenuManager) {
270: ISelection selection = commonNavigator.getCommonViewer()
271: .getSelection();
272: actionService.setContext(new ActionContext(selection));
273: actionService.fillContextMenu(aMenuManager);
274: }
275:
276: /**
277: * <p>
278: * Initializes and registers the context menu.
279: * </p>
280: */
281: protected void initContextMenu() {
282: MenuManager menuMgr = new MenuManager(contentService
283: .getViewerDescriptor().getPopupMenuId());
284: menuMgr.setRemoveAllWhenShown(true);
285: menuMgr.addMenuListener(new IMenuListener() {
286:
287: public void menuAboutToShow(IMenuManager manager) {
288: fillContextMenu(manager);
289: }
290: });
291: TreeViewer commonViewer = commonNavigator.getCommonViewer();
292: Menu menu = menuMgr.createContextMenu(commonViewer.getTree());
293:
294: commonViewer.getTree().setMenu(menu);
295:
296: actionService.prepareMenuForPlatformContributions(menuMgr,
297: commonViewer, false);
298:
299: }
300:
301: protected void initViewMenu() {
302: IMenuManager viewMenu = commonNavigator.getViewSite()
303: .getActionBars().getMenuManager();
304: viewMenu.add(new Separator(
305: IWorkbenchActionConstants.MB_ADDITIONS));
306: viewMenu.add(new Separator(
307: IWorkbenchActionConstants.MB_ADDITIONS + "-end"));//$NON-NLS-1$
308:
309: updateActionBars.schedule(DELAY);
310:
311: }
312:
313: /**
314: * @param aSelection
315: * The current selection from the {@link CommonViewer}
316: */
317: protected void updateStatusBar(ISelection aSelection) {
318:
319: Image img = null;
320: if (aSelection != null && !aSelection.isEmpty()
321: && aSelection instanceof IStructuredSelection) {
322: img = labelProvider
323: .getImage(((IStructuredSelection) aSelection)
324: .getFirstElement());
325: }
326:
327: statusLineManager.setMessage(img, commonDescriptionProvider
328: .getDescription(aSelection));
329: }
330:
331: /**
332: *
333: * @return The action service used by this manager
334: */
335: public NavigatorActionService getNavigatorActionService() {
336: return actionService;
337: }
338:
339: }
|