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 ReferencesSearchGroup extends ActionGroup { 48 49 private static final String MENU_TEXT= SearchMessages.group_references; 50 51 private IWorkbenchSite fSite; 52 private JavaEditor fEditor; 53 private IActionBars fActionBars; 54 55 private String fGroupId; 56 57 private FindReferencesAction fFindReferencesAction; 58 private FindReferencesInProjectAction fFindReferencesInProjectAction; 59 private FindReferencesInHierarchyAction fFindReferencesInHierarchyAction; 60 private FindReferencesInWorkingSetAction fFindReferencesInWorkingSetAction; 61 62 69 public ReferencesSearchGroup(IWorkbenchSite site) { 70 fSite= site; 71 fGroupId= IContextMenuConstants.GROUP_SEARCH; 72 73 fFindReferencesAction= new FindReferencesAction(site); 74 fFindReferencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_WORKSPACE); 75 76 fFindReferencesInProjectAction= new FindReferencesInProjectAction(site); 77 fFindReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_PROJECT); 78 79 fFindReferencesInHierarchyAction= new FindReferencesInHierarchyAction(site); 80 fFindReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_HIERARCHY); 81 82 fFindReferencesInWorkingSetAction= new FindReferencesInWorkingSetAction(site); 83 fFindReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_WORKING_SET); 84 85 ISelectionProvider provider= fSite.getSelectionProvider(); 87 ISelection selection= provider.getSelection(); 88 registerAction(fFindReferencesAction, provider, selection); 89 registerAction(fFindReferencesInProjectAction, provider, selection); 90 registerAction(fFindReferencesInHierarchyAction, provider, selection); 91 registerAction(fFindReferencesInWorkingSetAction, provider, selection); 92 } 93 94 95 99 public ReferencesSearchGroup(JavaEditor editor) { 100 Assert.isNotNull(editor); 101 fEditor= editor; 102 fSite= fEditor.getSite(); 103 fGroupId= ITextEditorActionConstants.GROUP_FIND; 104 105 fFindReferencesAction= new FindReferencesAction(editor); 106 fFindReferencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_WORKSPACE); 107 fEditor.setAction("SearchReferencesInWorkspace", fFindReferencesAction); 109 fFindReferencesInProjectAction= new FindReferencesInProjectAction(fEditor); 110 fFindReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_PROJECT); 111 fEditor.setAction("SearchReferencesInProject", fFindReferencesInProjectAction); 113 fFindReferencesInHierarchyAction= new FindReferencesInHierarchyAction(fEditor); 114 fFindReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_HIERARCHY); 115 fEditor.setAction("SearchReferencesInHierarchy", fFindReferencesInHierarchyAction); 117 fFindReferencesInWorkingSetAction= new FindReferencesInWorkingSetAction(fEditor); 118 fFindReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_WORKING_SET); 119 fEditor.setAction("SearchReferencesInWorkingSet", fFindReferencesInWorkingSetAction); } 121 122 private void registerAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection) { 123 action.update(selection); 124 provider.addSelectionChangedListener(action); 125 } 126 127 132 protected String getName() { 133 return MENU_TEXT; 134 } 135 136 139 public void fillActionBars(IActionBars actionBars) { 140 Assert.isNotNull(actionBars); 141 super.fillActionBars(actionBars); 142 fActionBars= actionBars; 143 updateGlobalActionHandlers(); 144 } 145 146 147 private void addAction(IAction action, IMenuManager manager) { 148 if (action.isEnabled()) { 149 manager.add(action); 150 } 151 } 152 153 private void addWorkingSetAction(IWorkingSet[] workingSets, IMenuManager manager) { 154 FindAction action; 155 if (fEditor != null) 156 action= new WorkingSetFindAction(fEditor, new FindReferencesInWorkingSetAction(fEditor, workingSets), SearchUtil.toString(workingSets)); 157 else 158 action= new WorkingSetFindAction(fSite, new FindReferencesInWorkingSetAction(fSite, workingSets), SearchUtil.toString(workingSets)); 159 action.update(getContext().getSelection()); 160 addAction(action, manager); 161 } 162 163 164 167 public void fillContextMenu(IMenuManager manager) { 168 MenuManager javaSearchMM= new MenuManager(getName(), IContextMenuConstants.GROUP_SEARCH); 169 addAction(fFindReferencesAction, javaSearchMM); 170 addAction(fFindReferencesInProjectAction, javaSearchMM); 171 addAction(fFindReferencesInHierarchyAction, javaSearchMM); 172 173 javaSearchMM.add(new Separator()); 174 175 Iterator iter= SearchUtil.getLRUWorkingSets().sortedIterator(); 176 while (iter.hasNext()) { 177 addWorkingSetAction((IWorkingSet[]) iter.next(), javaSearchMM); 178 } 179 addAction(fFindReferencesInWorkingSetAction, javaSearchMM); 180 181 if (!javaSearchMM.isEmpty()) 182 manager.appendToGroup(fGroupId, javaSearchMM); 183 } 184 185 188 public void dispose() { 189 ISelectionProvider provider= fSite.getSelectionProvider(); 190 if (provider != null) { 191 disposeAction(fFindReferencesAction, provider); 192 disposeAction(fFindReferencesInProjectAction, provider); 193 disposeAction(fFindReferencesInHierarchyAction, provider); 194 disposeAction(fFindReferencesInWorkingSetAction, provider); 195 } 196 fFindReferencesAction= null; 197 fFindReferencesInProjectAction= null; 198 fFindReferencesInHierarchyAction= null; 199 fFindReferencesInWorkingSetAction= null; 200 updateGlobalActionHandlers(); 201 super.dispose(); 202 } 203 204 private void updateGlobalActionHandlers() { 205 if (fActionBars != null) { 206 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_REFERENCES_IN_WORKSPACE, fFindReferencesAction); 207 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_REFERENCES_IN_PROJECT, fFindReferencesInProjectAction); 208 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_REFERENCES_IN_HIERARCHY, fFindReferencesInHierarchyAction); 209 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_REFERENCES_IN_WORKING_SET, fFindReferencesInWorkingSetAction); 210 } 211 } 212 213 private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) { 214 if (action != null) 215 provider.removeSelectionChangedListener(action); 216 } 217 } 218 | Popular Tags |