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 DeclarationsSearchGroup extends ActionGroup { 48 49 private static final String MENU_TEXT= SearchMessages.group_declarations; 50 51 private IWorkbenchSite fSite; 52 private JavaEditor fEditor; 53 private IActionBars fActionBars; 54 55 private String fGroupId; 56 57 private FindDeclarationsAction fFindDeclarationsAction; 58 private FindDeclarationsInProjectAction fFindDeclarationsInProjectAction; 59 private FindDeclarationsInWorkingSetAction fFindDeclarationsInWorkingSetAction; 60 private FindDeclarationsInHierarchyAction fFindDeclarationsInHierarchyAction; 61 62 69 public DeclarationsSearchGroup(IWorkbenchSite site) { 70 fSite= site; 71 fGroupId= IContextMenuConstants.GROUP_SEARCH; 72 73 fFindDeclarationsAction= new FindDeclarationsAction(site); 74 fFindDeclarationsAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_WORKSPACE); 75 76 fFindDeclarationsInProjectAction= new FindDeclarationsInProjectAction(site); 77 fFindDeclarationsInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_PROJECTS); 78 79 fFindDeclarationsInHierarchyAction= new FindDeclarationsInHierarchyAction(site); 80 fFindDeclarationsInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_HIERARCHY); 81 82 fFindDeclarationsInWorkingSetAction= new FindDeclarationsInWorkingSetAction(site); 83 fFindDeclarationsInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_WORKING_SET); 84 85 ISelectionProvider provider= fSite.getSelectionProvider(); 87 ISelection selection= provider.getSelection(); 88 registerAction(fFindDeclarationsAction, provider, selection); 89 registerAction(fFindDeclarationsInProjectAction, provider, selection); 90 registerAction(fFindDeclarationsInHierarchyAction, provider, selection); 91 registerAction(fFindDeclarationsInWorkingSetAction, provider, selection); 92 } 93 94 99 public DeclarationsSearchGroup(JavaEditor editor) { 100 Assert.isNotNull(editor); 101 fEditor= editor; 102 fSite= fEditor.getSite(); 103 fGroupId= ITextEditorActionConstants.GROUP_FIND; 104 105 fFindDeclarationsAction= new FindDeclarationsAction(fEditor); 106 fFindDeclarationsAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_WORKSPACE); 107 fEditor.setAction("SearchDeclarationsInWorkspace", fFindDeclarationsAction); 109 fFindDeclarationsInProjectAction= new FindDeclarationsInProjectAction(fEditor); 110 fFindDeclarationsInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_PROJECTS); 111 fEditor.setAction("SearchDeclarationsInProjects", fFindDeclarationsInProjectAction); 113 fFindDeclarationsInHierarchyAction= new FindDeclarationsInHierarchyAction(fEditor); 114 fFindDeclarationsInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_HIERARCHY); 115 fEditor.setAction("SearchDeclarationsInHierarchy", fFindDeclarationsInHierarchyAction); 117 fFindDeclarationsInWorkingSetAction= new FindDeclarationsInWorkingSetAction(fEditor); 118 fFindDeclarationsInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_WORKING_SET); 119 fEditor.setAction("SearchDeclarationsInWorkingSet", fFindDeclarationsInWorkingSetAction); } 121 122 private void registerAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection) { 123 action.update(selection); 124 provider.addSelectionChangedListener(action); 125 } 126 127 130 public void fillActionBars(IActionBars actionBars) { 131 Assert.isNotNull(actionBars); 132 super.fillActionBars(actionBars); 133 fActionBars= actionBars; 134 updateGlobalActionHandlers(); 135 } 136 137 private void addAction(IAction action, IMenuManager manager) { 138 if (action.isEnabled()) { 139 manager.add(action); 140 } 141 } 142 143 private void addWorkingSetAction(IWorkingSet[] workingSets, IMenuManager manager) { 144 FindAction action; 145 if (fEditor != null) 146 action= new WorkingSetFindAction(fEditor, new FindDeclarationsInWorkingSetAction(fEditor, workingSets), SearchUtil.toString(workingSets)); 147 else 148 action= new WorkingSetFindAction(fSite, new FindDeclarationsInWorkingSetAction(fSite, workingSets), SearchUtil.toString(workingSets)); 149 action.update(getContext().getSelection()); 150 addAction(action, manager); 151 } 152 153 154 157 public void fillContextMenu(IMenuManager manager) { 158 IMenuManager javaSearchMM= new MenuManager(MENU_TEXT, IContextMenuConstants.GROUP_SEARCH); 159 addAction(fFindDeclarationsAction, javaSearchMM); 160 addAction(fFindDeclarationsInProjectAction, javaSearchMM); 161 addAction(fFindDeclarationsInHierarchyAction, javaSearchMM); 162 163 javaSearchMM.add(new Separator()); 164 165 Iterator iter= SearchUtil.getLRUWorkingSets().sortedIterator(); 166 while (iter.hasNext()) { 167 addWorkingSetAction((IWorkingSet[]) iter.next(), javaSearchMM); 168 } 169 addAction(fFindDeclarationsInWorkingSetAction, javaSearchMM); 170 171 if (!javaSearchMM.isEmpty()) 172 manager.appendToGroup(fGroupId, javaSearchMM); 173 } 174 175 178 public void dispose() { 179 ISelectionProvider provider= fSite.getSelectionProvider(); 180 if (provider != null) { 181 disposeAction(fFindDeclarationsAction, provider); 182 disposeAction(fFindDeclarationsInProjectAction, provider); 183 disposeAction(fFindDeclarationsInHierarchyAction, provider); 184 disposeAction(fFindDeclarationsInWorkingSetAction, provider); 185 } 186 fFindDeclarationsAction= null; 187 fFindDeclarationsInProjectAction= null; 188 fFindDeclarationsInHierarchyAction= null; 189 fFindDeclarationsInWorkingSetAction= null; 190 updateGlobalActionHandlers(); 191 super.dispose(); 192 } 193 194 private void updateGlobalActionHandlers() { 195 if (fActionBars != null) { 196 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_DECLARATIONS_IN_WORKSPACE, fFindDeclarationsAction); 197 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_DECLARATIONS_IN_PROJECT, fFindDeclarationsInProjectAction); 198 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_DECLARATIONS_IN_HIERARCHY, fFindDeclarationsInHierarchyAction); 199 fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_DECLARATIONS_IN_WORKING_SET, fFindDeclarationsInWorkingSetAction); 200 } 201 } 202 203 private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) { 204 if (action != null) 205 provider.removeSelectionChangedListener(action); 206 } 207 } 208 | Popular Tags |