1) Fixed issue #844: Short form of ternary operator (since PHP 5.3).
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / PHPDebugHover.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.debug.ui;
12
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;
19
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;
24
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;
49
50 public class PHPDebugHover implements IJavaEditorTextHover,
51                 ITextHoverExtension, ISelectionListener, IPartListener {
52
53         protected IEditorPart fEditor;
54
55         protected ISelection fSelection = null;
56
57         /*
58          * (non-Javadoc)
59          *
60          * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
61          */
62         public void partActivated(IWorkbenchPart part) {
63         }
64
65         /*
66          * (non-Javadoc)
67          *
68          * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
69          */
70         public void partBroughtToTop(IWorkbenchPart part) {
71         }
72
73         /*
74          * (non-Javadoc)
75          *
76          * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
77          */
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);
83                         fSelection = null;
84                         fEditor = null;
85                 }
86         }
87
88         /*
89          * (non-Javadoc)
90          *
91          * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
92          */
93         public void partDeactivated(IWorkbenchPart part) {
94         }
95
96         /*
97          * (non-Javadoc)
98          *
99          * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
100          */
101         public void partOpened(IWorkbenchPart part) {
102         }
103
104         /*
105          * (non-Javadoc)
106          *
107          * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
108          *      org.eclipse.jface.viewers.ISelection)
109          */
110         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
111                 fSelection = selection;
112         }
113
114         public PHPDebugHover() {
115         }
116
117         /*
118          * (non-Javadoc)
119          *
120          * @see org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover#setEditor(org.eclipse.ui.IEditorPart)
121          */
122         public void setEditor(IEditorPart editor) {
123                 if (editor != null) {
124                         fEditor = editor;
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() {
130                                 public void run() {
131                                         fSelection = page
132                                                         .getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
133                                 }
134                         };
135                         PHPDebugUiPlugin.getStandardDisplay().asyncExec(r);
136                 }
137         }
138
139         /*
140          * (non-Javadoc)
141          *
142          * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
143          *      int)
144          */
145         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
146                 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
147         }
148
149         /**
150          * Returns the stack frame in which to search for variables, or
151          * <code>null</code> if none.
152          *
153          * @return the stack frame in which to search for variables, or
154          *         <code>null</code> if none
155          */
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);
164                                 }
165                         }
166                 }
167                 return null;
168         }
169
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);
178                 }
179             }
180         }
181         return null;
182     }
183
184         /*
185          * (non-Javadoc)
186          *
187          * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
188          *      org.eclipse.jface.text.IRegion)
189          */
190         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
191             IVariable        variable = null;
192                 PHPStackFrame    frameDBG = null;
193                 XDebugStackFrame frameXD  = null;
194
195                 TreePath paths[] = ((ITreeSelection) fSelection).getPaths ();
196                 Object target = paths[0].getSegment (1);
197
198                 if (target.getClass().getName ().equals ("net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget") ) {
199                     frameXD = getXDebugFrame ();
200                 }
201                 else {
202                     frameDBG = getFrame();
203                 }
204
205                 if ((frameDBG != null) || (frameXD != null)) {
206                         try {
207                                 IDocument document = textViewer.getDocument();
208
209                                 if (document == null)
210                                         return null;
211
212                                 String variableName = document.get (hoverRegion.getOffset(), hoverRegion.getLength());
213
214                                 StringBuffer buffer = new StringBuffer();
215                                 try {
216                                     if (frameDBG != null) {
217                                         variable = frameDBG.findVariable (variableName);
218                                     }
219                                     else if (frameXD != null) {
220                                     variable = frameXD.findVariable (variableName);
221                                     }
222
223                                         if (variable != null) {
224                                                 appendVariable(buffer, variable);
225                                         }
226                                 } catch (DebugException x) {
227                                         // if (x.getStatus().getCode() !=
228                                         // IJavaThread.ERR_THREAD_NOT_SUSPENDED) {
229                                         PHPDebugUiPlugin.log(x);
230                                         // }
231                                 }
232
233                                 if (buffer.length() > 0) {
234                                         return buffer.toString();
235                                 }
236
237                         } catch (BadLocationException x) {
238                                 PHPDebugUiPlugin.log(x);
239                         }
240                 }
241
242                 return null;
243         }
244
245         /**
246          * Append HTML for the given variable to the given buffer
247          */
248         private static void appendVariable(StringBuffer buffer, IVariable variable)
249                         throws DebugException {
250
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$
254
255                 String type = getTypeName(variable);
256                 String value = "<b><pre>" + variable.getValue().getValueString() + "</pre></b>"; //$NON-NLS-1$ //$NON-NLS-2$
257
258                 if (type == null) {
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);
263                         buffer.append('"');
264                 } else if (type.equals("boolean") ||          // in dbg it's boolean
265                            type.equals("bool")) {             // in XDebug it's bool   $NON-NLS-1$
266                         buffer.append(' ');
267                         buffer.append(value);
268                 } else {
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);
273                 }
274                 buffer.append("</p>"); //$NON-NLS-1$
275         }
276
277         private static String getTypeName(IVariable variable) throws DebugException {
278                 IValue value = variable.getValue();
279
280                 if (value instanceof PHPValue)
281                         return ((PHPValue) value).getReferenceTypeName();
282
283                 if (value instanceof XDebugAbstractValue)
284                         return ((XDebugAbstractValue) value).getReferenceTypeName ();
285
286                 return null;
287         }
288
289         /*
290          * (non-Javadoc)
291          *
292          * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
293          */
294 //      public IInformationControlCreator getInformationControlCreator() {
295 //              // if
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) {
300 ////incastrix
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));
306 //                      }
307 //              };
308 //              // }
309 //              // return null;
310 //      }
311
312         /*
313          * (non-Javadoc)
314          *
315          * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
316          */
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) {
322 //incastrix
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));
328                                 }
329                         };
330                 }
331                 return null;
332         }
333 }