Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 266938
Collapse All | Expand All

(-)ui/org/eclipse/jdt/internal/ui/actions/QuickMenuAction.java (-302 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 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.custom.StyledText;
17
import org.eclipse.swt.graphics.GC;
18
import org.eclipse.swt.graphics.Point;
19
import org.eclipse.swt.graphics.Rectangle;
20
import org.eclipse.swt.widgets.Control;
21
import org.eclipse.swt.widgets.Display;
22
import org.eclipse.swt.widgets.Menu;
23
import org.eclipse.swt.widgets.Table;
24
import org.eclipse.swt.widgets.TableItem;
25
import org.eclipse.swt.widgets.Tree;
26
import org.eclipse.swt.widgets.TreeItem;
27
28
import org.eclipse.jface.action.Action;
29
import org.eclipse.jface.action.IMenuManager;
30
import org.eclipse.jface.action.MenuManager;
31
32
import org.eclipse.ui.PlatformUI;
33
import org.eclipse.ui.keys.IBindingService;
34
35
/**
36
 * A quick menu actions provides support to assign short cuts
37
 * to sub menus.
38
 *
39
 * @since 3.0
40
 */
41
public abstract class QuickMenuAction extends Action {
42
43
	private static final int CHAR_INDENT= 3;
44
45
	/**
46
	 * Creates a new quick menu action with the given command id.
47
	 *
48
	 * @param commandId the command id of the short cut used to open
49
	 *  the sub menu
50
	 */
51
	public QuickMenuAction(String commandId) {
52
		setActionDefinitionId(commandId);
53
	}
54
55
	/**
56
	 * {@inheritDoc}
57
	 */
58
	public void run() {
59
		Display display= Display.getCurrent();
60
		if (display == null)
61
			return;
62
		Control focus= display.getFocusControl();
63
		if (focus == null || focus.isDisposed())
64
			return;
65
66
		MenuManager menu= new MenuManager();
67
		fillMenu(menu);
68
		final Menu widget= menu.createContextMenu(focus.getShell());
69
		Point location= computeMenuLocation(focus, widget);
70
		if (location == null)
71
			return;
72
		widget.setLocation(location);
73
		widget.setVisible(true);
74
		while (!widget.isDisposed() && widget.isVisible()) {
75
			if (!display.readAndDispatch())
76
				display.sleep();
77
		}
78
		if (!widget.isDisposed()) {
79
			widget.dispose();
80
		}
81
	}
82
83
	/**
84
	 * Hook to fill a menu manager with the items of the sub menu.
85
	 *
86
	 * @param menu the sub menu to fill
87
	 */
88
	protected abstract void fillMenu(IMenuManager menu);
89
90
	/**
91
	 * Adds the shortcut to the given menu text and returns it.
92
	 *
93
	 * @param menuText the menu text
94
	 * @return the menu text with the shortcut
95
	 * @since 3.1
96
	 */
97
	public String addShortcut(String menuText) {
98
		String shortcut= getShortcutString();
99
		if (menuText == null || shortcut == null)
100
			return menuText;
101
102
		return menuText + '\t' + shortcut;
103
	}
104
105
	/**
106
	 * Returns the shortcut assigned to the sub menu or <code>null</code> if
107
	 * no short cut is assigned.
108
	 *
109
	 * @return the shortcut as a human readable string or <code>null</code>
110
	 */
111
	private String getShortcutString() {
112
		IBindingService bindingService= (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class);
113
		if (bindingService == null)
114
			return null;
115
		return bindingService.getBestActiveBindingFormattedFor(getActionDefinitionId());
116
	}
117
118
	private Point computeMenuLocation(Control focus, Menu menu) {
119
		Point cursorLocation= focus.getDisplay().getCursorLocation();
120
		Rectangle clientArea= null;
121
		Point result= null;
122
		if (focus instanceof StyledText) {
123
			StyledText styledText= (StyledText)focus;
124
			clientArea= styledText.getClientArea();
125
			result= computeMenuLocation(styledText);
126
		} else if (focus instanceof Tree) {
127
			Tree tree= (Tree)focus;
128
			clientArea= tree.getClientArea();
129
			result= computeMenuLocation(tree);
130
		} else if (focus instanceof Table) {
131
			Table table= (Table)focus;
132
			clientArea= table.getClientArea();
133
			result= computeMenuLocation(table);
134
		}
135
		if (result == null) {
136
			result= focus.toControl(cursorLocation);
137
		}
138
		if (clientArea != null && !clientArea.contains(result)) {
139
			result= new Point(
140
				clientArea.x + clientArea.width  / 2,
141
				clientArea.y + clientArea.height / 2);
142
		}
143
		Rectangle shellArea= focus.getShell().getClientArea();
144
		if (!shellArea.contains(focus.getShell().toControl(focus.toDisplay(result)))) {
145
			result= new Point(
146
				shellArea.x + shellArea.width  / 2,
147
				shellArea.y + shellArea.height / 2);
148
		}
149
		return focus.toDisplay(result);
150
	}
151
152
	/**
153
	 * Hook to compute the menu location if the focus widget is
154
	 * a styled text widget.
155
	 *
156
	 * @param text the styled text widget that has the focus
157
	 *
158
	 * @return a widget relative position of the menu to pop up or
159
	 *  <code>null</code> if now position inside the widget can
160
	 *  be computed
161
	 */
162
	protected Point computeMenuLocation(StyledText text) {
163
		int offset= text.getCaretOffset();
164
		Point result= text.getLocationAtOffset(offset);
165
		result.y+= text.getLineHeight(offset);
166
		if (!text.getClientArea().contains(result))
167
			return null;
168
		return result;
169
	}
170
171
	/**
172
	 * Hook to compute the menu location if the focus widget is
173
	 * a tree widget.
174
	 *
175
	 * @param tree the tree widget that has the focus
176
	 *
177
	 * @return a widget relative position of the menu to pop up or
178
	 *  <code>null</code> if now position inside the widget can
179
	 *  be computed
180
	 */
181
	protected Point computeMenuLocation(Tree tree) {
182
		TreeItem[] items= tree.getSelection();
183
		Rectangle clientArea= tree.getClientArea();
184
		switch (items.length) {
185
			case 0:
186
				return null;
187
			case 1:
188
				Rectangle bounds= items[0].getBounds();
189
				Rectangle intersect= clientArea.intersection(bounds);
190
				if (intersect != null && intersect.height == bounds.height) {
191
					return new Point(
192
						Math.max(0, bounds.x + getAvarageCharWith(tree) * CHAR_INDENT),
193
						bounds.y + bounds.height);
194
				} else {
195
					return null;
196
				}
197
			default:
198
				Rectangle[] rectangles= new Rectangle[items.length];
199
				for (int i= 0; i < rectangles.length; i++) {
200
					rectangles[i]= items[i].getBounds();
201
				}
202
				Point cursorLocation= tree.getDisplay().getCursorLocation();
203
				Point result= findBestLocation(getIncludedPositions(rectangles, clientArea),
204
					tree.toControl(cursorLocation));
205
				if (result != null)
206
					result.x= result.x + getAvarageCharWith(tree) * CHAR_INDENT;
207
				return result;
208
		}
209
	}
210
211
	/**
212
	 * Hook to compute the menu location if the focus widget is
213
	 * a table widget.
214
	 *
215
	 * @param table the table widget that has the focus
216
	 *
217
	 * @return a widget relative position of the menu to pop up or
218
	 *  <code>null</code> if now position inside the widget can
219
	 *  be computed
220
	 */
221
	protected Point computeMenuLocation(Table table) {
222
		TableItem[] items= table.getSelection();
223
		Rectangle clientArea= table.getClientArea();
224
		switch (items.length) {
225
			case 0: {
226
				return null;
227
			} case 1: {
228
				Rectangle bounds= items[0].getBounds(0);
229
				Rectangle iBounds= items[0].getImageBounds(0);
230
				Rectangle intersect= clientArea.intersection(bounds);
231
				if (intersect != null && intersect.height == bounds.height) {
232
					return new Point(
233
						Math.max(0, bounds.x + iBounds.width + getAvarageCharWith(table) * CHAR_INDENT),
234
						bounds.y + bounds.height);
235
				} else {
236
					return null;
237
				}
238
			} default: {
239
				Rectangle[] rectangles= new Rectangle[items.length];
240
				for (int i= 0; i < rectangles.length; i++) {
241
					rectangles[i]= items[i].getBounds(0);
242
				}
243
				Rectangle iBounds= items[0].getImageBounds(0);
244
				Point cursorLocation= table.getDisplay().getCursorLocation();
245
				Point result= findBestLocation(getIncludedPositions(rectangles, clientArea),
246
					table.toControl(cursorLocation));
247
				if (result != null)
248
					result.x= result.x + iBounds.width + getAvarageCharWith(table) * CHAR_INDENT;
249
				return result;
250
			}
251
		}
252
	}
253
254
	private Point[] getIncludedPositions(Rectangle[] rectangles, Rectangle widgetBounds) {
255
		List result= new ArrayList();
256
		for (int i= 0; i < rectangles.length; i++) {
257
			Rectangle rectangle= rectangles[i];
258
			Rectangle intersect= widgetBounds.intersection(rectangle);
259
			if (intersect != null && intersect.height == rectangle.height) {
260
				result.add(new Point(intersect.x, intersect.y + intersect.height));
261
			}
262
		}
263
		return (Point[]) result.toArray(new Point[result.size()]);
264
	}
265
266
	private Point findBestLocation(Point[] points, Point relativeCursor) {
267
		Point result= null;
268
		double bestDist= Double.MAX_VALUE;
269
		for (int i= 0; i < points.length; i++) {
270
			Point point= points[i];
271
			int a= 0;
272
			int b= 0;
273
			if (point.x > relativeCursor.x) {
274
				a= point.x - relativeCursor.x;
275
			} else {
276
				a= relativeCursor.x - point.x;
277
			}
278
			if (point.y > relativeCursor.y) {
279
				b= point.y - relativeCursor.y;
280
			} else {
281
				b= relativeCursor.y - point.y;
282
			}
283
			double dist= Math.sqrt(a * a + b * b);
284
			if (dist < bestDist) {
285
				result= point;
286
				bestDist= dist;
287
			}
288
		}
289
		return result;
290
	}
291
292
	private int getAvarageCharWith(Control control) {
293
		GC gc= null;
294
		try {
295
			gc= new GC(control);
296
			return gc.getFontMetrics().getAverageCharWidth();
297
		} finally {
298
			if (gc != null)
299
				gc.dispose();
300
		}
301
	}
302
}
(-)ui/org/eclipse/jdt/internal/ui/actions/WorkbenchRunnableAdapter.java (-2 / +9 lines)
Lines 33-39 Link Here
33
33
34
/**
34
/**
35
 * An {@link IRunnableWithProgress} that adapts an {@link IWorkspaceRunnable} so that is can be
35
 * An {@link IRunnableWithProgress} that adapts an {@link IWorkspaceRunnable} so that is can be
36
 * executed inside an {@link IRunnableContext}.
36
 * executed inside an {@link IRunnableContext}. The runnable is run as an
37
 * {@linkplain JavaCore#run(IWorkspaceRunnable, ISchedulingRule, IProgressMonitor) atomic Java model operation}.
37
 * <p>
38
 * <p>
38
 * {@link OperationCanceledException}s thrown by the
39
 * {@link OperationCanceledException}s thrown by the
39
 * adapted runnable are caught and re-thrown as {@link InterruptedException}s.
40
 * adapted runnable are caught and re-thrown as {@link InterruptedException}s.
Lines 71-77 Link Here
71
	 * 
72
	 * 
72
	 * @param runnable the runnable
73
	 * @param runnable the runnable
73
	 * @param rule the scheduling rule, or <code>null</code>
74
	 * @param rule the scheduling rule, or <code>null</code>
74
	 * @param transfer <code>true</code> iff the rule is to be transfered to the model context
75
	 * @param transfer <code>true</code> iff the rule is to be transfered to the modal context
75
	 *            thread
76
	 *            thread
76
	 */
77
	 */
77
	public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule, boolean transfer) {
78
	public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule, boolean transfer) {
Lines 80-85 Link Here
80
		fTransfer= transfer;
81
		fTransfer= transfer;
81
	}
82
	}
82
83
84
	/**
85
	 * Returns the scheduling rule, or <code>null</code> if none.
86
	 * 
87
	 * @return the scheduling rule, or <code>null</code> if none
88
	 * @since 3.5
89
	 */
83
	public ISchedulingRule getSchedulingRule() {
90
	public ISchedulingRule getSchedulingRule() {
84
		return fRule;
91
		return fRule;
85
	}
92
	}
(-)ui/org/eclipse/jdt/internal/ui/actions/JDTQuickMenuAction.java (-85 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 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.custom.StyledText;
14
import org.eclipse.swt.graphics.Point;
15
16
import org.eclipse.jface.text.IRegion;
17
import org.eclipse.jface.text.ITextSelection;
18
import org.eclipse.jface.text.ITextViewerExtension5;
19
import org.eclipse.jface.text.Region;
20
import org.eclipse.jface.text.source.ISourceViewer;
21
22
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
23
import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
24
25
26
public abstract class JDTQuickMenuAction extends QuickMenuAction {
27
28
	private JavaEditor fEditor;
29
30
	public JDTQuickMenuAction(String commandId) {
31
		super(commandId);
32
	}
33
34
	public JDTQuickMenuAction(JavaEditor editor, String commandId) {
35
		super(commandId);
36
		fEditor= editor;
37
	}
38
39
	protected JavaEditor getJavaEditor() {
40
		return fEditor;
41
	}
42
43
	protected Point computeMenuLocation(StyledText text) {
44
		if (fEditor == null || text != fEditor.getViewer().getTextWidget())
45
			return null;
46
		return computeWordStart();
47
	}
48
49
	private Point computeWordStart() {
50
		ITextSelection selection= (ITextSelection)fEditor.getSelectionProvider().getSelection();
51
		IRegion textRegion= JavaWordFinder.findWord(fEditor.getViewer().getDocument(), selection.getOffset());
52
		if (textRegion == null)
53
			return null;
54
55
		IRegion widgetRegion= modelRange2WidgetRange(textRegion);
56
		if (widgetRegion == null)
57
			return null;
58
59
		int start= widgetRegion.getOffset();
60
61
		StyledText styledText= fEditor.getViewer().getTextWidget();
62
		Point result= styledText.getLocationAtOffset(start);
63
		result.y+= styledText.getLineHeight(start);
64
65
		if (!styledText.getClientArea().contains(result))
66
			return null;
67
		return result;
68
	}
69
70
	private IRegion modelRange2WidgetRange(IRegion region) {
71
		ISourceViewer viewer= fEditor.getViewer();
72
		if (viewer instanceof ITextViewerExtension5) {
73
			ITextViewerExtension5 extension= (ITextViewerExtension5)viewer;
74
			return extension.modelRange2WidgetRange(region);
75
		}
76
77
		IRegion visibleRegion= viewer.getVisibleRegion();
78
		int start= region.getOffset() - visibleRegion.getOffset();
79
		int end= start + region.getLength();
80
		if (end > visibleRegion.getLength())
81
			end= visibleRegion.getLength();
82
83
		return new Region(start, end - start);
84
	}
85
}
(-)ui/org/eclipse/jdt/internal/ui/actions/OccurrencesSearchMenuAction.java (-3 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 189-199 Link Here
189
		if (activeActions.size() == 1) {
189
		if (activeActions.size() == 1) {
190
			((IAction) activeActions.get(0)).run();
190
			((IAction) activeActions.get(0)).run();
191
		} else {
191
		} else {
192
			(new JDTQuickMenuAction(editor, IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE_QUICK_MENU) {
192
			new JDTQuickMenuCreator(editor) {
193
				protected void fillMenu(IMenuManager menu) {
193
				protected void fillMenu(IMenuManager menu) {
194
					fillQuickMenu(menu, activeActions);
194
					fillQuickMenu(menu, activeActions);
195
				}
195
				}
196
			}).run();
196
			}.createMenu();
197
		}
197
		}
198
	}
198
	}
199
199
(-)ui/org/eclipse/jdt/internal/ui/actions/SurroundWithTemplateMenuAction.java (-3 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 220-231 Link Here
220
220
221
		final CompilationUnitEditor editor= (CompilationUnitEditor)activePart;
221
		final CompilationUnitEditor editor= (CompilationUnitEditor)activePart;
222
222
223
		(new JDTQuickMenuAction(editor, SURROUND_WITH_QUICK_MENU_ACTION_ID) {
223
		new JDTQuickMenuCreator(editor) {
224
			protected void fillMenu(IMenuManager menu) {
224
			protected void fillMenu(IMenuManager menu) {
225
				SurroundWithTryCatchAction surroundWithTryCatch= createSurroundWithTryCatchAction(editor);
225
				SurroundWithTryCatchAction surroundWithTryCatch= createSurroundWithTryCatchAction(editor);
226
				SurroundWithTemplateMenuAction.fillMenu(menu, editor, surroundWithTryCatch);
226
				SurroundWithTemplateMenuAction.fillMenu(menu, editor, surroundWithTryCatch);
227
			}
227
			}
228
		}).run();
228
		}.createMenu();
229
	}
229
	}
230
230
231
	/**
231
	/**
(-)ui/org/eclipse/jdt/internal/ui/actions/SurroundWithActionGroup.java (-15 / +2 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 20-28 Link Here
20
import org.eclipse.jface.text.ITextSelection;
20
import org.eclipse.jface.text.ITextSelection;
21
21
22
import org.eclipse.ui.IActionBars;
22
import org.eclipse.ui.IActionBars;
23
import org.eclipse.ui.PlatformUI;
24
import org.eclipse.ui.actions.ActionGroup;
23
import org.eclipse.ui.actions.ActionGroup;
25
import org.eclipse.ui.keys.IBindingService;
26
24
27
import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
25
import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
28
import org.eclipse.jdt.ui.actions.JdtActionConstants;
26
import org.eclipse.jdt.ui.actions.JdtActionConstants;
Lines 65-76 Link Here
65
63
66
		String menuText= ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTemplateSubMenuName;
64
		String menuText= ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTemplateSubMenuName;
67
65
68
		String shortcutString= getShortcutString();
69
		if (shortcutString != null) {
70
			menuText= menuText + '\t' + shortcutString;
71
		}
72
73
		MenuManager subMenu = new MenuManager(menuText, SurroundWithTemplateMenuAction.SURROUND_WITH_QUICK_MENU_ACTION_ID);
66
		MenuManager subMenu = new MenuManager(menuText, SurroundWithTemplateMenuAction.SURROUND_WITH_QUICK_MENU_ACTION_ID);
67
		subMenu.setActionDefinitionId(SurroundWithTemplateMenuAction.SURROUND_WITH_QUICK_MENU_ACTION_ID);
74
		menu.appendToGroup(fGroup, subMenu);
68
		menu.appendToGroup(fGroup, subMenu);
75
		subMenu.add(new Action() {});
69
		subMenu.add(new Action() {});
76
		subMenu.addMenuListener(new IMenuListener() {
70
		subMenu.addMenuListener(new IMenuListener() {
Lines 81-93 Link Here
81
		});
75
		});
82
	}
76
	}
83
77
84
	private String getShortcutString() {
85
		IBindingService bindingService= (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class);
86
		if (bindingService == null)
87
			return null;
88
		return bindingService.getBestActiveBindingFormattedFor(SurroundWithTemplateMenuAction.SURROUND_WITH_QUICK_MENU_ACTION_ID);
89
	}
90
91
	private static SurroundWithTryCatchAction createSurroundWithTryCatchAction(CompilationUnitEditor editor) {
78
	private static SurroundWithTryCatchAction createSurroundWithTryCatchAction(CompilationUnitEditor editor) {
92
		SurroundWithTryCatchAction result= new SurroundWithTryCatchAction(editor);
79
		SurroundWithTryCatchAction result= new SurroundWithTryCatchAction(editor);
93
		result.setText(ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTryCatchActionName);
80
		result.setText(ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTryCatchActionName);
(-)ui/org/eclipse/jdt/ui/actions/OccurrencesSearchGroup.java (-15 / +2 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 26-34 Link Here
26
26
27
import org.eclipse.ui.IActionBars;
27
import org.eclipse.ui.IActionBars;
28
import org.eclipse.ui.IWorkbenchSite;
28
import org.eclipse.ui.IWorkbenchSite;
29
import org.eclipse.ui.PlatformUI;
30
import org.eclipse.ui.actions.ActionGroup;
29
import org.eclipse.ui.actions.ActionGroup;
31
import org.eclipse.ui.keys.IBindingService;
32
30
33
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
31
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
34
32
Lines 177-188 Link Here
177
	 */
175
	 */
178
	public void fillContextMenu(IMenuManager manager) {
176
	public void fillContextMenu(IMenuManager manager) {
179
		String menuText= SearchMessages.group_occurrences;
177
		String menuText= SearchMessages.group_occurrences;
180
		String shortcut= getShortcutString();
181
		if (shortcut != null) {
182
			menuText= menuText + '\t' + shortcut;
183
		}
184
185
		MenuManager javaSearchMM= new MenuManager(menuText, IContextMenuConstants.GROUP_SEARCH);
178
		MenuManager javaSearchMM= new MenuManager(menuText, IContextMenuConstants.GROUP_SEARCH);
179
		javaSearchMM.setActionDefinitionId(IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE_QUICK_MENU);
186
		javaSearchMM.add(new Action() {
180
		javaSearchMM.add(new Action() {
187
		});
181
		});
188
		javaSearchMM.addMenuListener(new IMenuListener() {
182
		javaSearchMM.addMenuListener(new IMenuListener() {
Lines 230-242 Link Here
230
		fMethodExitOccurrencesAction.update(javaSelection);
224
		fMethodExitOccurrencesAction.update(javaSelection);
231
	}
225
	}
232
226
233
	private String getShortcutString() {
234
		IBindingService bindingService= (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class);
235
		if (bindingService == null)
236
			return null;
237
		return bindingService.getBestActiveBindingFormattedFor(IJavaEditorActionDefinitionIds.SEARCH_OCCURRENCES_IN_FILE_QUICK_MENU);
238
	}
239
240
	/*
227
	/*
241
	 * Method declared on ActionGroup.
228
	 * Method declared on ActionGroup.
242
	 */
229
	 */
(-)ui/org/eclipse/jdt/ui/actions/RefactorActionGroup.java (-20 / +11 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 18-23 Link Here
18
import org.eclipse.swt.events.MenuEvent;
18
import org.eclipse.swt.events.MenuEvent;
19
import org.eclipse.swt.widgets.Menu;
19
import org.eclipse.swt.widgets.Menu;
20
20
21
import org.eclipse.core.commands.IHandler;
21
import org.eclipse.core.commands.operations.IUndoContext;
22
import org.eclipse.core.commands.operations.IUndoContext;
22
23
23
import org.eclipse.core.runtime.PerformanceStats;
24
import org.eclipse.core.runtime.PerformanceStats;
Lines 30-36 Link Here
30
import org.eclipse.jface.action.IMenuManager;
31
import org.eclipse.jface.action.IMenuManager;
31
import org.eclipse.jface.action.MenuManager;
32
import org.eclipse.jface.action.MenuManager;
32
import org.eclipse.jface.action.Separator;
33
import org.eclipse.jface.action.Separator;
33
import org.eclipse.jface.commands.ActionHandler;
34
import org.eclipse.jface.viewers.ISelection;
34
import org.eclipse.jface.viewers.ISelection;
35
import org.eclipse.jface.viewers.ISelectionChangedListener;
35
import org.eclipse.jface.viewers.ISelectionChangedListener;
36
import org.eclipse.jface.viewers.ISelectionProvider;
36
import org.eclipse.jface.viewers.ISelectionProvider;
Lines 58-64 Link Here
58
import org.eclipse.jdt.internal.ui.actions.ActionUtil;
58
import org.eclipse.jdt.internal.ui.actions.ActionUtil;
59
import org.eclipse.jdt.internal.ui.actions.ExtractSuperClassAction;
59
import org.eclipse.jdt.internal.ui.actions.ExtractSuperClassAction;
60
import org.eclipse.jdt.internal.ui.actions.IntroduceParameterObjectAction;
60
import org.eclipse.jdt.internal.ui.actions.IntroduceParameterObjectAction;
61
import org.eclipse.jdt.internal.ui.actions.JDTQuickMenuAction;
61
import org.eclipse.jdt.internal.ui.actions.JDTQuickMenuCreator;
62
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
62
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
63
import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
63
import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
64
import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
64
import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
Lines 180-195 Link Here
180
180
181
	private static final String QUICK_MENU_ID= "org.eclipse.jdt.ui.edit.text.java.refactor.quickMenu"; //$NON-NLS-1$
181
	private static final String QUICK_MENU_ID= "org.eclipse.jdt.ui.edit.text.java.refactor.quickMenu"; //$NON-NLS-1$
182
182
183
	private class RefactorQuickAccessAction extends JDTQuickMenuAction {
184
		public RefactorQuickAccessAction(JavaEditor editor) {
185
			super(editor, QUICK_MENU_ID);
186
		}
187
		protected void fillMenu(IMenuManager menu) {
188
			fillQuickMenu(menu);
189
		}
190
	}
191
192
	private JDTQuickMenuAction fQuickAccessAction;
193
	private IHandlerActivation fQuickAccessHandlerActivation;
183
	private IHandlerActivation fQuickAccessHandlerActivation;
194
	private IHandlerService fHandlerService;
184
	private IHandlerService fHandlerService;
195
185
Lines 438-445 Link Here
438
	private void installQuickAccessAction() {
428
	private void installQuickAccessAction() {
439
		fHandlerService= (IHandlerService)fSite.getService(IHandlerService.class);
429
		fHandlerService= (IHandlerService)fSite.getService(IHandlerService.class);
440
		if (fHandlerService != null) {
430
		if (fHandlerService != null) {
441
			fQuickAccessAction= new RefactorQuickAccessAction(fEditor);
431
			IHandler handler= new JDTQuickMenuCreator(fEditor) {
442
			fQuickAccessHandlerActivation= fHandlerService.activateHandler(fQuickAccessAction.getActionDefinitionId(), new ActionHandler(fQuickAccessAction));
432
				protected void fillMenu(IMenuManager menu) {
433
					fillQuickMenu(menu);
434
				}
435
			}.createHandler();
436
			fQuickAccessHandlerActivation= fHandlerService.activateHandler(QUICK_MENU_ID, handler);
443
		}
437
		}
444
	}
438
	}
445
439
Lines 566-576 Link Here
566
	}
560
	}
567
561
568
	private void addRefactorSubmenu(IMenuManager menu) {
562
	private void addRefactorSubmenu(IMenuManager menu) {
569
		String menuText= ActionMessages.RefactorMenu_label;
563
		MenuManager refactorSubmenu= new MenuManager(ActionMessages.RefactorMenu_label, MENU_ID);
570
		if (fQuickAccessAction != null) {
564
		refactorSubmenu.setActionDefinitionId(QUICK_MENU_ID);
571
			menuText= fQuickAccessAction.addShortcut(menuText);
572
		}
573
		IMenuManager refactorSubmenu= new MenuManager(menuText, MENU_ID);
574
		if (fEditor != null) {
565
		if (fEditor != null) {
575
			final ITypeRoot element= getEditorInput();
566
			final ITypeRoot element= getEditorInput();
576
			if (element != null && ActionUtil.isOnBuildPath(element)) {
567
			if (element != null && ActionUtil.isOnBuildPath(element)) {
(-)ui/org/eclipse/jdt/ui/actions/GenerateActionGroup.java (-20 / +12 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 14-26 Link Here
14
import java.util.Iterator;
14
import java.util.Iterator;
15
import java.util.List;
15
import java.util.List;
16
16
17
import org.eclipse.core.commands.IHandler;
18
17
import org.eclipse.core.runtime.Assert;
19
import org.eclipse.core.runtime.Assert;
18
20
19
import org.eclipse.jface.action.IAction;
21
import org.eclipse.jface.action.IAction;
20
import org.eclipse.jface.action.IMenuManager;
22
import org.eclipse.jface.action.IMenuManager;
21
import org.eclipse.jface.action.MenuManager;
23
import org.eclipse.jface.action.MenuManager;
22
import org.eclipse.jface.action.Separator;
24
import org.eclipse.jface.action.Separator;
23
import org.eclipse.jface.commands.ActionHandler;
24
import org.eclipse.jface.viewers.ISelection;
25
import org.eclipse.jface.viewers.ISelection;
25
import org.eclipse.jface.viewers.ISelectionChangedListener;
26
import org.eclipse.jface.viewers.ISelectionChangedListener;
26
import org.eclipse.jface.viewers.ISelectionProvider;
27
import org.eclipse.jface.viewers.ISelectionProvider;
Lines 45-51 Link Here
45
import org.eclipse.jdt.internal.ui.actions.AddTaskAction;
46
import org.eclipse.jdt.internal.ui.actions.AddTaskAction;
46
import org.eclipse.jdt.internal.ui.actions.AllCleanUpsAction;
47
import org.eclipse.jdt.internal.ui.actions.AllCleanUpsAction;
47
import org.eclipse.jdt.internal.ui.actions.FindBrokenNLSKeysAction;
48
import org.eclipse.jdt.internal.ui.actions.FindBrokenNLSKeysAction;
48
import org.eclipse.jdt.internal.ui.actions.JDTQuickMenuAction;
49
import org.eclipse.jdt.internal.ui.actions.JDTQuickMenuCreator;
49
import org.eclipse.jdt.internal.ui.actions.MultiSortMembersAction;
50
import org.eclipse.jdt.internal.ui.actions.MultiSortMembersAction;
50
import org.eclipse.jdt.internal.ui.javaeditor.AddImportOnSelectionAction;
51
import org.eclipse.jdt.internal.ui.javaeditor.AddImportOnSelectionAction;
51
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
52
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
Lines 144-159 Link Here
144
145
145
	private static final String QUICK_MENU_ID= "org.eclipse.jdt.ui.edit.text.java.source.quickMenu"; //$NON-NLS-1$
146
	private static final String QUICK_MENU_ID= "org.eclipse.jdt.ui.edit.text.java.source.quickMenu"; //$NON-NLS-1$
146
147
147
	private class SourceQuickAccessAction extends JDTQuickMenuAction {
148
		public SourceQuickAccessAction(CompilationUnitEditor editor) {
149
			super(editor, QUICK_MENU_ID);
150
		}
151
		protected void fillMenu(IMenuManager menu) {
152
			fillQuickMenu(menu);
153
		}
154
	}
155
156
	private JDTQuickMenuAction fQuickAccessAction;
157
	private IHandlerActivation fQuickAccessHandlerActivation;
148
	private IHandlerActivation fQuickAccessHandlerActivation;
158
	private IHandlerService fHandlerService;
149
	private IHandlerService fHandlerService;
159
150
Lines 368-375 Link Here
368
	private void installQuickAccessAction() {
359
	private void installQuickAccessAction() {
369
		fHandlerService= (IHandlerService)fSite.getService(IHandlerService.class);
360
		fHandlerService= (IHandlerService)fSite.getService(IHandlerService.class);
370
		if (fHandlerService != null) {
361
		if (fHandlerService != null) {
371
			fQuickAccessAction= new SourceQuickAccessAction(fEditor);
362
			IHandler handler= new JDTQuickMenuCreator(fEditor) {
372
			fQuickAccessHandlerActivation= fHandlerService.activateHandler(fQuickAccessAction.getActionDefinitionId(), new ActionHandler(fQuickAccessAction));
363
				protected void fillMenu(IMenuManager menu) {
364
					fillQuickMenu(menu);
365
				}
366
			}.createHandler();
367
			fQuickAccessHandlerActivation= fHandlerService.activateHandler(QUICK_MENU_ID, handler);
373
		}
368
		}
374
	}
369
	}
375
370
Lines 408-418 Link Here
408
	 */
403
	 */
409
	public void fillContextMenu(IMenuManager menu) {
404
	public void fillContextMenu(IMenuManager menu) {
410
		super.fillContextMenu(menu);
405
		super.fillContextMenu(menu);
411
		String menuText= ActionMessages.SourceMenu_label;
406
		MenuManager subMenu= new MenuManager(ActionMessages.SourceMenu_label, MENU_ID);
412
		if (fQuickAccessAction != null) {
407
		subMenu.setActionDefinitionId(QUICK_MENU_ID);
413
			menuText= fQuickAccessAction.addShortcut(menuText);
414
		}
415
		IMenuManager subMenu= new MenuManager(menuText, MENU_ID);
416
		int added= 0;
408
		int added= 0;
417
		if (isEditorOwner()) {
409
		if (isEditorOwner()) {
418
			added= fillEditorSubMenu(subMenu);
410
			added= fillEditorSubMenu(subMenu);
(-)ui/org/eclipse/jdt/internal/ui/actions/JDTQuickMenuCreator.java (+98 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 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.custom.StyledText;
14
import org.eclipse.swt.graphics.Point;
15
16
import org.eclipse.core.commands.AbstractHandler;
17
import org.eclipse.core.commands.ExecutionEvent;
18
import org.eclipse.core.commands.ExecutionException;
19
import org.eclipse.core.commands.IHandler;
20
21
import org.eclipse.jface.text.IRegion;
22
import org.eclipse.jface.text.ITextSelection;
23
import org.eclipse.jface.text.ITextViewerExtension5;
24
import org.eclipse.jface.text.Region;
25
import org.eclipse.jface.text.source.ISourceViewer;
26
27
import org.eclipse.ui.actions.QuickMenuCreator;
28
29
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
30
import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
31
32
/**
33
 * Java editor aware quick menu creator. In the given editor, the menu will be aligned with the word
34
 * at the current offset.
35
 * 
36
 * @since 3.5
37
 */
38
public abstract class JDTQuickMenuCreator extends QuickMenuCreator {
39
40
	private final JavaEditor fEditor;
41
42
	public JDTQuickMenuCreator(JavaEditor editor) {
43
		fEditor= editor;
44
	}
45
46
	protected Point computeMenuLocation(StyledText text) {
47
		if (fEditor == null || text != fEditor.getViewer().getTextWidget())
48
			return super.computeMenuLocation(text);
49
		return computeWordStart();
50
	}
51
52
	private Point computeWordStart() {
53
		ITextSelection selection= (ITextSelection)fEditor.getSelectionProvider().getSelection();
54
		IRegion textRegion= JavaWordFinder.findWord(fEditor.getViewer().getDocument(), selection.getOffset());
55
		if (textRegion == null)
56
			return null;
57
58
		IRegion widgetRegion= modelRange2WidgetRange(textRegion);
59
		if (widgetRegion == null)
60
			return null;
61
62
		int start= widgetRegion.getOffset();
63
64
		StyledText styledText= fEditor.getViewer().getTextWidget();
65
		Point result= styledText.getLocationAtOffset(start);
66
		result.y+= styledText.getLineHeight(start);
67
68
		if (!styledText.getClientArea().contains(result))
69
			return null;
70
		return result;
71
	}
72
73
	private IRegion modelRange2WidgetRange(IRegion region) {
74
		ISourceViewer viewer= fEditor.getViewer();
75
		if (viewer instanceof ITextViewerExtension5) {
76
			ITextViewerExtension5 extension= (ITextViewerExtension5)viewer;
77
			return extension.modelRange2WidgetRange(region);
78
		}
79
80
		IRegion visibleRegion= viewer.getVisibleRegion();
81
		int start= region.getOffset() - visibleRegion.getOffset();
82
		int end= start + region.getLength();
83
		if (end > visibleRegion.getLength())
84
			end= visibleRegion.getLength();
85
86
		return new Region(start, end - start);
87
	}
88
89
	public IHandler createHandler() {
90
		return new AbstractHandler() {
91
			public Object execute(ExecutionEvent event) throws ExecutionException {
92
				createMenu();
93
				return null;
94
			}
95
		};
96
	}
97
98
}

Return to bug 266938