removed action for line delimiter conversion; this is provided by IDE now
[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 
35  * context menu 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         private boolean fIsEditorOwner;
47         private PHPOpenDeclarationAction fOpen;
48
49         /**
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>.
53          * 
54          * @param part the view part that owns this action group
55          */
56         public OpenEditorActionGroup(IViewPart part) {
57                 fSite= part.getSite();
58                 fOpen= new PHPOpenDeclarationAction(fSite);
59                 fOpen.setActionDefinitionId(PHPEditorActionDefinitionIds.OPEN_EDITOR);
60                 initialize(fSite.getSelectionProvider());
61         }
62         
63         /**
64          * Note: This constructor is for internal use only. Clients should not call this constructor.
65          */
66         public OpenEditorActionGroup(PHPEditor part) {
67                 fIsEditorOwner= true;
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());
73         }
74
75         /**
76          * Returns the open action managed by this action group. 
77          * 
78          * @return the open action. Returns <code>null</code> if the group
79          *      doesn't provide any open action
80          */
81         public IAction getOpenAction() {
82                 return fOpen;
83         }
84
85         private void initialize(ISelectionProvider provider) {
86                 ISelection selection= provider.getSelection();
87                 fOpen.update(selection);
88                 if (!fIsEditorOwner) {
89                         provider.addSelectionChangedListener(fOpen);
90                 }
91         }
92
93         /* (non-Javadoc)
94          * Method declared in ActionGroup
95          */
96         public void fillActionBars(IActionBars actionBar) {
97                 super.fillActionBars(actionBar);
98                 setGlobalActionHandlers(actionBar);
99         }
100         
101         /* (non-Javadoc)
102          * Method declared in ActionGroup
103          */
104         public void fillContextMenu(IMenuManager menu) {
105                 super.fillContextMenu(menu);
106                 appendToGroup(menu, fOpen);
107                 if (!fIsEditorOwner) {
108                         addOpenWithMenu(menu);
109                 }
110         }
111
112         /*
113          * @see ActionGroup#dispose()
114          */
115         public void dispose() {
116                 ISelectionProvider provider= fSite.getSelectionProvider();
117                 provider.removeSelectionChangedListener(fOpen);
118                 super.dispose();
119         }
120         
121         private void setGlobalActionHandlers(IActionBars actionBars) {
122                 actionBars.setGlobalActionHandler(PHPdtActionConstants.OPEN, fOpen);
123         }
124         
125         private void appendToGroup(IMenuManager menu, IAction action) {
126                 if (action.isEnabled())
127                         menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
128         }
129         
130         private void addOpenWithMenu(IMenuManager menu) {
131                 ISelection selection= getContext().getSelection();
132                 if (selection.isEmpty() || !(selection instanceof IStructuredSelection))
133                         return;
134                 IStructuredSelection ss= (IStructuredSelection)selection;
135                 if (ss.size() != 1)
136                         return;
137
138                 Object o= ss.getFirstElement();
139                 if (!(o instanceof IAdaptable))
140                         return;
141
142                 IAdaptable element= (IAdaptable)o;
143                 Object resource= element.getAdapter(IResource.class);
144                 if (!(resource instanceof IFile))
145                         return; 
146
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));
150
151                 // Add the submenu.
152                 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
153         }
154 }