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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
13 import org.eclipse.swt.custom.StyledText;
14 import org.eclipse.swt.graphics.Point;
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;
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;
26 import org.eclipse.ui.actions.QuickMenuCreator;
28 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
31 * Java editor aware quick menu creator. In the given editor, the menu will be aligned with the word
32 * at the current offset.
36 public abstract class JDTQuickMenuCreator extends QuickMenuCreator {
38 public final JavaEditor fEditor;
41 * Create a JDT quick menu creator
42 * @param editor a Java editor, or <code>null</code> if none
44 public JDTQuickMenuCreator(JavaEditor editor) {
49 protected Point computeMenuLocation(StyledText text) {
50 if (fEditor == null || text != fEditor.getViewer().getTextWidget())
51 return super.computeMenuLocation(text);
52 return computeWordStart();
55 private Point computeWordStart() {
56 return fEditor.generated_7893933557945652389(this);
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);
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();
72 return new Region(start, end - start);
76 * Returns a handler that can create and open the quick menu.
78 * @return a handler that can create and open the quick menu
80 public IHandler createHandler() {
81 return new AbstractHandler() {
82 public Object execute(ExecutionEvent event) throws ExecutionException {