KickJava   Java API By Example, From Geeks To Geeks.

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


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 read 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 ReadReferencesSearchGroup extends ActionGroup {
48
49     private static final String JavaDoc MENU_TEXT= SearchMessages.group_readReferences;
50
51     private IWorkbenchSite fSite;
52     private JavaEditor fEditor;
53     private IActionBars fActionBars;
54     
55     private String JavaDoc fGroupId;
56
57     private FindReadReferencesAction fFindReadReferencesAction;
58     private FindReadReferencesInProjectAction fFindReadReferencesInProjectAction;
59     private FindReadReferencesInHierarchyAction fFindReadReferencesInHierarchyAction;
60     private FindReadReferencesInWorkingSetAction fFindReadReferencesInWorkingSetAction;
61     
62     /**
63      * Creates a new <code>ReadReferencesSearchGroup</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 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         // register the actions as selection listeners
86
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     /**
95      * Note: This constructor is for internal use only. Clients should not call this constructor.
96      * @param editor the Java editor
97      */

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

107         fFindReadReferencesInProjectAction= new FindReadReferencesInProjectAction(fEditor);
108         fFindReadReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_READ_ACCESS_IN_PROJECT);
109         fEditor.setAction("SearchReadAccessInProject", fFindReadReferencesInProjectAction); //$NON-NLS-1$
110

111         fFindReadReferencesInHierarchyAction= new FindReadReferencesInHierarchyAction(fEditor);
112         fFindReadReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_HIERARCHY);
113         fEditor.setAction("SearchReadAccessInHierarchy", fFindReadReferencesInHierarchyAction); //$NON-NLS-1$
114

115         fFindReadReferencesInWorkingSetAction= new FindReadReferencesInWorkingSetAction(fEditor);
116         fFindReadReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKING_SET);
117         fEditor.setAction("SearchReadAccessInWorkingSet", fFindReadReferencesInWorkingSetAction); //$NON-NLS-1$
118
}
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     /* (non-Javadoc)
143      * Method declared on ActionGroup.
144      */

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

166     public void fillActionBars(IActionBars actionBars) {
167         Assert.isNotNull(actionBars);
168         super.fillActionBars(actionBars);
169         fActionBars= actionBars;
170         updateGlobalActionHandlers();
171     }
172
173     /*
174      * Method declared on ActionGroup.
175      */

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