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;
22 import net.sourceforge.phpeclipse.ui.WebUI;
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
44 IJavaEditorTextHover {
46 private IEditorPart fEditor;
48 private ICommand fCommand;
50 ICommandManager commandManager = PlatformUI.getWorkbench()
51 .getCommandSupport().getCommandManager();
53 // commandManager.getCommand(PHPEditorActionDefinitionIds.SHOW_JAVADOC);
54 // if (!fCommand.isDefined())
59 * @see IJavaEditorTextHover#setEditor(IEditorPart)
61 public void setEditor(IEditorPart editor) {
65 protected IEditorPart getEditor() {
69 // protected ICodeAssist getCodeAssist() {
70 // if (fEditor != null) {
71 // IEditorInput input= fEditor.getEditorInput();
72 // if (input instanceof IClassFileEditorInput) {
73 // IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
74 // return cfeInput.getClassFile();
77 // IWorkingCopyManager manager=
78 // PHPeclipsePlugin.getDefault().getWorkingCopyManager();
79 // return manager.getWorkingCopy(input);
86 * @see ITextHover#getHoverRegion(ITextViewer, int)
88 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
89 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
93 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
95 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
97 // ICodeAssist resolve= getCodeAssist();
98 // if (resolve != null) {
100 // IJavaElement[] result= null;
102 // synchronized (resolve) {
103 // result= resolve.codeSelect(hoverRegion.getOffset(),
104 // hoverRegion.getLength());
107 // if (result == null)
110 // int nResults= result.length;
111 // if (nResults == 0)
114 // return getHoverInfo(result);
116 // } catch (JavaModelException x) {
117 // PHPeclipsePlugin.log(x.getStatus());
124 * Provides hover information for the given Java elements.
126 * @return the hover information string
129 protected String getHoverInfo(IJavaElement[] javaElements) {
134 * @see ITextHoverExtension#getHoverControlCreator()
137 public IInformationControlCreator getHoverControlCreator() {
138 return new IInformationControlCreator() {
139 public IInformationControl createInformationControl(Shell parent) {
140 return new DefaultInformationControl(parent, SWT.NONE,
141 new HTMLTextPresenter(true),
142 getTooltipAffordanceString());
148 * Returns the tool tip affordance string.
150 * @return the affordance string or <code>null</code> if disabled or no
151 * key binding is defined
154 protected String getTooltipAffordanceString() {
155 if (!WebUI.getDefault().getPreferenceStore().getBoolean(
156 PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
159 KeySequence[] sequences = getKeySequences();
160 if (sequences == null)
163 String keySequence = sequences[0].format();
164 return JavaHoverMessages.getFormattedString(
165 "JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
169 * Returns the array of valid key sequence bindings for the show tool tip
170 * description command.
172 * @return the array with the {@link KeySequence}s
176 private KeySequence[] getKeySequences() {
177 if (fCommand != null) {
178 List list = fCommand.getKeySequenceBindings();
179 if (!list.isEmpty()) {
180 KeySequence[] keySequences = new KeySequence[list.size()];
181 for (int i = 0; i < keySequences.length; i++) {
182 keySequences[i] = ((IKeySequenceBinding) list.get(i))