KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > OccurrencesSearchMenuAction


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.internal.ui.actions;
12
13 import org.eclipse.swt.widgets.Control;
14 import org.eclipse.swt.widgets.Menu;
15
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.ActionContributionItem;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.jface.viewers.ISelection;
21
22 import org.eclipse.ui.IPartService;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
26 import org.eclipse.ui.actions.RetargetAction;
27
28 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
29 import org.eclipse.jdt.ui.actions.JdtActionConstants;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
33 import org.eclipse.jdt.internal.ui.search.SearchMessages;
34
35 /**
36  * <p>
37  * This is required because of
38  * https://bugs.eclipse.org/bugs/show_bug.cgi?id=79162
39  * and
40  * https://bugs.eclipse.org/bugs/show_bug.cgi?id=137679
41  * </p>
42  */

43 public class OccurrencesSearchMenuAction implements IWorkbenchWindowPulldownDelegate2 {
44     
45     private static Action NO_ACTION_AVAILABLE= new Action(SearchMessages.group_occurrences_quickMenu_noEntriesAvailable) {
46         public boolean isEnabled() {
47             return false;
48         }
49     };
50
51     private Menu fMenu;
52
53     private IPartService fPartService;
54     private RetargetAction[] fRetargetActions;
55     
56     /**
57      * {@inheritDoc}
58      */

59     public Menu getMenu(Menu parent) {
60         setMenu(new Menu(parent));
61         fillMenu(fMenu);
62         return fMenu;
63     }
64
65     /**
66      * {@inheritDoc}
67      */

68     public Menu getMenu(Control parent) {
69         setMenu(new Menu(parent));
70         fillMenu(fMenu);
71         return fMenu;
72     }
73
74     /**
75      * {@inheritDoc}
76      */

77     public void dispose() {
78         setMenu(null);
79         disposeSubmenuActions();
80     }
81
82     private RetargetAction createSubmenuAction(IPartService partService, String JavaDoc actionID, String JavaDoc text, String JavaDoc actionDefinitionId) {
83         RetargetAction action= new RetargetAction(actionID, text);
84         action.setActionDefinitionId(actionDefinitionId);
85
86         partService.addPartListener(action);
87         IWorkbenchPart activePart = partService.getActivePart();
88         if (activePart != null) {
89             action.partActivated(activePart);
90         }
91         return action;
92     }
93     
94     private void disposeSubmenuActions() {
95         if (fPartService != null && fRetargetActions != null) {
96             for (int i= 0; i < fRetargetActions.length; i++) {
97                 fPartService.removePartListener(fRetargetActions[i]);
98                 fRetargetActions[i].dispose();
99             }
100         }
101         fRetargetActions= null;
102         fPartService= null;
103     }
104     
105     /**
106      * {@inheritDoc}
107      */

108     public void init(IWorkbenchWindow window) {
109         disposeSubmenuActions(); // paranoia code: double initialization should not happen
110
if (window != null) {
111             fPartService= window.getPartService();
112             if (fPartService != null) {
113                 fRetargetActions= new RetargetAction[] {
114                     createSubmenuAction(fPartService, JdtActionConstants.FIND_OCCURRENCES_IN_FILE, SearchMessages.Search_FindOccurrencesInFile_shortLabel, IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE),
115                     createSubmenuAction(fPartService, JdtActionConstants.FIND_IMPLEMENT_OCCURRENCES, ActionMessages.FindImplementOccurrencesAction_text, IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENT_OCCURRENCES_IN_FILE),
116                     createSubmenuAction(fPartService, JdtActionConstants.FIND_EXCEPTION_OCCURRENCES, ActionMessages.FindExceptionOccurrences_text, IJavaEditorActionDefinitionIds.SEARCH_EXCEPTION_OCCURRENCES_IN_FILE),
117                 };
118             }
119         }
120     }
121
122     /**
123      * {@inheritDoc}
124      */

125     public void run(IAction action) {
126         JavaEditor editor= null;
127         IWorkbenchPart activePart= JavaPlugin.getActivePage().getActivePart();
128         if (activePart instanceof JavaEditor)
129             editor= (JavaEditor) activePart;
130         
131         (new JDTQuickMenuAction(editor, IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE_QUICK_MENU) {
132             protected void fillMenu(IMenuManager menu) {
133                 fillQuickMenu(menu);
134             }
135         }).run();
136
137     }
138
139     /**
140      * {@inheritDoc}
141      */

142     public void selectionChanged(IAction action, ISelection selection) {
143     }
144     
145     private void fillQuickMenu(IMenuManager manager) {
146         IAction[] actions= fRetargetActions;
147         if (actions != null) {
148             boolean hasAction= false;
149             for (int i= 0; i < actions.length; i++) {
150                 IAction action= actions[i];
151                 if (action.isEnabled()) {
152                     hasAction= true;
153                     manager.add(action);
154                 }
155             }
156             if (!hasAction) {
157                 manager.add(NO_ACTION_AVAILABLE);
158             }
159         } else {
160             manager.add(NO_ACTION_AVAILABLE);
161         }
162     }
163     
164     /**
165      * The menu to show in the workbench menu
166      */

167     private void fillMenu(Menu menu) {
168         if (fRetargetActions != null) {
169             for (int i= 0; i < fRetargetActions.length; i++) {
170                 ActionContributionItem item= new ActionContributionItem(fRetargetActions[i]);
171                 item.fill(menu, -1);
172             }
173         } else {
174             // can only happen if 'init' was not called: programming error
175
ActionContributionItem item= new ActionContributionItem(NO_ACTION_AVAILABLE);
176             item.fill(menu, -1);
177         }
178     }
179     
180     private void setMenu(Menu menu) {
181         if (fMenu != null) {
182             fMenu.dispose();
183         }
184         fMenu = menu;
185     }
186 }
187
Popular Tags