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