1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench;
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.IContributionItem;
18 import org.eclipse.jface.action.IContributionManager;
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.jface.action.IStatusLineManager;
21 import org.eclipse.ui.IActionBars;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.actions.ActionFactory;
24 import org.eclipse.ui.part.EditorActionBarContributor;
31 * Manages the installation and removal of global actions for
32 * the same type of editors.
34 * If instantiated and used as-is, this contributor connects to all of the workbench defined
35 * global editor actions the corresponding actions of the current editor. It also adds addition
36 * actions for searching and navigation (go to line) as well as a set of status fields.</p>
38 * Subclasses may override the following methods:
40 * <li><code>contributeToMenu</code> - extend to contribute to menu</li>
41 * <li><code>contributeToToolBar</code> - reimplement to contribute to tool bar</li>
42 * <li><code>contributeToStatusLine</code> - reimplement to contribute to status line</li>
43 * <li><code>setActiveEditor</code> - extend to react to editor changes</li>
46 * @see org.eclipse.ui.texteditor.ITextEditorActionConstants
48 public class ResourceEditorActionContributor extends EditorActionBarContributor {
51 /** The global actions to be connected with editor actions */
52 private final static String[] ACTIONS= {
53 ActionFactory.UNDO.getId(),
54 ActionFactory.REDO.getId(),
55 ActionFactory.CUT.getId(),
56 ActionFactory.COPY.getId(),
57 ActionFactory.PASTE.getId(),
58 ActionFactory.DELETE.getId(),
59 ActionFactory.SELECT_ALL.getId(),
60 ActionFactory.FIND.getId(),
61 ActionFactory.PRINT.getId(),
62 ActionFactory.PROPERTIES.getId(),
63 //ActionFactory.REVERT.getId(),
67 * Status field definition.
70 @SuppressWarnings("unused")
71 private static class StatusFieldDef {
73 private String category;
74 private String actionId;
75 private boolean visible;
76 private int widthInChars;
78 private StatusFieldDef(String category, String actionId, boolean visible, int widthInChars) {
79 Assert.isNotNull(category);
80 this.category= category;
81 this.actionId= actionId;
82 this.visible= visible;
83 this.widthInChars= widthInChars;
88 * The status fields to be set to the editor
91 // private final static StatusFieldDef[] STATUS_FIELD_DEFS= {
92 // new StatusFieldDef(ITextEditorActionConstants.STATUS_CATEGORY_FIND_FIELD, null, false, EditorMessages.Editor_FindIncremental_reverse_name.length() + 15),
93 // new StatusFieldDef(ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE, null, true, StatusLineContributionItem.DEFAULT_WIDTH_IN_CHARS + 1),
94 // new StatusFieldDef(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_MODE, ITextEditorActionDefinitionIds.TOGGLE_OVERWRITE, true, StatusLineContributionItem.DEFAULT_WIDTH_IN_CHARS),
95 // new StatusFieldDef(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION, ITextEditorActionConstants.GOTO_LINE, true, StatusLineContributionItem.DEFAULT_WIDTH_IN_CHARS)
99 * The active editor part.
101 private IEditorPart activeEditorPart;
103 * The find next action.
106 // private RetargetTextEditorAction fFindNext;
108 * The find previous action.
111 // private RetargetTextEditorAction fFindPrevious;
113 * The incremental find action.
116 // private RetargetTextEditorAction fIncrementalFind;
118 * The reverse incremental find action.
121 // private RetargetTextEditorAction fIncrementalFindReverse;
123 * The go to line action.
125 // private RetargetTextEditorAction fGotoLine;
127 * The word completion action.
130 // private RetargetTextEditorAction fHippieCompletion;
132 * The map of status fields.
135 // private Map fStatusFields;
139 * Creates an empty editor action bar contributor. The action bars are
140 * furnished later via the <code>init</code> method.
142 * @see org.eclipse.ui.IEditorActionBarContributor#init(org.eclipse.ui.IActionBars, org.eclipse.ui.IWorkbenchPage)
144 public ResourceEditorActionContributor() {
146 // fFindNext= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindNext."); //$NON-NLS-1$
147 // fFindNext.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_NEXT);
148 // fFindPrevious= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindPrevious."); //$NON-NLS-1$
149 // fFindPrevious.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_PREVIOUS);
150 // fIncrementalFind= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindIncremental."); //$NON-NLS-1$
151 // fIncrementalFind.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_INCREMENTAL);
152 // fIncrementalFindReverse= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindIncrementalReverse."); //$NON-NLS-1$
153 // fIncrementalFindReverse.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_INCREMENTAL_REVERSE);
154 // fGotoLine= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.GotoLine."); //$NON-NLS-1$
155 // fGotoLine.setActionDefinitionId(ITextEditorActionDefinitionIds.LINE_GOTO);
156 // fHippieCompletion= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.HippieCompletion."); //$NON-NLS-1$
157 // fHippieCompletion.setActionDefinitionId(ITextEditorActionDefinitionIds.HIPPIE_COMPLETION);
159 // fStatusFields= new HashMap(3);
160 // for (int i= 0; i < STATUS_FIELD_DEFS.length; i++) {
161 // StatusFieldDef fieldDef= STATUS_FIELD_DEFS[i];
162 // fStatusFields.put(fieldDef, new StatusLineContributionItem(fieldDef.category, fieldDef.visible, fieldDef.widthInChars));
167 * Returns the active editor part.
169 * @return the active editor part
171 protected final IEditorPart getActiveEditorPart() {
172 return activeEditorPart;
176 * Returns the action registered with the given text editor.
178 * @param editor the editor, or <code>null</code>
179 * @param actionId the action id
180 * @return the action, or <code>null</code> if none
182 protected final IAction getAction(IResourceEditorPart editor, String actionId) {
184 // return (editor == null || actionId == null ? null : editor.getAction(actionId));
188 * The method installs the global action handlers for the given text editor.
190 * This method cannot be overridden by subclasses.</p>
192 * @param part the active editor part
195 private void doSetActiveEditor(IEditorPart part) {
197 if (activeEditorPart == part)
200 // if (fActiveEditorPart instanceof ITextEditorExtension) {
201 // ITextEditorExtension extension= (ITextEditorExtension) fActiveEditorPart;
202 // for (int i= 0; i < STATUS_FIELD_DEFS.length; i++)
203 // extension.setStatusField(null, STATUS_FIELD_DEFS[i].category);
206 activeEditorPart= part;
207 IResourceEditorPart editor = (part instanceof IResourceEditorPart) ? (IResourceEditorPart) part : null;
209 IActionBars actionBars= getActionBars();
210 for (int i= 0; i < ACTIONS.length; i++)
211 actionBars.setGlobalActionHandler(ACTIONS[i], getAction(editor, ACTIONS[i]));
213 // fFindNext.setAction(getAction(editor, ITextEditorActionConstants.FIND_NEXT));
214 // fFindPrevious.setAction(getAction(editor, ITextEditorActionConstants.FIND_PREVIOUS));
215 // fIncrementalFind.setAction(getAction(editor, ITextEditorActionConstants.FIND_INCREMENTAL));
216 // fIncrementalFindReverse.setAction(getAction(editor, ITextEditorActionConstants.FIND_INCREMENTAL_REVERSE));
217 // fGotoLine.setAction(getAction(editor, ITextEditorActionConstants.GOTO_LINE));
218 // fHippieCompletion.setAction(getAction(editor, ITextEditorActionConstants.HIPPIE_COMPLETION));
220 // for (int i= 0; i < STATUS_FIELD_DEFS.length; i++) {
221 // if (fActiveEditorPart instanceof ITextEditorExtension) {
222 // StatusLineContributionItem statusField= (StatusLineContributionItem) fStatusFields.get(STATUS_FIELD_DEFS[i]);
223 // statusField.setActionHandler(getAction(editor, STATUS_FIELD_DEFS[i].actionId));
224 // ITextEditorExtension extension= (ITextEditorExtension) fActiveEditorPart;
225 // extension.setStatusField(statusField, STATUS_FIELD_DEFS[i].category);
231 * The <code>BasicTextEditorActionContributor</code> implementation of this
232 * <code>IEditorActionBarContributor</code> method installs the global
233 * action handler for the given text editor by calling a private helper
236 * Subclasses may extend.</p>
238 * @param part {@inheritDoc}
240 public void setActiveEditor(IEditorPart part) {
241 doSetActiveEditor(part);
245 * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
247 public void contributeToMenu(IMenuManager menu) {
249 // IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
250 // if (editMenu != null) {
251 // editMenu.prependToGroup(IWorkbenchActionConstants.FIND_EXT, fIncrementalFindReverse);
252 // editMenu.prependToGroup(IWorkbenchActionConstants.FIND_EXT, fIncrementalFind);
253 // editMenu.prependToGroup(IWorkbenchActionConstants.FIND_EXT, fFindPrevious);
254 // editMenu.prependToGroup(IWorkbenchActionConstants.FIND_EXT, fFindNext);
256 // addOrInsert(editMenu, new Separator(ITextEditorActionConstants.GROUP_OPEN));
257 // addOrInsert(editMenu, new Separator(ITextEditorActionConstants.GROUP_INFORMATION));
258 // addOrInsert(editMenu, new Separator(ITextEditorActionConstants.GROUP_ASSIST));
259 // addOrInsert(editMenu, new Separator(ITextEditorActionConstants.GROUP_GENERATE));
260 // addOrInsert(editMenu, new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
262 // editMenu.appendToGroup(ITextEditorActionConstants.GROUP_ASSIST, fHippieCompletion);
265 // IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
266 // if (navigateMenu != null) {
267 // navigateMenu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, fGotoLine);
272 * The <code>item</code> is {@link IContributionManager#add(IContributionItem) added} to
273 * <code>menu</code> if no item with the same id currently exists. If there already is an
274 * contribution item with the same id, the new item gets
275 * {@link IContributionManager#insertAfter(String, IContributionItem) inserted after} it.
277 * @param menu the contribution manager
278 * @param item the contribution item
281 @SuppressWarnings("unused")
282 private void addOrInsert(IContributionManager menu, IContributionItem item) {
283 String id= item.getId();
284 if (menu.find(id) == null)
287 menu.insertAfter(id, item);
291 * @see EditorActionBarContributor#contributeToStatusLine(org.eclipse.jface.action.IStatusLineManager)
294 public void contributeToStatusLine(IStatusLineManager statusLineManager) {
295 super.contributeToStatusLine(statusLineManager);
296 // for (int i= 0; i < STATUS_FIELD_DEFS.length; i++)
297 // statusLineManager.add((IContributionItem)fStatusFields.get(STATUS_FIELD_DEFS[i]));
301 * @see org.eclipse.ui.IEditorActionBarContributor#dispose()
304 public void dispose() {
305 doSetActiveEditor(null);