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 context menu
35 * 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;
47 private boolean fIsEditorOwner;
49 private PHPOpenDeclarationAction fOpen;
52 * Creates a new <code>OpenActionGroup</code>. The group requires that
53 * the selection provided by the part's selection provider is of type <code>
54 * org.eclipse.jface.viewers.IStructuredSelection</code>.
57 * the view part that owns this action group
59 // public OpenEditorActionGroup(IViewPart part) {
60 // fSite = part.getSite();
61 // fOpen = new PHPOpenDeclarationAction(fSite);
62 // fOpen.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_EDITOR);
63 // initialize(fSite.getSelectionProvider());
67 * Note: This constructor is for internal use only. Clients should not call
70 public OpenEditorActionGroup(PHPEditor part) {
71 fIsEditorOwner = true;
72 fOpen = new PHPOpenDeclarationAction(part);
73 fOpen.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_EDITOR);
74 part.setAction("OpenEditor", fOpen); //$NON-NLS-1$
75 fSite = part.getEditorSite();
76 initialize(fSite.getSelectionProvider());
80 * Returns the open action managed by this action group.
82 * @return the open action. Returns <code>null</code> if the group doesn't
83 * provide any open action
85 // public IAction getOpenAction() {
89 private void initialize(ISelectionProvider provider) {
90 ISelection selection = provider.getSelection();
91 fOpen.update(selection);
92 if (!fIsEditorOwner) {
93 provider.addSelectionChangedListener(fOpen);
98 * (non-Javadoc) Method declared in ActionGroup
100 public void fillActionBars(IActionBars actionBar) {
101 super.fillActionBars(actionBar);
102 setGlobalActionHandlers(actionBar);
106 * (non-Javadoc) Method declared in ActionGroup
108 public void fillContextMenu(IMenuManager menu) {
109 super.fillContextMenu(menu);
110 appendToGroup(menu, fOpen);
111 if (!fIsEditorOwner) {
112 addOpenWithMenu(menu);
117 * @see ActionGroup#dispose()
119 public void dispose() {
120 ISelectionProvider provider = fSite.getSelectionProvider();
121 provider.removeSelectionChangedListener(fOpen);
125 private void setGlobalActionHandlers(IActionBars actionBars) {
126 actionBars.setGlobalActionHandler(PHPdtActionConstants.OPEN, fOpen);
129 private void appendToGroup(IMenuManager menu, IAction action) {
130 if (action.isEnabled())
131 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
134 private void addOpenWithMenu(IMenuManager menu) {
135 ISelection selection = getContext().getSelection();
136 if (selection.isEmpty() || !(selection instanceof IStructuredSelection))
138 IStructuredSelection ss = (IStructuredSelection) selection;
142 Object o = ss.getFirstElement();
143 if (!(o instanceof IAdaptable))
146 IAdaptable element = (IAdaptable) o;
147 Object resource = element.getAdapter(IResource.class);
148 if (!(resource instanceof IFile))
151 // Create a menu flyout.
152 IMenuManager submenu = new MenuManager(ActionMessages
153 .getString("OpenWithMenu.label")); //$NON-NLS-1$
154 submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource));
157 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);