1 11 package org.eclipse.core.internal.resources; 12 13 import java.util.Map ; 14 import org.eclipse.core.internal.utils.Messages; 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.core.runtime.jobs.ISchedulingRule; 18 import org.eclipse.osgi.util.NLS; 19 20 32 public class Marker extends PlatformObject implements IMarker { 33 34 35 protected long id; 36 37 38 protected IResource resource; 39 40 43 Marker(IResource resource, long id) { 44 Assert.isLegal(resource != null); 45 this.resource = resource; 46 this.id = id; 47 } 48 49 53 private void checkInfo(MarkerInfo info) throws CoreException { 54 if (info == null) { 55 String message = NLS.bind(Messages.resources_markerNotFound, Long.toString(id)); 56 throw new ResourceException(new ResourceStatus(IResourceStatus.MARKER_NOT_FOUND, resource.getFullPath(), message)); 57 } 58 } 59 60 63 public void delete() throws CoreException { 64 final ISchedulingRule rule = getWorkspace().getRuleFactory().markerRule(resource); 65 try { 66 getWorkspace().prepareOperation(rule, null); 67 getWorkspace().beginOperation(true); 68 getWorkspace().getMarkerManager().removeMarker(getResource(), getId()); 69 } finally { 70 getWorkspace().endOperation(rule, false, null); 71 } 72 } 73 74 77 public boolean equals(Object object) { 78 if (!(object instanceof IMarker)) 79 return false; 80 IMarker other = (IMarker) object; 81 return (id == other.getId() && resource.equals(other.getResource())); 82 } 83 84 87 public boolean exists() { 88 return getInfo() != null; 89 } 90 91 94 public Object getAttribute(String attributeName) throws CoreException { 95 Assert.isNotNull(attributeName); 96 MarkerInfo info = getInfo(); 97 checkInfo(info); 98 return info.getAttribute(attributeName); 99 } 100 101 104 public int getAttribute(String attributeName, int defaultValue) { 105 Assert.isNotNull(attributeName); 106 MarkerInfo info = getInfo(); 107 if (info == null) 108 return defaultValue; 109 Object value = info.getAttribute(attributeName); 110 if (value instanceof Integer ) 111 return ((Integer ) value).intValue(); 112 return defaultValue; 113 } 114 115 118 public String getAttribute(String attributeName, String defaultValue) { 119 Assert.isNotNull(attributeName); 120 MarkerInfo info = getInfo(); 121 if (info == null) 122 return defaultValue; 123 Object value = info.getAttribute(attributeName); 124 if (value instanceof String ) 125 return (String ) value; 126 return defaultValue; 127 } 128 129 132 public boolean getAttribute(String attributeName, boolean defaultValue) { 133 Assert.isNotNull(attributeName); 134 MarkerInfo info = getInfo(); 135 if (info == null) 136 return defaultValue; 137 Object value = info.getAttribute(attributeName); 138 if (value instanceof Boolean ) 139 return ((Boolean ) value).booleanValue(); 140 return defaultValue; 141 } 142 143 146 public Map getAttributes() throws CoreException { 147 MarkerInfo info = getInfo(); 148 checkInfo(info); 149 return info.getAttributes(); 150 } 151 152 155 public Object [] getAttributes(String [] attributeNames) throws CoreException { 156 Assert.isNotNull(attributeNames); 157 MarkerInfo info = getInfo(); 158 checkInfo(info); 159 return info.getAttributes(attributeNames); 160 } 161 162 165 public long getCreationTime() throws CoreException { 166 MarkerInfo info = getInfo(); 167 checkInfo(info); 168 return info.getCreationTime(); 169 } 170 171 174 public long getId() { 175 return id; 176 } 177 178 protected MarkerInfo getInfo() { 179 return getWorkspace().getMarkerManager().findMarkerInfo(resource, id); 180 } 181 182 185 public IResource getResource() { 186 return resource; 187 } 188 189 192 public String getType() throws CoreException { 193 MarkerInfo info = getInfo(); 194 checkInfo(info); 195 return info.getType(); 196 } 197 198 203 private Workspace getWorkspace() { 204 return resource == null ? null : (Workspace) resource.getWorkspace(); 205 } 206 207 public int hashCode() { 208 return (int) id + resource.hashCode(); 209 } 210 211 214 public boolean isSubtypeOf(String type) throws CoreException { 215 return getWorkspace().getMarkerManager().isSubtype(getType(), type); 216 } 217 218 221 public void setAttribute(String attributeName, int value) throws CoreException { 222 setAttribute(attributeName, new Integer (value)); 223 } 224 225 228 public void setAttribute(String attributeName, Object value) throws CoreException { 229 Assert.isNotNull(attributeName); 230 Workspace workspace = getWorkspace(); 231 MarkerManager manager = workspace.getMarkerManager(); 232 try { 233 workspace.prepareOperation(null, null); 234 workspace.beginOperation(true); 235 MarkerInfo markerInfo = getInfo(); 236 checkInfo(markerInfo); 237 238 boolean needDelta = !manager.hasDelta(resource.getFullPath(), id); 240 MarkerInfo oldInfo = needDelta ? (MarkerInfo) markerInfo.clone() : null; 241 markerInfo.setAttribute(attributeName, value); 242 if (manager.isPersistent(markerInfo)) 243 ((Resource) resource).getResourceInfo(false, true).set(ICoreConstants.M_MARKERS_SNAP_DIRTY); 244 if (needDelta) { 245 MarkerDelta delta = new MarkerDelta(IResourceDelta.CHANGED, resource, oldInfo); 246 manager.changedMarkers(resource, new MarkerDelta[] {delta}); 247 } 248 } finally { 249 workspace.endOperation(null, false, null); 250 } 251 } 252 253 256 public void setAttribute(String attributeName, boolean value) throws CoreException { 257 setAttribute(attributeName, value ? Boolean.TRUE : Boolean.FALSE); 258 } 259 260 263 public void setAttributes(String [] attributeNames, Object [] values) throws CoreException { 264 Assert.isNotNull(attributeNames); 265 Assert.isNotNull(values); 266 Workspace workspace = getWorkspace(); 267 MarkerManager manager = workspace.getMarkerManager(); 268 try { 269 workspace.prepareOperation(null, null); 270 workspace.beginOperation(true); 271 MarkerInfo markerInfo = getInfo(); 272 checkInfo(markerInfo); 273 274 boolean needDelta = !manager.hasDelta(resource.getFullPath(), id); 276 MarkerInfo oldInfo = needDelta ? (MarkerInfo) markerInfo.clone() : null; 277 markerInfo.setAttributes(attributeNames, values); 278 if (manager.isPersistent(markerInfo)) 279 ((Resource) resource).getResourceInfo(false, true).set(ICoreConstants.M_MARKERS_SNAP_DIRTY); 280 if (needDelta) { 281 MarkerDelta delta = new MarkerDelta(IResourceDelta.CHANGED, resource, oldInfo); 282 manager.changedMarkers(resource, new MarkerDelta[] {delta}); 283 } 284 } finally { 285 workspace.endOperation(null, false, null); 286 } 287 } 288 289 292 public void setAttributes(Map values) throws CoreException { 293 Workspace workspace = getWorkspace(); 294 MarkerManager manager = workspace.getMarkerManager(); 295 try { 296 workspace.prepareOperation(null, null); 297 workspace.beginOperation(true); 298 MarkerInfo markerInfo = getInfo(); 299 checkInfo(markerInfo); 300 301 boolean needDelta = !manager.hasDelta(resource.getFullPath(), id); 303 MarkerInfo oldInfo = needDelta ? (MarkerInfo) markerInfo.clone() : null; 304 markerInfo.setAttributes(values); 305 if (manager.isPersistent(markerInfo)) 306 ((Resource) resource).getResourceInfo(false, true).set(ICoreConstants.M_MARKERS_SNAP_DIRTY); 307 if (needDelta) { 308 MarkerDelta delta = new MarkerDelta(IResourceDelta.CHANGED, resource, oldInfo); 309 manager.changedMarkers(resource, new MarkerDelta[] {delta}); 310 } 311 } finally { 312 workspace.endOperation(null, false, null); 313 } 314 } 315 } 316 | Popular Tags |