Fix #1368081: First suggestion in #1368081 seems to be working better
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / PHPDebugHover.java
1
2 /*******************************************************************************
3  * Copyright (c) 2000, 2003 IBM Corporation and others.
4  * All rights reserved. This program and the accompanying materials 
5  * are made available under the terms of the Common Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/cpl-v10.html
8  * 
9  * Contributors:
10  *     IBM Corporation - initial API and implementation
11  *******************************************************************************/
12 package net.sourceforge.phpdt.internal.debug.ui;
13
14
15 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
16 import net.sourceforge.phpdt.internal.debug.core.model.PHPValue;
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
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.debug.core.DebugException;
24 import org.eclipse.debug.core.model.IValue;
25 import org.eclipse.debug.core.model.IVariable;
26 import org.eclipse.debug.ui.IDebugUIConstants;
27 import org.eclipse.jface.text.BadLocationException;
28 import org.eclipse.jface.text.DefaultInformationControl;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.IInformationControl;
31 import org.eclipse.jface.text.IInformationControlCreator;
32 import org.eclipse.jface.text.IRegion;
33 import org.eclipse.jface.text.ITextHoverExtension;
34 import org.eclipse.jface.text.ITextViewer;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.ui.IEditorPart;
40 import org.eclipse.ui.IPartListener;
41 import org.eclipse.ui.ISelectionListener;
42 import org.eclipse.ui.IWorkbenchPage;
43 import org.eclipse.ui.IWorkbenchPart;
44
45
46 public class PHPDebugHover implements IJavaEditorTextHover, ITextHoverExtension, ISelectionListener, IPartListener {
47                 
48         protected IEditorPart fEditor;
49         protected ISelection fSelection = null;
50         
51         /* (non-Javadoc)
52          * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
53          */
54         public void partActivated(IWorkbenchPart part) {
55         }
56
57         /* (non-Javadoc)
58          * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
59          */
60         public void partBroughtToTop(IWorkbenchPart part) {
61         }
62
63         /* (non-Javadoc)
64          * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
65          */
66         public void partClosed(IWorkbenchPart part) {
67                 if (part.equals(fEditor)) {
68                         IWorkbenchPage page = fEditor.getSite().getPage();
69                         page.removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
70                         page.removePartListener(this);
71                         fSelection = null;
72                         fEditor = null;
73                 }
74         }
75
76         /* (non-Javadoc)
77          * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
78          */
79         public void partDeactivated(IWorkbenchPart part) {
80         }
81
82         /* (non-Javadoc)
83          * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
84          */
85         public void partOpened(IWorkbenchPart part) {
86         }
87
88         /* (non-Javadoc)
89          * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
90          */
91         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
92                 fSelection = selection;
93         }
94
95         public PHPDebugHover() {
96         }
97
98         /* (non-Javadoc)
99          * @see org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
100          */
101         public void setEditor(IEditorPart editor) {
102                 if (editor != null) {
103                         fEditor= editor;
104                         final IWorkbenchPage page = editor.getSite().getPage();
105                         page.addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
106                         page.addPartListener(this);
107                         // initialize selection
108                         Runnable r = new Runnable() {
109                                 public void run() {
110                                         fSelection = page.getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
111                                 }
112                         };
113                         PHPDebugUiPlugin.getStandardDisplay().asyncExec(r);
114                 }
115         }
116                 
117         /* (non-Javadoc)
118          * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
119          */
120         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
121                 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
122         }
123         
124         /**
125          * Returns the stack frame in which to search for variables, or <code>null</code>
126          * if none.
127          * 
128          * @return the stack frame in which to search for variables, or <code>null</code>
129          * if none
130          */
131         protected PHPStackFrame getFrame() {
132                 if (fSelection instanceof IStructuredSelection) {
133                         IStructuredSelection selection = (IStructuredSelection)fSelection;
134                         if (selection.size() == 1) {
135                                 Object el = selection.getFirstElement();
136                                 if (el instanceof IAdaptable) {
137                                         return (PHPStackFrame)((IAdaptable)el).getAdapter(PHPStackFrame.class); 
138                                 }
139                         }
140                 }
141                 return null;
142         }
143                 
144         /* (non-Javadoc)
145          * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
146          */
147         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
148                 PHPStackFrame frame = getFrame();                               
149                 if (frame != null) {
150                         try {
151                                 
152                                 IDocument document= textViewer.getDocument();
153                                 if (document == null)
154                                         return null;
155                                         
156                                 String variableName= document.get(hoverRegion.getOffset(), hoverRegion.getLength());
157                                                                                                                                 
158                                 StringBuffer buffer= new StringBuffer();        
159                                 try {
160                                         IVariable variable= frame.findVariable(variableName);
161                                         if (variable != null) {
162                                                 appendVariable(buffer, variable);
163                                         }
164                                 } catch (DebugException x) {
165 //                                      if (x.getStatus().getCode() != IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
166                                                 PHPDebugUiPlugin.log(x);
167 //                                      }
168                                 }
169                                 
170                                 if (buffer.length() > 0) {
171                                         return buffer.toString();
172                                 }
173                         
174                         } catch (BadLocationException x) {
175                                 PHPDebugUiPlugin.log(x);
176                         }
177                 }
178
179                 return null;
180         }
181
182         /**
183          * Append HTML for the given variable to the given buffer
184          */
185         private static void appendVariable(StringBuffer buffer, IVariable variable) throws DebugException {
186
187                 buffer.append("<p>"); //$NON-NLS-1$
188                 buffer.append("<pre>").append(variable.getName()).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
189                 buffer.append(" ="); //$NON-NLS-1$
190                 
191                 String type= getTypeName(variable);
192                 String value= "<b><pre>" + variable.getValue().getValueString() + "</pre></b>"; //$NON-NLS-1$ //$NON-NLS-2$
193                 
194                 if (type == null) {
195                         buffer.append(" null"); //$NON-NLS-1$
196                 } else if (type.equals("java.lang.String")) { //$NON-NLS-1$
197                         buffer.append(" \""); //$NON-NLS-1$
198                         buffer.append(value);
199                         buffer.append('"');
200                 } else if (type.equals("boolean")) { //$NON-NLS-1$
201                         buffer.append(' ');
202                         buffer.append(value);
203                 } else {
204                         buffer.append(" ("); //$NON-NLS-1$
205                         buffer.append("<pre>").append(type).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
206                         buffer.append(") "); //$NON-NLS-1$
207                         buffer.append(value);                   
208                 }               
209                 buffer.append("</p>"); //$NON-NLS-1$
210         }
211
212         private static String getTypeName(IVariable variable) throws DebugException {
213                 IValue value= variable.getValue();
214                 if (value instanceof PHPValue) 
215                         return((PHPValue) value).getReferenceTypeName();
216                 return null;
217         }
218
219         /* (non-Javadoc)
220          * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
221          */
222         public IInformationControlCreator getInformationControlCreator() {
223 //              if (Platform.getPlugin("org.eclipse.jdt.ui").getPluginPreferences().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE)) { //$NON-NLS-1$
224                         return new IInformationControlCreator() {
225                                 public IInformationControl createInformationControl(Shell parent) {
226                                         return new DefaultInformationControl(parent, SWT.NONE, 
227                                                 new HTMLTextPresenter(true),
228                                                 PHPDebugUiMessages.getString("JavaDebugHover.16")); //$NON-NLS-1$
229                                 }
230                         };
231 //              }
232 //              return null;
233         }
234         
235         /* (non-Javadoc)
236          * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
237          */
238         public IInformationControlCreator getHoverControlCreator() {
239                 if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE)) { //$NON-NLS-1$
240                         return new IInformationControlCreator() {
241                                 public IInformationControl createInformationControl(Shell parent) {
242                                         return new DefaultInformationControl(parent, SWT.NONE, 
243                                                 new HTMLTextPresenter(true),
244                                                 PHPDebugUiMessages.getString("PHPDebugHover.16")); //$NON-NLS-1$
245                                 }
246                         };
247                 }
248                 return null;
249         }
250 }