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.actions.PHPEditorActionDefinitionIds;
21 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import org.eclipse.jface.text.DefaultInformationControl;
25 import org.eclipse.jface.text.IInformationControl;
26 import org.eclipse.jface.text.IInformationControlCreator;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.ITextViewer;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.commands.ICommand;
34 import org.eclipse.ui.commands.ICommandManager;
35 import org.eclipse.ui.commands.IKeySequenceBinding;
36 import org.eclipse.ui.keys.KeySequence;
39 * Abstract class for providing hover information for Java elements.
43 public abstract class AbstractJavaEditorTextHover implements IJavaEditorTextHover {
46 private IEditorPart fEditor;
47 private ICommand fCommand;
49 ICommandManager commandManager= PlatformUI.getWorkbench().getCommandSupport().getCommandManager();
50 // fCommand= commandManager.getCommand(PHPEditorActionDefinitionIds.SHOW_JAVADOC);
51 // if (!fCommand.isDefined())
56 * @see IJavaEditorTextHover#setEditor(IEditorPart)
58 public void setEditor(IEditorPart editor) {
62 protected IEditorPart getEditor() {
66 // protected ICodeAssist getCodeAssist() {
67 // if (fEditor != null) {
68 // IEditorInput input= fEditor.getEditorInput();
69 // if (input instanceof IClassFileEditorInput) {
70 // IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
71 // return cfeInput.getClassFile();
74 // IWorkingCopyManager manager= PHPeclipsePlugin.getDefault().getWorkingCopyManager();
75 // return manager.getWorkingCopy(input);
82 * @see ITextHover#getHoverRegion(ITextViewer, int)
84 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
85 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
89 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
91 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
93 // ICodeAssist resolve= getCodeAssist();
94 // if (resolve != null) {
96 // IJavaElement[] result= null;
98 // synchronized (resolve) {
99 // result= resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
102 // if (result == null)
105 // int nResults= result.length;
106 // if (nResults == 0)
109 // return getHoverInfo(result);
111 // } catch (JavaModelException x) {
112 // PHPeclipsePlugin.log(x.getStatus());
119 * Provides hover information for the given Java elements.
121 * @return the hover information string
124 protected String getHoverInfo(IJavaElement[] javaElements) {
128 * @see ITextHoverExtension#getHoverControlCreator()
131 public IInformationControlCreator getHoverControlCreator() {
132 return new IInformationControlCreator() {
133 public IInformationControl createInformationControl(Shell parent) {
134 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), getTooltipAffordanceString());
140 * Returns the tool tip affordance string.
142 * @return the affordance string or <code>null</code> if disabled or no key binding is defined
145 protected String getTooltipAffordanceString() {
146 if (!PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
149 KeySequence[] sequences= getKeySequences();
150 if (sequences == null)
153 String keySequence= sequences[0].format();
154 return JavaHoverMessages.getFormattedString("JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
158 * Returns the array of valid key sequence bindings for the
159 * show tool tip description command.
161 * @return the array with the {@link KeySequence}s
165 private KeySequence[] getKeySequences() {
166 if (fCommand != null) {
167 List list= fCommand.getKeySequenceBindings();
168 if (!list.isEmpty()) {
169 KeySequence[] keySequences= new KeySequence[list.size()];
170 for (int i= 0; i < keySequences.length; i++) {
171 keySequences[i]= ((IKeySequenceBinding) list.get(i)).getKeySequence();