ba23080f4fda1b4147b661957974821c861d184d
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / java / hover / AbstractJavaEditorTextHover.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
13
14 import java.util.List;
15
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;
23
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;
38 /**
39  * Abstract class for providing hover information for Java elements.
40  * 
41  * @since 2.1
42  */
43 public abstract class AbstractJavaEditorTextHover implements
44                 IJavaEditorTextHover {
45
46         private IEditorPart fEditor;
47
48         private ICommand fCommand;
49         {
50 //              ICommandManager commandManager = PlatformUI.getWorkbench()
51 //                              .getCommandSupport().getCommandManager();
52                 // fCommand=
53                 // commandManager.getCommand(PHPEditorActionDefinitionIds.SHOW_JAVADOC);
54                 // if (!fCommand.isDefined())
55                 fCommand = null;
56         }
57
58         /*
59          * @see IJavaEditorTextHover#setEditor(IEditorPart)
60          */
61         public void setEditor(IEditorPart editor) {
62                 fEditor = editor;
63         }
64
65         protected IEditorPart getEditor() {
66                 return fEditor;
67         }
68
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();
75         // }
76         //
77         // IWorkingCopyManager manager=
78         // PHPeclipsePlugin.getDefault().getWorkingCopyManager();
79         // return manager.getWorkingCopy(input);
80         // }
81         //
82         // return null;
83         // }
84
85         /*
86          * @see ITextHover#getHoverRegion(ITextViewer, int)
87          */
88         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
89                 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
90         }
91
92         /*
93          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
94          */
95         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
96
97                 // ICodeAssist resolve= getCodeAssist();
98                 // if (resolve != null) {
99                 // try {
100                 // IJavaElement[] result= null;
101                 //
102                 // synchronized (resolve) {
103                 // result= resolve.codeSelect(hoverRegion.getOffset(),
104                 // hoverRegion.getLength());
105                 // }
106                 //
107                 // if (result == null)
108                 // return null;
109                 //
110                 // int nResults= result.length;
111                 // if (nResults == 0)
112                 // return null;
113                 //
114                 // return getHoverInfo(result);
115                 //
116                 // } catch (JavaModelException x) {
117                 // PHPeclipsePlugin.log(x.getStatus());
118                 // }
119                 // }
120                 return null;
121         }
122
123         /**
124          * Provides hover information for the given Java elements.
125          * 
126          * @return the hover information string
127          * @since 2.1
128          */
129         protected String getHoverInfo(IJavaElement[] javaElements) {
130                 return null;
131         }
132
133         /*
134          * @see ITextHoverExtension#getHoverControlCreator()
135          * @since 3.0
136          */
137         public IInformationControlCreator getHoverControlCreator() {
138                 return new IInformationControlCreator() {
139                         public IInformationControl createInformationControl(Shell parent) {
140                                 //incastrix
141                                 //                              return new DefaultInformationControl(parent, SWT.NONE,
142 //                                              new HTMLTextPresenter(true),
143 //                                              getTooltipAffordanceString());
144                                 return new DefaultInformationControl(parent, getTooltipAffordanceString(),
145                                                 new HTMLTextPresenter(true));
146                         }
147                 };
148         }
149
150         /**
151          * Returns the tool tip affordance string.
152          * 
153          * @return the affordance string or <code>null</code> if disabled or no
154          *         key binding is defined
155          * @since 3.0
156          */
157         protected String getTooltipAffordanceString() {
158                 if (!WebUI.getDefault().getPreferenceStore().getBoolean(
159                                 PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
160                         return null;
161
162                 KeySequence[] sequences = getKeySequences();
163                 if (sequences == null)
164                         return null;
165
166                 String keySequence = sequences[0].format();
167                 return JavaHoverMessages.getFormattedString(
168                                 "JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
169         }
170
171         /**
172          * Returns the array of valid key sequence bindings for the show tool tip
173          * description command.
174          * 
175          * @return the array with the {@link KeySequence}s
176          * 
177          * @since 3.0
178          */
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))
186                                                         .getKeySequence();
187                                 }
188                                 return keySequences;
189                         }
190                 }
191                 return null;
192         }
193 }