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;
37 //import org.eclipse.jface.bindings.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) {
141 // return new DefaultInformationControl(parent, SWT.NONE,
142 // new HTMLTextPresenter(true),
143 // getTooltipAffordanceString());
144 return new DefaultInformationControl(parent, getTooltipAffordanceString(),
145 new HTMLTextPresenter(true));
151 * Returns the tool tip affordance string.
153 * @return the affordance string or <code>null</code> if disabled or no
154 * key binding is defined
157 protected String getTooltipAffordanceString() {
158 if (!WebUI.getDefault().getPreferenceStore().getBoolean(
159 PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
162 KeySequence[] sequences = getKeySequences();
163 if (sequences == null)
166 String keySequence = sequences[0].format();
167 return JavaHoverMessages.getFormattedString(
168 "JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
172 * Returns the array of valid key sequence bindings for the show tool tip
173 * description command.
175 * @return the array with the {@link KeySequence}s
179 private KeySequence[] getKeySequences() {
180 if (fCommand != null) {
181 List list = fCommand.getKeySequenceBindings();
182 if (!list.isEmpty()) {
183 KeySequence[] keySequences = new KeySequence[list.size()];
184 for (int i = 0; i < keySequences.length; i++) {
185 keySequences[i] = ((IKeySequenceBinding) list.get(i))