KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > JDTQuickMenuAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 JavaDoc commandId) {
31         super(commandId);
32     }
33
34     public JDTQuickMenuAction(JavaEditor editor, String JavaDoc commandId) {
35         super(commandId);
36         fEditor= editor;
37     }
38
39     protected Point computeMenuLocation(StyledText text) {
40         if (fEditor == null || text != fEditor.getViewer().getTextWidget())
41             return null;
42         return computeWordStart();
43     }
44     
45     private Point computeWordStart() {
46         ITextSelection selection= (ITextSelection)fEditor.getSelectionProvider().getSelection();
47         IRegion textRegion= JavaWordFinder.findWord(fEditor.getViewer().getDocument(), selection.getOffset());
48         if (textRegion == null)
49             return null;
50                 
51         IRegion widgetRegion= modelRange2WidgetRange(textRegion);
52         if (widgetRegion == null)
53             return null;
54         
55         int start= widgetRegion.getOffset();
56                 
57         StyledText styledText= fEditor.getViewer().getTextWidget();
58         Point result= styledText.getLocationAtOffset(start);
59         result.y+= styledText.getLineHeight(start);
60         
61         if (!styledText.getClientArea().contains(result))
62             return null;
63         return result;
64     }
65     
66     private IRegion modelRange2WidgetRange(IRegion region) {
67         ISourceViewer viewer= fEditor.getViewer();
68         if (viewer instanceof ITextViewerExtension5) {
69             ITextViewerExtension5 extension= (ITextViewerExtension5)viewer;
70             return extension.modelRange2WidgetRange(region);
71         }
72         
73         IRegion visibleRegion= viewer.getVisibleRegion();
74         int start= region.getOffset() - visibleRegion.getOffset();
75         int end= start + region.getLength();
76         if (end > visibleRegion.getLength())
77             end= visibleRegion.getLength();
78             
79         return new Region(start, end - start);
80     }
81 }
82
Popular Tags