KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > DeclarationsSearchGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui.actions;
12
13 import java.util.Iterator JavaDoc;
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 /**
38  * Action group that adds the search for declarations actions to a
39  * context menu and the global menu bar.
40  *
41  * <p>
42  * This class may be instantiated; it is not intended to be subclassed.
43  * </p>
44  *
45  * @since 2.0
46  */

47 public class DeclarationsSearchGroup extends ActionGroup {
48
49     private static final String JavaDoc MENU_TEXT= SearchMessages.group_declarations;
50     
51     private IWorkbenchSite fSite;
52     private JavaEditor fEditor;
53     private IActionBars fActionBars;
54     
55     private String JavaDoc fGroupId;
56
57     private FindDeclarationsAction fFindDeclarationsAction;
58     private FindDeclarationsInProjectAction fFindDeclarationsInProjectAction;
59     private FindDeclarationsInWorkingSetAction fFindDeclarationsInWorkingSetAction;
60     private FindDeclarationsInHierarchyAction fFindDeclarationsInHierarchyAction;
61
62     /**
63      * Creates a new <code>DeclarationsSearchGroup</code>. The group requires
64      * that the selection provided by the site's selection provider is of type <code>
65      * IStructuredSelection</code>.
66      *
67      * @param site the workbench site that owns this action group
68      */

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         // register the actions as selection listeners
86
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     /**
95      * Note: This constructor is for internal use only. Clients should not call this constructor.
96      *
97      * @param editor the Java editor
98      */

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); //$NON-NLS-1$
108

109         fFindDeclarationsInProjectAction= new FindDeclarationsInProjectAction(fEditor);
110         fFindDeclarationsInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_PROJECTS);
111         fEditor.setAction("SearchDeclarationsInProjects", fFindDeclarationsInProjectAction); //$NON-NLS-1$
112

113         fFindDeclarationsInHierarchyAction= new FindDeclarationsInHierarchyAction(fEditor);
114         fFindDeclarationsInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_HIERARCHY);
115         fEditor.setAction("SearchDeclarationsInHierarchy", fFindDeclarationsInHierarchyAction); //$NON-NLS-1$
116

117         fFindDeclarationsInWorkingSetAction= new FindDeclarationsInWorkingSetAction(fEditor);
118         fFindDeclarationsInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_DECLARATIONS_IN_WORKING_SET);
119         fEditor.setAction("SearchDeclarationsInWorkingSet", fFindDeclarationsInWorkingSetAction); //$NON-NLS-1$
120
}
121
122     private void registerAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection) {
123         action.update(selection);
124         provider.addSelectionChangedListener(action);
125     }
126
127     /* (non-Javadoc)
128      * Method declared on ActionGroup.
129      */

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     /* (non-Javadoc)
155      * Method declared on ActionGroup.
156      */

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 JavaDoc 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     /*
176      * Method declared on ActionGroup.
177      */

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