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
10 * IBM Corporation - initial API and implementation
11 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.debug.ui;
15 import org.eclipse.core.runtime.IAdaptable;
16 //import org.eclipse.core.runtime.Platform;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.model.IValue;
19 import org.eclipse.debug.core.model.IVariable;
20 import org.eclipse.debug.ui.IDebugUIConstants;
21 //import org.eclipse.jdt.debug.core.IJavaStackFrame;
22 //import org.eclipse.jdt.debug.core.IJavaThread;
23 //import org.eclipse.jdt.debug.core.IJavaType;
24 //import org.eclipse.jdt.debug.core.IJavaValue;
25 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
26 //import org.eclipse.jdt.ui.PreferenceConstants;
27 //import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;
28 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
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.swt.SWT;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.ui.IEditorPart;
42 import org.eclipse.ui.IPartListener;
43 import org.eclipse.ui.ISelectionListener;
44 import org.eclipse.ui.IWorkbenchPage;
45 import org.eclipse.ui.IWorkbenchPart;
47 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
48 import net.sourceforge.phpdt.internal.debug.core.model.PHPValue;
49 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
52 public class PHPDebugHover implements IJavaEditorTextHover, ITextHoverExtension, ISelectionListener, IPartListener {
54 protected IEditorPart fEditor;
55 protected ISelection fSelection = null;
58 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
60 public void partActivated(IWorkbenchPart part) {
64 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
66 public void partBroughtToTop(IWorkbenchPart part) {
70 * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
72 public void partClosed(IWorkbenchPart part) {
73 if (part.equals(fEditor)) {
74 IWorkbenchPage page = fEditor.getSite().getPage();
75 page.removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
76 page.removePartListener(this);
83 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
85 public void partDeactivated(IWorkbenchPart part) {
89 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
91 public void partOpened(IWorkbenchPart part) {
95 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
97 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
98 fSelection = selection;
101 public PHPDebugHover() {
105 * @see org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
107 public void setEditor(IEditorPart editor) {
108 if (editor != null) {
110 final IWorkbenchPage page = editor.getSite().getPage();
111 page.addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
112 page.addPartListener(this);
113 // initialize selection
114 Runnable r = new Runnable() {
116 fSelection = page.getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
119 PHPDebugUiPlugin.getStandardDisplay().asyncExec(r);
124 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
126 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
127 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
131 * Returns the stack frame in which to search for variables, or <code>null</code>
134 * @return the stack frame in which to search for variables, or <code>null</code>
137 protected PHPStackFrame getFrame() {
138 if (fSelection instanceof IStructuredSelection) {
139 IStructuredSelection selection = (IStructuredSelection)fSelection;
140 if (selection.size() == 1) {
141 Object el = selection.getFirstElement();
142 if (el instanceof IAdaptable) {
143 return (PHPStackFrame)((IAdaptable)el).getAdapter(PHPStackFrame.class);
151 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
153 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
154 PHPStackFrame frame = getFrame();
158 IDocument document= textViewer.getDocument();
159 if (document == null)
162 String variableName= document.get(hoverRegion.getOffset(), hoverRegion.getLength());
164 StringBuffer buffer= new StringBuffer();
166 IVariable variable= frame.findVariable(variableName);
167 if (variable != null) {
168 appendVariable(buffer, variable);
170 } catch (DebugException x) {
171 // if (x.getStatus().getCode() != IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
172 PHPDebugUiPlugin.log(x);
176 if (buffer.length() > 0) {
177 return buffer.toString();
180 } catch (BadLocationException x) {
181 PHPDebugUiPlugin.log(x);
189 * Append HTML for the given variable to the given buffer
191 private static void appendVariable(StringBuffer buffer, IVariable variable) throws DebugException {
193 buffer.append("<p>"); //$NON-NLS-1$
194 buffer.append("<pre>").append(variable.getName()).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
195 buffer.append(" ="); //$NON-NLS-1$
197 String type= getTypeName(variable);
198 String value= "<b><pre>" + variable.getValue().getValueString() + "</pre></b>"; //$NON-NLS-1$ //$NON-NLS-2$
201 buffer.append(" null"); //$NON-NLS-1$
202 } else if (type.equals("java.lang.String")) { //$NON-NLS-1$
203 buffer.append(" \""); //$NON-NLS-1$
204 buffer.append(value);
206 } else if (type.equals("boolean")) { //$NON-NLS-1$
208 buffer.append(value);
210 buffer.append(" ("); //$NON-NLS-1$
211 buffer.append("<pre>").append(type).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
212 buffer.append(") "); //$NON-NLS-1$
213 buffer.append(value);
215 buffer.append("</p>"); //$NON-NLS-1$
218 private static String getTypeName(IVariable variable) throws DebugException {
219 IValue value= variable.getValue();
220 if (value instanceof PHPValue)
221 return((PHPValue) value).getReferenceTypeName();
226 * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
228 public IInformationControlCreator getInformationControlCreator() {
229 // if (Platform.getPlugin("org.eclipse.jdt.ui").getPluginPreferences().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE)) { //$NON-NLS-1$
230 return new IInformationControlCreator() {
231 public IInformationControl createInformationControl(Shell parent) {
232 return new DefaultInformationControl(parent, SWT.NONE,
233 new HTMLTextPresenter(true),
234 PHPDebugUiMessages.getString("JavaDebugHover.16")); //$NON-NLS-1$