KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.ui.actions;
12
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.action.MenuManager;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionProvider;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19
20 import org.eclipse.ui.IActionBars;
21 import org.eclipse.ui.IViewPart;
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.actions.ActionFactory;
26 import org.eclipse.ui.actions.ActionGroup;
27 import org.eclipse.ui.actions.ContributionItemFactory;
28 import org.eclipse.ui.dialogs.PropertyDialogAction;
29 import org.eclipse.ui.keys.IBindingService;
30 import org.eclipse.ui.part.Page;
31 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
32
33 import org.eclipse.jdt.ui.IContextMenuConstants;
34
35 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
36 import org.eclipse.jdt.internal.ui.callhierarchy.OpenCallHierarchyAction;
37 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
38
39 /**
40  * Action group that adds actions to open a new JDT view part or an external
41  * viewer to a context menu and the global menu bar.
42  *
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  *
47  * @since 2.0
48  */

49 public class OpenViewActionGroup extends ActionGroup {
50
51     private boolean fEditorIsOwner;
52     private boolean fIsTypeHiararchyViewerOwner;
53     private boolean fIsCallHiararchyViewerOwner;
54     
55     private ISelectionProvider fSelectionProvider;
56
57     private OpenSuperImplementationAction fOpenSuperImplementation;
58     private OpenExternalJavadocAction fOpenExternalJavadoc;
59     private OpenTypeHierarchyAction fOpenTypeHierarchy;
60     private OpenCallHierarchyAction fOpenCallHierarchy;
61     private PropertyDialogAction fOpenPropertiesDialog;
62     
63     private boolean fShowOpenPropertiesAction= true;
64     private boolean fShowShowInMenu= true;
65     
66     /**
67      * Creates a new <code>OpenActionGroup</code>. The group requires
68      * that the selection provided by the page's selection provider is
69      * of type {@link IStructuredSelection}.
70      *
71      * @param page the page that owns this action group
72      */

73     public OpenViewActionGroup(Page page) {
74         createSiteActions(page.getSite(), null);
75     }
76     
77     /**
78      * Creates a new <code>OpenActionGroup</code>. The group requires
79      * that the selection provided by the given selection provider is
80      * of type {@link IStructuredSelection}.
81      *
82      * @param page the page that owns this action group
83      * @param selectionProvider the selection provider used instead of the
84      * page selection provider.
85      *
86      * @since 3.2
87      */

88     public OpenViewActionGroup(Page page, ISelectionProvider selectionProvider) {
89         createSiteActions(page.getSite(), selectionProvider);
90     }
91     
92     /**
93      * Creates a new <code>OpenActionGroup</code>. The group requires
94      * that the selection provided by the part's selection provider is
95      * of type {@link IStructuredSelection}.
96      *
97      * @param part the view part that owns this action group
98      */

99     public OpenViewActionGroup(IViewPart part) {
100         this(part, null);
101     }
102     
103     /**
104      * Creates a new <code>OpenActionGroup</code>. The group requires
105      * that the selection provided by the given selection provider is of type
106      * {@link IStructuredSelection}.
107      *
108      * @param part the view part that owns this action group
109      * @param selectionProvider the selection provider used instead of the
110      * page selection provider.
111      *
112      * @since 3.2
113      */

114     public OpenViewActionGroup(IViewPart part, ISelectionProvider selectionProvider) {
115         createSiteActions(part.getSite(), selectionProvider);
116         // we do a name check here to avoid class loading.
117
String JavaDoc partName= part.getClass().getName();
118         fIsTypeHiararchyViewerOwner= "org.eclipse.jdt.internal.ui.typehierarchy.TypeHierarchyViewPart".equals(partName); //$NON-NLS-1$
119
fIsCallHiararchyViewerOwner= "org.eclipse.jdt.internal.ui.callhierarchy.CallHierarchyViewPart".equals(partName); //$NON-NLS-1$
120
}
121     
122     /**
123      * Creates a new <code>OpenActionGroup</code>. The group requires
124      * that the selection provided by the given selection provider is of type
125      * {@link IStructuredSelection}.
126      *
127      * @param site the site that will own the action group.
128      * @param selectionProvider the selection provider used instead of the
129      * page selection provider.
130      *
131      * @since 3.2
132      */

133     public OpenViewActionGroup(IWorkbenchSite site, ISelectionProvider selectionProvider) {
134         createSiteActions(site, selectionProvider);
135     }
136     
137     /**
138      * Note: This constructor is for internal use only. Clients should not call this constructor.
139      * @param part the editor part
140      */

141     public OpenViewActionGroup(JavaEditor part) {
142         fEditorIsOwner= true;
143         fShowShowInMenu= false;
144
145         fOpenSuperImplementation= new OpenSuperImplementationAction(part);
146         fOpenSuperImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_SUPER_IMPLEMENTATION);
147         part.setAction("OpenSuperImplementation", fOpenSuperImplementation); //$NON-NLS-1$
148

149         fOpenExternalJavadoc= new OpenExternalJavadocAction(part);
150         fOpenExternalJavadoc.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EXTERNAL_JAVADOC);
151         part.setAction("OpenExternalJavadoc", fOpenExternalJavadoc); //$NON-NLS-1$
152

153         fOpenTypeHierarchy= new OpenTypeHierarchyAction(part);
154         fOpenTypeHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY);
155         part.setAction("OpenTypeHierarchy", fOpenTypeHierarchy); //$NON-NLS-1$
156

157         fOpenCallHierarchy= new OpenCallHierarchyAction(part);
158         fOpenCallHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_CALL_HIERARCHY);
159         part.setAction("OpenCallHierarchy", fOpenCallHierarchy); //$NON-NLS-1$
160

161         initialize(part.getEditorSite().getSelectionProvider());
162     }
163     
164     /**
165      * Specifies if this action group also contains the 'Properties' action ({@link PropertyDialogAction}).
166      * By default, the action is contained in the group.
167      *
168      * @param enable If set, the 'Properties' action is part of this action group
169      * @since 3.3
170      */

171     public void containsOpenPropertiesAction(boolean enable) {
172         fShowOpenPropertiesAction= enable;
173     }
174     
175     /**
176      * Specifies if this action group also contains the 'Show In' menu (See {@link ContributionItemFactory#VIEWS_SHOW_IN}).
177      * By default, the action is contained in the group except for editors.
178      *
179      * @param enable If set, the 'Show In' menu is part of this action group
180      * @since 3.3
181      */

182     public void containsShowInMenu(boolean enable) {
183         fShowShowInMenu= enable;
184     }
185     
186     private void createSiteActions(IWorkbenchSite site, ISelectionProvider specialProvider) {
187         fOpenSuperImplementation= new OpenSuperImplementationAction(site);
188         fOpenSuperImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_SUPER_IMPLEMENTATION);
189         fOpenSuperImplementation.setSpecialSelectionProvider(specialProvider);
190         
191         fOpenExternalJavadoc= new OpenExternalJavadocAction(site);
192         fOpenExternalJavadoc.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EXTERNAL_JAVADOC);
193         fOpenExternalJavadoc.setSpecialSelectionProvider(specialProvider);
194
195         fOpenTypeHierarchy= new OpenTypeHierarchyAction(site);
196         fOpenTypeHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY);
197         fOpenTypeHierarchy.setSpecialSelectionProvider(specialProvider);
198
199         fOpenCallHierarchy= new OpenCallHierarchyAction(site);
200         fOpenCallHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_CALL_HIERARCHY);
201         fOpenCallHierarchy.setSpecialSelectionProvider(specialProvider);
202
203         ISelectionProvider provider= specialProvider != null ? specialProvider : site.getSelectionProvider();
204         
205         fOpenPropertiesDialog= new PropertyDialogAction(site, provider);
206         fOpenPropertiesDialog.setActionDefinitionId(IWorkbenchActionDefinitionIds.PROPERTIES);
207         
208         initialize(provider);
209     }
210
211     private void initialize(ISelectionProvider provider) {
212         fSelectionProvider= provider;
213         ISelection selection= provider.getSelection();
214         fOpenSuperImplementation.update(selection);
215         fOpenExternalJavadoc.update(selection);
216         fOpenTypeHierarchy.update(selection);
217         fOpenCallHierarchy.update(selection);
218         if (!fEditorIsOwner) {
219             if (fShowOpenPropertiesAction) {
220                 if (selection instanceof IStructuredSelection) {
221                     fOpenPropertiesDialog.selectionChanged((IStructuredSelection) selection);
222                 } else {
223                     fOpenPropertiesDialog.selectionChanged(selection);
224                 }
225             }
226             provider.addSelectionChangedListener(fOpenSuperImplementation);
227             provider.addSelectionChangedListener(fOpenExternalJavadoc);
228             provider.addSelectionChangedListener(fOpenTypeHierarchy);
229             provider.addSelectionChangedListener(fOpenCallHierarchy);
230             // no need to register the open properties dialog action since it registers itself
231
}
232     }
233
234     /* (non-Javadoc)
235      * Method declared in ActionGroup
236      */

237     public void fillActionBars(IActionBars actionBar) {
238         super.fillActionBars(actionBar);
239         setGlobalActionHandlers(actionBar);
240     }
241     
242     /* (non-Javadoc)
243      * Method declared in ActionGroup
244      */

245     public void fillContextMenu(IMenuManager menu) {
246         super.fillContextMenu(menu);
247         if (!fIsTypeHiararchyViewerOwner)
248             appendToGroup(menu, fOpenTypeHierarchy);
249         if (!fIsCallHiararchyViewerOwner)
250             appendToGroup(menu, fOpenCallHierarchy);
251         
252         if (fShowShowInMenu) {
253             MenuManager showInSubMenu= new MenuManager(getShowInMenuLabel());
254             IWorkbenchWindow workbenchWindow= fOpenSuperImplementation.getSite().getWorkbenchWindow();
255             showInSubMenu.add(ContributionItemFactory.VIEWS_SHOW_IN.create(workbenchWindow));
256             menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, showInSubMenu);
257         }
258
259         IStructuredSelection selection= getStructuredSelection();
260         if (fShowOpenPropertiesAction && selection != null)
261             menu.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, fOpenPropertiesDialog);
262     }
263     
264     private String JavaDoc getShowInMenuLabel() {
265         String JavaDoc keyBinding= null;
266         
267         IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
268         if (bindingService != null)
269             keyBinding= bindingService.getBestActiveBindingFormattedFor("org.eclipse.ui.navigate.showInQuickMenu"); //$NON-NLS-1$
270

271         if (keyBinding == null)
272             keyBinding= ""; //$NON-NLS-1$
273

274         return ActionMessages.OpenViewActionGroup_showInAction_label + '\t' + keyBinding;
275     }
276
277     /*
278      * @see ActionGroup#dispose()
279      */

280     public void dispose() {
281         fSelectionProvider.removeSelectionChangedListener(fOpenSuperImplementation);
282         fSelectionProvider.removeSelectionChangedListener(fOpenExternalJavadoc);
283         fSelectionProvider.removeSelectionChangedListener(fOpenTypeHierarchy);
284         fSelectionProvider.removeSelectionChangedListener(fOpenCallHierarchy);
285         super.dispose();
286     }
287     
288     private void setGlobalActionHandlers(IActionBars actionBars) {
289         actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_SUPER_IMPLEMENTATION, fOpenSuperImplementation);
290         actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_EXTERNAL_JAVA_DOC, fOpenExternalJavadoc);
291         actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_TYPE_HIERARCHY, fOpenTypeHierarchy);
292         actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_CALL_HIERARCHY, fOpenCallHierarchy);
293         
294         if (!fEditorIsOwner && fShowOpenPropertiesAction)
295             actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), fOpenPropertiesDialog);
296     }
297     
298     private void appendToGroup(IMenuManager menu, IAction action) {
299         if (action.isEnabled())
300             menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
301     }
302     
303     private IStructuredSelection getStructuredSelection() {
304         ISelection selection= getContext().getSelection();
305         if (selection instanceof IStructuredSelection)
306             return (IStructuredSelection)selection;
307         return null;
308     }
309     
310
311     
312     
313 }
314
Popular Tags