]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 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 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.ITextViewerExtension5; | |
23 | import org.eclipse.jface.text.Region; | |
24 | import org.eclipse.jface.text.source.ISourceViewer; | |
25 | ||
26 | import org.eclipse.ui.actions.QuickMenuCreator; | |
27 | ||
28 | import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; | |
29 | ||
30 | /** | |
31 | * Java editor aware quick menu creator. In the given editor, the menu will be aligned with the word | |
32 | * at the current offset. | |
33 | * | |
34 | * @since 3.5 | |
35 | */ | |
36 | public abstract class JDTQuickMenuCreator extends QuickMenuCreator { | |
37 | ||
38 | public final JavaEditor fEditor; | |
39 | ||
40 | /** | |
41 | * Create a JDT quick menu creator | |
42 | * @param editor a Java editor, or <code>null</code> if none | |
43 | */ | |
44 | public JDTQuickMenuCreator(JavaEditor editor) { | |
45 | fEditor= editor; | |
46 | } | |
47 | ||
48 | @Override | |
49 | protected Point computeMenuLocation(StyledText text) { | |
50 | if (fEditor == null || text != fEditor.getViewer().getTextWidget()) | |
51 | return super.computeMenuLocation(text); | |
52 | return computeWordStart(); | |
53 | } | |
54 | ||
55 | private Point computeWordStart() { | |
56 | return fEditor.generated_7893933557945652389(this); | |
57 | } | |
58 | ||
59 | public IRegion modelRange2WidgetRange(IRegion region) { | |
60 | ISourceViewer viewer= fEditor.getViewer(); | |
61 | if (viewer instanceof ITextViewerExtension5) { | |
62 | ITextViewerExtension5 extension= (ITextViewerExtension5)viewer; | |
63 | return extension.modelRange2WidgetRange(region); | |
64 | } | |
65 | ||
66 | IRegion visibleRegion= viewer.getVisibleRegion(); | |
67 | int start= region.getOffset() - visibleRegion.getOffset(); | |
68 | int end= start + region.getLength(); | |
69 | if (end > visibleRegion.getLength()) | |
70 | end= visibleRegion.getLength(); | |
71 | ||
72 | return new Region(start, end - start); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Returns a handler that can create and open the quick menu. | |
77 | * | |
78 | * @return a handler that can create and open the quick menu | |
79 | */ | |
80 | public IHandler createHandler() { | |
81 | return new AbstractHandler() { | |
82 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
83 | createMenu(); | |
84 | return null; | |
85 | } | |
86 | }; | |
87 | } | |
88 | ||
89 | } |