01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
11:
12: import org.eclipse.swt.custom.StyledText;
13: import org.eclipse.swt.graphics.Point;
14:
15: import org.eclipse.jface.text.IRegion;
16: import org.eclipse.jface.text.ITextSelection;
17: import org.eclipse.jface.text.ITextViewerExtension5;
18: import org.eclipse.jface.text.Region;
19: import org.eclipse.jface.text.source.ISourceViewer;
20:
21: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
22: import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
23:
24: public abstract class JDTQuickMenuAction extends QuickMenuAction {
25:
26: private JavaEditor fEditor;
27:
28: public JDTQuickMenuAction(String commandId) {
29: super (commandId);
30: }
31:
32: public JDTQuickMenuAction(JavaEditor editor, String commandId) {
33: super (commandId);
34: fEditor = editor;
35: }
36:
37: protected Point computeMenuLocation(StyledText text) {
38: if (fEditor == null
39: || text != fEditor.getViewer().getTextWidget())
40: return null;
41: return computeWordStart();
42: }
43:
44: private Point computeWordStart() {
45: ITextSelection selection = (ITextSelection) fEditor
46: .getSelectionProvider().getSelection();
47: IRegion textRegion = JavaWordFinder.findWord(fEditor
48: .getViewer().getDocument(), selection.getOffset());
49: if (textRegion == null)
50: return null;
51:
52: IRegion widgetRegion = modelRange2WidgetRange(textRegion);
53: if (widgetRegion == null)
54: return null;
55:
56: int start = widgetRegion.getOffset();
57:
58: StyledText styledText = fEditor.getViewer().getTextWidget();
59: Point result = styledText.getLocationAtOffset(start);
60: result.y += styledText.getLineHeight(start);
61:
62: if (!styledText.getClientArea().contains(result))
63: return null;
64: return result;
65: }
66:
67: private IRegion modelRange2WidgetRange(IRegion region) {
68: ISourceViewer viewer = fEditor.getViewer();
69: if (viewer instanceof ITextViewerExtension5) {
70: ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
71: return extension.modelRange2WidgetRange(region);
72: }
73:
74: IRegion visibleRegion = viewer.getVisibleRegion();
75: int start = region.getOffset() - visibleRegion.getOffset();
76: int end = start + region.getLength();
77: if (end > visibleRegion.getLength())
78: end = visibleRegion.getLength();
79:
80: return new Region(start, end - start);
81: }
82: }
|