KickJava   Java API By Example, From Geeks To Geeks.

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


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 references 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 ReferencesSearchGroup extends ActionGroup {
48
49     private static final String JavaDoc MENU_TEXT= SearchMessages.group_references;
50
51     private IWorkbenchSite fSite;
52     private JavaEditor fEditor;
53     private IActionBars fActionBars;
54     
55     private String JavaDoc fGroupId;
56     
57     private FindReferencesAction fFindReferencesAction;
58     private FindReferencesInProjectAction fFindReferencesInProjectAction;
59     private FindReferencesInHierarchyAction fFindReferencesInHierarchyAction;
60     private FindReferencesInWorkingSetAction fFindReferencesInWorkingSetAction;
61
62     /**
63      * Creates a new <code>ReferencesSearchGroup</code>. The group requires
64      * that the selection provided by the site's selection provider is of type <code>
65      * org.eclipse.jface.viewers.IStructuredSelection</code>.
66      *
67      * @param site the view part that owns this action group
68      */

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

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

109         fFindReferencesInProjectAction= new FindReferencesInProjectAction(fEditor);
110         fFindReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_PROJECT);
111         fEditor.setAction("SearchReferencesInProject", fFindReferencesInProjectAction); //$NON-NLS-1$
112

113         fFindReferencesInHierarchyAction= new FindReferencesInHierarchyAction(fEditor);
114         fFindReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_HIERARCHY);
115         fEditor.setAction("SearchReferencesInHierarchy", fFindReferencesInHierarchyAction); //$NON-NLS-1$
116

117         fFindReferencesInWorkingSetAction= new FindReferencesInWorkingSetAction(fEditor);
118         fFindReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_REFERENCES_IN_WORKING_SET);
119         fEditor.setAction("SearchReferencesInWorkingSet", fFindReferencesInWorkingSetAction); //$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     /**
128      * Note: this method is for internal use only. Clients should not call this method.
129      *
130      * @return the menu label
131      */

132     protected String JavaDoc getName() {
133         return MENU_TEXT;
134     }
135     
136     /* (non-Javadoc)
137      * Method declared in ActionGroup
138      */

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

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 JavaDoc 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     /*
186      * Overrides method declared in ActionGroup
187      */

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