1 11 package org.eclipse.jdt.internal.ui.javaeditor; 12 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.jface.action.IAction; 20 import org.eclipse.jface.action.IMenuListener; 21 import org.eclipse.jface.action.IMenuManager; 22 import org.eclipse.jface.action.MenuManager; 23 import org.eclipse.jface.action.Separator; 24 25 import org.eclipse.ui.IActionBars; 26 import org.eclipse.ui.IEditorPart; 27 import org.eclipse.ui.IWorkbenchActionConstants; 28 import org.eclipse.ui.IWorkbenchPage; 29 import org.eclipse.ui.actions.RetargetAction; 30 import org.eclipse.ui.texteditor.ITextEditor; 31 import org.eclipse.ui.texteditor.ITextEditorActionConstants; 32 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 33 import org.eclipse.ui.texteditor.RetargetTextEditorAction; 34 35 import org.eclipse.ui.ide.IDEActionFactory; 36 37 import org.eclipse.jdt.ui.actions.JdtActionConstants; 38 39 import org.eclipse.jdt.internal.ui.JavaPluginImages; 40 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory; 41 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry; 42 43 44 public class BasicCompilationUnitEditorActionContributor extends BasicJavaEditorActionContributor { 45 46 50 private final class MenuListener implements IMenuListener { 51 private final IMenuManager fMenu; 52 53 public MenuListener(IMenuManager menu) { 54 fMenu= menu; 55 } 56 57 public void menuAboutToShow(IMenuManager manager) { 58 for (int i= 0; i < fSpecificAssistActions.length; i++) { 59 fSpecificAssistActions[i].update(); 60 } 61 } 62 63 public void dispose() { 64 fMenu.removeMenuListener(this); 65 } 66 } 67 68 protected RetargetAction fRetargetContentAssist; 69 protected RetargetTextEditorAction fContentAssist; 70 protected RetargetTextEditorAction fContextInformation; 71 protected RetargetTextEditorAction fQuickAssistAction; 72 protected RetargetTextEditorAction fChangeEncodingAction; 73 74 75 protected SpecificContentAssistAction[] fSpecificAssistActions; 76 77 private MenuListener fContentAssistMenuListener; 78 79 80 public BasicCompilationUnitEditorActionContributor() { 81 82 fRetargetContentAssist= new RetargetAction(JdtActionConstants.CONTENT_ASSIST, JavaEditorMessages.ContentAssistProposal_label); 83 fRetargetContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); 84 fRetargetContentAssist.setImageDescriptor(JavaPluginImages.DESC_ELCL_CODE_ASSIST); 85 fRetargetContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST); 86 markAsPartListener(fRetargetContentAssist); 87 88 fContentAssist= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "ContentAssistProposal."); fContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); 90 fContentAssist.setImageDescriptor(JavaPluginImages.DESC_ELCL_CODE_ASSIST); 91 fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST); 92 93 fContextInformation= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "ContentAssistContextInformation."); fContextInformation.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION); 95 96 fQuickAssistAction= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "CorrectionAssistProposal."); fQuickAssistAction.setActionDefinitionId(ITextEditorActionDefinitionIds.QUICK_ASSIST); 98 99 fChangeEncodingAction= new RetargetTextEditorAction(JavaEditorMessages.getBundleForConstructedKeys(), "Editor.ChangeEncodingAction."); } 101 102 105 public void contributeToMenu(IMenuManager menu) { 106 107 super.contributeToMenu(menu); 108 if (fContentAssistMenuListener != null) 109 fContentAssistMenuListener.dispose(); 110 111 IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT); 112 if (editMenu != null) { 113 editMenu.add(fChangeEncodingAction); 114 IMenuManager caMenu= new MenuManager(JavaEditorMessages.BasicEditorActionContributor_specific_content_assist_menu, "specific_content_assist"); editMenu.insertAfter(ITextEditorActionConstants.GROUP_ASSIST, caMenu); 116 117 caMenu.add(fRetargetContentAssist); 118 Collection descriptors= CompletionProposalComputerRegistry.getDefault().getProposalCategories(); 119 List specificAssistActions= new ArrayList (descriptors.size()); 120 for (Iterator it= descriptors.iterator(); it.hasNext();) { 121 final CompletionProposalCategory cat= (CompletionProposalCategory) it.next(); 122 if (cat.hasComputers()) { 123 IAction caAction= new SpecificContentAssistAction(cat); 124 caMenu.add(caAction); 125 specificAssistActions.add(caAction); 126 } 127 } 128 fSpecificAssistActions= (SpecificContentAssistAction[]) specificAssistActions.toArray(new SpecificContentAssistAction[specificAssistActions.size()]); 129 if (fSpecificAssistActions.length > 0) { 130 fContentAssistMenuListener= new MenuListener(caMenu); 131 caMenu.addMenuListener(fContentAssistMenuListener); 132 } 133 caMenu.add(new Separator("context_info")); caMenu.add(fContextInformation); 135 136 editMenu.appendToGroup(ITextEditorActionConstants.GROUP_ASSIST, fQuickAssistAction); 137 } 138 } 139 140 143 public void setActiveEditor(IEditorPart part) { 144 super.setActiveEditor(part); 145 146 ITextEditor textEditor= null; 147 if (part instanceof ITextEditor) 148 textEditor= (ITextEditor) part; 149 150 fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); fContextInformation.setAction(getAction(textEditor, "ContentAssistContextInformation")); fQuickAssistAction.setAction(getAction(textEditor, ITextEditorActionConstants.QUICK_ASSIST)); 153 154 if (fSpecificAssistActions != null) { 155 for (int i= 0; i < fSpecificAssistActions.length; i++) { 156 SpecificContentAssistAction assistAction= fSpecificAssistActions[i]; 157 assistAction.setActiveEditor(part); 158 } 159 } 160 161 fChangeEncodingAction.setAction(getAction(textEditor, ITextEditorActionConstants.CHANGE_ENCODING)); 162 163 IActionBars actionBars= getActionBars(); 164 actionBars.setGlobalActionHandler(JdtActionConstants.SHIFT_RIGHT, getAction(textEditor, "ShiftRight")); actionBars.setGlobalActionHandler(JdtActionConstants.SHIFT_LEFT, getAction(textEditor, "ShiftLeft")); 167 actionBars.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), getAction(textEditor, IDEActionFactory.ADD_TASK.getId())); 168 actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), getAction(textEditor, IDEActionFactory.BOOKMARK.getId())); 169 } 170 171 174 public void init(IActionBars bars, IWorkbenchPage page) { 175 super.init(bars, page); 176 bars.setGlobalActionHandler(JdtActionConstants.CONTENT_ASSIST, fContentAssist); 178 } 179 180 184 public void dispose() { 185 if (fRetargetContentAssist != null) { 186 fRetargetContentAssist.dispose(); 187 fRetargetContentAssist= null; 188 } 189 if (fContentAssistMenuListener != null) { 190 fContentAssistMenuListener.dispose(); 191 fContentAssistMenuListener= null; 192 } 193 super.dispose(); 194 } 195 } 196 | Popular Tags |