1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
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;
20 import org.eclipse.ui.IActionBars;
21 import org.eclipse.ui.IViewPart;
22 import org.eclipse.ui.IWorkbenchCommandConstants;
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.actions.ActionFactory;
27 import org.eclipse.ui.actions.ActionGroup;
28 import org.eclipse.ui.actions.ContributionItemFactory;
29 import org.eclipse.ui.dialogs.PropertyDialogAction;
30 import org.eclipse.ui.keys.IBindingService;
31 import org.eclipse.ui.part.Page;
33 import org.eclipse.jdt.ui.IContextMenuConstants;
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;
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.
44 * This class may be instantiated; it is not intended to be subclassed.
49 * @noextend This class is not intended to be subclassed by clients.
51 public class OpenViewActionGroup extends ActionGroup {
53 private boolean fEditorIsOwner;
54 private boolean fIsTypeHiararchyViewerOwner;
55 private boolean fIsCallHiararchyViewerOwner;
57 private ISelectionProvider fSelectionProvider;
59 private OpenSuperImplementationAction fOpenSuperImplementation;
60 private OpenImplementationAction fOpenImplementation;
62 private OpenAttachedJavadocAction fOpenAttachedJavadoc;
63 private OpenTypeHierarchyAction fOpenTypeHierarchy;
64 private OpenCallHierarchyAction fOpenCallHierarchy;
65 private PropertyDialogAction fOpenPropertiesDialog;
67 private boolean fShowOpenPropertiesAction= true;
68 private boolean fShowShowInMenu= true;
71 * Creates a new <code>OpenActionGroup</code>. The group requires
72 * that the selection provided by the page's selection provider is
73 * of type {@link IStructuredSelection}.
75 * @param page the page that owns this action group
77 public OpenViewActionGroup(Page page) {
78 createSiteActions(page.getSite(), null);
82 * Creates a new <code>OpenActionGroup</code>. The group requires
83 * that the selection provided by the given selection provider is
84 * of type {@link IStructuredSelection}.
86 * @param page the page that owns this action group
87 * @param selectionProvider the selection provider used instead of the
88 * page selection provider.
92 public OpenViewActionGroup(Page page, ISelectionProvider selectionProvider) {
93 createSiteActions(page.getSite(), selectionProvider);
97 * Creates a new <code>OpenActionGroup</code>. The group requires
98 * that the selection provided by the part's selection provider is
99 * of type {@link IStructuredSelection}.
101 * @param part the view part that owns this action group
103 public OpenViewActionGroup(IViewPart part) {
108 * Creates a new <code>OpenActionGroup</code>. The group requires
109 * that the selection provided by the given selection provider is of type
110 * {@link IStructuredSelection}.
112 * @param part the view part that owns this action group
113 * @param selectionProvider the selection provider used instead of the
114 * page selection provider.
118 public OpenViewActionGroup(IViewPart part, ISelectionProvider selectionProvider) {
119 createSiteActions(part.getSite(), selectionProvider);
120 // we do a name check here to avoid class loading.
121 String partName= part.getClass().getName();
122 fIsTypeHiararchyViewerOwner= "org.eclipse.jdt.internal.ui.typehierarchy.TypeHierarchyViewPart".equals(partName); //$NON-NLS-1$
123 fIsCallHiararchyViewerOwner= "org.eclipse.jdt.internal.ui.callhierarchy.CallHierarchyViewPart".equals(partName); //$NON-NLS-1$
127 * Creates a new <code>OpenActionGroup</code>. The group requires
128 * that the selection provided by the given selection provider is of type
129 * {@link IStructuredSelection}.
131 * @param site the site that will own the action group.
132 * @param selectionProvider the selection provider used instead of the
133 * page selection provider.
137 public OpenViewActionGroup(IWorkbenchSite site, ISelectionProvider selectionProvider) {
138 createSiteActions(site, selectionProvider);
142 * Note: This constructor is for internal use only. Clients should not call this constructor.
143 * @param part the editor part
145 * @noreference This constructor is not intended to be referenced by clients.
147 public OpenViewActionGroup(JavaEditor part) {
148 fEditorIsOwner= true;
149 fShowShowInMenu= false;
151 fOpenImplementation= new OpenImplementationAction(part);
152 fOpenImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_IMPLEMENTATION);
153 part.setAction("OpenImplementation", fOpenImplementation); //$NON-NLS-1$
155 fOpenSuperImplementation= new OpenSuperImplementationAction(part);
156 fOpenSuperImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_SUPER_IMPLEMENTATION);
157 part.setAction("OpenSuperImplementation", fOpenSuperImplementation); //$NON-NLS-1$
159 fOpenAttachedJavadoc= new OpenAttachedJavadocAction(part);
160 fOpenAttachedJavadoc.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_ATTACHED_JAVADOC);
161 part.setAction("OpenAttachedJavadoc", fOpenAttachedJavadoc); //$NON-NLS-1$
163 fOpenTypeHierarchy= new OpenTypeHierarchyAction(part);
164 fOpenTypeHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY);
165 part.setAction("OpenTypeHierarchy", fOpenTypeHierarchy); //$NON-NLS-1$
167 fOpenCallHierarchy= new OpenCallHierarchyAction(part);
168 fOpenCallHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_CALL_HIERARCHY);
169 part.setAction("OpenCallHierarchy", fOpenCallHierarchy); //$NON-NLS-1$
171 initialize(part.getEditorSite().getSelectionProvider());
175 * Specifies if this action group also contains the 'Properties' action ({@link PropertyDialogAction}).
176 * By default, the action is contained in the group.
178 * @param enable If set, the 'Properties' action is part of this action group
181 public void containsOpenPropertiesAction(boolean enable) {
182 fShowOpenPropertiesAction= enable;
186 * Specifies if this action group also contains the 'Show In' menu (See {@link ContributionItemFactory#VIEWS_SHOW_IN}).
187 * By default, the action is contained in the group except for editors.
189 * @param enable If set, the 'Show In' menu is part of this action group
192 public void containsShowInMenu(boolean enable) {
193 fShowShowInMenu= enable;
196 private void createSiteActions(IWorkbenchSite site, ISelectionProvider specialProvider) {
197 fOpenImplementation= new OpenImplementationAction(site);
198 fOpenImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_IMPLEMENTATION);
199 fOpenImplementation.setSpecialSelectionProvider(specialProvider);
201 fOpenSuperImplementation= new OpenSuperImplementationAction(site);
202 fOpenSuperImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_SUPER_IMPLEMENTATION);
203 fOpenSuperImplementation.setSpecialSelectionProvider(specialProvider);
205 fOpenAttachedJavadoc= new OpenAttachedJavadocAction(site);
206 fOpenAttachedJavadoc.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_ATTACHED_JAVADOC);
207 fOpenAttachedJavadoc.setSpecialSelectionProvider(specialProvider);
209 fOpenTypeHierarchy= new OpenTypeHierarchyAction(site);
210 fOpenTypeHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY);
211 fOpenTypeHierarchy.setSpecialSelectionProvider(specialProvider);
213 fOpenCallHierarchy= new OpenCallHierarchyAction(site);
214 fOpenCallHierarchy.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_CALL_HIERARCHY);
215 fOpenCallHierarchy.setSpecialSelectionProvider(specialProvider);
217 ISelectionProvider provider= specialProvider != null ? specialProvider : site.getSelectionProvider();
219 fOpenPropertiesDialog= new PropertyDialogAction(site, provider);
220 fOpenPropertiesDialog.setActionDefinitionId(IWorkbenchCommandConstants.FILE_PROPERTIES);
222 initialize(provider);
225 private void initialize(ISelectionProvider provider) {
226 fSelectionProvider= provider;
227 ISelection selection= provider.getSelection();
228 fOpenImplementation.update(selection);
229 fOpenSuperImplementation.update(selection);
230 fOpenAttachedJavadoc.update(selection);
231 fOpenTypeHierarchy.update(selection);
232 fOpenCallHierarchy.update(selection);
233 if (!fEditorIsOwner) {
234 if (fShowOpenPropertiesAction) {
235 if (selection instanceof IStructuredSelection) {
236 fOpenPropertiesDialog.selectionChanged((IStructuredSelection) selection);
238 fOpenPropertiesDialog.selectionChanged(selection);
241 provider.addSelectionChangedListener(fOpenImplementation);
242 provider.addSelectionChangedListener(fOpenSuperImplementation);
243 provider.addSelectionChangedListener(fOpenAttachedJavadoc);
244 provider.addSelectionChangedListener(fOpenTypeHierarchy);
245 provider.addSelectionChangedListener(fOpenCallHierarchy);
246 // no need to register the open properties dialog action since it registers itself
251 * Method declared in ActionGroup
254 public void fillActionBars(IActionBars actionBar) {
255 super.fillActionBars(actionBar);
256 setGlobalActionHandlers(actionBar);
260 * Method declared in ActionGroup
263 public void fillContextMenu(IMenuManager menu) {
264 super.fillContextMenu(menu);
265 if (!fIsTypeHiararchyViewerOwner)
266 appendToGroup(menu, fOpenTypeHierarchy);
267 if (!fIsCallHiararchyViewerOwner)
268 appendToGroup(menu, fOpenCallHierarchy);
270 if (fShowShowInMenu) {
271 MenuManager showInSubMenu= new MenuManager(getShowInMenuLabel());
272 IWorkbenchWindow workbenchWindow= fOpenSuperImplementation.getSite().getWorkbenchWindow();
273 showInSubMenu.add(ContributionItemFactory.VIEWS_SHOW_IN.create(workbenchWindow));
274 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, showInSubMenu);
277 IStructuredSelection selection= getStructuredSelection();
278 if (fShowOpenPropertiesAction && selection != null && fOpenPropertiesDialog.isApplicableForSelection())
279 menu.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, fOpenPropertiesDialog);
282 private String getShowInMenuLabel() {
283 String keyBinding= null;
285 IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
286 if (bindingService != null)
287 keyBinding= bindingService.getBestActiveBindingFormattedFor(IWorkbenchCommandConstants.NAVIGATE_SHOW_IN_QUICK_MENU);
289 if (keyBinding == null)
290 keyBinding= ""; //$NON-NLS-1$
292 return ActionMessages.OpenViewActionGroup_showInAction_label + '\t' + keyBinding;
296 * @see ActionGroup#dispose()
299 public void dispose() {
300 fSelectionProvider.removeSelectionChangedListener(fOpenImplementation);
301 fSelectionProvider.removeSelectionChangedListener(fOpenSuperImplementation);
302 fSelectionProvider.removeSelectionChangedListener(fOpenAttachedJavadoc);
303 fSelectionProvider.removeSelectionChangedListener(fOpenTypeHierarchy);
304 fSelectionProvider.removeSelectionChangedListener(fOpenCallHierarchy);
308 private void setGlobalActionHandlers(IActionBars actionBars) {
309 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_IMPLEMENTATION, fOpenImplementation);
310 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_SUPER_IMPLEMENTATION, fOpenSuperImplementation);
311 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_ATTACHED_JAVA_DOC, fOpenAttachedJavadoc);
312 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_TYPE_HIERARCHY, fOpenTypeHierarchy);
313 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN_CALL_HIERARCHY, fOpenCallHierarchy);
315 if (!fEditorIsOwner && fShowOpenPropertiesAction)
316 actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), fOpenPropertiesDialog);
319 private void appendToGroup(IMenuManager menu, IAction action) {
320 if (action.isEnabled())
321 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
324 private IStructuredSelection getStructuredSelection() {
325 ISelection selection= getContext().getSelection();
326 if (selection instanceof IStructuredSelection)
327 return (IStructuredSelection)selection;