9b4195603583eb0a13d7c62268bc93720cfec73a
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / PHPDebugHover.java
1
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
8  * 
9  * Contributors:
10  *     IBM Corporation - initial API and implementation
11  *******************************************************************************/
12 package net.sourceforge.phpdt.internal.debug.ui;
13
14
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;
46
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;
50
51
52 public class PHPDebugHover implements IJavaEditorTextHover, ITextHoverExtension, ISelectionListener, IPartListener {
53                 
54         protected IEditorPart fEditor;
55         protected ISelection fSelection = null;
56         
57         /* (non-Javadoc)
58          * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
59          */
60         public void partActivated(IWorkbenchPart part) {
61         }
62
63         /* (non-Javadoc)
64          * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
65          */
66         public void partBroughtToTop(IWorkbenchPart part) {
67         }
68
69         /* (non-Javadoc)
70          * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
71          */
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);
77                         fSelection = null;
78                         fEditor = null;
79                 }
80         }
81
82         /* (non-Javadoc)
83          * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
84          */
85         public void partDeactivated(IWorkbenchPart part) {
86         }
87
88         /* (non-Javadoc)
89          * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
90          */
91         public void partOpened(IWorkbenchPart part) {
92         }
93
94         /* (non-Javadoc)
95          * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
96          */
97         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
98                 fSelection = selection;
99         }
100
101         public PHPDebugHover() {
102         }
103
104         /* (non-Javadoc)
105          * @see org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
106          */
107         public void setEditor(IEditorPart editor) {
108                 if (editor != null) {
109                         fEditor= editor;
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() {
115                                 public void run() {
116                                         fSelection = page.getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
117                                 }
118                         };
119                         PHPDebugUiPlugin.getStandardDisplay().asyncExec(r);
120                 }
121         }
122                 
123         /* (non-Javadoc)
124          * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
125          */
126         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
127                 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
128         }
129         
130         /**
131          * Returns the stack frame in which to search for variables, or <code>null</code>
132          * if none.
133          * 
134          * @return the stack frame in which to search for variables, or <code>null</code>
135          * if none
136          */
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); 
144                                 }
145                         }
146                 }
147                 return null;
148         }
149                 
150         /* (non-Javadoc)
151          * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
152          */
153         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
154                 PHPStackFrame frame = getFrame();                               
155                 if (frame != null) {
156                         try {
157                                 
158                                 IDocument document= textViewer.getDocument();
159                                 if (document == null)
160                                         return null;
161                                         
162                                 String variableName= document.get(hoverRegion.getOffset(), hoverRegion.getLength());
163                                                                                                                                 
164                                 StringBuffer buffer= new StringBuffer();        
165                                 try {
166                                         IVariable variable= frame.findVariable(variableName);
167                                         if (variable != null) {
168                                                 appendVariable(buffer, variable);
169                                         }
170                                 } catch (DebugException x) {
171 //                                      if (x.getStatus().getCode() != IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
172                                                 PHPDebugUiPlugin.log(x);
173 //                                      }
174                                 }
175                                 
176                                 if (buffer.length() > 0) {
177                                         return buffer.toString();
178                                 }
179                         
180                         } catch (BadLocationException x) {
181                                 PHPDebugUiPlugin.log(x);
182                         }
183                 }
184
185                 return null;
186         }
187
188         /**
189          * Append HTML for the given variable to the given buffer
190          */
191         private static void appendVariable(StringBuffer buffer, IVariable variable) throws DebugException {
192
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$
196                 
197                 String type= getTypeName(variable);
198                 String value= "<b><pre>" + variable.getValue().getValueString() + "</pre></b>"; //$NON-NLS-1$ //$NON-NLS-2$
199                 
200                 if (type == null) {
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);
205                         buffer.append('"');
206                 } else if (type.equals("boolean")) { //$NON-NLS-1$
207                         buffer.append(' ');
208                         buffer.append(value);
209                 } else {
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);                   
214                 }               
215                 buffer.append("</p>"); //$NON-NLS-1$
216         }
217
218         private static String getTypeName(IVariable variable) throws DebugException {
219                 IValue value= variable.getValue();
220                 if (value instanceof PHPValue) 
221                         return((PHPValue) value).getReferenceTypeName();
222                 return null;
223         }
224
225         /* (non-Javadoc)
226          * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
227          */
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$
235                                 }
236                         };
237 //              }
238 //              return null;
239         }
240 }