Refactor and enabled value view in variable view.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / php / launching / PHPDebugModelPresentation.java
1 /**********************************************************************
2  Copyright (c) 2000, 2002 IBM Corp. 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 implementation
10  Vicente Fernando - www.alfersoft.com.ar
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
13
14 import java.io.File;
15 import java.util.HashMap;
16
17 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugLineBreakpoint;
18
19 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
20 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugThread;
21 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugStackFrame;
22 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugVariable;
23 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugValue;
24 import net.sourceforge.phpeclipse.xdebug.ui.XDebugUIPluginImages;
25 //import net.sourceforge.phpeclipse.xdebug.ui.XDebugUIPlugin;
26 //import net.sourceforge.phpeclipse.xdebug.ui.php.launching.CopyOfPHPDebugModelPresentation.StorageEditorInput;
27
28 //import net.sourceforge.phpdt.internal.debug.core.model.IPHPDebugTarget;
29
30 //import net.sourceforge.phpdt.internal.debug.core.model.PHPThread;
31 //import net.sourceforge.phpdt.internal.debug.core.model.PHPValue;
32 //import net.sourceforge.phpdt.internal.debug.core.model.PHPVariable;
33
34 import org.eclipse.core.resources.IFile;
35 import org.eclipse.core.resources.IMarker;
36 import org.eclipse.core.resources.IStorage;
37 import org.eclipse.core.runtime.CoreException;
38 import org.eclipse.core.runtime.PlatformObject;
39 import org.eclipse.debug.core.DebugException;
40 import org.eclipse.debug.core.DebugPlugin;
41 import org.eclipse.debug.core.model.IBreakpoint;
42 import org.eclipse.debug.core.model.ILineBreakpoint;
43 import org.eclipse.debug.core.model.IValue;
44 import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
45 import org.eclipse.debug.ui.DebugUITools;
46 import org.eclipse.debug.ui.IDebugModelPresentation;
47 import org.eclipse.debug.ui.IDebugUIConstants;
48 import org.eclipse.debug.ui.IValueDetailListener;
49 import org.eclipse.jface.resource.ImageDescriptor;
50 import org.eclipse.jface.viewers.LabelProvider;
51 import org.eclipse.swt.graphics.Image;
52 import org.eclipse.ui.IEditorDescriptor;
53 import org.eclipse.ui.IEditorInput;
54 import org.eclipse.ui.IEditorRegistry;
55 import org.eclipse.ui.IPersistableElement;
56 import org.eclipse.ui.IStorageEditorInput;
57 import org.eclipse.ui.PlatformUI;
58 import org.eclipse.ui.part.FileEditorInput;
59
60 /**
61  * @see IDebugModelPresentation
62  */
63 public class PHPDebugModelPresentation extends LabelProvider implements
64                 IDebugModelPresentation {
65
66         protected HashMap fAttributes = new HashMap(3);
67
68         public PHPDebugModelPresentation() {
69                 super();
70         }
71
72         /**
73          * @see IDebugModelPresentation#getEditorId(IEditorInput, Object)
74          */
75         public String getEditorId(IEditorInput input, Object inputObject) {
76                 IEditorRegistry registry = PlatformUI.getWorkbench()
77                                 .getEditorRegistry();
78                 IEditorDescriptor descriptor = registry.getDefaultEditor(input
79                                 .getName());
80                 if (descriptor != null)
81                         return descriptor.getId();
82
83                 return null;
84         }
85
86         /**
87          * @see IDebugModelPresentation#setAttribute(String, Object)
88          */
89         public void setAttribute(String id, Object value) {
90                 if (value == null) {
91                         return;
92                 }
93                 fAttributes.put(id, value);
94         }
95
96         /**
97          * @see IDebugModelPresentation#getEditorInput(Object)
98          */
99         public IEditorInput getEditorInput(Object element) {
100
101                 if (element instanceof IFile) {
102                         return new FileEditorInput((IFile)element);
103                 }
104                 if( element instanceof LocalFileStorage) {
105                         LocalFileStorage lfc= (LocalFileStorage)element;
106                         return new StorageEditorInput(lfc,lfc.getFile());
107                 }
108                 if (element instanceof ILineBreakpoint) {
109                         return new FileEditorInput((IFile)((ILineBreakpoint)element).getMarker().getResource());
110                 }
111                 return null;
112         }
113         
114         /**
115          * @see IDebugModelPresentation#getImage(Object)
116          */
117         public Image getImage(Object element) {
118                 if (element instanceof XDebugLineBreakpoint) {
119                         return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT);
120                 } else if (element instanceof IMarker) {
121                         return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT);
122                 } else if (element instanceof XDebugStackFrame
123                                 || element instanceof XDebugThread
124                                 || element instanceof XDebugTarget) {
125                         return getDebugElementImage(element);
126                 } else if (element instanceof XDebugVariable) {
127                         return getVariableImage((XDebugVariable) element);
128                 } else if (element instanceof XDebugValue) {
129                         return getValueImage((XDebugValue) element);
130                 }
131                 return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT);
132         }
133
134 //      private Image getVariableImage(XDebugVariable phpVar) {
135                 /*
136                  * if (phpVar != null) { if (phpVar.isLocal()) return
137                  * DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_VARIABLE); if
138                  * (phpVar.isHashValue()) return
139                  * DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_VARIABLE); }
140                  */
141         //      return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_VARIABLE);
142         //}
143         
144         private Image getVariableImage(XDebugVariable phpVar) {
145                 if (phpVar.getVisibility().equals("protected")) {
146                         return XDebugUIPluginImages.get(XDebugUIPluginImages.IMG_FIELD_PROTECTED);                      
147                 }  else if (phpVar.getVisibility().equals("private")) {
148                         return (XDebugUIPluginImages.get(XDebugUIPluginImages.IMG_FIELD_PRIVATE));                      
149                 }
150
151                 return XDebugUIPluginImages.get(XDebugUIPluginImages.IMG_FIELD_PUBLIC);                 
152         }
153         private Image getValueImage(XDebugValue phpVar) {
154                 if (phpVar != null) {
155                         return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_VARIABLE);
156                 }
157                 return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_VARIABLE);
158         }
159
160         /**
161          * @see IDebugModelPresentation#getText(Object)
162          */
163         public String getText(Object element) {
164                 try {
165                         if (element instanceof XDebugLineBreakpoint) {
166                                 return getBreakpointText((IBreakpoint) element);
167                         } else if (element instanceof XDebugVariable) {
168                                 XDebugVariable phpVar = (XDebugVariable) element;
169                                 return phpVar.getName() + "= " + phpVar.getValueString();//toString();
170                         }
171                 } catch (CoreException e) {
172                         //return PHPDebugUiMessages
173                                 //      .getString("PHPDebugModelPresentation.<not responding>"); //$NON-NLS-1$
174                 }
175                 return null;
176         }
177
178         /**
179          * @see IDebugModelPresentation#computeDetail(IValue, IValueDetailListener)
180          */
181         public void computeDetail(IValue value, IValueDetailListener listener) {
182                 String detail = "";
183                 try {
184                         detail = value.getValueString();
185                 } catch (DebugException e) {
186                 }
187                 listener.detailComputed(value, detail);
188                 //return;
189         }
190
191         protected IBreakpoint getBreakpoint(IMarker marker) {
192                 return DebugPlugin.getDefault().getBreakpointManager().getBreakpoint(
193                                 marker);
194         }
195
196         protected String getBreakpointText(IBreakpoint breakpoint)
197                         throws CoreException {
198                 if (breakpoint instanceof XDebugLineBreakpoint) {
199                         return getLineBreakpointText((XDebugLineBreakpoint) breakpoint);
200                 }
201                 return ""; //$NON-NLS-1$
202         }
203
204         protected String getLineBreakpointText(XDebugLineBreakpoint breakpoint)
205                         throws CoreException {
206                 StringBuffer label = new StringBuffer();
207
208                 label.append(breakpoint.getMarker().getResource().getFullPath());
209                 label.append(" ["); //$NON-NLS-1$
210                 //label.append(PHPDebugUiMessages
211                         //      .getString("PHPDebugModelPresentation.line")); //$NON-NLS-1$
212                 label.append(' ');
213                 label.append(breakpoint.getLineNumber());
214                 label.append(']');
215
216                 /*if (breakpoint.getHitCount() > 0) {
217                         label.append(" [skip count ");
218                         label.append(breakpoint.getHitCount());
219                         label.append(']');
220                 }*/
221
222                 /*if (breakpoint.isConditionEnabled()) {
223                         label.append(" [conditional]");
224                 }*/
225
226                 return label.toString();
227         }
228
229         /**
230          * Returns the image associated with the given element or <code>null</code>
231          * if none is defined.
232          */
233         protected Image getDebugElementImage(Object element) {
234                 Image image = null;
235                 if (element instanceof XDebugThread) {
236                         XDebugThread thread = (XDebugThread) element;
237                         if (thread.isSuspended()) {
238                                 image = DebugUITools
239                                                 .getImage(IDebugUIConstants.IMG_OBJS_THREAD_SUSPENDED);
240                         } else if (thread.isTerminated()) {
241                                 image = DebugUITools
242                                                 .getImage(IDebugUIConstants.IMG_OBJS_THREAD_TERMINATED);
243                         } else {
244                                 image = DebugUITools
245                                                 .getImage(IDebugUIConstants.IMG_OBJS_THREAD_RUNNING);
246                         }
247                 } else if (element instanceof XDebugStackFrame) {
248                         image = DebugUITools
249                                         .getImage(IDebugUIConstants.IMG_OBJS_STACKFRAME);
250                 } else if (element instanceof XDebugTarget) {
251                         XDebugTarget debugTarget = (XDebugTarget) element;
252                         if (debugTarget.isTerminated()) {
253                                 image = DebugUITools
254                                                 .getImage(IDebugUIConstants.IMG_OBJS_DEBUG_TARGET_TERMINATED);
255                         } else {
256                                 image = DebugUITools
257                                                 .getImage(IDebugUIConstants.IMG_OBJS_DEBUG_TARGET);
258                         }
259                 }
260                 return image;
261         }
262
263     class StorageEditorInput extends PlatformObject implements
264         IStorageEditorInput {
265 private File fFile;
266
267 private IStorage fStorage;
268
269 public StorageEditorInput(IStorage storage, File file) {
270         super();
271         fStorage = storage;
272         fFile = file;
273 }
274
275 public IStorage getStorage() {
276         return fStorage;
277 }
278
279 public ImageDescriptor getImageDescriptor() {
280         return null;
281 }
282
283 public String getName() {
284         return getStorage().getName();
285 }
286
287 public IPersistableElement getPersistable() {
288         return null;
289 }
290
291 public String getToolTipText() {
292         return getStorage().getFullPath().toOSString();
293 }
294
295 public boolean equals(Object object) {
296         return object instanceof StorageEditorInput
297                         && getStorage().equals(
298                                         ((StorageEditorInput) object).getStorage());
299 }
300
301 public int hashCode() {
302         return getStorage().hashCode();
303 }
304
305 public boolean exists() {
306         return fFile.exists();
307 }
308 }
309 }