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 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;
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;
46 public class PHPDebugHover implements IJavaEditorTextHover, ITextHoverExtension, ISelectionListener, IPartListener {
48 protected IEditorPart fEditor;
49 protected ISelection fSelection = null;
52 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
54 public void partActivated(IWorkbenchPart part) {
58 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
60 public void partBroughtToTop(IWorkbenchPart part) {
64 * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
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);
77 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
79 public void partDeactivated(IWorkbenchPart part) {
83 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
85 public void partOpened(IWorkbenchPart part) {
89 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
91 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
92 fSelection = selection;
95 public PHPDebugHover() {
99 * @see org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
101 public void setEditor(IEditorPart editor) {
102 if (editor != null) {
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() {
110 fSelection = page.getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
113 PHPDebugUiPlugin.getStandardDisplay().asyncExec(r);
118 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
120 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
121 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
125 * Returns the stack frame in which to search for variables, or <code>null</code>
128 * @return the stack frame in which to search for variables, or <code>null</code>
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);
145 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
147 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
148 PHPStackFrame frame = getFrame();
152 IDocument document= textViewer.getDocument();
153 if (document == null)
156 String variableName= document.get(hoverRegion.getOffset(), hoverRegion.getLength());
158 StringBuffer buffer= new StringBuffer();
160 IVariable variable= frame.findVariable(variableName);
161 if (variable != null) {
162 appendVariable(buffer, variable);
164 } catch (DebugException x) {
165 // if (x.getStatus().getCode() != IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
166 PHPDebugUiPlugin.log(x);
170 if (buffer.length() > 0) {
171 return buffer.toString();
174 } catch (BadLocationException x) {
175 PHPDebugUiPlugin.log(x);
183 * Append HTML for the given variable to the given buffer
185 private static void appendVariable(StringBuffer buffer, IVariable variable) throws DebugException {
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$
191 String type= getTypeName(variable);
192 String value= "<b><pre>" + variable.getValue().getValueString() + "</pre></b>"; //$NON-NLS-1$ //$NON-NLS-2$
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);
200 } else if (type.equals("boolean")) { //$NON-NLS-1$
202 buffer.append(value);
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);
209 buffer.append("</p>"); //$NON-NLS-1$
212 private static String getTypeName(IVariable variable) throws DebugException {
213 IValue value= variable.getValue();
214 if (value instanceof PHPValue)
215 return((PHPValue) value).getReferenceTypeName();
220 * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
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$
236 * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
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$