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