KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > DefaultLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * QNX Software Systems - Mikhail Khodjaiants - Registers View (Bug 53640)
11  *******************************************************************************/

12 package org.eclipse.debug.internal.ui;
13
14
15 import org.eclipse.core.resources.IMarker;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.debug.core.DebugException;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.ILaunch;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.ILaunchConfigurationType;
24 import org.eclipse.debug.core.ILaunchDelegate;
25 import org.eclipse.debug.core.ILaunchManager;
26 import org.eclipse.debug.core.model.IBreakpoint;
27 import org.eclipse.debug.core.model.IDebugElement;
28 import org.eclipse.debug.core.model.IDebugTarget;
29 import org.eclipse.debug.core.model.IDisconnect;
30 import org.eclipse.debug.core.model.IExpression;
31 import org.eclipse.debug.core.model.ILineBreakpoint;
32 import org.eclipse.debug.core.model.IProcess;
33 import org.eclipse.debug.core.model.IRegister;
34 import org.eclipse.debug.core.model.IRegisterGroup;
35 import org.eclipse.debug.core.model.IStackFrame;
36 import org.eclipse.debug.core.model.ITerminate;
37 import org.eclipse.debug.core.model.IThread;
38 import org.eclipse.debug.core.model.IValue;
39 import org.eclipse.debug.core.model.IVariable;
40 import org.eclipse.debug.core.model.IWatchExpression;
41 import org.eclipse.debug.core.model.IWatchpoint;
42 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
43 import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition;
44 import org.eclipse.debug.ui.IDebugUIConstants;
45 import org.eclipse.jface.resource.ImageDescriptor;
46 import org.eclipse.jface.viewers.ILabelProvider;
47 import org.eclipse.jface.viewers.ILabelProviderListener;
48 import org.eclipse.swt.graphics.Image;
49 import org.eclipse.ui.model.IWorkbenchAdapter;
50
51 import com.ibm.icu.text.MessageFormat;
52
53 public class DefaultLabelProvider implements ILabelProvider {
54
55     /**
56      * @see ILabelProvider#getImage(Object)
57      */

58     public Image getImage(Object JavaDoc element) {
59         String JavaDoc key= getImageKey(element);
60         if (key == null && element instanceof ILaunch) {
61             return null;
62         }
63         if (key == null && element instanceof IAdaptable) {
64             IWorkbenchAdapter de= (IWorkbenchAdapter) ((IAdaptable) element).getAdapter(IWorkbenchAdapter.class);
65             if (de != null) {
66                 ImageDescriptor descriptor= de.getImageDescriptor(element);
67                 if( descriptor != null) {
68                     return descriptor.createImage();
69                 }
70             }
71             return null;
72         }
73         if(element instanceof LaunchShortcutExtension) {
74             return ((LaunchShortcutExtension)element).getImageDescriptor().createImage();
75         }
76         return DebugPluginImages.getImage(key);
77     }
78     
79     /**
80      * Returns the key (<code>String</code>) of the default image
81      * appropriate for the given element or <code>null</code>
82      * if no default image is defined.
83      */

84     public String JavaDoc getImageKey(Object JavaDoc element) {
85         if (element instanceof IDebugElement) {
86             // Group elements into debug elements and non-debug elements
87
// to reduce the number of instanceof checks performed
88
if (element instanceof IRegister) {
89                 return IDebugUIConstants.IMG_OBJS_REGISTER;
90             } else if (element instanceof IRegisterGroup) {
91                 return IDebugUIConstants.IMG_OBJS_REGISTER_GROUP;
92             } else if (element instanceof IVariable || element instanceof IValue) {
93                 if (element instanceof IndexedVariablePartition) {
94                     return IInternalDebugUIConstants.IMG_OBJS_ARRAY_PARTITION;
95                 }
96                 return IDebugUIConstants.IMG_OBJS_VARIABLE;
97             } else if (element instanceof IStackFrame) {
98                 if (((IStackFrame)element).getThread().isSuspended()) {
99                     return IDebugUIConstants.IMG_OBJS_STACKFRAME;
100                 }
101                 return IDebugUIConstants.IMG_OBJS_STACKFRAME_RUNNING;
102             } else if (element instanceof IThread) {
103                 IThread thread = (IThread)element;
104                 if (thread.isSuspended()) {
105                     return IDebugUIConstants.IMG_OBJS_THREAD_SUSPENDED;
106                 } else if (thread.isTerminated()) {
107                     return IDebugUIConstants.IMG_OBJS_THREAD_TERMINATED;
108                 } else {
109                     return IDebugUIConstants.IMG_OBJS_THREAD_RUNNING;
110                 }
111             } else if (element instanceof IDebugTarget) {
112                 IDebugTarget target= (IDebugTarget) element;
113                 if (target.isTerminated() || target.isDisconnected()) {
114                     return IDebugUIConstants.IMG_OBJS_DEBUG_TARGET_TERMINATED;
115                 } else if (target.isSuspended()) {
116                     return IDebugUIConstants.IMG_OBJS_DEBUG_TARGET_SUSPENDED;
117                 } else {
118                     return IDebugUIConstants.IMG_OBJS_DEBUG_TARGET;
119                 }
120             } else if (element instanceof IExpression) {
121                 return IDebugUIConstants.IMG_OBJS_EXPRESSION;
122             }
123         } else {
124             if (element instanceof IMarker) {
125                 return getMarkerImageKey((IMarker)element);
126             } else if (element instanceof IBreakpoint) {
127                 return getBreakpointImageKey((IBreakpoint)element);
128             } else if (element instanceof IProcess) {
129                 if (((IProcess) element).isTerminated()) {
130                     return IDebugUIConstants.IMG_OBJS_OS_PROCESS_TERMINATED;
131                 }
132                 return IDebugUIConstants.IMG_OBJS_OS_PROCESS;
133             } else if (element instanceof ILaunch) {
134                 // determine the image from the launch config type
135
ILaunch launch= (ILaunch)element;
136                 ILaunchConfiguration configuration = launch.getLaunchConfiguration();
137                 if (configuration != null) {
138                     try {
139                         return configuration.getType().getIdentifier();
140                     } catch (CoreException e) {
141                         DebugUIPlugin.log(e);
142                         return null;
143                     }
144                 }
145                 // if no config, use the old "mode" way
146
if (launch.getLaunchMode().equals(ILaunchManager.DEBUG_MODE)) {
147                     return IDebugUIConstants.IMG_OBJS_LAUNCH_DEBUG;
148                 } else if (launch.isTerminated()) {
149                     return IDebugUIConstants.IMG_OBJS_LAUNCH_RUN_TERMINATED;
150                 } else {
151                     return IDebugUIConstants.IMG_OBJS_LAUNCH_RUN;
152                 }
153             } else if (element instanceof ILaunchConfigurationType) {
154                 return ((ILaunchConfigurationType)element).getIdentifier();
155             } else if (element instanceof ILaunchConfiguration) {
156                 try {
157                     return ((ILaunchConfiguration)element).getType().getIdentifier();
158                 } catch (CoreException e) {
159                     DebugUIPlugin.log(e);
160                     return null;
161                 }
162             }
163         }
164         return null;
165     }
166
167     /**
168      * @see ILabelProvider#getText(Object)
169      */

170     public String JavaDoc getText(Object JavaDoc element) {
171         StringBuffer JavaDoc label= new StringBuffer JavaDoc();
172         try {
173             // Group elements into debug elements and non-debug elements
174
// to reduce the number of instanceof checks performed
175
if (element instanceof IDebugElement) {
176                 if (element instanceof IStackFrame) {
177                     label.append(((IStackFrame)element).getName());
178                 } else if (element instanceof IndexedVariablePartition) {
179                     label.append(((IndexedVariablePartition)element).getName());
180                 } else if (element instanceof IVariable) {
181                     label.append(getVariableText((IVariable)element));
182                 } else if (element instanceof IThread) {
183                     label.append(((IThread)element).getName());
184                 } else if (element instanceof IDebugTarget) {
185                     label.append((((IDebugTarget)element).getName()));
186                 } else if (element instanceof IExpression) {
187                     label.append(getExpressionText((IExpression)element));
188                 } else if (element instanceof IRegisterGroup) {
189                     label.append(getRegisterGroupText((IRegisterGroup)element));
190                 } else if (element instanceof IValue) {
191                     label.append(((IValue)element).getValueString());
192                 }
193             } else {
194                 if (element instanceof IMarker) {
195                     label.append(getMarkerText((IMarker) element));
196                 } else if (element instanceof IBreakpoint) {
197                     label.append(getBreakpointText((IBreakpoint)element));
198                 } else if (element instanceof IProcess) {
199                     label.append(((IProcess) element).getLabel());
200                 } else if (element instanceof ILaunch) {
201                     label.append(getLaunchText((ILaunch) element));
202                 } else if (element instanceof ILaunchConfiguration) {
203                     label.append(((ILaunchConfiguration)element).getName());
204                 } else if (element instanceof ILaunchConfigurationType) {
205                     label.append(((ILaunchConfigurationType)element).getName());
206                 } else if(element instanceof ILaunchDelegate) {
207                     ILaunchDelegate delegate = (ILaunchDelegate) element;
208                     String JavaDoc name = delegate.getName();
209                     if(name == null) {
210                         name = delegate.getContributorName();
211                     }
212                     label.append(name);
213                 } else if(element instanceof LaunchShortcutExtension) {
214                     label.append(((LaunchShortcutExtension)element).getLabel());
215                 } else if (element instanceof String JavaDoc) {
216                     label.append(element);
217                 } else {
218                     label.append(getAdapterLabel(element));
219                 }
220             }
221             if (element instanceof ITerminate) {
222                 if (((ITerminate) element).isTerminated()) {
223                     String JavaDoc terminatedMessage= null;
224                     if (element instanceof IProcess) {
225                         IProcess process = (IProcess)element;
226                         int exit = process.getExitValue();
227                         terminatedMessage= MessageFormat.format(DebugUIMessages.DefaultLabelProvider_16, new String JavaDoc[]{new Integer JavaDoc(exit).toString()});
228                     } else {
229                         terminatedMessage= DebugUIMessages.DefaultLabelProvider_1;
230                     }
231                     label.insert(0, terminatedMessage);
232                 }
233             } else if (element instanceof IDisconnect) {
234                 if (((IDisconnect) element).isDisconnected()) {
235                     label.insert(0, DebugUIMessages.DefaultLabelProvider__disconnected__1);
236                 }
237             }
238         } catch (DebugException e) {
239             DebugUIPlugin.log(e);
240             label.append(DebugUIMessages.DefaultLabelProvider__unknown__1);
241         }
242         return label.toString();
243     }
244     
245     /**
246      * Returns default label for a breakpoint.
247      *
248      * @param breakpoint
249      * @return default label for a breakpoint
250      */

251     private String JavaDoc getBreakpointText(IBreakpoint breakpoint) {
252         IResource resource = breakpoint.getMarker().getResource();
253         StringBuffer JavaDoc label = new StringBuffer JavaDoc();
254         if (resource != null) {
255             label.append(resource.getName());
256         }
257         if (breakpoint instanceof ILineBreakpoint) {
258             try {
259                 int lineNumber = ((ILineBreakpoint)breakpoint).getLineNumber();
260                 label.append(MessageFormat.format(DebugUIMessages.DefaultLabelProvider_17, new String JavaDoc[]{Integer.toString(lineNumber)}));
261             } catch (CoreException e) {
262             }
263         }
264         return label.toString();
265     }
266
267     public String JavaDoc getAdapterLabel(Object JavaDoc object) {
268         if (object instanceof IAdaptable) {
269             IWorkbenchAdapter de= (IWorkbenchAdapter) ((IAdaptable) object).getAdapter(IWorkbenchAdapter.class);
270             if (de != null) {
271                 return de.getLabel(object);
272             }
273         }
274         return DebugUIMessages.DefaultLabelProvider__unknown__1;
275     }
276     
277     /**
278      * Used to render launch history items in the re-launch drop downs
279      */

280     protected String JavaDoc getLaunchText(ILaunch launch) {
281         if (launch.getLaunchConfiguration() == null || (!launch.getLaunchConfiguration().exists() && !launch.getLaunchConfiguration().isWorkingCopy())) {
282             return DebugUIMessages.DefaultLabelProvider__unknown__1;
283         }
284         // new launch configuration
285
ILaunchConfiguration config = launch.getLaunchConfiguration();
286         StringBuffer JavaDoc buff= new StringBuffer JavaDoc(config.getName());
287         buff.append(" ["); //$NON-NLS-1$
288
try {
289             buff.append(config.getType().getName());
290         } catch (CoreException e) {
291             DebugUIPlugin.log(e);
292         }
293         buff.append("]"); //$NON-NLS-1$
294
return buff.toString();
295     }
296
297     protected String JavaDoc getExpressionText(IExpression expression) {
298         if (expression instanceof IWatchExpression) {
299             return getWatchExpressionText((IWatchExpression) expression);
300         }
301         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(expression.getExpressionText());
302         String JavaDoc valueString= null;
303         IValue value= expression.getValue();
304         if (value != null) {
305             try {
306                 valueString= value.getValueString();
307             } catch (DebugException de) {
308                 DebugUIPlugin.log(de);
309             }
310         }
311         if (valueString != null && valueString.length() > 0) {
312             buffer.append("= "); //$NON-NLS-1$
313
buffer.append(valueString);
314         }
315         return buffer.toString();
316     }
317     
318     /**
319      * @param expression
320      * @return
321      */

322     protected String JavaDoc getWatchExpressionText(IWatchExpression expression) {
323         StringBuffer JavaDoc result= new StringBuffer JavaDoc();
324         result.append('"').append(expression.getExpressionText()).append('"');
325         if (expression.isPending()) {
326             result.append(DebugUIMessages.DefaultLabelProvider_12);
327         } else if (expression.hasErrors()) {
328             result.append(DebugUIMessages.DefaultLabelProvider_13);
329         } else {
330             IValue value= expression.getValue();
331             if (value != null) {
332                 String JavaDoc valueString= DebugUIPlugin.getModelPresentation().getText(value);
333                 if (valueString.length() > 0) {
334                     result.append(" = ").append(valueString); //$NON-NLS-1$
335
}
336             }
337         }
338         if (!expression.isEnabled()) {
339             result.append(DebugUIMessages.DefaultLabelProvider_15);
340         }
341         return result.toString();
342     }
343
344     protected String JavaDoc getVariableText(IVariable variable) {
345         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
346         try {
347             IValue value = variable.getValue();
348             buffer.append(variable.getName());
349             buffer.append(" = "); //$NON-NLS-1$
350
buffer.append(value.getValueString());
351         } catch (DebugException de) {
352             DebugUIPlugin.log(de);
353         }
354         return buffer.toString();
355     }
356     
357     protected String JavaDoc getRegisterGroupText(IRegisterGroup registerGroup) {
358         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
359         try {
360             buffer.append(registerGroup.getName());
361         } catch (DebugException de) {
362             DebugUIPlugin.log(de);
363         }
364         return buffer.toString();
365     }
366     
367     protected String JavaDoc getMarkerText(IMarker marker) {
368         try {
369             if (marker.exists() && marker.isSubtypeOf(IBreakpoint.BREAKPOINT_MARKER)) {
370                 return DebugUIMessages.DefaultLabelProvider_Breakpoint_1;
371             }
372         } catch (CoreException e) {
373             DebugUIPlugin.log(e);
374         }
375         return ""; //$NON-NLS-1$
376
}
377     
378     protected String JavaDoc getMarkerImageKey(IMarker marker) {
379         try {
380             IBreakpoint breakpoint= DebugPlugin.getDefault().getBreakpointManager().getBreakpoint(marker);
381             if (breakpoint != null && marker.exists()) {
382                 if (breakpoint.isEnabled()) {
383                     return IDebugUIConstants.IMG_OBJS_BREAKPOINT;
384                 }
385                 return IDebugUIConstants.IMG_OBJS_BREAKPOINT_DISABLED;
386             }
387         } catch (CoreException e) {
388         }
389         return null;
390     }
391     
392     protected String JavaDoc getBreakpointImageKey(IBreakpoint breakpoint) {
393         if (breakpoint != null && breakpoint.getMarker().exists()) {
394             try {
395                 boolean enabled = breakpoint.isEnabled();
396                 if (breakpoint instanceof IWatchpoint) {
397                     IWatchpoint watchpoint = (IWatchpoint) breakpoint;
398                     if (watchpoint.isAccess()) {
399                         if (watchpoint.isModification()) {
400                             //access and modification
401
if (enabled) {
402                                 return IDebugUIConstants.IMG_OBJS_WATCHPOINT;
403                             }
404                             return IDebugUIConstants.IMG_OBJS_WATCHPOINT_DISABLED;
405                         }
406                         if (enabled) {
407                             return IDebugUIConstants.IMG_OBJS_ACCESS_WATCHPOINT;
408                         }
409                         return IDebugUIConstants.IMG_OBJS_ACCESS_WATCHPOINT_DISABLED;
410                     } else if (watchpoint.isModification()) {
411                         if (enabled) {
412                             return IDebugUIConstants.IMG_OBJS_MODIFICATION_WATCHPOINT;
413                         }
414                         return IDebugUIConstants.IMG_OBJS_MODIFICATION_WATCHPOINT_DISABLED;
415                     } else {
416                         //neither access nor modification
417
return IDebugUIConstants.IMG_OBJS_WATCHPOINT_DISABLED;
418                     }
419                 }
420                 if (enabled) {
421                     return IDebugUIConstants.IMG_OBJS_BREAKPOINT;
422                 }
423                 return IDebugUIConstants.IMG_OBJS_BREAKPOINT_DISABLED;
424             } catch (CoreException e) {
425             }
426         }
427         return null;
428     }
429
430     /**
431      * @see IBaseLabelProvider#addListener(ILabelProviderListener)
432      */

433     public void addListener(ILabelProviderListener listener) {
434     }
435
436     /**
437      * @see IBaseLabelProvider#dispose()
438      */

439     public void dispose() {
440     }
441
442     /**
443      * @see IBaseLabelProvider#isLabelProperty(Object, String)
444      */

445     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
446         return false;
447     }
448
449     /**
450      * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
451      */

452     public void removeListener(ILabelProviderListener listener) {
453     }
454     
455     /**
456      * Returns the given string with special chars in escaped sequences.
457      *
458      * @param label
459      * @return the given string with special chars in escaped sequences
460      * @since 3.3
461      */

462     public static String JavaDoc escapeSpecialChars(String JavaDoc string) {
463         if (string == null) {
464             return null;
465         }
466         StringBuffer JavaDoc escaped = new StringBuffer JavaDoc();
467         for (int i = 0; i < string.length(); i++) {
468             char c = string.charAt(i);
469             switch (c) {
470                 case '\b':
471                     escaped.append("\\b"); //$NON-NLS-1$
472
break;
473                 case '\f':
474                     escaped.append("\\f"); //$NON-NLS-1$
475
break;
476                 case '\n':
477                     escaped.append("\\n"); //$NON-NLS-1$
478
break;
479                 case '\r':
480                     escaped.append("\\r"); //$NON-NLS-1$
481
break;
482                 case '\t':
483                     escaped.append("\\t"); //$NON-NLS-1$
484
break;
485                 case '\\':
486                     escaped.append("\\\\"); //$NON-NLS-1$
487
break;
488                 default:
489                     escaped.append(c);
490                     break;
491             }
492         }
493         return escaped.toString();
494     }
495     
496     /**
497      * Returns the string with escaped sequences replaced with single chars.
498      *
499      * @param string
500      * @return the string with escaped sequences replaced with single chars
501      * @since 3.3
502      */

503     public static String JavaDoc encodeEsacpedChars(String JavaDoc string) {
504         if (string == null) {
505             return null;
506         }
507         StringBuffer JavaDoc encoded = new StringBuffer JavaDoc();
508         if (string.length() == 1) {
509             return string;
510         }
511         for (int i = 0; i < string.length(); i++) {
512             char c = string.charAt(i);
513             if (c == '\\') {
514                 switch (string.charAt(i+1)) {
515                     case 'b':
516                         c= '\b';
517                         i++;
518                         break;
519                     case 'f':
520                         c= '\f';
521                         i++;
522                         break;
523                     case 'n':
524                         c= '\n';
525                         i++;
526                         break;
527                     case 'r':
528                         c= '\r';
529                         i++;
530                         break;
531                     case 't':
532                         c= '\t';
533                         i++;
534                         break;
535                     case '\'':
536                         c= '\'';
537                         i++;
538                         break;
539                     case '\\':
540                         c= '\\';
541                         i++;
542                         break;
543                     default :
544                         break;
545                 }
546             }
547             encoded.append(c);
548         }
549         return encoded.toString();
550     }
551 }
552
553
Popular Tags