KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > CommonNavigatorManager


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
49  * <p>
50  * Manages the non-viewer responsibilities of the Common Navigator View Part,
51  * including the display and population of the context menu and the registration
52  * of extensions for opening content.
53  * </p>
54  * <p>
55  * This class is not intended to be instantiated or subclassed by clients
56  * </p>
57  *
58  * @since 3.2
59  */

60 public final class CommonNavigatorManager implements ISelectionChangedListener {
61
62     // delay for updating the action bars (in ms)
63
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 JavaDoc label) {
89             super(label);
90         }
91           
92         public IStatus runInUIThread(IProgressMonitor monitor) {
93
94             SafeRunner.run(new ISafeRunnable() {
95                 /*
96                  * (non-Javadoc)
97                  *
98                  * @see org.eclipse.core.runtime.ISafeRunnable#run()
99                  */

100                 public void run() throws Exception JavaDoc {
101                     IStructuredSelection selection = new StructuredSelection(commonNavigator.getCommonViewer().getInput());
102                     actionService.setContext(new ActionContext(selection));
103                     actionService.fillActionBars(commonNavigator.getViewSite().getActionBars());
104                 }
105                 /*
106                  * (non-Javadoc)
107                  *
108                  * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
109                  */

110                 public void handleException(Throwable JavaDoc exception) {
111                     NavigatorPlugin.logError(0, exception.getMessage(), exception);
112                 }
113             });
114             return Status.OK_STATUS;
115         }
116     }
117
118     /**
119      * <p>
120      * Adds listeners to aNavigator to listen for selection changes and respond
121      * to mouse events.
122      * </p>
123      *
124      * @param aNavigator
125      * The CommonNavigator managed by this class. Requires a non-null
126      * value.
127      */

128     public CommonNavigatorManager(CommonNavigator aNavigator) {
129         this(aNavigator, null);
130     }
131     
132     /**
133      * <p>
134      * Adds listeners to aNavigator to listen for selection changes and respond
135      * to mouse events.
136      * </p>
137      *
138      * @param aNavigator
139      * The CommonNavigator managed by this class. Requires a non-null
140      * value.
141      */

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     /**
194      * <p>
195      * Called by {@link CommonNavigator} when the View Part is disposed.
196      *
197      */

198     public void dispose() {
199         commonNavigator.getCommonViewer().removeSelectionChangedListener(this);
200         commonNavigator.getCommonViewer().removeSelectionChangedListener(statusBarListener);
201         actionService.dispose();
202     }
203
204     /**
205      *
206      * @param anEvent
207      * An event indicating the current selection of the
208      * {@link CommonViewer}
209      *
210      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
211      */

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     /**
223      * @param aMemento
224      * Used to restore state of action extensions via the
225      * {@link NavigatorActionService}.
226      */

227     public void restoreState(IMemento aMemento) {
228         actionService.restoreState(aMemento);
229          
230     }
231
232     /**
233      * @param aMemento
234      * Used to save state of action extensions via the
235      * {@link NavigatorActionService}.
236      */

237     public void saveState(IMemento aMemento) {
238         actionService.saveState(aMemento);
239     }
240
241     /**
242      * <p>
243      * Fills aMenuManager with menu contributions from the
244      * {@link NavigatorActionService}.
245      * </p>
246      *
247      * @param aMenuManager
248      * A popup menu
249      * @see NavigatorActionService#fillContextMenu(IMenuManager)
250      *
251      */

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     /**
259      * <p>
260      * Initializes and registers the context menu.
261      * </p>
262      */

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"));//$NON-NLS-1$
289

290         updateActionBars.schedule(DELAY);
291         
292     }
293
294     /**
295      * @param aSelection
296      * The current selection from the {@link CommonViewer}
297      */

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     /**
312      *
313      * @return The action service used by this manager
314      */

315     public NavigatorActionService getNavigatorActionService() {
316         return actionService;
317     }
318
319 }
320
Popular Tags