1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.util.Iterator ; 14 15 import org.eclipse.core.runtime.Assert; 16 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.action.IMenuManager; 19 import org.eclipse.jface.action.MenuManager; 20 import org.eclipse.jface.action.Separator; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.ISelectionChangedListener; 23 import org.eclipse.jface.viewers.ISelectionProvider; 24 25 import org.eclipse.ui.IActionBars; 26 import org.eclipse.ui.IWorkbenchSite; 27 import org.eclipse.ui.IWorkingSet; 28 import org.eclipse.ui.actions.ActionGroup; 29 import org.eclipse.ui.texteditor.ITextEditorActionConstants; 30 31 import org.eclipse.jdt.ui.IContextMenuConstants; 32 33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 34 import org.eclipse.jdt.internal.ui.search.SearchMessages; 35 import org.eclipse.jdt.internal.ui.search.SearchUtil; 36 37 47 public class ImplementorsSearchGroup extends ActionGroup { 48 49 private static final String MENU_TEXT= SearchMessages.group_implementors; 50 51 private IWorkbenchSite fSite; 52 private JavaEditor fEditor; 53 private IActionBars fActionBars; 54 55 private String fGroupId; 56 57 private FindImplementorsAction fFindImplementorsAction; 58 private FindImplementorsInProjectAction fFindImplementorsInProjectAction; 59 private FindImplementorsInWorkingSetAction fFindImplementorsInWorkingSetAction; 60 61 68 public ImplementorsSearchGroup(IWorkbenchSite site) { 69 fSite= site; 70 fGroupId= IContextMenuConstants.GROUP_SEARCH; 71 72 fFindImplementorsAction= new FindImplementorsAction(site); 73 fFindImplementorsAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_WORKSPACE); 74 75 fFindImplementorsInProjectAction= new FindImplementorsInProjectAction(site); 76 fFindImplementorsInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_PROJECT); 77 78 fFindImplementorsInWorkingSetAction= new FindImplementorsInWorkingSetAction(site); 79 fFindImplementorsInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_WORKING_SET); 80 81 ISelectionProvider provider= fSite.getSelectionProvider(); 83 ISelection selection= provider.getSelection(); 84 registerAction(fFindImplementorsAction, provider, selection); 85 registerAction(fFindImplementorsInProjectAction, provider, selection); 86 registerAction(fFindImplementorsInWorkingSetAction, provider, selection); 87 } 88 89 93 public ImplementorsSearchGroup(JavaEditor editor) { 94 fEditor= editor; 95 fSite= fEditor.getSite(); 96 fGroupId= ITextEditorActionConstants.GROUP_FIND; 97 98 fFindImplementorsAction= new FindImplementorsAction(fEditor); 99 fFindImplementorsAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_WORKSPACE); 100 fEditor.setAction("SearchImplementorsInWorkspace", fFindImplementorsAction); 102 fFindImplementorsInProjectAction= new FindImplementorsInProjectAction(fEditor); 103 fFindImplementorsInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_PROJECT); 104 fEditor.setAction("SearchImplementorsInProject", fFindImplementorsInProjectAction); 106 fFindImplementorsInWorkingSetAction= new FindImplementorsInWorkingSetAction(fEditor); 107 fFindImplementorsInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_WORKING_SET); 108 fEditor.setAction("SearchImplementorsInWorkingSet", fFindImplementorsInWorkingSetAction); } 110 111 private void registerAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection){ 112 action.update(selection); 113 provider.addSelectionChangedListener(action); 114 } 115 116 private void addAction(IAction action, IMenuManager manager) { 117 if (action.isEnabled()) { 118 manager.add(action); 119 } 120 } 121 122 private void addWorkingSetAction(IWorkingSet[] workingSets, IMenuManager manager) { 123 FindAction action; 124 if (fEditor != null) 125 action= new WorkingSetFindAction(fEditor, new FindImplementorsInWorkingSetAction(fEditor, workingSets), SearchUtil.toString(workingSets)); 126 else 127 action= new WorkingSetFindAction(fSite, new FindImplementorsInWorkingSetAction(fSite, workingSets), SearchUtil.toString(workingSets)); 128 action.update(getContext().getSelection()); 129 addAction(action, manager); 130 } 131 132 133 136 public void fillContextMenu(IMenuManager manager) { 137 MenuManager javaSearchMM= new MenuManager(MENU_TEXT, IContextMenuConstants.GROUP_SEARCH); 138 addAction(fFindImplementorsAction, javaSearchMM); 139 addAction(fFindImplementorsInProjectAction, javaSearchMM); 140 141 javaSearchMM.add(new Separator()); 142 143 Iterator iter= SearchUtil.getLRUWorkingSets().sortedIterator(); 144 while (iter.hasNext()) { 145 addWorkingSetAction((IWorkingSet[]) iter.next(), javaSearchMM); 146 } 147 addAction(fFindImplementorsInWorkingSetAction, javaSearchMM); 148 149 if (!javaSearchMM.isEmpty()) 150 manager.appendToGroup(fGroupId, javaSearchMM); 151 } 152 153 154 157 public void fillActionBars(IActionBars actionBars) { 158 Assert.isNotNull(actionBars); 159 super.fillActionBars(actionBars); 160 fActionBars= actionBars; 161 updateGlobalActionHandlers(); 162 } 163 164 167 public void dispose() { 168 ISelectionProvider provider= fSite.getSelectionProvider(); 169 if (provider != null) { 170 disposeAction(fFindImplementorsAction, provider); 171 disposeAction(fFindImplementorsInProjectAction, provider); 172 disposeAction(fFindImplementorsInWorkingSetAction, provider); 173 } 174 super.dispose(); 175 fFindImplementorsAction= null; 176 fFindImplementorsInProjectAction= null; 177 fFindImplementorsInWorkingSetAction= null; 178 updateGlobalActionHandlers(); 179 } 180 181 private void updateGlobalActionHandlers() { 182 if (fActionBars != null) { 183 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_IMPLEMENTORS_IN_WORKSPACE, fFindImplementorsAction); 184 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_IMPLEMENTORS_IN_PROJECT, fFindImplementorsInProjectAction); 185 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_IMPLEMENTORS_IN_WORKING_SET, fFindImplementorsInWorkingSetAction); 186 } 187 } 188 189 private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) { 190 if (action != null) 191 provider.removeSelectionChangedListener(action); 192 } 193 } 194 195 196 | Popular Tags |