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.phpeditor.PHPEditor;
 
  18 import org.eclipse.core.resources.IFile;
 
  19 import org.eclipse.core.resources.IResource;
 
  20 import org.eclipse.core.runtime.IAdaptable;
 
  21 import org.eclipse.jface.action.IAction;
 
  22 import org.eclipse.jface.action.IMenuManager;
 
  23 import org.eclipse.jface.action.MenuManager;
 
  24 import org.eclipse.jface.viewers.ISelection;
 
  25 import org.eclipse.jface.viewers.ISelectionProvider;
 
  26 import org.eclipse.jface.viewers.IStructuredSelection;
 
  27 import org.eclipse.ui.IActionBars;
 
  28 import org.eclipse.ui.IViewPart;
 
  29 import org.eclipse.ui.IWorkbenchSite;
 
  30 import org.eclipse.ui.actions.ActionGroup;
 
  31 import org.eclipse.ui.actions.OpenWithMenu;
 
  34  * Action group that adds the actions opening a new editor to the 
 
  35  * context menu and the action bar's navigate menu.
 
  38  * This class may be instantiated; it is not intended to be subclassed.
 
  43 public class OpenEditorActionGroup extends ActionGroup {
 
  45         private IWorkbenchSite fSite;
 
  46         private boolean fIsEditorOwner;
 
  47         private PHPOpenDeclarationAction fOpen;
 
  50          * Creates a new <code>OpenActionGroup</code>. The group requires
 
  51          * that the selection provided by the part's selection provider is of type <code>
 
  52          * org.eclipse.jface.viewers.IStructuredSelection</code>.
 
  54          * @param part the view part that owns this action group
 
  56         public OpenEditorActionGroup(IViewPart part) {
 
  57                 fSite= part.getSite();
 
  58                 fOpen= new PHPOpenDeclarationAction(fSite);
 
  59                 fOpen.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_EDITOR);
 
  60                 initialize(fSite.getSelectionProvider());
 
  64          * Note: This constructor is for internal use only. Clients should not call this constructor.
 
  66         public OpenEditorActionGroup(PHPEditor part) {
 
  68                 fOpen= new PHPOpenDeclarationAction(part);
 
  69                 fOpen.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_EDITOR);
 
  70                 part.setAction("OpenEditor", fOpen); //$NON-NLS-1$
 
  71                 fSite= part.getEditorSite();
 
  72                 initialize(fSite.getSelectionProvider());
 
  76          * Returns the open action managed by this action group. 
 
  78          * @return the open action. Returns <code>null</code> if the group
 
  79          *      doesn't provide any open action
 
  81         public IAction getOpenAction() {
 
  85         private void initialize(ISelectionProvider provider) {
 
  86                 ISelection selection= provider.getSelection();
 
  87                 fOpen.update(selection);
 
  88                 if (!fIsEditorOwner) {
 
  89                         provider.addSelectionChangedListener(fOpen);
 
  94          * Method declared in ActionGroup
 
  96         public void fillActionBars(IActionBars actionBar) {
 
  97                 super.fillActionBars(actionBar);
 
  98                 setGlobalActionHandlers(actionBar);
 
 102          * Method declared in ActionGroup
 
 104         public void fillContextMenu(IMenuManager menu) {
 
 105                 super.fillContextMenu(menu);
 
 106                 appendToGroup(menu, fOpen);
 
 107                 if (!fIsEditorOwner) {
 
 108                         addOpenWithMenu(menu);
 
 113          * @see ActionGroup#dispose()
 
 115         public void dispose() {
 
 116                 ISelectionProvider provider= fSite.getSelectionProvider();
 
 117                 provider.removeSelectionChangedListener(fOpen);
 
 121         private void setGlobalActionHandlers(IActionBars actionBars) {
 
 122                 actionBars.setGlobalActionHandler(PHPdtActionConstants.OPEN, fOpen);
 
 125         private void appendToGroup(IMenuManager menu, IAction action) {
 
 126                 if (action.isEnabled())
 
 127                         menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
 
 130         private void addOpenWithMenu(IMenuManager menu) {
 
 131                 ISelection selection= getContext().getSelection();
 
 132                 if (selection.isEmpty() || !(selection instanceof IStructuredSelection))
 
 134                 IStructuredSelection ss= (IStructuredSelection)selection;
 
 138                 Object o= ss.getFirstElement();
 
 139                 if (!(o instanceof IAdaptable))
 
 142                 IAdaptable element= (IAdaptable)o;
 
 143                 Object resource= element.getAdapter(IResource.class);
 
 144                 if (!(resource instanceof IFile))
 
 147                 // Create a menu flyout.
 
 148                 IMenuManager submenu= new MenuManager(ActionMessages.getString("OpenWithMenu.label")); //$NON-NLS-1$
 
 149                 submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource));
 
 152                 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);