KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > ShowActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.ui.actions;
12
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17
18 import org.eclipse.ui.IActionBars;
19 import org.eclipse.ui.IViewPart;
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.eclipse.ui.actions.ActionGroup;
22 import org.eclipse.ui.part.Page;
23
24 import org.eclipse.jdt.ui.IContextMenuConstants;
25
26 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
27 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
28
29 /**
30  * Action group that adds the show actions to a context menu and
31  * the action bar's navigate menu.
32  *
33  * <p>
34  * This class may be instantiated; it is not intended to be subclassed.
35  * </p>
36  * @since 2.0
37  */

38 public class ShowActionGroup extends ActionGroup {
39
40     private boolean fIsPackageExplorer;
41
42     private IWorkbenchSite fSite;
43     private ShowInPackageViewAction fShowInPackagesViewAction;
44
45     /**
46      * Creates a new <code>ShowActionGroup</code>. The action requires
47      * that the selection provided by the page's selection provider is of type
48      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
49      *
50      * @param page the page that owns this action group
51      */

52     public ShowActionGroup(Page page) {
53         this(page.getSite());
54     }
55     
56     /**
57      * Creates a new <code>ShowActionGroup</code>. The action requires
58      * that the selection provided by the part's selection provider is of type
59      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
60      *
61      * @param part the view part that owns this action group
62      */

63     public ShowActionGroup(IViewPart part) {
64         this(part.getSite());
65         fIsPackageExplorer= part instanceof PackageExplorerPart;
66     }
67     
68     /**
69      * Note: This constructor is for internal use only. Clients should not call this constructor.
70      * @param part the Java editor
71      */

72     public ShowActionGroup(JavaEditor part) {
73         fShowInPackagesViewAction= new ShowInPackageViewAction(part);
74         fShowInPackagesViewAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SHOW_IN_PACKAGE_VIEW);
75         part.setAction("ShowInPackageView", fShowInPackagesViewAction); //$NON-NLS-1$
76

77         initialize(part.getSite(), true);
78     }
79
80     private ShowActionGroup(IWorkbenchSite site) {
81         fShowInPackagesViewAction= new ShowInPackageViewAction(site);
82         fShowInPackagesViewAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SHOW_IN_PACKAGE_VIEW);
83         
84         initialize(site , false);
85     }
86
87     private void initialize(IWorkbenchSite site, boolean isJavaEditor) {
88         fSite= site;
89         ISelectionProvider provider= fSite.getSelectionProvider();
90         ISelection selection= provider.getSelection();
91         fShowInPackagesViewAction.update(selection);
92         if (!isJavaEditor) {
93             provider.addSelectionChangedListener(fShowInPackagesViewAction);
94         }
95     }
96
97     /* (non-Javadoc)
98      * Method declared in ActionGroup
99      */

100     public void fillActionBars(IActionBars actionBar) {
101         super.fillActionBars(actionBar);
102         setGlobalActionHandlers(actionBar);
103     }
104     
105     /* (non-Javadoc)
106      * Method declared in ActionGroup
107      */

108     public void fillContextMenu(IMenuManager menu) {
109         super.fillContextMenu(menu);
110         if (!fIsPackageExplorer) {
111             appendToGroup(menu, fShowInPackagesViewAction);
112         }
113     }
114     
115     /*
116      * @see ActionGroup#dispose()
117      */

118     public void dispose() {
119         ISelectionProvider provider= fSite.getSelectionProvider();
120         provider.removeSelectionChangedListener(fShowInPackagesViewAction);
121         super.dispose();
122     }
123     
124     private void setGlobalActionHandlers(IActionBars actionBar) {
125         if (!fIsPackageExplorer)
126             actionBar.setGlobalActionHandler(JdtActionConstants.SHOW_IN_PACKAGE_VIEW, fShowInPackagesViewAction);
127     }
128     
129     private void appendToGroup(IMenuManager menu, IAction action) {
130         if (action.isEnabled())
131             menu.appendToGroup(IContextMenuConstants.GROUP_SHOW, action);
132     }
133 }
134
Popular Tags