]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorActionContributor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceEditorActionContributor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.workbench;
13
14
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;
25
26
27
28
29
30 /**
31  * Manages the installation and removal of global actions for
32  * the same type of editors.
33  * <p>
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>
37  * <p>
38  * Subclasses may override the following methods:
39  * <ul>
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>
44  * </ul>
45  * </p>
46  * @see org.eclipse.ui.texteditor.ITextEditorActionConstants
47  */
48 public class ResourceEditorActionContributor extends EditorActionBarContributor {
49
50
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(),
64         };
65
66         /**
67          * Status field definition.
68          * @since 3.0
69          */
70         @SuppressWarnings("unused")
71     private static class StatusFieldDef {
72
73                 private String category;
74                 private String actionId;
75                 private boolean visible;
76                 private int widthInChars;
77
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;
84                 }
85         }
86
87         /**
88          * The status fields to be set to the editor
89          * @since 3.0
90          */
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)
96 //      };
97
98         /**
99          * The active editor part.
100          */
101         private IEditorPart activeEditorPart;
102         /**
103          * The find next action.
104          * @since 2.0
105          */
106 //      private RetargetTextEditorAction fFindNext;
107         /**
108          * The find previous action.
109          * @since 2.0
110          */
111 //      private RetargetTextEditorAction fFindPrevious;
112         /**
113          * The incremental find action.
114          * @since 2.0
115          */
116 //      private RetargetTextEditorAction fIncrementalFind;
117         /**
118          * The reverse incremental find action.
119          * @since 2.1
120          */
121 //      private RetargetTextEditorAction fIncrementalFindReverse;
122         /**
123          * The go to line action.
124          */
125 //      private RetargetTextEditorAction fGotoLine;
126         /**
127          * The word completion action.
128          * @since 3.1
129          */
130 //      private RetargetTextEditorAction fHippieCompletion;
131         /**
132          * The map of status fields.
133          * @since 2.0
134          */
135 //      private Map fStatusFields;
136
137
138         /**
139          * Creates an empty editor action bar contributor. The action bars are
140          * furnished later via the <code>init</code> method.
141          *
142          * @see org.eclipse.ui.IEditorActionBarContributor#init(org.eclipse.ui.IActionBars, org.eclipse.ui.IWorkbenchPage)
143          */
144         public ResourceEditorActionContributor() {
145
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);
158
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));
163 //              }
164         }
165
166         /**
167          * Returns the active editor part.
168          *
169          * @return the active editor part
170          */
171         protected final IEditorPart getActiveEditorPart() {
172                 return activeEditorPart;
173         }
174
175         /**
176          * Returns the action registered with the given text editor.
177          *
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
181          */
182         protected final IAction getAction(IResourceEditorPart editor, String actionId) {
183             return null;
184 //              return (editor == null || actionId == null ? null : editor.getAction(actionId));
185         }
186
187         /**
188          * The method installs the global action handlers for the given text editor.
189          * <p>
190          * This method cannot be overridden by subclasses.</p>
191          *
192          * @param part the active editor part
193          * @since 2.0
194          */
195         private void doSetActiveEditor(IEditorPart part) {
196
197                 if (activeEditorPart == part)
198                         return;
199
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);
204 //              }
205
206                 activeEditorPart= part;
207                 IResourceEditorPart editor = (part instanceof IResourceEditorPart) ? (IResourceEditorPart) part : null;
208
209                 IActionBars actionBars= getActionBars();
210                 for (int i= 0; i < ACTIONS.length; i++)
211                         actionBars.setGlobalActionHandler(ACTIONS[i], getAction(editor, ACTIONS[i]));
212
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));
219 //
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);
226 //                      }
227 //              }
228         }
229
230         /**
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
234          * method.
235          * <p>
236          * Subclasses may extend.</p>
237          *
238          * @param part {@inheritDoc}
239          */
240         public void setActiveEditor(IEditorPart part) {
241                 doSetActiveEditor(part);
242         }
243
244         /*
245          * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
246          */
247         public void contributeToMenu(IMenuManager menu) {
248
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);
255 //
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));
261 //
262 //                      editMenu.appendToGroup(ITextEditorActionConstants.GROUP_ASSIST, fHippieCompletion);
263 //              }
264 //
265 //              IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE);
266 //              if (navigateMenu != null) {
267 //                      navigateMenu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, fGotoLine);
268 //              }
269         }
270
271         /**
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.
276          * 
277          * @param menu the contribution manager
278          * @param item the contribution item
279          * @since 3.2
280          */
281         @SuppressWarnings("unused")
282     private void addOrInsert(IContributionManager menu, IContributionItem item) {
283             String id= item.getId();
284                 if (menu.find(id) == null)
285                 menu.add(item);
286             else
287                 menu.insertAfter(id, item);
288     }
289
290         /*
291          * @see EditorActionBarContributor#contributeToStatusLine(org.eclipse.jface.action.IStatusLineManager)
292          * @since 2.0
293          */
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]));
298         }
299
300         /*
301          * @see org.eclipse.ui.IEditorActionBarContributor#dispose()
302          * @since 2.0
303          */
304         public void dispose() {
305                 doSetActiveEditor(null);
306                 super.dispose();
307         }
308 }