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 WriteReferencesSearchGroup extends ActionGroup { 48 49 private static final String MENU_TEXT= SearchMessages.group_writeReferences; 50 51 private IWorkbenchSite fSite; 52 private JavaEditor fEditor; 53 private IActionBars fActionBars; 54 55 private String fGroupId; 56 57 private FindWriteReferencesAction fFindWriteReferencesAction; 58 private FindWriteReferencesInProjectAction fFindWriteReferencesInProjectAction; 59 private FindWriteReferencesInHierarchyAction fFindWriteReferencesInHierarchyAction; 60 private FindWriteReferencesInWorkingSetAction fFindWriteReferencesInWorkingSetAction; 61 62 69 public WriteReferencesSearchGroup(IWorkbenchSite site) { 70 fSite= site; 71 fGroupId= IContextMenuConstants.GROUP_SEARCH; 72 73 fFindWriteReferencesAction= new FindWriteReferencesAction(site); 74 fFindWriteReferencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKSPACE); 75 76 fFindWriteReferencesInProjectAction= new FindWriteReferencesInProjectAction(site); 77 fFindWriteReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_PROJECT); 78 79 fFindWriteReferencesInHierarchyAction= new FindWriteReferencesInHierarchyAction(site); 80 fFindWriteReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_HIERARCHY); 81 82 fFindWriteReferencesInWorkingSetAction= new FindWriteReferencesInWorkingSetAction(site); 83 fFindWriteReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKING_SET); 84 85 ISelectionProvider provider= fSite.getSelectionProvider(); 87 ISelection selection= provider.getSelection(); 88 registerAction(fFindWriteReferencesAction, provider, selection); 89 registerAction(fFindWriteReferencesInProjectAction, provider, selection); 90 registerAction(fFindWriteReferencesInHierarchyAction, provider, selection); 91 registerAction(fFindWriteReferencesInWorkingSetAction, provider, selection); 92 } 93 94 98 public WriteReferencesSearchGroup(JavaEditor editor) { 99 fEditor= editor; 100 fSite= fEditor.getSite(); 101 fGroupId= ITextEditorActionConstants.GROUP_FIND; 102 103 fFindWriteReferencesAction= new FindWriteReferencesAction(fEditor); 104 fFindWriteReferencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKSPACE); 105 fEditor.setAction("SearchWriteAccessInWorkspace", fFindWriteReferencesAction); 107 fFindWriteReferencesInProjectAction= new FindWriteReferencesInProjectAction(fEditor); 108 fFindWriteReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_PROJECT); 109 fEditor.setAction("SearchWriteAccessInProject", fFindWriteReferencesInProjectAction); 111 fFindWriteReferencesInHierarchyAction= new FindWriteReferencesInHierarchyAction(fEditor); 112 fFindWriteReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_HIERARCHY); 113 fEditor.setAction("SearchWriteAccessInHierarchy", fFindWriteReferencesInHierarchyAction); 115 fFindWriteReferencesInWorkingSetAction= new FindWriteReferencesInWorkingSetAction(fEditor); 116 fFindWriteReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKING_SET); 117 fEditor.setAction("SearchWriteAccessInWorkingSet", fFindWriteReferencesInWorkingSetAction); } 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 FindWriteReferencesInWorkingSetAction(fEditor, workingSets), SearchUtil.toString(workingSets)); 135 else 136 action= new WorkingSetFindAction(fSite, new FindWriteReferencesInWorkingSetAction(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(fFindWriteReferencesAction, javaSearchMM); 148 addAction(fFindWriteReferencesInProjectAction, javaSearchMM); 149 addAction(fFindWriteReferencesInHierarchyAction, 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(fFindWriteReferencesInWorkingSetAction, 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(fFindWriteReferencesAction, provider); 180 disposeAction(fFindWriteReferencesInProjectAction, provider); 181 disposeAction(fFindWriteReferencesInHierarchyAction, provider); 182 disposeAction(fFindWriteReferencesInWorkingSetAction, provider); 183 } 184 fFindWriteReferencesAction= null; 185 fFindWriteReferencesInProjectAction= null; 186 fFindWriteReferencesInHierarchyAction= null; 187 fFindWriteReferencesInWorkingSetAction= null; 188 updateGlobalActionHandlers(); 189 super.dispose(); 190 } 191 192 private void updateGlobalActionHandlers() { 193 if (fActionBars != null) { 194 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_WRITE_ACCESS_IN_WORKSPACE, fFindWriteReferencesAction); 195 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_WRITE_ACCESS_IN_PROJECT, fFindWriteReferencesInProjectAction); 196 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_WRITE_ACCESS_IN_HIERARCHY, fFindWriteReferencesInHierarchyAction); 197 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_WRITE_ACCESS_IN_WORKING_SET, fFindWriteReferencesInWorkingSetAction); 198 } 199 } 200 201 private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) { 202 if (action != null) 203 provider.removeSelectionChangedListener(action); 204 } 205 } 206 | Popular Tags |