1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jface.action.IAction; 16 import org.eclipse.jface.action.IMenuManager; 17 import org.eclipse.jface.action.MenuManager; 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.ISelectionChangedListener; 20 import org.eclipse.jface.viewers.ISelectionProvider; 21 22 import org.eclipse.ui.IActionBars; 23 import org.eclipse.ui.IWorkbenchSite; 24 import org.eclipse.ui.PlatformUI; 25 import org.eclipse.ui.actions.ActionGroup; 26 import org.eclipse.ui.keys.IBindingService; 27 import org.eclipse.ui.texteditor.ITextEditorActionConstants; 28 29 import org.eclipse.jdt.ui.IContextMenuConstants; 30 31 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 32 import org.eclipse.jdt.internal.ui.search.SearchMessages; 33 34 44 public class OccurrencesSearchGroup extends ActionGroup { 45 46 private IWorkbenchSite fSite; 47 private JavaEditor fEditor; 48 private IActionBars fActionBars; 49 50 private String fGroupId; 51 52 private FindOccurrencesInFileAction fOccurrencesInFileAction; 53 private FindExceptionOccurrencesAction fExceptionOccurrencesAction; 54 private FindImplementOccurrencesAction fFindImplementorOccurrencesAction; 55 56 63 public OccurrencesSearchGroup(IWorkbenchSite site) { 64 fSite= site; 65 fGroupId= IContextMenuConstants.GROUP_SEARCH; 66 67 fOccurrencesInFileAction= new FindOccurrencesInFileAction(site); 68 fOccurrencesInFileAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE); 69 fOccurrencesInFileAction.setText(SearchMessages.Search_FindOccurrencesInFile_shortLabel); 71 72 fExceptionOccurrencesAction= new FindExceptionOccurrencesAction(site); 73 fExceptionOccurrencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_EXCEPTION_OCCURRENCES_IN_FILE); 74 75 fFindImplementorOccurrencesAction= new FindImplementOccurrencesAction(site); 76 fFindImplementorOccurrencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENT_OCCURRENCES_IN_FILE); 77 78 ISelectionProvider provider= fSite.getSelectionProvider(); 80 ISelection selection= provider.getSelection(); 81 registerAction(fOccurrencesInFileAction, provider, selection); 82 registerAction(fExceptionOccurrencesAction, provider, selection); 83 registerAction(fFindImplementorOccurrencesAction, provider, selection); 84 } 85 86 91 public OccurrencesSearchGroup(JavaEditor editor) { 92 fEditor= editor; 93 fSite= fEditor.getSite(); 94 fGroupId= ITextEditorActionConstants.GROUP_FIND; 95 96 fOccurrencesInFileAction= new FindOccurrencesInFileAction(fEditor); 97 fOccurrencesInFileAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE); 98 fOccurrencesInFileAction.setText(SearchMessages.Search_FindOccurrencesInFile_shortLabel); 100 fEditor.setAction("SearchOccurrencesInFile", fOccurrencesInFileAction); 102 fExceptionOccurrencesAction= new FindExceptionOccurrencesAction(fEditor); 103 fExceptionOccurrencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_EXCEPTION_OCCURRENCES_IN_FILE); 104 fEditor.setAction("SearchExceptionOccurrences", fExceptionOccurrencesAction); 106 fFindImplementorOccurrencesAction= new FindImplementOccurrencesAction(fEditor); 107 fFindImplementorOccurrencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENT_OCCURRENCES_IN_FILE); 108 fEditor.setAction("SearchImplementOccurrences", fFindImplementorOccurrencesAction); } 110 111 private void registerAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection){ 112 action.update(selection); 113 provider.addSelectionChangedListener(action); 114 } 115 116 private IAction[] getActions() { 117 IAction[] actions= new IAction[3]; 118 actions[0]= fOccurrencesInFileAction; 119 actions[1]= fExceptionOccurrencesAction; 120 actions[2]= fFindImplementorOccurrencesAction; 121 return actions; 122 } 123 124 127 public void fillContextMenu(IMenuManager manager) { 128 String menuText= SearchMessages.group_occurrences; 129 String shortcut= getShortcutString(); 130 if (shortcut != null) { 131 menuText= menuText + '\t' + shortcut; 132 } 133 134 MenuManager javaSearchMM= new MenuManager(menuText, IContextMenuConstants.GROUP_SEARCH); 135 IAction[] actions= getActions(); 136 for (int i= 0; i < actions.length; i++) { 137 IAction action= actions[i]; 138 if (action.isEnabled()) 139 javaSearchMM.add(action); 140 } 141 142 if (!javaSearchMM.isEmpty()) 143 manager.appendToGroup(fGroupId, javaSearchMM); 144 } 145 146 private String getShortcutString() { 147 IBindingService bindingService= (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class); 148 if (bindingService == null) 149 return null; 150 return bindingService.getBestActiveBindingFormattedFor(IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE_QUICK_MENU); 151 } 152 153 156 public void fillActionBars(IActionBars actionBars) { 157 Assert.isNotNull(actionBars); 158 super.fillActionBars(actionBars); 159 fActionBars= actionBars; 160 updateGlobalActionHandlers(); 161 } 162 163 166 public void dispose() { 167 ISelectionProvider provider= fSite.getSelectionProvider(); 168 if (provider != null) { 169 disposeAction(fFindImplementorOccurrencesAction, provider); 170 disposeAction(fExceptionOccurrencesAction, provider); 171 disposeAction(fOccurrencesInFileAction, provider); 172 } 173 super.dispose(); 174 fFindImplementorOccurrencesAction= null; 175 fExceptionOccurrencesAction= null; 176 fOccurrencesInFileAction= null; 177 updateGlobalActionHandlers(); 178 } 179 180 private void updateGlobalActionHandlers() { 181 if (fActionBars != null) { 182 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_OCCURRENCES_IN_FILE, fOccurrencesInFileAction); 183 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_EXCEPTION_OCCURRENCES, fExceptionOccurrencesAction); 184 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_IMPLEMENT_OCCURRENCES, fFindImplementorOccurrencesAction); 185 } 186 } 187 188 private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) { 189 if (action != null) 190 provider.removeSelectionChangedListener(action); 191 } 192 } 193 | Popular Tags |