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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.debug.ui;
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;
20 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugStackFrame;
21 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
22 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugValue;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.debug.core.DebugException;
26 import org.eclipse.debug.core.model.IValue;
27 import org.eclipse.debug.core.model.IVariable;
28 import org.eclipse.debug.ui.IDebugUIConstants;
29 import org.eclipse.jface.text.BadLocationException;
30 import org.eclipse.jface.text.DefaultInformationControl;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.jface.text.IInformationControl;
33 import org.eclipse.jface.text.IInformationControlCreator;
34 import org.eclipse.jface.text.IRegion;
35 import org.eclipse.jface.text.ITextHoverExtension;
36 import org.eclipse.jface.text.ITextViewer;
37 import org.eclipse.jface.viewers.ISelection;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.ITreeSelection;
40 import org.eclipse.jface.viewers.TreePath;
41 //import org.eclipse.swt.SWT;
42 import org.eclipse.swt.widgets.Shell;
43 import org.eclipse.ui.IEditorPart;
44 import org.eclipse.ui.IPartListener;
45 import org.eclipse.ui.ISelectionListener;
46 import org.eclipse.ui.IWorkbenchPage;
47 import org.eclipse.ui.IWorkbenchPart;
49 public class PHPDebugHover implements IJavaEditorTextHover,
50 ITextHoverExtension, ISelectionListener, IPartListener {
52 protected IEditorPart fEditor;
54 protected ISelection fSelection = null;
59 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
61 public void partActivated(IWorkbenchPart part) {
67 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
69 public void partBroughtToTop(IWorkbenchPart part) {
75 * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
77 public void partClosed(IWorkbenchPart part) {
78 if (part.equals(fEditor)) {
79 IWorkbenchPage page = fEditor.getSite().getPage();
80 page.removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
81 page.removePartListener(this);
90 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
92 public void partDeactivated(IWorkbenchPart part) {
98 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
100 public void partOpened(IWorkbenchPart part) {
106 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
107 * org.eclipse.jface.viewers.ISelection)
109 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
110 fSelection = selection;
113 public PHPDebugHover() {
119 * @see org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
121 public void setEditor(IEditorPart editor) {
122 if (editor != null) {
124 final IWorkbenchPage page = editor.getSite().getPage();
125 page.addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
126 page.addPartListener(this);
127 // initialize selection
128 Runnable r = new Runnable() {
131 .getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
134 PHPDebugUiPlugin.getStandardDisplay().asyncExec(r);
141 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
144 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
145 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
149 * Returns the stack frame in which to search for variables, or
150 * <code>null</code> if none.
152 * @return the stack frame in which to search for variables, or
153 * <code>null</code> if none
155 protected PHPStackFrame getFrame() {
156 if (fSelection instanceof IStructuredSelection) {
157 IStructuredSelection selection = (IStructuredSelection) fSelection;
158 if (selection.size() == 1) {
159 Object el = selection.getFirstElement();
160 if (el instanceof IAdaptable) {
161 return (PHPStackFrame) ((IAdaptable) el)
162 .getAdapter(PHPStackFrame.class);
169 protected XDebugStackFrame getXDebugFrame() {
170 if (fSelection instanceof IStructuredSelection) {
171 IStructuredSelection selection = (IStructuredSelection) fSelection;
172 if (selection.size() == 1) {
173 Object el = selection.getFirstElement();
174 if (el instanceof IAdaptable) {
175 return (XDebugStackFrame) ((IAdaptable) el)
176 .getAdapter(XDebugStackFrame.class);
186 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
187 * org.eclipse.jface.text.IRegion)
189 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
190 IVariable variable = null;
191 PHPStackFrame frameDBG = null;
192 XDebugStackFrame frameXD = null;
194 TreePath paths[] = ((ITreeSelection) fSelection).getPaths ();
195 Object target = paths[0].getSegment (1);
197 if (target.getClass().getName ().equals ("net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget") ) {
198 frameXD = getXDebugFrame ();
201 frameDBG = getFrame();
204 if ((frameDBG != null) || (frameXD != null)) {
206 IDocument document = textViewer.getDocument();
208 if (document == null)
211 String variableName = document.get (hoverRegion.getOffset(), hoverRegion.getLength());
213 StringBuffer buffer = new StringBuffer();
215 if (frameDBG != null) {
216 variable = frameDBG.findVariable(variableName);
218 else if (frameXD != null) {
219 if (variableName.startsWith ("$")) {
220 variable = frameXD.findVariable (variableName.substring (1));
223 variable = frameXD.findVariable (variableName);
227 if (variable != null) {
228 appendVariable(buffer, variable);
230 } catch (DebugException x) {
231 // if (x.getStatus().getCode() !=
232 // IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
233 PHPDebugUiPlugin.log(x);
237 if (buffer.length() > 0) {
238 return buffer.toString();
241 } catch (BadLocationException x) {
242 PHPDebugUiPlugin.log(x);
250 * Append HTML for the given variable to the given buffer
252 private static void appendVariable(StringBuffer buffer, IVariable variable)
253 throws DebugException {
255 buffer.append("<p>"); //$NON-NLS-1$
256 buffer.append("<pre>").append(variable.getName()).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
257 buffer.append(" ="); //$NON-NLS-1$
259 String type = getTypeName(variable);
260 String value = "<b><pre>" + variable.getValue().getValueString() + "</pre></b>"; //$NON-NLS-1$ //$NON-NLS-2$
263 buffer.append(" null"); //$NON-NLS-1$
264 } else if (type.equals("java.lang.String")) { //$NON-NLS-1$
265 buffer.append(" \""); //$NON-NLS-1$
266 buffer.append(value);
268 } else if (type.equals("boolean") || // in dbg it's boolean
269 type.equals("bool")) { // in XDebug it's bool $NON-NLS-1$
271 buffer.append(value);
273 buffer.append(" ("); //$NON-NLS-1$
274 buffer.append("<pre>").append(type).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
275 buffer.append(") "); //$NON-NLS-1$
276 buffer.append(value);
278 buffer.append("</p>"); //$NON-NLS-1$
281 private static String getTypeName(IVariable variable) throws DebugException {
282 IValue value = variable.getValue();
284 if (value instanceof PHPValue)
285 return ((PHPValue) value).getReferenceTypeName();
287 if (value instanceof XDebugValue)
288 return ((XDebugValue) value).getReferenceTypeName ();
296 * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
298 // public IInformationControlCreator getInformationControlCreator() {
300 // // (Platform.getPlugin("org.eclipse.jdt.ui").getPluginPreferences().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
301 // // { //$NON-NLS-1$
302 // return new IInformationControlCreator() {
303 // public IInformationControl createInformationControl(Shell parent) {
305 // // return new DefaultInformationControl(parent, SWT.NONE,
306 //// new HTMLTextPresenter(true), PHPDebugUiMessages
307 //// .getString("JavaDebugHover.16")); //$NON-NLS-1$
308 // return new DefaultInformationControl(parent, PHPDebugUiMessages.getString("JavaDebugHover.16"),
309 // new HTMLTextPresenter(true));
319 * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
321 public IInformationControlCreator getHoverControlCreator() {
322 if (PreferenceConstants.getPreferenceStore().getBoolean(
323 PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE)) { //$NON-NLS-1$
324 return new IInformationControlCreator() {
325 public IInformationControl createInformationControl(Shell parent) {
327 // return new DefaultInformationControl(parent, SWT.NONE,
328 // new HTMLTextPresenter(true), PHPDebugUiMessages
329 // .getString("PHPDebugHover.16")); //$NON-NLS-1$
330 return new DefaultInformationControl(parent, PHPDebugUiMessages.getString("JavaDebugHover.16"),
331 new HTMLTextPresenter(true));