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 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;
43 public class PHPDebugHover implements IJavaEditorTextHover,
44 ITextHoverExtension, ISelectionListener, IPartListener {
46 protected IEditorPart fEditor;
48 protected ISelection fSelection = null;
53 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
55 public void partActivated(IWorkbenchPart part) {
61 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
63 public void partBroughtToTop(IWorkbenchPart part) {
69 * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
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);
84 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
86 public void partDeactivated(IWorkbenchPart part) {
92 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
94 public void partOpened(IWorkbenchPart part) {
100 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
101 * org.eclipse.jface.viewers.ISelection)
103 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
104 fSelection = selection;
107 public PHPDebugHover() {
113 * @see org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
115 public void setEditor(IEditorPart editor) {
116 if (editor != null) {
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() {
125 .getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
128 PHPDebugUiPlugin.getStandardDisplay().asyncExec(r);
135 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
138 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
139 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
143 * Returns the stack frame in which to search for variables, or
144 * <code>null</code> if none.
146 * @return the stack frame in which to search for variables, or
147 * <code>null</code> if none
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);
166 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
167 * org.eclipse.jface.text.IRegion)
169 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
170 PHPStackFrame frame = getFrame();
174 IDocument document = textViewer.getDocument();
175 if (document == null)
178 String variableName = document.get(hoverRegion.getOffset(),
179 hoverRegion.getLength());
181 StringBuffer buffer = new StringBuffer();
183 IVariable variable = frame.findVariable(variableName);
184 if (variable != null) {
185 appendVariable(buffer, variable);
187 } catch (DebugException x) {
188 // if (x.getStatus().getCode() !=
189 // IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
190 PHPDebugUiPlugin.log(x);
194 if (buffer.length() > 0) {
195 return buffer.toString();
198 } catch (BadLocationException x) {
199 PHPDebugUiPlugin.log(x);
207 * Append HTML for the given variable to the given buffer
209 private static void appendVariable(StringBuffer buffer, IVariable variable)
210 throws DebugException {
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$
216 String type = getTypeName(variable);
217 String value = "<b><pre>" + variable.getValue().getValueString() + "</pre></b>"; //$NON-NLS-1$ //$NON-NLS-2$
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);
225 } else if (type.equals("boolean")) { //$NON-NLS-1$
227 buffer.append(value);
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);
234 buffer.append("</p>"); //$NON-NLS-1$
237 private static String getTypeName(IVariable variable) throws DebugException {
238 IValue value = variable.getValue();
239 if (value instanceof PHPValue)
240 return ((PHPValue) value).getReferenceTypeName();
247 * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
249 public IInformationControlCreator getInformationControlCreator() {
251 // (Platform.getPlugin("org.eclipse.jdt.ui").getPluginPreferences().getBoolean(PreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE))
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$
267 * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
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$