KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > Marker


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.util.Map JavaDoc;
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 /**
21  * An abstract marker implementation.
22  * Subclasses must implement the <code>clone</code> method, and
23  * are free to declare additional field and method members.
24  * <p>
25  * Note: Marker objects do not store whether they are "standalone"
26  * vs. "attached" to the workspace. This information is maintained
27  * by the workspace.
28  * </p>
29  *
30  * @see IMarker
31  */

32 public class Marker extends PlatformObject implements IMarker {
33
34     /** Marker identifier. */
35     protected long id;
36
37     /** Resource with which this marker is associated. */
38     protected IResource resource;
39
40     /**
41      * Constructs a new marker object.
42      */

43     Marker(IResource resource, long id) {
44         Assert.isLegal(resource != null);
45         this.resource = resource;
46         this.id = id;
47     }
48
49     /**
50      * Checks the given marker info to ensure that it is not null.
51      * Throws an exception if it is.
52      */

53     private void checkInfo(MarkerInfo info) throws CoreException {
54         if (info == null) {
55             String JavaDoc 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     /**
61      * @see IMarker#delete()
62      */

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     /**
75      * @see IMarker#equals(Object)
76      */

77     public boolean equals(Object JavaDoc 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     /**
85      * @see IMarker#exists()
86      */

87     public boolean exists() {
88         return getInfo() != null;
89     }
90
91     /**
92      * @see IMarker#getAttribute(String)
93      */

94     public Object JavaDoc getAttribute(String JavaDoc attributeName) throws CoreException {
95         Assert.isNotNull(attributeName);
96         MarkerInfo info = getInfo();
97         checkInfo(info);
98         return info.getAttribute(attributeName);
99     }
100
101     /**
102      * @see IMarker#getAttribute(String, int)
103      */

104     public int getAttribute(String JavaDoc attributeName, int defaultValue) {
105         Assert.isNotNull(attributeName);
106         MarkerInfo info = getInfo();
107         if (info == null)
108             return defaultValue;
109         Object JavaDoc value = info.getAttribute(attributeName);
110         if (value instanceof Integer JavaDoc)
111             return ((Integer JavaDoc) value).intValue();
112         return defaultValue;
113     }
114
115     /**
116      * @see IMarker#getAttribute(String, String)
117      */

118     public String JavaDoc getAttribute(String JavaDoc attributeName, String JavaDoc defaultValue) {
119         Assert.isNotNull(attributeName);
120         MarkerInfo info = getInfo();
121         if (info == null)
122             return defaultValue;
123         Object JavaDoc value = info.getAttribute(attributeName);
124         if (value instanceof String JavaDoc)
125             return (String JavaDoc) value;
126         return defaultValue;
127     }
128
129     /**
130      * @see IMarker#getAttribute(String, boolean)
131      */

132     public boolean getAttribute(String JavaDoc attributeName, boolean defaultValue) {
133         Assert.isNotNull(attributeName);
134         MarkerInfo info = getInfo();
135         if (info == null)
136             return defaultValue;
137         Object JavaDoc value = info.getAttribute(attributeName);
138         if (value instanceof Boolean JavaDoc)
139             return ((Boolean JavaDoc) value).booleanValue();
140         return defaultValue;
141     }
142
143     /**
144      * @see IMarker#getAttributes()
145      */

146     public Map JavaDoc getAttributes() throws CoreException {
147         MarkerInfo info = getInfo();
148         checkInfo(info);
149         return info.getAttributes();
150     }
151
152     /**
153      * @see IMarker#getAttributes(String[])
154      */

155     public Object JavaDoc[] getAttributes(String JavaDoc[] attributeNames) throws CoreException {
156         Assert.isNotNull(attributeNames);
157         MarkerInfo info = getInfo();
158         checkInfo(info);
159         return info.getAttributes(attributeNames);
160     }
161
162     /**
163      * @see IMarker#getCreationTime()
164      */

165     public long getCreationTime() throws CoreException {
166         MarkerInfo info = getInfo();
167         checkInfo(info);
168         return info.getCreationTime();
169     }
170
171     /**
172      * @see IMarker#getId()
173      */

174     public long getId() {
175         return id;
176     }
177
178     protected MarkerInfo getInfo() {
179         return getWorkspace().getMarkerManager().findMarkerInfo(resource, id);
180     }
181
182     /**
183      * @see IMarker#getResource()
184      */

185     public IResource getResource() {
186         return resource;
187     }
188
189     /**
190      * @see IMarker#getType()
191      */

192     public String JavaDoc getType() throws CoreException {
193         MarkerInfo info = getInfo();
194         checkInfo(info);
195         return info.getType();
196     }
197
198     /**
199      * Returns the workspace which manages this marker. Returns
200      * <code>null</code> if this resource does not have an associated
201      * resource.
202      */

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     /**
212      * @see IMarker#isSubtypeOf(String)
213      */

214     public boolean isSubtypeOf(String JavaDoc type) throws CoreException {
215         return getWorkspace().getMarkerManager().isSubtype(getType(), type);
216     }
217
218     /**
219      * @see IMarker#setAttribute(String, int)
220      */

221     public void setAttribute(String JavaDoc attributeName, int value) throws CoreException {
222         setAttribute(attributeName, new Integer JavaDoc(value));
223     }
224
225     /**
226      * @see IMarker#setAttribute(String, Object)
227      */

228     public void setAttribute(String JavaDoc attributeName, Object JavaDoc 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             //only need to generate delta info if none already
239
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     /**
254      * @see IMarker#setAttribute(String, boolean)
255      */

256     public void setAttribute(String JavaDoc attributeName, boolean value) throws CoreException {
257         setAttribute(attributeName, value ? Boolean.TRUE : Boolean.FALSE);
258     }
259
260     /**
261      * @see IMarker#setAttributes(String[], Object[])
262      */

263     public void setAttributes(String JavaDoc[] attributeNames, Object JavaDoc[] 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             //only need to generate delta info if none already
275
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     /**
290      * @see IMarker#setAttributes(Map)
291      */

292     public void setAttributes(Map JavaDoc 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             //only need to generate delta info if none already
302
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