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