1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
13 import net.sourceforge.phpdt.internal.ui.actions.ActionMessages;
14 import net.sourceforge.phpdt.ui.IContextMenuConstants;
15 import net.sourceforge.phpeclipse.actions.PHPOpenDeclarationAction;
16 import net.sourceforge.phpeclipse.actions.PHPOpenDeclarationEditorAction;
17 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.MenuManager;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.ui.IActionBars;
29 import org.eclipse.ui.IViewPart;
30 import org.eclipse.ui.IWorkbenchSite;
31 import org.eclipse.ui.actions.ActionGroup;
32 import org.eclipse.ui.actions.OpenWithMenu;
35 * Action group that adds the actions opening a new editor to the
36 * context menu and the action bar's navigate menu.
39 * This class may be instantiated; it is not intended to be subclassed.
44 public class OpenEditorActionGroup extends ActionGroup {
46 private IWorkbenchSite fSite;
47 private boolean fIsEditorOwner;
48 private PHPOpenDeclarationAction fOpen;
51 * Creates a new <code>OpenActionGroup</code>. The group requires
52 * that the selection provided by the part's selection provider is of type <code>
53 * org.eclipse.jface.viewers.IStructuredSelection</code>.
55 * @param part the view part that owns this action group
57 public OpenEditorActionGroup(IViewPart part) {
58 fSite= part.getSite();
59 fOpen= new PHPOpenDeclarationAction(fSite);
60 fOpen.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_EDITOR);
61 initialize(fSite.getSelectionProvider());
65 * Note: This constructor is for internal use only. Clients should not call this constructor.
67 public OpenEditorActionGroup(PHPEditor part) {
69 fOpen= new PHPOpenDeclarationAction(part);
70 fOpen.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_EDITOR);
71 part.setAction("OpenEditor", fOpen); //$NON-NLS-1$
72 fSite= part.getEditorSite();
73 initialize(fSite.getSelectionProvider());
77 * Returns the open action managed by this action group.
79 * @return the open action. Returns <code>null</code> if the group
80 * doesn't provide any open action
82 public IAction getOpenAction() {
86 private void initialize(ISelectionProvider provider) {
87 ISelection selection= provider.getSelection();
88 fOpen.update(selection);
89 if (!fIsEditorOwner) {
90 provider.addSelectionChangedListener(fOpen);
95 * Method declared in ActionGroup
97 public void fillActionBars(IActionBars actionBar) {
98 super.fillActionBars(actionBar);
99 setGlobalActionHandlers(actionBar);
103 * Method declared in ActionGroup
105 public void fillContextMenu(IMenuManager menu) {
106 super.fillContextMenu(menu);
107 appendToGroup(menu, fOpen);
108 if (!fIsEditorOwner) {
109 addOpenWithMenu(menu);
114 * @see ActionGroup#dispose()
116 public void dispose() {
117 ISelectionProvider provider= fSite.getSelectionProvider();
118 provider.removeSelectionChangedListener(fOpen);
122 private void setGlobalActionHandlers(IActionBars actionBars) {
123 actionBars.setGlobalActionHandler(PHPdtActionConstants.OPEN, fOpen);
126 private void appendToGroup(IMenuManager menu, IAction action) {
127 if (action.isEnabled())
128 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
131 private void addOpenWithMenu(IMenuManager menu) {
132 ISelection selection= getContext().getSelection();
133 if (selection.isEmpty() || !(selection instanceof IStructuredSelection))
135 IStructuredSelection ss= (IStructuredSelection)selection;
139 Object o= ss.getFirstElement();
140 if (!(o instanceof IAdaptable))
143 IAdaptable element= (IAdaptable)o;
144 Object resource= element.getAdapter(IResource.class);
145 if (!(resource instanceof IFile))
148 // Create a menu flyout.
149 IMenuManager submenu= new MenuManager(ActionMessages.getString("OpenWithMenu.label")); //$NON-NLS-1$
150 submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource));
153 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);