]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/actions/OccurrencesSearchMenuAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / actions / OccurrencesSearchMenuAction.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
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;
21
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;
27
28 import org.eclipse.jface.text.ITextSelection;
29
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;
35
36 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
37 import org.eclipse.jdt.ui.actions.JdtActionConstants;
38 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
42
43 /**
44  * <p>
45  * This is required because of
46  * https://bugs.eclipse.org/bugs/show_bug.cgi?id=79162
47  * and
48  * https://bugs.eclipse.org/bugs/show_bug.cgi?id=137679
49  * </p>
50  */
51 public class OccurrencesSearchMenuAction implements IWorkbenchWindowPulldownDelegate2 {
52
53         private static Action NO_ACTION_AVAILABLE= new Action(ActionMessages.OccurrencesSearchMenuAction_no_entries_available) {
54                 @Override
55                 public boolean isEnabled() {
56                         return false;
57                 }
58         };
59
60         private Menu fMenu;
61
62         private IPartService fPartService;
63         private RetargetAction[] fRetargetActions;
64
65         /**
66          * {@inheritDoc}
67          */
68         public Menu getMenu(Menu parent) {
69                 setMenu(new Menu(parent));
70                 fillMenu(fMenu);
71                 initMenu(fMenu);
72                 return fMenu;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         public Menu getMenu(Control parent) {
79                 setMenu(new Menu(parent));
80                 fillMenu(fMenu);
81                 initMenu(fMenu);
82                 return fMenu;
83         }
84
85         protected void initMenu(Menu menu) {
86                 menu.addMenuListener(new MenuAdapter() {
87                         @Override
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++) {
92                                         items[i].dispose();
93                                 }
94                                 fillMenu(m);
95                         }
96                 });
97         }
98
99         /**
100          * {@inheritDoc}
101          */
102         public void dispose() {
103                 setMenu(null);
104                 disposeSubmenuActions();
105         }
106
107         private RetargetAction createSubmenuAction(IPartService partService, String actionID, String text, String actionDefinitionId) {
108                 RetargetAction action= new RetargetAction(actionID, text);
109                 action.setActionDefinitionId(actionDefinitionId);
110
111                 partService.addPartListener(action);
112                 IWorkbenchPart activePart = partService.getActivePart();
113                 if (activePart != null) {
114                         action.partActivated(activePart);
115                 }
116                 return action;
117         }
118
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();
124                         }
125                 }
126                 fRetargetActions= null;
127                 fPartService= null;
128         }
129
130         /**
131          * {@inheritDoc}
132          */
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)
144                                 };
145                         }
146                 }
147         }
148
149         /**
150          * {@inheritDoc}
151          */
152         public void run(IAction a) {
153                 if (fRetargetActions == null)
154                         return;
155
156                 JavaEditor editor;
157                 ISelection selection;
158
159                 IWorkbenchPart activePart= JavaPlugin.getActivePage().getActivePart();
160                 if (activePart instanceof JavaEditor) {
161                         selection= getEditorSelection((JavaEditor) activePart);
162                         if (selection == null)
163                                 return;
164
165                         if (selection instanceof ITextSelection) {
166                                 editor= (JavaEditor) activePart;
167                         } else {
168                                 editor= null;
169                         }
170                 } else {
171                         editor= null;
172                         selection= activePart.getSite().getSelectionProvider().getSelection();
173                 }
174
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);
183                                 }
184                         }
185                 }
186                 if (activeActions.size() == 1) {
187                         activeActions.get(0).run();
188                 } else {
189                         new JDTQuickMenuCreator(editor) {
190                                 @Override
191                                 protected void fillMenu(IMenuManager menu) {
192                                         fillQuickMenu(menu, activeActions);
193                                 }
194                         }.createMenu();
195                 }
196         }
197
198         private void updateActions() {
199                 IWorkbenchPart activePart= JavaPlugin.getActivePage().getActivePart();
200                 if (!(activePart instanceof JavaEditor))
201                         return;
202
203                 ISelection javaSelection= getEditorSelection((JavaEditor) activePart);
204                 if (javaSelection == null)
205                         return;
206
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);
212                         }
213                 }
214         }
215
216         private ISelection getEditorSelection(JavaEditor editor) {
217                 return editor.generated_1581605054860211799();
218         }
219
220         /**
221          * {@inheritDoc}
222          */
223         public void selectionChanged(IAction action, ISelection selection) {
224         }
225
226         private void fillQuickMenu(IMenuManager manager, List<IAction> activeActions) {
227                 if (activeActions.isEmpty()) {
228                         manager.add(NO_ACTION_AVAILABLE);
229                 } else {
230                         for (int i= 0; i < activeActions.size(); i++) {
231                                 manager.add(activeActions.get(i));
232                         }
233                 }
234         }
235
236         /**
237          * The menu to show in the workbench menu
238          * @param menu the menu to fill
239          */
240         public void fillMenu(Menu menu) {
241                 if (fRetargetActions != null) {
242                         updateActions();
243                         for (int i= 0; i < fRetargetActions.length; i++) {
244                                 ActionContributionItem item= new ActionContributionItem(fRetargetActions[i]);
245                                 item.fill(menu, -1);
246                         }
247                 } else {
248                         // can only happen if 'init' was not called: programming error
249                         ActionContributionItem item= new ActionContributionItem(NO_ACTION_AVAILABLE);
250                         item.fill(menu, -1);
251                 }
252         }
253
254         private void setMenu(Menu menu) {
255                 if (fMenu != null) {
256                         fMenu.dispose();
257                 }
258                 fMenu = menu;
259         }
260 }