KickJava   Java API By Example, From Geeks To Geeks.

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


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 write 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 WriteReferencesSearchGroup extends ActionGroup {
48
49     private static final String JavaDoc MENU_TEXT= SearchMessages.group_writeReferences;
50
51     private IWorkbenchSite fSite;
52     private JavaEditor fEditor;
53     private IActionBars fActionBars;
54         
55     private String JavaDoc fGroupId;
56
57     private FindWriteReferencesAction fFindWriteReferencesAction;
58     private FindWriteReferencesInProjectAction fFindWriteReferencesInProjectAction;
59     private FindWriteReferencesInHierarchyAction fFindWriteReferencesInHierarchyAction;
60     private FindWriteReferencesInWorkingSetAction fFindWriteReferencesInWorkingSetAction;
61     
62     /**
63      * Creates a new <code>WriteReferencesSearchGroup</code>. The action
64      * requires that the selection provided by the site's selection provider is of
65      * type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
66      *
67      * @param site the view part that owns this action group
68      */

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

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

107         fFindWriteReferencesInProjectAction= new FindWriteReferencesInProjectAction(fEditor);
108         fFindWriteReferencesInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_PROJECT);
109         fEditor.setAction("SearchWriteAccessInProject", fFindWriteReferencesInProjectAction); //$NON-NLS-1$
110

111         fFindWriteReferencesInHierarchyAction= new FindWriteReferencesInHierarchyAction(fEditor);
112         fFindWriteReferencesInHierarchyAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_HIERARCHY);
113         fEditor.setAction("SearchWriteAccessInHierarchy", fFindWriteReferencesInHierarchyAction); //$NON-NLS-1$
114

115         fFindWriteReferencesInWorkingSetAction= new FindWriteReferencesInWorkingSetAction(fEditor);
116         fFindWriteReferencesInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_WRITE_ACCESS_IN_WORKING_SET);
117         fEditor.setAction("SearchWriteAccessInWorkingSet", fFindWriteReferencesInWorkingSetAction); //$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 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     /* (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(fFindWriteReferencesAction, javaSearchMM);
148         addAction(fFindWriteReferencesInProjectAction, javaSearchMM);
149         addAction(fFindWriteReferencesInHierarchyAction, 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(fFindWriteReferencesInWorkingSetAction, 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(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