KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > BasicTextEditorActionContributor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.ui.texteditor;
13
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IContributionItem;
23 import org.eclipse.jface.action.IContributionManager;
24 import org.eclipse.jface.action.IMenuManager;
25 import org.eclipse.jface.action.IStatusLineManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.IWorkbenchActionConstants;
30 import org.eclipse.ui.part.EditorActionBarContributor;
31
32
33
34 /**
35  * Manages the installation and removal of global actions for
36  * the same type of editors.
37  * <p>
38  * If instantiated and used as-is, this contributor connects to all of the workbench defined
39  * global editor actions the corresponding actions of the current editor. It also adds addition
40  * actions for searching and navigation (go to line) as well as a set of status fields.</p>
41  * <p>
42  * Subclasses may override the following methods:
43  * <ul>
44  * <li><code>contributeToMenu</code> - extend to contribute to menu</li>
45  * <li><code>contributeToToolBar</code> - reimplement to contribute to tool bar</li>
46  * <li><code>contributeToStatusLine</code> - reimplement to contribute to status line</li>
47  * <li><code>setActiveEditor</code> - extend to react to editor changes</li>
48  * </ul>
49  * </p>
50  * @see org.eclipse.ui.texteditor.ITextEditorActionConstants
51  */

52 public class BasicTextEditorActionContributor extends EditorActionBarContributor {
53
54
55     /** The global actions to be connected with editor actions */
56     private final static String JavaDoc[] ACTIONS= {
57         ITextEditorActionConstants.UNDO,
58         ITextEditorActionConstants.REDO,
59         ITextEditorActionConstants.CUT,
60         ITextEditorActionConstants.COPY,
61         ITextEditorActionConstants.PASTE,
62         ITextEditorActionConstants.DELETE,
63         ITextEditorActionConstants.SELECT_ALL,
64         ITextEditorActionConstants.FIND,
65         ITextEditorActionConstants.PRINT,
66         ITextEditorActionConstants.PROPERTIES,
67         ITextEditorActionConstants.REVERT
68     };
69
70     /**
71      * Status field definition.
72      * @since 3.0
73      */

74     private static class StatusFieldDef {
75
76         private String JavaDoc category;
77         private String JavaDoc actionId;
78         private boolean visible;
79         private int widthInChars;
80
81         private StatusFieldDef(String JavaDoc category, String JavaDoc actionId, boolean visible, int widthInChars) {
82             Assert.isNotNull(category);
83             this.category= category;
84             this.actionId= actionId;
85             this.visible= visible;
86             this.widthInChars= widthInChars;
87         }
88     }
89
90     /**
91      * The status fields to be set to the editor
92      * @since 3.0
93      */

94     private final static StatusFieldDef[] STATUS_FIELD_DEFS= {
95         new StatusFieldDef(ITextEditorActionConstants.STATUS_CATEGORY_FIND_FIELD, null, false, EditorMessages.Editor_FindIncremental_reverse_name.length() + 15),
96         new StatusFieldDef(ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE, null, true, StatusLineContributionItem.DEFAULT_WIDTH_IN_CHARS + 1),
97         new StatusFieldDef(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_MODE, ITextEditorActionDefinitionIds.TOGGLE_OVERWRITE, true, StatusLineContributionItem.DEFAULT_WIDTH_IN_CHARS),
98         new StatusFieldDef(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION, ITextEditorActionConstants.GOTO_LINE, true, StatusLineContributionItem.DEFAULT_WIDTH_IN_CHARS)
99     };
100
101     /**
102      * The active editor part.
103      */

104     private IEditorPart fActiveEditorPart;
105     /**
106      * The find next action.
107      * @since 2.0
108      */

109     private RetargetTextEditorAction fFindNext;
110     /**
111      * The find previous action.
112      * @since 2.0
113      */

114     private RetargetTextEditorAction fFindPrevious;
115     /**
116      * The incremental find action.
117      * @since 2.0
118      */

119     private RetargetTextEditorAction fIncrementalFind;
120     /**
121      * The reverse incremental find action.
122      * @since 2.1
123      */

124     private RetargetTextEditorAction fIncrementalFindReverse;
125     /**
126      * The go to line action.
127      */

128     private RetargetTextEditorAction fGotoLine;
129     /**
130      * The word completion action.
131      * @since 3.1
132      */

133     private RetargetTextEditorAction fHippieCompletion;
134     /**
135      * The map of status fields.
136      * @since 2.0
137      */

138     private Map JavaDoc fStatusFields;
139
140
141     /**
142      * Creates an empty editor action bar contributor. The action bars are
143      * furnished later via the <code>init</code> method.
144      *
145      * @see org.eclipse.ui.IEditorActionBarContributor#init(org.eclipse.ui.IActionBars, org.eclipse.ui.IWorkbenchPage)
146      */

147     public BasicTextEditorActionContributor() {
148
149         fFindNext= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindNext."); //$NON-NLS-1$
150
fFindNext.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_NEXT);
151         fFindPrevious= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindPrevious."); //$NON-NLS-1$
152
fFindPrevious.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_PREVIOUS);
153         fIncrementalFind= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindIncremental."); //$NON-NLS-1$
154
fIncrementalFind.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_INCREMENTAL);
155         fIncrementalFindReverse= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindIncrementalReverse."); //$NON-NLS-1$
156
fIncrementalFindReverse.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_INCREMENTAL_REVERSE);
157         fGotoLine= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.GotoLine."); //$NON-NLS-1$
158
fGotoLine.setActionDefinitionId(ITextEditorActionDefinitionIds.LINE_GOTO);
159         fHippieCompletion= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.HippieCompletion."); //$NON-NLS-1$
160
fHippieCompletion.setActionDefinitionId(ITextEditorActionDefinitionIds.HIPPIE_COMPLETION);
161
162         fStatusFields= new HashMap JavaDoc(3);
163         for (int i= 0; i < STATUS_FIELD_DEFS.length; i++) {
164             StatusFieldDef fieldDef= STATUS_FIELD_DEFS[i];
165             fStatusFields.put(fieldDef, new StatusLineContributionItem(fieldDef.category, fieldDef.visible, fieldDef.widthInChars));
166         }
167     }
168
169     /**
170      * Returns the active editor part.
171      *
172      * @return the active editor part
173      */

174     protected final IEditorPart getActiveEditorPart() {
175         return fActiveEditorPart;
176     }
177
178     /**
179      * Returns the action registered with the given text editor.
180      *
181      * @param editor the editor, or <code>null</code>
182      * @param actionId the action id
183      * @return the action, or <code>null</code> if none
184      */

185     protected final IAction getAction(ITextEditor editor, String JavaDoc actionId) {
186         return (editor == null || actionId == null ? null : editor.getAction(actionId));
187     }
188
189     /**
190      * The method installs the global action handlers for the given text editor.
191      * <p>
192      * This method cannot be overridden by subclasses.</p>
193      *
194      * @param part the active editor part
195      * @since 2.0
196      */

197     private void doSetActiveEditor(IEditorPart part) {
198
199         if (fActiveEditorPart == part)
200             return;
201
202         if (fActiveEditorPart instanceof ITextEditorExtension) {
203             ITextEditorExtension extension= (ITextEditorExtension) fActiveEditorPart;
204             for (int i= 0; i < STATUS_FIELD_DEFS.length; i++)
205                 extension.setStatusField(null, STATUS_FIELD_DEFS[i].category);
206         }
207
208         fActiveEditorPart= part;
209         ITextEditor editor= (part instanceof ITextEditor) ? (ITextEditor) part : null;
210
211         IActionBars actionBars= getActionBars();
212         for (int i= 0; i < ACTIONS.length; i++)
213             actionBars.setGlobalActionHandler(ACTIONS[i], getAction(editor, ACTIONS[i]));
214         actionBars.setGlobalActionHandler(ITextEditorActionDefinitionIds.SHOW_WHITESPACE_CHARACTERS, getAction(editor, ITextEditorActionConstants.SHOW_WHITESPACE_CHARACTERS));
215
216         fFindNext.setAction(getAction(editor, ITextEditorActionConstants.FIND_NEXT));
217         fFindPrevious.setAction(getAction(editor, ITextEditorActionConstants.FIND_PREVIOUS));
218         fIncrementalFind.setAction(getAction(editor, ITextEditorActionConstants.FIND_INCREMENTAL));
219         fIncrementalFindReverse.setAction(getAction(editor, ITextEditorActionConstants.FIND_INCREMENTAL_REVERSE));
220         fGotoLine.setAction(getAction(editor, ITextEditorActionConstants.GOTO_LINE));
221         fHippieCompletion.setAction(getAction(editor, ITextEditorActionConstants.HIPPIE_COMPLETION));
222
223         for (int i= 0; i < STATUS_FIELD_DEFS.length; i++) {
224             if (fActiveEditorPart instanceof ITextEditorExtension) {
225                 StatusLineContributionItem statusField= (StatusLineContributionItem) fStatusFields.get(STATUS_FIELD_DEFS[i]);
226                 statusField.setActionHandler(getAction(editor, STATUS_FIELD_DEFS[i].actionId));
227                 ITextEditorExtension extension= (ITextEditorExtension) fActiveEditorPart;
228                 extension.setStatusField(statusField, STATUS_FIELD_DEFS[i].category);
229             }
230         }
231     }
232
233     /**
234      * The <code>BasicTextEditorActionContributor</code> implementation of this
235      * <code>IEditorActionBarContributor</code> method installs the global
236      * action handler for the given text editor by calling a private helper
237      * method.
238      * <p>
239      * Subclasses may extend.</p>
240      *
241      * @param part {@inheritDoc}
242      */

243     public void setActiveEditor(IEditorPart part) {
244         doSetActiveEditor(part);
245     }
246
247     /*
248      * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
249      */

250     public void contributeToMenu(IMenuManager menu) {
251
252         IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
253         if (editMenu != null) {
254             editMenu.prependToGroup(IWorkbenchActionConstants.FIND_EXT, fIncrementalFindReverse);
255             editMenu.prependToGroup(IWorkbenchActionConstants.FIND_EXT, fIncrementalFind);
256             editMenu.prependToGroup(IWorkbenchActionConstants.FIND_EXT, fFindPrevious);
257             editMenu.prependToGroup(IWorkbenchActionConstants.FIND_EXT, fFindNext);
258
259             addOrInsert(editMenu, new Separator(ITextEditorActionConstants.GROUP_OPEN));
260             addOrInsert(editMenu, new Separator(ITextEditorActionConstants.GROUP_INFORMATION));
261             addOrInsert(editMenu, new Separator(ITextEditorActionConstants.GROUP_ASSIST));
262             addOrInsert(editMenu, new Separator(ITextEditorActionConstants.GROUP_GENERATE));
263             addOrInsert(editMenu, new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
264
265             editMenu.appendToGroup(ITextEditorActionConstants.GROUP_ASSIST, fHippieCompletion);
266         }
267
268         IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
269         if (navigateMenu != null) {
270             navigateMenu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, fGotoLine);
271         }
272     }
273
274     /**
275      * The <code>item</code> is {@link IContributionManager#add(IContributionItem) added} to
276      * <code>menu</code> if no item with the same id currently exists. If there already is an
277      * contribution item with the same id, the new item gets
278      * {@link IContributionManager#insertAfter(String, IContributionItem) inserted after} it.
279      *
280      * @param menu the contribution manager
281      * @param item the contribution item
282      * @since 3.2
283      */

284     private void addOrInsert(IContributionManager menu, IContributionItem item) {
285         String JavaDoc id= item.getId();
286         if (menu.find(id) == null)
287             menu.add(item);
288         else
289             menu.insertAfter(id, item);
290     }
291
292     /*
293      * @see EditorActionBarContributor#contributeToStatusLine(org.eclipse.jface.action.IStatusLineManager)
294      * @since 2.0
295      */

296     public void contributeToStatusLine(IStatusLineManager statusLineManager) {
297         super.contributeToStatusLine(statusLineManager);
298         for (int i= 0; i < STATUS_FIELD_DEFS.length; i++)
299             statusLineManager.add((IContributionItem)fStatusFields.get(STATUS_FIELD_DEFS[i]));
300     }
301
302     /*
303      * @see org.eclipse.ui.IEditorActionBarContributor#dispose()
304      * @since 2.0
305      */

306     public void dispose() {
307         doSetActiveEditor(null);
308         super.dispose();
309     }
310 }
311
Popular Tags