de8385872cc235539cfefa0a8dbeea4ada9d156d
[phpeclipse.git] /
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                                 return new DefaultInformationControl(parent, SWT.NONE,
141                                                 new HTMLTextPresenter(true),
142                                                 getTooltipAffordanceString());
143                         }
144                 };
145         }
146
147         /**
148          * Returns the tool tip affordance string.
149          * 
150          * @return the affordance string or <code>null</code> if disabled or no
151          *         key binding is defined
152          * @since 3.0
153          */
154         protected String getTooltipAffordanceString() {
155                 if (!WebUI.getDefault().getPreferenceStore().getBoolean(
156                                 PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
157                         return null;
158
159                 KeySequence[] sequences = getKeySequences();
160                 if (sequences == null)
161                         return null;
162
163                 String keySequence = sequences[0].format();
164                 return JavaHoverMessages.getFormattedString(
165                                 "JavaTextHover.makeStickyHint", keySequence); //$NON-NLS-1$
166         }
167
168         /**
169          * Returns the array of valid key sequence bindings for the show tool tip
170          * description command.
171          * 
172          * @return the array with the {@link KeySequence}s
173          * 
174          * @since 3.0
175          */
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))
183                                                         .getKeySequence();
184                                 }
185                                 return keySequences;
186                         }
187                 }
188                 return null;
189         }
190 }