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