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