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