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