fixed bug with some attributes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / actions / OpenEditorActionGroup.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
12
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;
18
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;
33
34 /**
35  * Action group that adds the actions opening a new editor to the 
36  * context menu and the action bar's navigate menu.
37  *
38  * <p>
39  * This class may be instantiated; it is not intended to be subclassed.
40  * </p>
41  * 
42  * @since 2.0
43  */
44 public class OpenEditorActionGroup extends ActionGroup {
45
46         private IWorkbenchSite fSite;
47         private boolean fIsEditorOwner;
48         private PHPOpenDeclarationAction fOpen;
49
50         /**
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>.
54          * 
55          * @param part the view part that owns this action group
56          */
57         public OpenEditorActionGroup(IViewPart part) {
58                 fSite= part.getSite();
59                 fOpen= new PHPOpenDeclarationAction(fSite);
60                 fOpen.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_EDITOR);
61                 initialize(fSite.getSelectionProvider());
62         }
63         
64         /**
65          * Note: This constructor is for internal use only. Clients should not call this constructor.
66          */
67         public OpenEditorActionGroup(PHPEditor part) {
68                 fIsEditorOwner= true;
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());
74         }
75
76         /**
77          * Returns the open action managed by this action group. 
78          * 
79          * @return the open action. Returns <code>null</code> if the group
80          *      doesn't provide any open action
81          */
82         public IAction getOpenAction() {
83                 return fOpen;
84         }
85
86         private void initialize(ISelectionProvider provider) {
87                 ISelection selection= provider.getSelection();
88                 fOpen.update(selection);
89                 if (!fIsEditorOwner) {
90                         provider.addSelectionChangedListener(fOpen);
91                 }
92         }
93
94         /* (non-Javadoc)
95          * Method declared in ActionGroup
96          */
97         public void fillActionBars(IActionBars actionBar) {
98                 super.fillActionBars(actionBar);
99                 setGlobalActionHandlers(actionBar);
100         }
101         
102         /* (non-Javadoc)
103          * Method declared in ActionGroup
104          */
105         public void fillContextMenu(IMenuManager menu) {
106                 super.fillContextMenu(menu);
107                 appendToGroup(menu, fOpen);
108                 if (!fIsEditorOwner) {
109                         addOpenWithMenu(menu);
110                 }
111         }
112
113         /*
114          * @see ActionGroup#dispose()
115          */
116         public void dispose() {
117                 ISelectionProvider provider= fSite.getSelectionProvider();
118                 provider.removeSelectionChangedListener(fOpen);
119                 super.dispose();
120         }
121         
122         private void setGlobalActionHandlers(IActionBars actionBars) {
123                 actionBars.setGlobalActionHandler(PHPdtActionConstants.OPEN, fOpen);
124         }
125         
126         private void appendToGroup(IMenuManager menu, IAction action) {
127                 if (action.isEnabled())
128                         menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
129         }
130         
131         private void addOpenWithMenu(IMenuManager menu) {
132                 ISelection selection= getContext().getSelection();
133                 if (selection.isEmpty() || !(selection instanceof IStructuredSelection))
134                         return;
135                 IStructuredSelection ss= (IStructuredSelection)selection;
136                 if (ss.size() != 1)
137                         return;
138
139                 Object o= ss.getFirstElement();
140                 if (!(o instanceof IAdaptable))
141                         return;
142
143                 IAdaptable element= (IAdaptable)o;
144                 Object resource= element.getAdapter(IResource.class);
145                 if (!(resource instanceof IFile))
146                         return; 
147
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));
151
152                 // Add the submenu.
153                 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
154         }
155 }