001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import org.eclipse.swt.widgets.Control;
013: import org.eclipse.swt.widgets.Menu;
014:
015: import org.eclipse.jface.action.Action;
016: import org.eclipse.jface.action.ActionContributionItem;
017: import org.eclipse.jface.action.IAction;
018: import org.eclipse.jface.action.IMenuManager;
019: import org.eclipse.jface.viewers.ISelection;
020:
021: import org.eclipse.ui.IPartService;
022: import org.eclipse.ui.IWorkbenchPart;
023: import org.eclipse.ui.IWorkbenchWindow;
024: import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
025: import org.eclipse.ui.actions.RetargetAction;
026:
027: import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
028: import org.eclipse.jdt.ui.actions.JdtActionConstants;
029:
030: import org.eclipse.jdt.internal.ui.JavaPlugin;
031: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
032: import org.eclipse.jdt.internal.ui.search.SearchMessages;
033:
034: /**
035: * <p>
036: * This is required because of
037: * https://bugs.eclipse.org/bugs/show_bug.cgi?id=79162
038: * and
039: * https://bugs.eclipse.org/bugs/show_bug.cgi?id=137679
040: * </p>
041: */
042: public class OccurrencesSearchMenuAction implements
043: IWorkbenchWindowPulldownDelegate2 {
044:
045: private static Action NO_ACTION_AVAILABLE = new Action(
046: SearchMessages.group_occurrences_quickMenu_noEntriesAvailable) {
047: public boolean isEnabled() {
048: return false;
049: }
050: };
051:
052: private Menu fMenu;
053:
054: private IPartService fPartService;
055: private RetargetAction[] fRetargetActions;
056:
057: /**
058: * {@inheritDoc}
059: */
060: public Menu getMenu(Menu parent) {
061: setMenu(new Menu(parent));
062: fillMenu(fMenu);
063: return fMenu;
064: }
065:
066: /**
067: * {@inheritDoc}
068: */
069: public Menu getMenu(Control parent) {
070: setMenu(new Menu(parent));
071: fillMenu(fMenu);
072: return fMenu;
073: }
074:
075: /**
076: * {@inheritDoc}
077: */
078: public void dispose() {
079: setMenu(null);
080: disposeSubmenuActions();
081: }
082:
083: private RetargetAction createSubmenuAction(
084: IPartService partService, String actionID, String text,
085: String actionDefinitionId) {
086: RetargetAction action = new RetargetAction(actionID, text);
087: action.setActionDefinitionId(actionDefinitionId);
088:
089: partService.addPartListener(action);
090: IWorkbenchPart activePart = partService.getActivePart();
091: if (activePart != null) {
092: action.partActivated(activePart);
093: }
094: return action;
095: }
096:
097: private void disposeSubmenuActions() {
098: if (fPartService != null && fRetargetActions != null) {
099: for (int i = 0; i < fRetargetActions.length; i++) {
100: fPartService.removePartListener(fRetargetActions[i]);
101: fRetargetActions[i].dispose();
102: }
103: }
104: fRetargetActions = null;
105: fPartService = null;
106: }
107:
108: /**
109: * {@inheritDoc}
110: */
111: public void init(IWorkbenchWindow window) {
112: disposeSubmenuActions(); // paranoia code: double initialization should not happen
113: if (window != null) {
114: fPartService = window.getPartService();
115: if (fPartService != null) {
116: fRetargetActions = new RetargetAction[] {
117: createSubmenuAction(
118: fPartService,
119: JdtActionConstants.FIND_OCCURRENCES_IN_FILE,
120: SearchMessages.Search_FindOccurrencesInFile_shortLabel,
121: IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE),
122: createSubmenuAction(
123: fPartService,
124: JdtActionConstants.FIND_IMPLEMENT_OCCURRENCES,
125: ActionMessages.FindImplementOccurrencesAction_text,
126: IJavaEditorActionDefinitionIds.SEARCH_IMPLEMENT_OCCURRENCES_IN_FILE),
127: createSubmenuAction(
128: fPartService,
129: JdtActionConstants.FIND_EXCEPTION_OCCURRENCES,
130: ActionMessages.FindExceptionOccurrences_text,
131: IJavaEditorActionDefinitionIds.SEARCH_EXCEPTION_OCCURRENCES_IN_FILE), };
132: }
133: }
134: }
135:
136: /**
137: * {@inheritDoc}
138: */
139: public void run(IAction action) {
140: JavaEditor editor = null;
141: IWorkbenchPart activePart = JavaPlugin.getActivePage()
142: .getActivePart();
143: if (activePart instanceof JavaEditor)
144: editor = (JavaEditor) activePart;
145:
146: (new JDTQuickMenuAction(
147: editor,
148: IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE_QUICK_MENU) {
149: protected void fillMenu(IMenuManager menu) {
150: fillQuickMenu(menu);
151: }
152: }).run();
153:
154: }
155:
156: /**
157: * {@inheritDoc}
158: */
159: public void selectionChanged(IAction action, ISelection selection) {
160: }
161:
162: private void fillQuickMenu(IMenuManager manager) {
163: IAction[] actions = fRetargetActions;
164: if (actions != null) {
165: boolean hasAction = false;
166: for (int i = 0; i < actions.length; i++) {
167: IAction action = actions[i];
168: if (action.isEnabled()) {
169: hasAction = true;
170: manager.add(action);
171: }
172: }
173: if (!hasAction) {
174: manager.add(NO_ACTION_AVAILABLE);
175: }
176: } else {
177: manager.add(NO_ACTION_AVAILABLE);
178: }
179: }
180:
181: /**
182: * The menu to show in the workbench menu
183: */
184: private void fillMenu(Menu menu) {
185: if (fRetargetActions != null) {
186: for (int i = 0; i < fRetargetActions.length; i++) {
187: ActionContributionItem item = new ActionContributionItem(
188: fRetargetActions[i]);
189: item.fill(menu, -1);
190: }
191: } else {
192: // can only happen if 'init' was not called: programming error
193: ActionContributionItem item = new ActionContributionItem(
194: NO_ACTION_AVAILABLE);
195: item.fill(menu, -1);
196: }
197: }
198:
199: private void setMenu(Menu menu) {
200: if (fMenu != null) {
201: fMenu.dispose();
202: }
203: fMenu = menu;
204: }
205: }
|