KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > OpenEditorActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui.actions;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.IAdaptable;
16
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.IWorkbenchSite;
27 import org.eclipse.ui.actions.ActionGroup;
28 import org.eclipse.ui.actions.OpenWithMenu;
29
30 import org.eclipse.jdt.ui.IContextMenuConstants;
31
32 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
34
35 /**
36  * Action group that adds the actions opening a new editor to the
37  * context menu and the action bar's navigate menu.
38  *
39  * <p>
40  * This class may be instantiated; it is not intended to be subclassed.
41  * </p>
42  *
43  * @since 2.0
44  */

45 public class OpenEditorActionGroup extends ActionGroup {
46
47     private IWorkbenchSite fSite;
48     private boolean fIsEditorOwner;
49     private OpenAction fOpen;
50
51     /**
52      * Creates a new <code>OpenActionGroup</code>. The group requires
53      * that the selection provided by the part's selection provider is of type <code>
54      * org.eclipse.jface.viewers.IStructuredSelection</code>.
55      *
56      * @param part the view part that owns this action group
57      */

58     public OpenEditorActionGroup(IViewPart part) {
59         fSite= part.getSite();
60         fOpen= new OpenAction(fSite);
61         fOpen.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
62         initialize(fSite.getSelectionProvider());
63     }
64     
65     /**
66      * Note: This constructor is for internal use only. Clients should not call this constructor.
67      * @param editor the Java editor
68      */

69     public OpenEditorActionGroup(JavaEditor editor) {
70         fIsEditorOwner= true;
71         fOpen= new OpenAction(editor);
72         fOpen.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
73         editor.setAction("OpenEditor", fOpen); //$NON-NLS-1$
74
fSite= editor.getEditorSite();
75         initialize(fSite.getSelectionProvider());
76     }
77
78     /**
79      * Returns the open action managed by this action group.
80      *
81      * @return the open action. Returns <code>null</code> if the group
82      * doesn't provide any open action
83      */

84     public IAction getOpenAction() {
85         return fOpen;
86     }
87
88     private void initialize(ISelectionProvider provider) {
89         ISelection selection= provider.getSelection();
90         fOpen.update(selection);
91         if (!fIsEditorOwner) {
92             provider.addSelectionChangedListener(fOpen);
93         }
94     }
95
96     /* (non-Javadoc)
97      * Method declared in ActionGroup
98      */

99     public void fillActionBars(IActionBars actionBar) {
100         super.fillActionBars(actionBar);
101         setGlobalActionHandlers(actionBar);
102     }
103     
104     /* (non-Javadoc)
105      * Method declared in ActionGroup
106      */

107     public void fillContextMenu(IMenuManager menu) {
108         super.fillContextMenu(menu);
109         appendToGroup(menu, fOpen);
110         if (!fIsEditorOwner) {
111             addOpenWithMenu(menu);
112         }
113     }
114
115     /*
116      * @see ActionGroup#dispose()
117      */

118     public void dispose() {
119         ISelectionProvider provider= fSite.getSelectionProvider();
120         provider.removeSelectionChangedListener(fOpen);
121         super.dispose();
122     }
123     
124     private void setGlobalActionHandlers(IActionBars actionBars) {
125         actionBars.setGlobalActionHandler(JdtActionConstants.OPEN, fOpen);
126     }
127     
128     private void appendToGroup(IMenuManager menu, IAction action) {
129         if (action.isEnabled())
130             menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
131     }
132     
133     private void addOpenWithMenu(IMenuManager menu) {
134         ISelection selection= getContext().getSelection();
135         if (selection.isEmpty() || !(selection instanceof IStructuredSelection))
136             return;
137         IStructuredSelection ss= (IStructuredSelection)selection;
138         if (ss.size() != 1)
139             return;
140
141         Object JavaDoc o= ss.getFirstElement();
142         if (!(o instanceof IAdaptable))
143             return;
144
145         IAdaptable element= (IAdaptable)o;
146         Object JavaDoc resource= element.getAdapter(IResource.class);
147         if (!(resource instanceof IFile))
148             return;
149
150         // Create a menu.
151
IMenuManager submenu= new MenuManager(ActionMessages.OpenWithMenu_label);
152         submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource));
153
154         // Add the submenu.
155
menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
156     }
157 }
158
Popular Tags