1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import com.ibm.icu.text.DateFormat; 15 import java.util.Date ; 16 import java.util.Iterator ; 17 18 import org.eclipse.core.resources.IMarker; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.jface.resource.JFaceResources; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.swt.graphics.Image; 27 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages; 28 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 29 import org.eclipse.ui.views.markers.MarkerViewUtil; 30 31 35 public final class Util { 36 37 static String EMPTY_STRING = ""; 39 static String TWO_LINE_FEED = "\n\n"; 41 static String LINE_FEED_AND_TAB = "\n\t"; 43 private static DateFormat format; 44 45 static final MarkerNode[] EMPTY_MARKER_ARRAY = new MarkerNode[0]; 46 47 55 public static String getProperty(String property, IMarker marker) { 56 if (marker == null || !marker.exists()) { 57 return EMPTY_STRING; 58 } 59 try { 60 Object obj = marker.getAttribute(property); 61 if (obj != null) { 62 return obj.toString(); 63 } 64 return EMPTY_STRING; 65 } catch (CoreException e) { 66 log(e); 67 return EMPTY_STRING; 68 } 69 } 70 71 77 public static String getCreationTime(long timestamp) { 78 if (format == null) { 79 format = DateFormat.getDateTimeInstance(DateFormat.LONG, 80 DateFormat.MEDIUM); 81 } 82 return format.format(new Date (timestamp)); 83 } 84 85 91 public static String getCreationTime(IMarker marker) { 92 try { 93 return getCreationTime(marker.getCreationTime()); 94 } catch (CoreException e) { 95 log(e); 96 return EMPTY_STRING; 97 } 98 } 99 100 108 public static String getContainerName(IMarker marker) { 109 110 if (!marker.exists()) 111 return Util.EMPTY_STRING; 112 113 try { 114 Object pathAttribute = marker 115 .getAttribute(MarkerViewUtil.PATH_ATTRIBUTE); 116 117 if (pathAttribute != null) { 118 return pathAttribute.toString(); 119 } 120 } catch (CoreException exception) { 121 log(exception); 123 } 124 125 IPath path = marker.getResource().getFullPath(); 126 int n = path.segmentCount() - 1; if (n <= 0) { 129 return Util.EMPTY_STRING; 130 } 131 int len = 0; 132 for (int i = 0; i < n; ++i) { 133 len += path.segment(i).length(); 134 } 135 if (n > 1) { 137 len += n - 1; 138 } 139 StringBuffer sb = new StringBuffer (len); 140 for (int i = 0; i < n; ++i) { 141 if (i != 0) { 142 sb.append('/'); 143 } 144 sb.append(path.segment(i)); 145 } 146 return sb.toString(); 147 } 148 149 154 public static void log(CoreException exception) { 155 IDEWorkbenchPlugin.log(exception.getLocalizedMessage(), exception); 156 } 157 158 166 public static String getResourceName(IMarker marker) { 167 168 if (!marker.exists()) 169 return Util.EMPTY_STRING; 170 171 try { 172 Object nameAttribute = marker 173 .getAttribute(MarkerViewUtil.NAME_ATTRIBUTE); 174 175 if (nameAttribute != null) { 176 return nameAttribute.toString(); 177 } 178 } catch (CoreException exception) { 179 log(exception); 180 } 181 182 return marker.getResource().getName(); 183 } 184 185 191 public static boolean isEditable(IMarker marker) { 192 if (marker == null) { 193 return false; 194 } 195 try { 196 return marker.isSubtypeOf(IMarker.BOOKMARK) 197 || (marker.isSubtypeOf(IMarker.TASK) && marker 198 .getAttribute(IMarker.USER_EDITABLE, true)); 199 } catch (CoreException e) { 200 return false; 201 } 202 } 203 204 210 public static IStatus errorStatus(Throwable exception) { 211 String message = exception.getLocalizedMessage(); 212 if (message == null) { 213 message = EMPTY_STRING; 214 } 215 return new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 216 IStatus.ERROR, message, exception); 217 } 218 219 static final int SHORT_DELAY = 100; 221 static final int LONG_DELAY = 30000; 223 225 private Util() { 226 super(); 227 } 228 229 235 public static Image getImage(int severity) { 236 237 if (severity == IMarker.SEVERITY_ERROR) { 238 return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_ERROR_PATH); 239 } 240 if (severity == IMarker.SEVERITY_WARNING) { 241 return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_WARNING_PATH); 242 } 243 if (severity == IMarker.SEVERITY_INFO) { 244 return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_INFO_PATH); 245 } 246 247 return null; 248 } 249 250 256 private static Image getIDEImage(String constantName) { 257 258 return JFaceResources.getResources().createImageWithDefault( 259 IDEInternalWorkbenchImages.getImageDescriptor(constantName)); 260 261 } 262 263 269 public static String getShortContainerName(IMarker marker) { 270 271 if (!marker.exists()) 272 return Util.EMPTY_STRING; 273 274 try { 275 Object pathAttribute = marker 276 .getAttribute(MarkerViewUtil.PATH_ATTRIBUTE); 277 278 if (pathAttribute != null) { 279 return pathAttribute.toString(); 280 } 281 } catch (CoreException exception) { 282 log(exception); 284 } 285 286 IResource resource = marker.getResource(); 287 int type = resource.getType(); 288 289 if (type == IResource.PROJECT) { 291 return resource.getName(); 292 } 293 294 if (type == IResource.ROOT) { 295 return MarkerMessages.Util_WorkspaceRoot; 296 } 297 298 String result = marker.getResource().getProjectRelativePath() 299 .removeLastSegments(1).toOSString(); 300 if (result.trim().length() == 0) { 301 return MarkerMessages.Util_ProjectRoot; 302 } 303 return result; 304 } 305 306 313 static boolean isSingleConcreteSelection(IStructuredSelection selection) { 314 if (selection != null && selection.size() == 1) { 315 Object first = selection.getFirstElement(); 316 if (first instanceof MarkerNode) { 317 return ((MarkerNode) first).isConcrete(); 318 } 319 } 320 return false; 321 } 322 323 329 public static boolean allConcreteSelection(IStructuredSelection selection) { 330 if (selection != null && selection.size() > 0) { 331 Iterator nodes = selection.iterator(); 332 while (nodes.hasNext()) { 333 if (((MarkerNode) nodes.next()).isConcrete()) { 334 continue; 335 } 336 return false; 337 } 338 return true; 339 } 340 return false; 341 } 342 } 343 | Popular Tags |