1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
14 import java.util.List;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
18 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
19 import net.sourceforge.phpdt.ui.PreferenceConstants;
20 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
21 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import org.eclipse.jface.text.DefaultInformationControl;
24 import org.eclipse.jface.text.IInformationControl;
25 import org.eclipse.jface.text.IInformationControlCreator;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITextViewer;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.commands.ICommand;
33 import org.eclipse.ui.commands.ICommandManager;
34 import org.eclipse.ui.commands.IKeySequenceBinding;
35 import org.eclipse.ui.keys.KeySequence;
38 * Abstract class for providing hover information for Java elements.
42 public abstract class AbstractJavaEditorTextHover implements
43 IJavaEditorTextHover {
45 private IEditorPart fEditor;
47 private ICommand fCommand;
49 ICommandManager commandManager = PlatformUI.getWorkbench()
50 .getCommandSupport().getCommandManager();
52 // commandManager.getCommand(PHPEditorActionDefinitionIds.SHOW_JAVADOC);
53 // if (!fCommand.isDefined())
58 * @see IJavaEditorTextHover#setEditor(IEditorPart)
60 public void setEditor(IEditorPart editor) {
64 protected IEditorPart getEditor() {
68 // protected ICodeAssist getCodeAssist() {
69 // if (fEditor != null) {
70 // IEditorInput input= fEditor.getEditorInput();
71 // if (input instanceof IClassFileEditorInput) {
72 // IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
73 // return cfeInput.getClassFile();
76 // IWorkingCopyManager manager=
77 // PHPeclipsePlugin.getDefault().getWorkingCopyManager();
78 // return manager.getWorkingCopy(input);
85 * @see ITextHover#getHoverRegion(ITextViewer, int)
87 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
88 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
92 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
94 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
96 // ICodeAssist resolve= getCodeAssist();
97 // if (resolve != null) {
99 // IJavaElement[] result= null;
101 // synchronized (resolve) {
102 // result= resolve.codeSelect(hoverRegion.getOffset(),
103 // hoverRegion.getLength());
106 // if (result == null)
109 // int nResults= result.length;
110 // if (nResults == 0)
113 // return getHoverInfo(result);
115 // } catch (JavaModelException x) {
116 // PHPeclipsePlugin.log(x.getStatus());
123 * Provides hover information for the given Java elements.
125 * @return the hover information string
128 protected String getHoverInfo(IJavaElement[] javaElements) {
133 * @see ITextHoverExtension#getHoverControlCreator()
136 public IInformationControlCreator getHoverControlCreator() {
137 return new IInformationControlCreator() {
138 public IInformationControl createInformationControl(Shell parent) {
139 return new DefaultInformationControl(parent, SWT.NONE,
140 new HTMLTextPresenter(true),
141 getTooltipAffordanceString());
147 * Returns the tool tip affordance string.
149 * @return the affordance string or <code>null</code> if disabled or no
150 * key binding is defined
153 protected String getTooltipAffordanceString() {
154 if (!PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(
155 PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
158 KeySequence[] sequences = getKeySequences();
159 if (sequences == null)
162 String keySequence = sequences[0].format();
163 return JavaHoverMessages.getFormattedString(
164 "JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
168 * Returns the array of valid key sequence bindings for the show tool tip
169 * description command.
171 * @return the array with the {@link KeySequence}s
175 private KeySequence[] getKeySequences() {
176 if (fCommand != null) {
177 List list = fCommand.getKeySequenceBindings();
178 if (!list.isEmpty()) {
179 KeySequence[] keySequences = new KeySequence[list.size()];
180 for (int i = 0; i < keySequences.length; i++) {
181 keySequences[i] = ((IKeySequenceBinding) list.get(i))