1 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 43 public class TextEditorActionContributor extends BasicTextEditorActionContributor { 44 45 49 private RetargetTextEditorAction fChangeEncodingAction; 50 54 private RetargetTextEditorAction fQuickAssistAction; 55 59 private IContributionItem fQuickAssistMenuEntry; 60 61 private RetargetTextEditorAction fRetargetShowInformationAction; 62 63 66 public TextEditorActionContributor() { 67 fChangeEncodingAction= new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ChangeEncodingAction."); fQuickAssistAction= new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.QuickAssist."); fQuickAssistAction.setActionDefinitionId(ITextEditorActionDefinitionIds.QUICK_ASSIST); 70 fQuickAssistMenuEntry= new ActionContributionItem(fQuickAssistAction); 71 72 fRetargetShowInformationAction= new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ShowInformation."); fRetargetShowInformationAction.setActionDefinitionId(ITextEditorActionDefinitionIds.SHOW_INFORMATION); 74 } 75 76 82 private void doSetActiveEditor(final IEditorPart part) { 83 84 ITextEditor textEditor= null; 85 if (part instanceof ITextEditor) 86 textEditor= (ITextEditor) part; 87 88 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 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 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 142 public void setActiveEditor(IEditorPart part) { 143 super.setActiveEditor(part); 144 doSetActiveEditor(part); 145 } 146 147 150 public void dispose() { 151 doSetActiveEditor(null); 152 super.dispose(); 153 } 154 155 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 |