KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21
22 import org.eclipse.ui.IActionBars;
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.actions.ActionGroup;
26 import org.eclipse.ui.keys.IBindingService;
27 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
28
29 import org.eclipse.jdt.ui.IContextMenuConstants;
30
31 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
32 import org.eclipse.jdt.internal.ui.search.SearchMessages;
33
34 /**
35  * Action group that adds the occurrences in file actions
36  * to a context menu and the global menu bar.
37  *
38  * <p>
39  * This class may be instantiated; it is not intended to be subclassed.
40  * </p>
41  *
42  * @since 3.1
43  */

44 public class OccurrencesSearchGroup extends ActionGroup {
45
46     private IWorkbenchSite fSite;
47     private JavaEditor fEditor;
48     private IActionBars fActionBars;
49     
50     private String JavaDoc fGroupId;
51
52     private FindOccurrencesInFileAction fOccurrencesInFileAction;
53     private FindExceptionOccurrencesAction fExceptionOccurrencesAction;
54     private FindImplementOccurrencesAction fFindImplementorOccurrencesAction;
55
56     /**
57      * Creates a new <code>ImplementorsSearchGroup</code>. The group
58      * requires that the selection provided by the site's selection provider
59      * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
60      *
61      * @param site the view part that owns this action group
62      */

63     public OccurrencesSearchGroup(IWorkbenchSite site) {
64         fSite= site;
65         fGroupId= IContextMenuConstants.GROUP_SEARCH;
66         
67         fOccurrencesInFileAction= new FindOccurrencesInFileAction(site);
68         fOccurrencesInFileAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE);
69         // Need to reset the label
70
fOccurrencesInFileAction.setText(SearchMessages.Search_FindOccurrencesInFile_shortLabel);
71
72         fExceptionOccurrencesAction= new FindExceptionOccurrencesAction(site);
73         fExceptionOccurrencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_EXCEPTION_OCCURRENCES_IN_FILE);
74
75         fFindImplementorOccurrencesAction= new FindImplementOccurrencesAction(site);
76         fFindImplementorOccurrencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENT_OCCURRENCES_IN_FILE);
77
78         // register the actions as selection listeners
79
ISelectionProvider provider= fSite.getSelectionProvider();
80         ISelection selection= provider.getSelection();
81         registerAction(fOccurrencesInFileAction, provider, selection);
82         registerAction(fExceptionOccurrencesAction, provider, selection);
83         registerAction(fFindImplementorOccurrencesAction, provider, selection);
84     }
85
86     /**
87      * Note: This constructor is for internal use only. Clients should not call this constructor.
88      *
89      * @param editor the Java editor
90      */

91     public OccurrencesSearchGroup(JavaEditor editor) {
92         fEditor= editor;
93         fSite= fEditor.getSite();
94         fGroupId= ITextEditorActionConstants.GROUP_FIND;
95
96         fOccurrencesInFileAction= new FindOccurrencesInFileAction(fEditor);
97         fOccurrencesInFileAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE);
98         // Need to reset the label
99
fOccurrencesInFileAction.setText(SearchMessages.Search_FindOccurrencesInFile_shortLabel);
100         fEditor.setAction("SearchOccurrencesInFile", fOccurrencesInFileAction); //$NON-NLS-1$
101

102         fExceptionOccurrencesAction= new FindExceptionOccurrencesAction(fEditor);
103         fExceptionOccurrencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_EXCEPTION_OCCURRENCES_IN_FILE);
104         fEditor.setAction("SearchExceptionOccurrences", fExceptionOccurrencesAction); //$NON-NLS-1$
105

106         fFindImplementorOccurrencesAction= new FindImplementOccurrencesAction(fEditor);
107         fFindImplementorOccurrencesAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENT_OCCURRENCES_IN_FILE);
108         fEditor.setAction("SearchImplementOccurrences", fFindImplementorOccurrencesAction); //$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 IAction[] getActions() {
117         IAction[] actions= new IAction[3];
118         actions[0]= fOccurrencesInFileAction;
119         actions[1]= fExceptionOccurrencesAction;
120         actions[2]= fFindImplementorOccurrencesAction;
121         return actions;
122     }
123
124     /*
125      * Method declared on ActionGroup.
126      */

127     public void fillContextMenu(IMenuManager manager) {
128         String JavaDoc menuText= SearchMessages.group_occurrences;
129         String JavaDoc shortcut= getShortcutString();
130         if (shortcut != null) {
131             menuText= menuText + '\t' + shortcut;
132         }
133
134         MenuManager javaSearchMM= new MenuManager(menuText, IContextMenuConstants.GROUP_SEARCH);
135         IAction[] actions= getActions();
136         for (int i= 0; i < actions.length; i++) {
137             IAction action= actions[i];
138             if (action.isEnabled())
139                 javaSearchMM.add(action);
140         }
141         
142         if (!javaSearchMM.isEmpty())
143             manager.appendToGroup(fGroupId, javaSearchMM);
144     }
145     
146     private String JavaDoc getShortcutString() {
147         IBindingService bindingService= (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class);
148         if (bindingService == null)
149             return null;
150         return bindingService.getBestActiveBindingFormattedFor(IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE_QUICK_MENU);
151     }
152
153     /*
154      * Method declared on ActionGroup.
155      */

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

166     public void dispose() {
167         ISelectionProvider provider= fSite.getSelectionProvider();
168         if (provider != null) {
169             disposeAction(fFindImplementorOccurrencesAction, provider);
170             disposeAction(fExceptionOccurrencesAction, provider);
171             disposeAction(fOccurrencesInFileAction, provider);
172         }
173         super.dispose();
174         fFindImplementorOccurrencesAction= null;
175         fExceptionOccurrencesAction= null;
176         fOccurrencesInFileAction= null;
177         updateGlobalActionHandlers();
178     }
179
180     private void updateGlobalActionHandlers() {
181         if (fActionBars != null) {
182             fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_OCCURRENCES_IN_FILE, fOccurrencesInFileAction);
183             fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_EXCEPTION_OCCURRENCES, fExceptionOccurrencesAction);
184             fActionBars.setGlobalActionHandler(JdtActionConstants.FIND_IMPLEMENT_OCCURRENCES, fFindImplementorOccurrencesAction);
185         }
186     }
187
188     private void disposeAction(ISelectionChangedListener action, ISelectionProvider provider) {
189         if (action != null)
190             provider.removeSelectionChangedListener(action);
191     }
192 }
193
Popular Tags