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 ReadReferencesSearchGroup extends ActionGroup { 48 49 private static final String MENU_TEXT= SearchMessages.group_readReferences; 50 51 private IWorkbenchSite fSite; 52 private JavaEditor fEditor; 53 private IActionBars fActionBars; 54 55 private String fGroupId; 56 57 private FindReadReferencesAction fFindReadReferencesAction; 58 private FindReadReferencesInProjectAction fFindReadReferencesInProjectAction; 59 private FindReadReferencesInHierarchyAction fFindReadReferencesInHierarchyAction; 60 private FindReadReferencesInWorkingSetAction fFindReadReferencesInWorkingSetAction; 61 62 69 public ReadReferencesSearchGroup(IWorkbenchSite site) { 70 fSite= site; 71 fGroupId= IContextMenuConstants.GROUP_SEARCH; 72 73 fFindReadReferencesAction= new FindReadReferencesAction(site); 74 fFindReadReferencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKSPACE); 75 76 fFindReadReferencesInProjectAction= new FindReadReferencesInProjectAction(site); 77 fFindReadReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_READ_ACCESS_IN_PROJECT); 78 79 fFindReadReferencesInHierarchyAction= new FindReadReferencesInHierarchyAction(site); 80 fFindReadReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_HIERARCHY); 81 82 fFindReadReferencesInWorkingSetAction= new FindReadReferencesInWorkingSetAction(site); 83 fFindReadReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKING_SET); 84 85 ISelectionProvider provider= fSite.getSelectionProvider(); 87 ISelection selection= provider.getSelection(); 88 registerAction(fFindReadReferencesAction, provider, selection); 89 registerAction(fFindReadReferencesInProjectAction, provider, selection); 90 registerAction(fFindReadReferencesInHierarchyAction, provider, selection); 91 registerAction(fFindReadReferencesInWorkingSetAction, provider, selection); 92 } 93 94 98 public ReadReferencesSearchGroup(JavaEditor editor) { 99 fEditor= editor; 100 fSite= fEditor.getSite(); 101 fGroupId= ITextEditorActionConstants.GROUP_FIND; 102 103 fFindReadReferencesAction= new FindReadReferencesAction(fEditor); 104 fFindReadReferencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKSPACE); 105 fEditor.setAction("SearchReadAccessInWorkspace", fFindReadReferencesAction); 107 fFindReadReferencesInProjectAction= new FindReadReferencesInProjectAction(fEditor); 108 fFindReadReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_READ_ACCESS_IN_PROJECT); 109 fEditor.setAction("SearchReadAccessInProject", fFindReadReferencesInProjectAction); 111 fFindReadReferencesInHierarchyAction= new FindReadReferencesInHierarchyAction(fEditor); 112 fFindReadReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_HIERARCHY); 113 fEditor.setAction("SearchReadAccessInHierarchy", fFindReadReferencesInHierarchyAction); 115 fFindReadReferencesInWorkingSetAction= new FindReadReferencesInWorkingSetAction(fEditor); 116 fFindReadReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKING_SET); 117 fEditor.setAction("SearchReadAccessInWorkingSet", fFindReadReferencesInWorkingSetAction); } 119 120 private void registerAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection) { 121 action.update(selection); 122 provider.addSelectionChangedListener(action); 123 } 124 125 private void addAction(IAction action, IMenuManager manager) { 126 if (action.isEnabled()) { 127 manager.add(action); 128 } 129 } 130 131 private void addWorkingSetAction(IWorkingSet[] workingSets, IMenuManager manager) { 132 FindAction action; 133 if (fEditor != null) 134 action= new WorkingSetFindAction(fEditor, new FindReadReferencesInWorkingSetAction(fEditor, workingSets), SearchUtil.toString(workingSets)); 135 else 136 action= new WorkingSetFindAction(fSite, new FindReadReferencesInWorkingSetAction(fSite, workingSets), SearchUtil.toString(workingSets)); 137 action.update(getContext().getSelection()); 138 addAction(action, manager); 139 } 140 141 142 145 public void fillContextMenu(IMenuManager manager) { 146 MenuManager javaSearchMM= new MenuManager(MENU_TEXT, IContextMenuConstants.GROUP_SEARCH); 147 addAction(fFindReadReferencesAction, javaSearchMM); 148 addAction(fFindReadReferencesInProjectAction, javaSearchMM); 149 addAction(fFindReadReferencesInHierarchyAction, javaSearchMM); 150 151 javaSearchMM.add(new Separator()); 152 153 Iterator iter= SearchUtil.getLRUWorkingSets().sortedIterator(); 154 while (iter.hasNext()) { 155 addWorkingSetAction((IWorkingSet[]) iter.next(), javaSearchMM); 156 } 157 addAction(fFindReadReferencesInWorkingSetAction, javaSearchMM); 158 159 if (!javaSearchMM.isEmpty()) 160 manager.appendToGroup(fGroupId, javaSearchMM); 161 } 162 163 166 public void fillActionBars(IActionBars actionBars) { 167 Assert.isNotNull(actionBars); 168 super.fillActionBars(actionBars); 169 fActionBars= actionBars; 170 updateGlobalActionHandlers(); 171 } 172 173 176 public void dispose() { 177 ISelectionProvider provider= fSite.getSelectionProvider(); 178 if (provider != null) { 179 disposeAction(fFindReadReferencesAction, provider); 180 disposeAction(fFindReadReferencesInProjectAction, provider); 181 disposeAction(fFindReadReferencesInHierarchyAction, provider); 182 disposeAction(fFindReadReferencesInWorkingSetAction, provider); 183 } 184 fFindReadReferencesAction= null; 185 fFindReadReferencesInProjectAction= null; 186 fFindReadReferencesInHierarchyAction= null; 187 fFindReadReferencesInWorkingSetAction= null; 188 updateGlobalActionHandlers(); 189 super.dispose(); 190 } 191 192 private void updateGlobalActionHandlers() { 193 if (fActionBars != null) { 194 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_READ_ACCESS_IN_WORKSPACE, fFindReadReferencesAction); 195 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_READ_ACCESS_IN_PROJECT, fFindReadReferencesInProjectAction); 196 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_READ_ACCESS_IN_HIERARCHY, fFindReadReferencesInHierarchyAction); 197 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_READ_ACCESS_IN_WORKING_SET, fFindReadReferencesInWorkingSetAction); 198 } 199 } 200 201 private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) { 202 if (action != null) 203 provider.removeSelectionChangedListener(action); 204 } 205 } 206 | Popular Tags |