KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > editors > text > TextEditorActionContributor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.editors.text;
12
13 import org.eclipse.jface.action.ActionContributionItem;
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.action.IContributionItem;
16 import org.eclipse.jface.action.IMenuManager;
17
18 import org.eclipse.ui.IActionBars;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.IWorkbenchActionConstants;
21 import org.eclipse.ui.actions.ActionFactory;
22 import org.eclipse.ui.ide.IDEActionFactory;
23 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
24 import org.eclipse.ui.texteditor.ITextEditor;
25 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
26 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
27 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
28
29
30 /**
31  * Manages the installation and de-installation of global actions for the default text editor.
32  * <p>
33  * If instantiated and used as-is, this contributor connects the following global actions:
34  * <ul>
35  * <li>Add Bookmark</li>
36  * <li>Add Task</li>
37  * <li>Change Encoding</li>
38  * <li>Quick Assist</li>
39  * </ul>
40  *
41  * @since 2.0
42  */

43 public class TextEditorActionContributor extends BasicTextEditorActionContributor {
44
45     /**
46      * Change encoding action.
47      * @since 3.1
48      */

49     private RetargetTextEditorAction fChangeEncodingAction;
50     /**
51      * Quick assist assistant action.
52      * @since 3.3
53      */

54     private RetargetTextEditorAction fQuickAssistAction;
55     /**
56      * Quick assist menu contribution item.
57      * @since 3.3
58      */

59     private IContributionItem fQuickAssistMenuEntry;
60     
61     private RetargetTextEditorAction fRetargetShowInformationAction;
62
63     /**
64      * Creates a new contributor.
65      */

66     public TextEditorActionContributor() {
67         fChangeEncodingAction= new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ChangeEncodingAction."); //$NON-NLS-1$
68
fQuickAssistAction= new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.QuickAssist."); //$NON-NLS-1$
69
fQuickAssistAction.setActionDefinitionId(ITextEditorActionDefinitionIds.QUICK_ASSIST);
70         fQuickAssistMenuEntry= new ActionContributionItem(fQuickAssistAction);
71         
72         fRetargetShowInformationAction= new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ShowInformation."); //$NON-NLS-1$
73
fRetargetShowInformationAction.setActionDefinitionId(ITextEditorActionDefinitionIds.SHOW_INFORMATION);
74     }
75
76     /**
77      * Internally sets the active editor to the actions provided by this contributor.
78      * Cannot be overridden by subclasses.
79      *
80      * @param part the editor
81      */

82     private void doSetActiveEditor(final IEditorPart part) {
83
84         ITextEditor textEditor= null;
85         if (part instanceof ITextEditor)
86             textEditor= (ITextEditor) part;
87
88         /** The global actions to be connected with editor actions */
89         IActionBars actionBars= getActionBars();
90         
91         actionBars.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), getAction(textEditor, IDEActionFactory.ADD_TASK.getId()));
92         actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), getAction(textEditor, IDEActionFactory.BOOKMARK.getId()));
93         
94         IAction action= getAction(textEditor, ITextEditorActionConstants.NEXT);
95         actionBars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION, action);
96         actionBars.setGlobalActionHandler(ITextEditorActionConstants.NEXT, action);
97         action= getAction(textEditor, ITextEditorActionConstants.PREVIOUS);
98         actionBars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION, action);
99         actionBars.setGlobalActionHandler(ITextEditorActionConstants.PREVIOUS, action);
100
101         action= getAction(textEditor, ActionFactory.REFRESH.getId());
102         actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), action);
103
104         fChangeEncodingAction.setAction(getAction(textEditor, ITextEditorActionConstants.CHANGE_ENCODING));
105         
106         IAction quickAssistAction= getAction(textEditor, ITextEditorActionConstants.QUICK_ASSIST);
107         fQuickAssistAction.setAction(quickAssistAction);
108
109         if (textEditor == null)
110             return;
111         
112         // Update Quick Assist menu entry - for now don't show disabled entry
113
IMenuManager menuMgr= textEditor.getEditorSite().getActionBars().getMenuManager();
114         IMenuManager editMenu= menuMgr.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
115         if (editMenu != null) {
116             boolean isEnabled= quickAssistAction != null && quickAssistAction.isEnabled();
117             fQuickAssistMenuEntry.setVisible(isEnabled);
118             editMenu.update(true);
119         }
120         
121         fRetargetShowInformationAction.setAction(getAction(textEditor, ITextEditorActionConstants.SHOW_INFORMATION));
122     }
123     
124     /*
125      * @see org.eclipse.ui.texteditor.BasicTextEditorActionContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
126      * @since 3.3
127      */

128     public void contributeToMenu(IMenuManager menu) {
129         super.contributeToMenu(menu);
130         
131         IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
132         if (editMenu != null) {
133             editMenu.appendToGroup(ITextEditorActionConstants.GROUP_ASSIST, fQuickAssistMenuEntry);
134             fQuickAssistMenuEntry.setVisible(false);
135             editMenu.appendToGroup(ITextEditorActionConstants.GROUP_INFORMATION, fRetargetShowInformationAction);
136         }
137     }
138
139     /*
140      * @see IEditorActionBarContributor#setActiveEditor(org.eclipse.ui.IEditorPart)
141      */

142     public void setActiveEditor(IEditorPart part) {
143         super.setActiveEditor(part);
144         doSetActiveEditor(part);
145     }
146
147     /*
148      * @see IEditorActionBarContributor#dispose()
149      */

150     public void dispose() {
151         doSetActiveEditor(null);
152         super.dispose();
153     }
154
155     /*
156      * @see EditorActionBarContributor#init(org.eclipse.ui.IActionBars)
157      */

158     public void init(IActionBars bars) {
159         super.init(bars);
160
161         IMenuManager menuManager= bars.getMenuManager();
162         IMenuManager editMenu= menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
163         if (editMenu != null)
164             editMenu.add(fChangeEncodingAction);
165     }}
166
Popular Tags