1 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 58 public Image getImage(Object element) { 59 String 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 84 public String getImageKey(Object element) { 85 if (element instanceof IDebugElement) { 86 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 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 (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 170 public String getText(Object element) { 171 StringBuffer label= new StringBuffer (); 172 try { 173 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 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 ) { 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 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 []{new Integer (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 251 private String getBreakpointText(IBreakpoint breakpoint) { 252 IResource resource = breakpoint.getMarker().getResource(); 253 StringBuffer label = new StringBuffer (); 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 []{Integer.toString(lineNumber)})); 261 } catch (CoreException e) { 262 } 263 } 264 return label.toString(); 265 } 266 267 public String getAdapterLabel(Object 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 280 protected String getLaunchText(ILaunch launch) { 281 if (launch.getLaunchConfiguration() == null || (!launch.getLaunchConfiguration().exists() && !launch.getLaunchConfiguration().isWorkingCopy())) { 282 return DebugUIMessages.DefaultLabelProvider__unknown__1; 283 } 284 ILaunchConfiguration config = launch.getLaunchConfiguration(); 286 StringBuffer buff= new StringBuffer (config.getName()); 287 buff.append(" ["); try { 289 buff.append(config.getType().getName()); 290 } catch (CoreException e) { 291 DebugUIPlugin.log(e); 292 } 293 buff.append("]"); return buff.toString(); 295 } 296 297 protected String getExpressionText(IExpression expression) { 298 if (expression instanceof IWatchExpression) { 299 return getWatchExpressionText((IWatchExpression) expression); 300 } 301 StringBuffer buffer= new StringBuffer (expression.getExpressionText()); 302 String 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("= "); buffer.append(valueString); 314 } 315 return buffer.toString(); 316 } 317 318 322 protected String getWatchExpressionText(IWatchExpression expression) { 323 StringBuffer result= new StringBuffer (); 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 valueString= DebugUIPlugin.getModelPresentation().getText(value); 333 if (valueString.length() > 0) { 334 result.append(" = ").append(valueString); } 336 } 337 } 338 if (!expression.isEnabled()) { 339 result.append(DebugUIMessages.DefaultLabelProvider_15); 340 } 341 return result.toString(); 342 } 343 344 protected String getVariableText(IVariable variable) { 345 StringBuffer buffer= new StringBuffer (); 346 try { 347 IValue value = variable.getValue(); 348 buffer.append(variable.getName()); 349 buffer.append(" = "); buffer.append(value.getValueString()); 351 } catch (DebugException de) { 352 DebugUIPlugin.log(de); 353 } 354 return buffer.toString(); 355 } 356 357 protected String getRegisterGroupText(IRegisterGroup registerGroup) { 358 StringBuffer buffer= new StringBuffer (); 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 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 ""; } 377 378 protected String 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 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 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 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 433 public void addListener(ILabelProviderListener listener) { 434 } 435 436 439 public void dispose() { 440 } 441 442 445 public boolean isLabelProperty(Object element, String property) { 446 return false; 447 } 448 449 452 public void removeListener(ILabelProviderListener listener) { 453 } 454 455 462 public static String escapeSpecialChars(String string) { 463 if (string == null) { 464 return null; 465 } 466 StringBuffer escaped = new StringBuffer (); 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"); break; 473 case '\f': 474 escaped.append("\\f"); break; 476 case '\n': 477 escaped.append("\\n"); break; 479 case '\r': 480 escaped.append("\\r"); break; 482 case '\t': 483 escaped.append("\\t"); break; 485 case '\\': 486 escaped.append("\\\\"); break; 488 default: 489 escaped.append(c); 490 break; 491 } 492 } 493 return escaped.toString(); 494 } 495 496 503 public static String encodeEsacpedChars(String string) { 504 if (string == null) { 505 return null; 506 } 507 StringBuffer encoded = new StringBuffer (); 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 |