1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.eclipse.swt.events.MenuAdapter;
17 import org.eclipse.swt.events.MenuEvent;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.swt.widgets.MenuItem;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.action.ActionContributionItem;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.text.ITextSelection;
30 import org.eclipse.ui.IPartService;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
34 import org.eclipse.ui.actions.RetargetAction;
36 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
37 import org.eclipse.jdt.ui.actions.JdtActionConstants;
38 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
45 * This is required because of
46 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=79162
48 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=137679
51 public class OccurrencesSearchMenuAction implements IWorkbenchWindowPulldownDelegate2 {
53 private static Action NO_ACTION_AVAILABLE= new Action(ActionMessages.OccurrencesSearchMenuAction_no_entries_available) {
55 public boolean isEnabled() {
62 private IPartService fPartService;
63 private RetargetAction[] fRetargetActions;
68 public Menu getMenu(Menu parent) {
69 setMenu(new Menu(parent));
78 public Menu getMenu(Control parent) {
79 setMenu(new Menu(parent));
85 protected void initMenu(Menu menu) {
86 menu.addMenuListener(new MenuAdapter() {
88 public void menuShown(MenuEvent e) {
89 Menu m= (Menu) e.widget;
90 MenuItem[] items= m.getItems();
91 for (int i= 0; i < items.length; i++) {
102 public void dispose() {
104 disposeSubmenuActions();
107 private RetargetAction createSubmenuAction(IPartService partService, String actionID, String text, String actionDefinitionId) {
108 RetargetAction action= new RetargetAction(actionID, text);
109 action.setActionDefinitionId(actionDefinitionId);
111 partService.addPartListener(action);
112 IWorkbenchPart activePart = partService.getActivePart();
113 if (activePart != null) {
114 action.partActivated(activePart);
119 private void disposeSubmenuActions() {
120 if (fPartService != null && fRetargetActions != null) {
121 for (int i= 0; i < fRetargetActions.length; i++) {
122 fPartService.removePartListener(fRetargetActions[i]);
123 fRetargetActions[i].dispose();
126 fRetargetActions= null;
133 public void init(IWorkbenchWindow window) {
134 disposeSubmenuActions(); // paranoia code: double initialization should not happen
135 if (window != null) {
136 fPartService= window.getPartService();
137 if (fPartService != null) {
138 fRetargetActions= new RetargetAction[] {
139 createSubmenuAction(fPartService, JdtActionConstants.FIND_OCCURRENCES_IN_FILE, ActionMessages.OccurrencesSearchMenuAction_occurrences_in_file_label, IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE),
140 createSubmenuAction(fPartService, JdtActionConstants.FIND_IMPLEMENT_OCCURRENCES, ActionMessages.OccurrencesSearchMenuAction_implementing_methods_label, IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENT_OCCURRENCES_IN_FILE),
141 createSubmenuAction(fPartService, JdtActionConstants.FIND_EXCEPTION_OCCURRENCES, ActionMessages.OccurrencesSearchMenuAction_throwing_exception_label, IJavaEditorActionDefinitionIds.SEARCH_EXCEPTION_OCCURRENCES_IN_FILE),
142 createSubmenuAction(fPartService, JdtActionConstants.FIND_METHOD_EXIT_OCCURRENCES, ActionMessages.OccurrencesSearchMenuAction_method_exits_label, IJavaEditorActionDefinitionIds.SEARCH_METHOD_EXIT_OCCURRENCES),
143 createSubmenuAction(fPartService, JdtActionConstants.FIND_BREAK_CONTINUE_TARGET_OCCURRENCES, ActionMessages.OccurrencesSearchMenuAction_break_continue_target_label, IJavaEditorActionDefinitionIds.SEARCH_BREAK_CONTINUE_TARGET_OCCURRENCES)
152 public void run(IAction a) {
153 if (fRetargetActions == null)
157 ISelection selection;
159 IWorkbenchPart activePart= JavaPlugin.getActivePage().getActivePart();
160 if (activePart instanceof JavaEditor) {
161 selection= getEditorSelection((JavaEditor) activePart);
162 if (selection == null)
165 if (selection instanceof ITextSelection) {
166 editor= (JavaEditor) activePart;
172 selection= activePart.getSite().getSelectionProvider().getSelection();
175 final ArrayList<IAction> activeActions= new ArrayList<IAction>(fRetargetActions.length);
176 for (int i= 0; i < fRetargetActions.length; i++) {
177 RetargetAction action= fRetargetActions[i];
178 IAction actionHandler= action.getActionHandler();
179 if (actionHandler instanceof SelectionDispatchAction) {
180 ((SelectionDispatchAction) actionHandler).update(selection);
181 if (actionHandler.isEnabled()) {
182 activeActions.add(actionHandler);
186 if (activeActions.size() == 1) {
187 activeActions.get(0).run();
189 new JDTQuickMenuCreator(editor) {
191 protected void fillMenu(IMenuManager menu) {
192 fillQuickMenu(menu, activeActions);
198 private void updateActions() {
199 IWorkbenchPart activePart= JavaPlugin.getActivePage().getActivePart();
200 if (!(activePart instanceof JavaEditor))
203 ISelection javaSelection= getEditorSelection((JavaEditor) activePart);
204 if (javaSelection == null)
207 for (int i= 0; i < fRetargetActions.length; i++) {
208 RetargetAction action= fRetargetActions[i];
209 IAction actionHandler= action.getActionHandler();
210 if (actionHandler instanceof SelectionDispatchAction) {
211 ((SelectionDispatchAction) actionHandler).update(javaSelection);
216 private ISelection getEditorSelection(JavaEditor editor) {
217 return editor.generated_1581605054860211799();
223 public void selectionChanged(IAction action, ISelection selection) {
226 private void fillQuickMenu(IMenuManager manager, List<IAction> activeActions) {
227 if (activeActions.isEmpty()) {
228 manager.add(NO_ACTION_AVAILABLE);
230 for (int i= 0; i < activeActions.size(); i++) {
231 manager.add(activeActions.get(i));
237 * The menu to show in the workbench menu
238 * @param menu the menu to fill
240 public void fillMenu(Menu menu) {
241 if (fRetargetActions != null) {
243 for (int i= 0; i < fRetargetActions.length; i++) {
244 ActionContributionItem item= new ActionContributionItem(fRetargetActions[i]);
248 // can only happen if 'init' was not called: programming error
249 ActionContributionItem item= new ActionContributionItem(NO_ACTION_AVAILABLE);
254 private void setMenu(Menu menu) {