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.XDebugAbstractValue;
21 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugStackFrame;
22 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
23 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugValue;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.debug.core.DebugException;
27 import org.eclipse.debug.core.model.IValue;
28 import org.eclipse.debug.core.model.IVariable;
29 import org.eclipse.debug.ui.IDebugUIConstants;
30 import org.eclipse.jface.text.BadLocationException;
31 import org.eclipse.jface.text.DefaultInformationControl;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.IInformationControl;
34 import org.eclipse.jface.text.IInformationControlCreator;
35 import org.eclipse.jface.text.IRegion;
36 import org.eclipse.jface.text.ITextHoverExtension;
37 import org.eclipse.jface.text.ITextViewer;
38 import org.eclipse.jface.viewers.ISelection;
39 import org.eclipse.jface.viewers.IStructuredSelection;
40 import org.eclipse.jface.viewers.ITreeSelection;
41 import org.eclipse.jface.viewers.TreePath;
42 //import org.eclipse.swt.SWT;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.ui.IEditorPart;
45 import org.eclipse.ui.IPartListener;
46 import org.eclipse.ui.ISelectionListener;
47 import org.eclipse.ui.IWorkbenchPage;
48 import org.eclipse.ui.IWorkbenchPart;
50 public class PHPDebugHover implements IJavaEditorTextHover,
51 ITextHoverExtension, ISelectionListener, IPartListener {
53 protected IEditorPart fEditor;
55 protected ISelection fSelection = null;
60 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
62 public void partActivated(IWorkbenchPart part) {
68 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
70 public void partBroughtToTop(IWorkbenchPart part) {
76 * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
78 public void partClosed(IWorkbenchPart part) {
79 if (part.equals(fEditor)) {
80 IWorkbenchPage page = fEditor.getSite().getPage();
81 page.removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
82 page.removePartListener(this);
91 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
93 public void partDeactivated(IWorkbenchPart part) {
99 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
101 public void partOpened(IWorkbenchPart part) {
107 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
108 * org.eclipse.jface.viewers.ISelection)
110 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
111 fSelection = selection;
114 public PHPDebugHover() {
120 * @see org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
122 public void setEditor(IEditorPart editor) {
123 if (editor != null) {
125 final IWorkbenchPage page = editor.getSite().getPage();
126 page.addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
127 page.addPartListener(this);
128 // initialize selection
129 Runnable r = new Runnable() {
132 .getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
135 PHPDebugUiPlugin.getStandardDisplay().asyncExec(r);
142 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
145 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
146 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
150 * Returns the stack frame in which to search for variables, or
151 * <code>null</code> if none.
153 * @return the stack frame in which to search for variables, or
154 * <code>null</code> if none
156 protected PHPStackFrame getFrame() {
157 if (fSelection instanceof IStructuredSelection) {
158 IStructuredSelection selection = (IStructuredSelection) fSelection;
159 if (selection.size() == 1) {
160 Object el = selection.getFirstElement();
161 if (el instanceof IAdaptable) {
162 return (PHPStackFrame) ((IAdaptable) el)
163 .getAdapter(PHPStackFrame.class);
170 protected XDebugStackFrame getXDebugFrame() {
171 if (fSelection instanceof IStructuredSelection) {
172 IStructuredSelection selection = (IStructuredSelection) fSelection;
173 if (selection.size() == 1) {
174 Object el = selection.getFirstElement();
175 if (el instanceof IAdaptable) {
176 return (XDebugStackFrame) ((IAdaptable) el)
177 .getAdapter(XDebugStackFrame.class);
187 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
188 * org.eclipse.jface.text.IRegion)
190 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
191 IVariable variable = null;
192 PHPStackFrame frameDBG = null;
193 XDebugStackFrame frameXD = null;
195 TreePath paths[] = ((ITreeSelection) fSelection).getPaths ();
196 Object target = paths[0].getSegment (1);
198 if (target.getClass().getName ().equals ("net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget") ) {
199 frameXD = getXDebugFrame ();
202 frameDBG = getFrame();
205 if ((frameDBG != null) || (frameXD != null)) {
207 IDocument document = textViewer.getDocument();
209 if (document == null)
212 String variableName = document.get (hoverRegion.getOffset(), hoverRegion.getLength());
214 StringBuffer buffer = new StringBuffer();
216 if (frameDBG != null) {
217 variable = frameDBG.findVariable (variableName);
219 else if (frameXD != null) {
220 variable = frameXD.findVariable (variableName);
223 if (variable != null) {
224 appendVariable(buffer, variable);
226 } catch (DebugException x) {
227 // if (x.getStatus().getCode() !=
228 // IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
229 PHPDebugUiPlugin.log(x);
233 if (buffer.length() > 0) {
234 return buffer.toString();
237 } catch (BadLocationException x) {
238 PHPDebugUiPlugin.log(x);
246 * Append HTML for the given variable to the given buffer
248 private static void appendVariable(StringBuffer buffer, IVariable variable)
249 throws DebugException {
251 buffer.append("<p>"); //$NON-NLS-1$
252 buffer.append("<pre>").append(variable.getName()).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
253 buffer.append(" ="); //$NON-NLS-1$
255 String type = getTypeName(variable);
256 String value = "<b><pre>" + variable.getValue().getValueString() + "</pre></b>"; //$NON-NLS-1$ //$NON-NLS-2$
259 buffer.append(" null"); //$NON-NLS-1$
260 } else if (type.equals("java.lang.String")) { //$NON-NLS-1$
261 buffer.append(" \""); //$NON-NLS-1$
262 buffer.append(value);
264 } else if (type.equals("boolean") || // in dbg it's boolean
265 type.equals("bool")) { // in XDebug it's bool $NON-NLS-1$
267 buffer.append(value);
269 buffer.append(" ("); //$NON-NLS-1$
270 buffer.append("<pre>").append(type).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
271 buffer.append(") "); //$NON-NLS-1$
272 buffer.append(value);
274 buffer.append("</p>"); //$NON-NLS-1$
277 private static String getTypeName(IVariable variable) throws DebugException {
278 IValue value = variable.getValue();
280 if (value instanceof PHPValue)
281 return ((PHPValue) value).getReferenceTypeName();
283 if (value instanceof XDebugAbstractValue)
284 return ((XDebugAbstractValue) value).getReferenceTypeName ();
292 * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
294 // public IInformationControlCreator getInformationControlCreator() {
296 // // (Platform.getPlugin("org.eclipse.jdt.ui").getPluginPreferences().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
297 // // { //$NON-NLS-1$
298 // return new IInformationControlCreator() {
299 // public IInformationControl createInformationControl(Shell parent) {
301 // // return new DefaultInformationControl(parent, SWT.NONE,
302 //// new HTMLTextPresenter(true), PHPDebugUiMessages
303 //// .getString("JavaDebugHover.16")); //$NON-NLS-1$
304 // return new DefaultInformationControl(parent, PHPDebugUiMessages.getString("JavaDebugHover.16"),
305 // new HTMLTextPresenter(true));
315 * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
317 public IInformationControlCreator getHoverControlCreator() {
318 if (PreferenceConstants.getPreferenceStore().getBoolean(
319 PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE)) { //$NON-NLS-1$
320 return new IInformationControlCreator() {
321 public IInformationControl createInformationControl(Shell parent) {
323 // return new DefaultInformationControl(parent, SWT.NONE,
324 // new HTMLTextPresenter(true), PHPDebugUiMessages
325 // .getString("PHPDebugHover.16")); //$NON-NLS-1$
326 return new DefaultInformationControl(parent, PHPDebugUiMessages.getString("JavaDebugHover.16"),
327 new HTMLTextPresenter(true));