Fix #759.
[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                         if (((IMarker) element).getAttribute(IBreakpoint.ENABLED, false)) {
122                                 return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT);
123                         } else {
124                                 return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT_DISABLED);                           
125                         }
126                 } else if (element instanceof XDebugStackFrame
127                                 || element instanceof XDebugThread
128                                 || element instanceof XDebugTarget) {
129                         return getDebugElementImage(element);
130                 } else if (element instanceof XDebugVariable) {
131                         return getVariableImage((XDebugVariable) element);
132                 } else if (element instanceof XDebugValue) {
133                         return getValueImage((XDebugValue) element);
134                 }
135                 return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT);
136         }
137
138         private Image getVariableImage(XDebugVariable phpVar) {
139                 if (phpVar.getVisibility().equals("protected")) {
140                         return XDebugUIPluginImages.get(XDebugUIPluginImages.IMG_FIELD_PROTECTED);                      
141                 }  else if (phpVar.getVisibility().equals("private")) {
142                         return (XDebugUIPluginImages.get(XDebugUIPluginImages.IMG_FIELD_PRIVATE));                      
143                 }
144
145                 return XDebugUIPluginImages.get(XDebugUIPluginImages.IMG_FIELD_PUBLIC);                 
146         }
147         private Image getValueImage(XDebugValue phpVar) {
148                 if (phpVar != null) {
149                         return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_VARIABLE);
150                 }
151                 return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_VARIABLE);
152         }
153
154         /**
155          * @see IDebugModelPresentation#getText(Object)
156          */
157         public String getText(Object element) {
158                 try {
159                         if (element instanceof XDebugLineBreakpoint) {
160                                 return getBreakpointText((IBreakpoint) element);
161                         } else if (element instanceof XDebugVariable) {
162                                 XDebugVariable phpVar = (XDebugVariable) element;
163                                 return phpVar.getName() + "= " + phpVar.getValueString();//toString();
164                         }
165                 } catch (CoreException e) {
166                         //return PHPDebugUiMessages
167                                 //      .getString("PHPDebugModelPresentation.<not responding>"); //$NON-NLS-1$
168                 }
169                 return null;
170         }
171
172         /**
173          * @see IDebugModelPresentation#computeDetail(IValue, IValueDetailListener)
174          */
175         public void computeDetail(IValue value, IValueDetailListener listener) {
176                 String detail = "";
177                 try {
178                         detail = value.getValueString();
179                 } catch (DebugException e) {
180                 }
181                 listener.detailComputed(value, detail);
182                 //return;
183         }
184
185         protected IBreakpoint getBreakpoint(IMarker marker) {
186                 return DebugPlugin.getDefault().getBreakpointManager().getBreakpoint(
187                                 marker);
188         }
189
190         protected String getBreakpointText(IBreakpoint breakpoint)
191                         throws CoreException {
192                 if (breakpoint instanceof XDebugLineBreakpoint) {
193                         return getLineBreakpointText((XDebugLineBreakpoint) breakpoint);
194                 }
195                 return ""; //$NON-NLS-1$
196         }
197
198         protected String getLineBreakpointText(XDebugLineBreakpoint breakpoint)
199                         throws CoreException {
200                 StringBuffer label = new StringBuffer();
201
202                 label.append(breakpoint.getMarker().getResource().getFullPath());
203                 label.append(" ["); //$NON-NLS-1$
204                 //label.append(PHPDebugUiMessages
205                         //      .getString("PHPDebugModelPresentation.line")); //$NON-NLS-1$
206                 label.append(' ');
207                 label.append(breakpoint.getLineNumber());
208                 label.append(']');
209
210                 /*if (breakpoint.getHitCount() > 0) {
211                         label.append(" [skip count ");
212                         label.append(breakpoint.getHitCount());
213                         label.append(']');
214                 }*/
215
216                 /*if (breakpoint.isConditionEnabled()) {
217                         label.append(" [conditional]");
218                 }*/
219
220                 return label.toString();
221         }
222
223         /**
224          * Returns the image associated with the given element or <code>null</code>
225          * if none is defined.
226          */
227         protected Image getDebugElementImage(Object element) {
228                 Image image = null;
229                 if (element instanceof XDebugThread) {
230                         XDebugThread thread = (XDebugThread) element;
231                         if (thread.isSuspended()) {
232                                 image = DebugUITools
233                                                 .getImage(IDebugUIConstants.IMG_OBJS_THREAD_SUSPENDED);
234                         } else if (thread.isTerminated()) {
235                                 image = DebugUITools
236                                                 .getImage(IDebugUIConstants.IMG_OBJS_THREAD_TERMINATED);
237                         } else {
238                                 image = DebugUITools
239                                                 .getImage(IDebugUIConstants.IMG_OBJS_THREAD_RUNNING);
240                         }
241                 } else if (element instanceof XDebugStackFrame) {
242                         image = DebugUITools
243                                         .getImage(IDebugUIConstants.IMG_OBJS_STACKFRAME);
244                 } else if (element instanceof XDebugTarget) {
245                         XDebugTarget debugTarget = (XDebugTarget) element;
246                         if (debugTarget.isTerminated()) {
247                                 image = DebugUITools
248                                                 .getImage(IDebugUIConstants.IMG_OBJS_DEBUG_TARGET_TERMINATED);
249                         } else {
250                                 image = DebugUITools
251                                                 .getImage(IDebugUIConstants.IMG_OBJS_DEBUG_TARGET);
252                         }
253                 }
254                 return image;
255         }
256
257     class StorageEditorInput extends PlatformObject implements
258         IStorageEditorInput {
259 private File fFile;
260
261 private IStorage fStorage;
262
263 public StorageEditorInput(IStorage storage, File file) {
264         super();
265         fStorage = storage;
266         fFile = file;
267 }
268
269 public IStorage getStorage() {
270         return fStorage;
271 }
272
273 public ImageDescriptor getImageDescriptor() {
274         return null;
275 }
276
277 public String getName() {
278         return getStorage().getName();
279 }
280
281 public IPersistableElement getPersistable() {
282         return null;
283 }
284
285 public String getToolTipText() {
286         return getStorage().getFullPath().toOSString();
287 }
288
289 public boolean equals(Object object) {
290         return object instanceof StorageEditorInput
291                         && getStorage().equals(
292                                         ((StorageEditorInput) object).getStorage());
293 }
294
295 public int hashCode() {
296         return getStorage().hashCode();
297 }
298
299 public boolean exists() {
300         return fFile.exists();
301 }
302 }
303 }