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 IJavaEditorTextHover {
45 private IEditorPart fEditor;
46 private ICommand fCommand;
48 ICommandManager commandManager= PlatformUI.getWorkbench().getCommandSupport().getCommandManager();
49 // fCommand= commandManager.getCommand(PHPEditorActionDefinitionIds.SHOW_JAVADOC);
50 // if (!fCommand.isDefined())
55 * @see IJavaEditorTextHover#setEditor(IEditorPart)
57 public void setEditor(IEditorPart editor) {
61 protected IEditorPart getEditor() {
65 // protected ICodeAssist getCodeAssist() {
66 // if (fEditor != null) {
67 // IEditorInput input= fEditor.getEditorInput();
68 // if (input instanceof IClassFileEditorInput) {
69 // IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
70 // return cfeInput.getClassFile();
73 // IWorkingCopyManager manager= PHPeclipsePlugin.getDefault().getWorkingCopyManager();
74 // return manager.getWorkingCopy(input);
81 * @see ITextHover#getHoverRegion(ITextViewer, int)
83 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
84 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
88 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
90 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
92 // ICodeAssist resolve= getCodeAssist();
93 // if (resolve != null) {
95 // IJavaElement[] result= null;
97 // synchronized (resolve) {
98 // result= resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
101 // if (result == null)
104 // int nResults= result.length;
105 // if (nResults == 0)
108 // return getHoverInfo(result);
110 // } catch (JavaModelException x) {
111 // PHPeclipsePlugin.log(x.getStatus());
118 * Provides hover information for the given Java elements.
120 * @return the hover information string
123 protected String getHoverInfo(IJavaElement[] javaElements) {
127 * @see ITextHoverExtension#getHoverControlCreator()
130 public IInformationControlCreator getHoverControlCreator() {
131 return new IInformationControlCreator() {
132 public IInformationControl createInformationControl(Shell parent) {
133 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), getTooltipAffordanceString());
139 * Returns the tool tip affordance string.
141 * @return the affordance string or <code>null</code> if disabled or no key binding is defined
144 protected String getTooltipAffordanceString() {
145 if (!PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
148 KeySequence[] sequences= getKeySequences();
149 if (sequences == null)
152 String keySequence= sequences[0].format();
153 return JavaHoverMessages.getFormattedString("JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
157 * Returns the array of valid key sequence bindings for the
158 * show tool tip description command.
160 * @return the array with the {@link KeySequence}s
164 private KeySequence[] getKeySequences() {
165 if (fCommand != null) {
166 List list= fCommand.getKeySequenceBindings();
167 if (!list.isEmpty()) {
168 KeySequence[] keySequences= new KeySequence[list.size()];
169 for (int i= 0; i < keySequences.length; i++) {
170 keySequences[i]= ((IKeySequenceBinding) list.get(i)).getKeySequence();