1 11 12 package org.eclipse.ui.texteditor; 13 14 15 import java.util.HashMap ; 16 import java.util.Map ; 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 52 public class BasicTextEditorActionContributor extends EditorActionBarContributor { 53 54 55 56 private final static String [] 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 74 private static class StatusFieldDef { 75 76 private String category; 77 private String actionId; 78 private boolean visible; 79 private int widthInChars; 80 81 private StatusFieldDef(String category, String 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 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 104 private IEditorPart fActiveEditorPart; 105 109 private RetargetTextEditorAction fFindNext; 110 114 private RetargetTextEditorAction fFindPrevious; 115 119 private RetargetTextEditorAction fIncrementalFind; 120 124 private RetargetTextEditorAction fIncrementalFindReverse; 125 128 private RetargetTextEditorAction fGotoLine; 129 133 private RetargetTextEditorAction fHippieCompletion; 134 138 private Map fStatusFields; 139 140 141 147 public BasicTextEditorActionContributor() { 148 149 fFindNext= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindNext."); fFindNext.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_NEXT); 151 fFindPrevious= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindPrevious."); fFindPrevious.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_PREVIOUS); 153 fIncrementalFind= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindIncremental."); fIncrementalFind.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_INCREMENTAL); 155 fIncrementalFindReverse= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.FindIncrementalReverse."); fIncrementalFindReverse.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_INCREMENTAL_REVERSE); 157 fGotoLine= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.GotoLine."); fGotoLine.setActionDefinitionId(ITextEditorActionDefinitionIds.LINE_GOTO); 159 fHippieCompletion= new RetargetTextEditorAction(EditorMessages.getBundleForConstructedKeys(), "Editor.HippieCompletion."); fHippieCompletion.setActionDefinitionId(ITextEditorActionDefinitionIds.HIPPIE_COMPLETION); 161 162 fStatusFields= new HashMap (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 174 protected final IEditorPart getActiveEditorPart() { 175 return fActiveEditorPart; 176 } 177 178 185 protected final IAction getAction(ITextEditor editor, String actionId) { 186 return (editor == null || actionId == null ? null : editor.getAction(actionId)); 187 } 188 189 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 243 public void setActiveEditor(IEditorPart part) { 244 doSetActiveEditor(part); 245 } 246 247 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 284 private void addOrInsert(IContributionManager menu, IContributionItem item) { 285 String id= item.getId(); 286 if (menu.find(id) == null) 287 menu.add(item); 288 else 289 menu.insertAfter(id, item); 290 } 291 292 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 306 public void dispose() { 307 doSetActiveEditor(null); 308 super.dispose(); 309 } 310 } 311 | Popular Tags |