KickJava   Java API By Example, From Geeks To Geeks.

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


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 implementors 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 ImplementorsSearchGroup extends ActionGroup {
48
49     private static final String JavaDoc MENU_TEXT= SearchMessages.group_implementors;
50
51     private IWorkbenchSite fSite;
52     private JavaEditor fEditor;
53     private IActionBars fActionBars;
54     
55     private String JavaDoc fGroupId;
56
57     private FindImplementorsAction fFindImplementorsAction;
58     private FindImplementorsInProjectAction fFindImplementorsInProjectAction;
59     private FindImplementorsInWorkingSetAction fFindImplementorsInWorkingSetAction;
60
61     /**
62      * Creates a new <code>ImplementorsSearchGroup</code>. The group
63      * requires that the selection provided by the site's selection provider
64      * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
65      *
66      * @param site the view part that owns this action group
67      */

68     public ImplementorsSearchGroup(IWorkbenchSite site) {
69         fSite= site;
70         fGroupId= IContextMenuConstants.GROUP_SEARCH;
71
72         fFindImplementorsAction= new FindImplementorsAction(site);
73         fFindImplementorsAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_WORKSPACE);
74
75         fFindImplementorsInProjectAction= new FindImplementorsInProjectAction(site);
76         fFindImplementorsInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_PROJECT);
77
78         fFindImplementorsInWorkingSetAction= new FindImplementorsInWorkingSetAction(site);
79         fFindImplementorsInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_WORKING_SET);
80
81         // register the actions as selection listeners
82
ISelectionProvider provider= fSite.getSelectionProvider();
83         ISelection selection= provider.getSelection();
84         registerAction(fFindImplementorsAction, provider, selection);
85         registerAction(fFindImplementorsInProjectAction, provider, selection);
86         registerAction(fFindImplementorsInWorkingSetAction, provider, selection);
87     }
88
89     /**
90      * Note: This constructor is for internal use only. Clients should not call this constructor.
91      * @param editor the Java editor
92      */

93     public ImplementorsSearchGroup(JavaEditor editor) {
94         fEditor= editor;
95         fSite= fEditor.getSite();
96         fGroupId= ITextEditorActionConstants.GROUP_FIND;
97
98         fFindImplementorsAction= new FindImplementorsAction(fEditor);
99         fFindImplementorsAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_WORKSPACE);
100         fEditor.setAction("SearchImplementorsInWorkspace", fFindImplementorsAction); //$NON-NLS-1$
101

102         fFindImplementorsInProjectAction= new FindImplementorsInProjectAction(fEditor);
103         fFindImplementorsInProjectAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_PROJECT);
104         fEditor.setAction("SearchImplementorsInProject", fFindImplementorsInProjectAction); //$NON-NLS-1$
105

106         fFindImplementorsInWorkingSetAction= new FindImplementorsInWorkingSetAction(fEditor);
107         fFindImplementorsInWorkingSetAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENTORS_IN_WORKING_SET);
108         fEditor.setAction("SearchImplementorsInWorkingSet", fFindImplementorsInWorkingSetAction); //$NON-NLS-1$
109
}
110
111     private void registerAction(SelectionDispatchAction action, ISelectionProvider provider, ISelection selection){
112         action.update(selection);
113         provider.addSelectionChangedListener(action);
114     }
115     
116     private void addAction(IAction action, IMenuManager manager) {
117         if (action.isEnabled()) {
118             manager.add(action);
119         }
120     }
121     
122     private void addWorkingSetAction(IWorkingSet[] workingSets, IMenuManager manager) {
123         FindAction action;
124         if (fEditor != null)
125             action= new WorkingSetFindAction(fEditor, new FindImplementorsInWorkingSetAction(fEditor, workingSets), SearchUtil.toString(workingSets));
126         else
127             action= new WorkingSetFindAction(fSite, new FindImplementorsInWorkingSetAction(fSite, workingSets), SearchUtil.toString(workingSets));
128         action.update(getContext().getSelection());
129         addAction(action, manager);
130     }
131     
132     
133     /* (non-Javadoc)
134      * Method declared on ActionGroup.
135      */

136     public void fillContextMenu(IMenuManager manager) {
137         MenuManager javaSearchMM= new MenuManager(MENU_TEXT, IContextMenuConstants.GROUP_SEARCH);
138         addAction(fFindImplementorsAction, javaSearchMM);
139         addAction(fFindImplementorsInProjectAction, javaSearchMM);
140         
141         javaSearchMM.add(new Separator());
142         
143         Iterator JavaDoc iter= SearchUtil.getLRUWorkingSets().sortedIterator();
144         while (iter.hasNext()) {
145             addWorkingSetAction((IWorkingSet[]) iter.next(), javaSearchMM);
146         }
147         addAction(fFindImplementorsInWorkingSetAction, javaSearchMM);
148
149         if (!javaSearchMM.isEmpty())
150             manager.appendToGroup(fGroupId, javaSearchMM);
151     }
152     
153
154     /*
155      * Method declared on ActionGroup.
156      */

157     public void fillActionBars(IActionBars actionBars) {
158         Assert.isNotNull(actionBars);
159         super.fillActionBars(actionBars);
160         fActionBars= actionBars;
161         updateGlobalActionHandlers();
162     }
163     
164     /*
165      * Method declared on ActionGroup.
166      */

167     public void dispose() {
168         ISelectionProvider provider= fSite.getSelectionProvider();
169         if (provider != null) {
170             disposeAction(fFindImplementorsAction, provider);
171             disposeAction(fFindImplementorsInProjectAction, provider);
172             disposeAction(fFindImplementorsInWorkingSetAction, provider);
173         }
174         super.dispose();
175         fFindImplementorsAction= null;
176         fFindImplementorsInProjectAction= null;
177         fFindImplementorsInWorkingSetAction= null;
178         updateGlobalActionHandlers();
179     }
180
181     private void updateGlobalActionHandlers() {
182         if (fActionBars != null) {
183             fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_IMPLEMENTORS_IN_WORKSPACE, fFindImplementorsAction);
184             fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_IMPLEMENTORS_IN_PROJECT, fFindImplementorsInProjectAction);
185             fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_IMPLEMENTORS_IN_WORKING_SET, fFindImplementorsInWorkingSetAction);
186         }
187     }
188
189     private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) {
190         if (action != null)
191             provider.removeSelectionChangedListener(action);
192     }
193 }
194
195
196
Popular Tags