1 11 12 package org.eclipse.ui.views.tasklist; 13 14 import com.ibm.icu.text.DateFormat; 15 import com.ibm.icu.text.MessageFormat; 16 import java.util.Date ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import org.eclipse.core.resources.IMarker; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.jface.resource.ImageRegistry; 25 import org.eclipse.jface.viewers.IBasicPropertyConstants; 26 import org.eclipse.swt.graphics.Image; 27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 28 import org.eclipse.ui.internal.views.tasklist.TaskListMessages; 29 30 33 class MarkerUtil implements IMarkerConstants { 34 35 private static Map imageDescriptors; 36 37 private static ImageRegistry imageRegistry = new ImageRegistry(); 38 39 private static MessageFormat line = new MessageFormat(TaskListMessages.TaskList_line); 40 41 private static MessageFormat lineAndLocation = new MessageFormat( 42 TaskListMessages.TaskList_lineAndLocation); 43 44 static { 45 createImageDescriptors(); 46 } 47 48 51 private MarkerUtil() { 52 } 53 54 57 static void createImageDescriptors() { 58 59 60 imageDescriptors = new HashMap (51); 61 imageDescriptors 62 .put( 63 "header_complete", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/header_complete.gif")); imageDescriptors 65 .put( 66 "header_priority", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/header_priority.gif")); imageDescriptors.put( 68 "task", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/taskmrk_tsk.gif")); imageDescriptors.put( 70 "error", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/error_tsk.gif")); imageDescriptors.put( 72 "warn", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/warn_tsk.gif")); imageDescriptors.put( 74 "info", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/info_tsk.gif")); imageDescriptors.put( 76 "hprio", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/hprio_tsk.gif")); imageDescriptors.put( 78 "lprio", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/lprio_tsk.gif")); imageDescriptors 80 .put( 81 "complete_tsk", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/complete_tsk.gif")); imageDescriptors 83 .put( 84 "incomplete_tsk", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/incomplete_tsk.gif")); imageDescriptors.put( 86 "gotoobj", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/gotoobj_tsk.gif")); imageDescriptors.put( 88 "addtsk", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/addtsk_tsk.gif")); imageDescriptors 90 .put( 91 "addtsk_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/addtsk_tsk.gif")); imageDescriptors 93 .put( 94 "showcomplete", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/showcomplete_tsk.gif")); imageDescriptors 96 .put( 97 "selected_mode", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/selected_mode.gif")); imageDescriptors 99 .put( 100 "selected_mode_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/selected_mode.gif")); imageDescriptors 102 .put( 103 "showchild_mode", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/showchild_mode.gif")); imageDescriptors 105 .put( 106 "showchild_mode_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/showchild_mode.gif")); imageDescriptors 108 .put( 109 "showerr_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/showerr_tsk.gif")); imageDescriptors 111 .put( 112 "showwarn_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/showwarn_tsk.gif")); imageDescriptors 114 .put( 115 "showtsk_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/showtsk_tsk.gif")); imageDescriptors.put( 117 "filter", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/filter_ps.gif")); } 119 120 123 public static int getCharEnd(IMarker marker) { 124 return marker.getAttribute(IMarker.CHAR_END, -1); 125 } 126 127 130 public static int getCharStart(IMarker marker) { 131 return marker.getAttribute(IMarker.CHAR_START, -1); 132 } 133 134 139 public static Image getCompleteImage(IMarker marker) { 140 if (isMarkerType(marker, IMarker.TASK)) { 141 if (isComplete(marker)) { 142 return getImage("complete_tsk"); } else if (!isReadOnly(marker)) { 144 return getImage("incomplete_tsk"); } 146 } 147 return null; 148 } 149 150 154 public static String getCompleteText(IMarker marker) { 155 if (isMarkerType(marker, IMarker.TASK)) { 156 if (isComplete(marker)) { 157 return TaskListMessages.TaskList_completed; 158 } else { 159 return TaskListMessages.TaskList_notCompleted; 160 } 161 } 162 return ""; } 164 165 168 public static String getKindText(IMarker marker) { 169 if (isMarkerType(marker, IMarker.TASK)) { 170 return TaskListMessages.TaskList_task; 171 } 172 switch (getSeverity(marker)) { 173 case IMarker.SEVERITY_ERROR: 174 return TaskListMessages.TaskList_error; 175 case IMarker.SEVERITY_WARNING: 176 return TaskListMessages.TaskList_warning; 177 case IMarker.SEVERITY_INFO: 178 return TaskListMessages.TaskList_info; 179 } 180 return ""; } 182 183 186 public static String getContainerName(IMarker marker) { 187 IPath path = marker.getResource().getFullPath(); 188 int n = path.segmentCount() - 1; if (n <= 0) { 190 return ""; } 192 int len = 0; 193 for (int i = 0; i < n; ++i) { 194 len += path.segment(i).length(); 195 } 196 if (n > 1) { 198 len += n - 1; 199 } 200 StringBuffer sb = new StringBuffer (len); 201 for (int i = 0; i < n; ++i) { 202 if (i != 0) { 203 sb.append('/'); 204 } 205 sb.append(path.segment(i)); 206 } 207 return sb.toString(); 208 } 209 210 213 static Image getImage(String key) { 214 Image image = imageRegistry.get(key); 215 if (image == null) { 216 ImageDescriptor desc = getImageDescriptor(key); 217 if (desc != null) { 218 image = desc.createImage(false); 219 if (image == null) { 220 System.err 221 .println("TaskList: Error creating image for " + key); } 223 imageRegistry.put(key, image); 224 } 225 } 226 return image; 227 } 228 229 233 static public Image getImage(IMarker marker) { 234 if (isMarkerType(marker, IMarker.PROBLEM)) { 235 switch (getSeverity(marker)) { 236 case IMarker.SEVERITY_ERROR: 237 return getImage("error"); case IMarker.SEVERITY_WARNING: 239 return getImage("warn"); case IMarker.SEVERITY_INFO: 241 return getImage("info"); } 243 } else if (isMarkerType(marker, IMarker.TASK)) { 244 return getImage("task"); } 246 return null; 247 } 248 249 252 static ImageDescriptor getImageDescriptor(String key) { 253 ImageDescriptor desc = (ImageDescriptor) imageDescriptors.get(key); 254 if (desc == null) { 255 IDEWorkbenchPlugin.log("TaskList: No image descriptor for " + key); } 257 return desc; 258 } 259 260 263 public static String getLineAndLocation(IMarker marker) { 264 int lineNumber = getLineNumber(marker); 265 String location = getLocation(marker); 266 return getLineAndLocation(lineNumber, location); 267 } 268 269 272 public static String getLineAndLocation(int lineNumber, String location) { 273 if (lineNumber == -1) { 274 if (location.equals("")) { return ""; } else { 277 return location; 278 } 279 } else { 280 if (location.equals("")) { return line 282 .format(new Object [] { Integer.toString(lineNumber) }); 283 } else { 284 return lineAndLocation.format(new Object [] { 285 Integer.toString(lineNumber), location }); 286 } 287 } 288 } 289 290 293 public static int getLineNumber(IMarker marker) { 294 return marker.getAttribute(IMarker.LINE_NUMBER, -1); 295 } 296 297 300 public static String getLocation(IMarker marker) { 301 return marker.getAttribute(IMarker.LOCATION, ""); } 303 304 308 public static String getMessage(IMarker marker) { 309 return marker.getAttribute(IMarker.MESSAGE, ""); } 311 312 317 static public int getNumericValue(String value) { 318 boolean negative = false; 319 int i = 0; 320 int len = value.length(); 321 322 if (i < len && value.charAt(i) == '#') { 325 ++i; 326 } 327 328 if (i < len && value.charAt(i) == '-') { 329 negative = true; 330 ++i; 331 } 332 333 int result = 0; 334 while (i < len) { 335 int digit = Character.digit(value.charAt(i++), 10); 336 if (digit < 0) { 337 return result; 338 } 339 result = result * 10 + digit; 340 } 341 if (negative) { 342 result = -result; 343 } 344 return result; 345 } 346 347 350 public static int getPriority(IMarker marker) { 351 return marker.getAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL); 352 } 353 354 358 public static Image getPriorityImage(IMarker marker) { 359 switch (getPriority(marker)) { 360 case IMarker.PRIORITY_HIGH: 361 return getImage("hprio"); case IMarker.PRIORITY_NORMAL: 363 return null; case IMarker.PRIORITY_LOW: 365 return getImage("lprio"); } 367 return null; 368 } 369 370 373 public static String getPriorityText(IMarker marker) { 374 if (!isMarkerType(marker, IMarker.TASK)) { 375 return ""; } 377 378 switch (getPriority(marker)) { 379 case IMarker.PRIORITY_HIGH: 380 return TaskListMessages.TaskList_high; 381 case IMarker.PRIORITY_NORMAL: 382 return TaskListMessages.TaskList_normal; 383 case IMarker.PRIORITY_LOW: 384 return TaskListMessages.TaskList_low; 385 } 386 return ""; } 388 389 394 static public Object getProperty(Object object, String key) { 395 IMarker marker = (IMarker) object; 396 397 if (IBasicPropertyConstants.P_IMAGE == key) { 402 return getImage(marker); 403 } 404 if (IMarkerConstants.P_COMPLETE_IMAGE == key) { 405 return getCompleteImage(marker); 406 } 407 if (IMarkerConstants.P_PRIORITY_IMAGE == key) { 408 return getPriorityImage(marker); 409 } 410 if (IMarker.MESSAGE == key) { 411 return getMessage(marker); 412 } 413 if (IMarkerConstants.P_RESOURCE_NAME == key) { 414 return getResourceName(marker); 415 } 416 if (IMarkerConstants.P_CONTAINER_NAME == key) { 417 return getContainerName(marker); 418 } 419 if (IMarkerConstants.P_LINE_AND_LOCATION == key) { 420 return getLineAndLocation(marker); 421 } 422 if (IMarker.PRIORITY == key) { 423 return new Integer (IMarker.PRIORITY_HIGH - getPriority(marker)); 425 } 426 if (IMarker.DONE == key) { 427 return isComplete(marker) ? Boolean.TRUE : Boolean.FALSE; 428 } 429 if (IBasicPropertyConstants.P_TEXT == key) { 430 return getMessage(marker); 431 } 432 return null; 433 } 434 435 439 public static String getResourceName(IMarker marker) { 440 return marker.getResource().getName(); 441 } 442 443 446 public static int getSeverity(IMarker marker) { 447 return marker.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING); 448 } 449 450 455 public static boolean isComplete(IMarker marker) { 456 return marker.getAttribute(IMarker.DONE, false); 457 } 458 459 463 public static boolean isEditable(IMarker marker) { 464 return isMarkerType(marker, IMarker.TASK) && !isReadOnly(marker); 465 } 466 467 470 public static boolean isMarkerType(IMarker marker, String type) { 471 try { 472 return marker.isSubtypeOf(type); 473 } catch (CoreException e) { 474 return false; 475 } 476 } 477 478 481 public static boolean isReadOnly(IMarker marker) { 482 return !marker.getAttribute(IMarker.USER_EDITABLE, true); 483 } 484 485 488 public static String getCreationTime(IMarker marker) { 489 try { 490 return DateFormat.getDateTimeInstance(DateFormat.LONG, 491 DateFormat.MEDIUM).format( 492 new Date (marker.getCreationTime())); 493 } catch (CoreException e) { 494 return null; 495 } 496 } 497 } 498 | Popular Tags |