KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.IPath;
17
18 /**
19  * @see IMarkerDelta
20  */

21 public class MarkerDelta implements IMarkerDelta, IMarkerSetElement {
22     protected int kind;
23     protected IResource resource;
24     protected MarkerInfo info;
25
26     /**
27      * Creates a new marker delta.
28      */

29     public MarkerDelta(int kind, IResource resource, MarkerInfo info) {
30         this.kind = kind;
31         this.resource = resource;
32         this.info = info;
33     }
34
35     /* (non-Javadoc)
36      * @see IMarkerDelta#getAttribute(String)
37      */

38     public Object JavaDoc getAttribute(String JavaDoc attributeName) {
39         return info.getAttribute(attributeName);
40     }
41
42     /* (non-Javadoc)
43      * @see IMarkerDelta#getAttribute(String, int)
44      */

45     public int getAttribute(String JavaDoc attributeName, int defaultValue) {
46         Object JavaDoc value = info.getAttribute(attributeName);
47         if (value instanceof Integer JavaDoc)
48             return ((Integer JavaDoc) value).intValue();
49         return defaultValue;
50     }
51
52     /* (non-Javadoc)
53      * @see IMarkerDelta#getAttribute(String, String)
54      */

55     public String JavaDoc getAttribute(String JavaDoc attributeName, String JavaDoc defaultValue) {
56         Object JavaDoc value = info.getAttribute(attributeName);
57         if (value instanceof String JavaDoc)
58             return (String JavaDoc) value;
59         return defaultValue;
60     }
61
62     /* (non-Javadoc)
63      * @see IMarkerDelta#getAttribute(String, boolean)
64      */

65     public boolean getAttribute(String JavaDoc attributeName, boolean defaultValue) {
66         Object JavaDoc value = info.getAttribute(attributeName);
67         if (value instanceof Boolean JavaDoc)
68             return ((Boolean JavaDoc) value).booleanValue();
69         return defaultValue;
70     }
71
72     /* (non-Javadoc)
73      * @see IMarkerDelta#getAttributes()
74      */

75     public Map JavaDoc getAttributes() {
76         return info.getAttributes();
77     }
78
79     /* (non-Javadoc)
80      * @see IMarkerDelta#getAttributes(String[])
81      */

82     public Object JavaDoc[] getAttributes(String JavaDoc[] attributeNames) {
83         return info.getAttributes(attributeNames);
84     }
85
86     /* (non-Javadoc)
87      * @see IMarkerDelta#getId()
88      */

89     public long getId() {
90         return info.getId();
91     }
92
93     /* (non-Javadoc)
94      * @see IMarkerDelta#getKind()
95      */

96     public int getKind() {
97         return kind;
98     }
99
100     /* (non-Javadoc)
101      * @see IMarkerDelta#getMarker()
102      */

103     public IMarker getMarker() {
104         return new Marker(resource, getId());
105     }
106
107     /* (non-Javadoc)
108      * @see IMarkerDelta#getResource()
109      */

110     public IResource getResource() {
111         return resource;
112     }
113
114     /* (non-Javadoc)
115      * @see IMarkerDelta#getType()
116      */

117     public String JavaDoc getType() {
118         return info.getType();
119     }
120
121     /* (non-Javadoc)
122      * @see IMarkerDelta#isSubtypeOf(String)
123      */

124     public boolean isSubtypeOf(String JavaDoc superType) {
125         return ((Workspace) getResource().getWorkspace()).getMarkerManager().isSubtype(getType(), superType);
126     }
127
128     /**
129      * Merge two Maps of (IPath->MarkerSet) representing changes. Use the old
130      * map to store the result so we don't have to build a new map to return.
131      */

132     public static Map JavaDoc merge(Map JavaDoc oldChanges, Map JavaDoc newChanges) {
133         if (oldChanges == null)
134             //don't worry about copying since the new changes are no longer used
135
return newChanges;
136         if (newChanges == null)
137             return oldChanges;
138         for (Iterator JavaDoc it = newChanges.keySet().iterator(); it.hasNext();) {
139             IPath key = (IPath) it.next();
140             MarkerSet oldSet = (MarkerSet) oldChanges.get(key);
141             MarkerSet newSet = (MarkerSet) newChanges.get(key);
142             if (oldSet == null)
143                 oldChanges.put(key, newSet);
144             else
145                 merge(oldSet, newSet.elements());
146         }
147         return oldChanges;
148     }
149
150     /**
151      * Merge two sets of marker changes. Both sets must be on the same resource. Use the original set
152      * of changes to store the result so we don't have to build a completely different set to return.
153      *
154      * add + add = N/A
155      * add + remove = nothing (no delta)
156      * add + change = add
157      * remove + add = N/A
158      * remove + remove = N/A
159      * remove + change = N/A
160      * change + add = N/A
161      * change + change = change (note: info held onto by the marker delta should be that of the oldest change, and not replaced when composed)
162      * change + remove = remove (note: info held onto by the marker delta should be that of the oldest change, and not replaced when changed to a remove)
163      */

164     protected static MarkerSet merge(MarkerSet oldChanges, IMarkerSetElement[] newChanges) {
165         if (oldChanges == null) {
166             MarkerSet result = new MarkerSet(newChanges.length);
167             for (int i = 0; i < newChanges.length; i++)
168                 result.add(newChanges[i]);
169             return result;
170         }
171         if (newChanges == null)
172             return oldChanges;
173
174         for (int i = 0; i < newChanges.length; i++) {
175             MarkerDelta newDelta = (MarkerDelta) newChanges[i];
176             MarkerDelta oldDelta = (MarkerDelta) oldChanges.get(newDelta.getId());
177             if (oldDelta == null) {
178                 oldChanges.add(newDelta);
179                 continue;
180             }
181             switch (oldDelta.getKind()) {
182                 case IResourceDelta.ADDED :
183                     switch (newDelta.getKind()) {
184                         case IResourceDelta.ADDED :
185                             // add + add = N/A
186
break;
187                         case IResourceDelta.REMOVED :
188                             // add + remove = nothing
189
// Remove the original ADD delta.
190
oldChanges.remove(oldDelta);
191                             break;
192                         case IResourceDelta.CHANGED :
193                             // add + change = add
194
break;
195                     }
196                     break;
197                 case IResourceDelta.REMOVED :
198                     switch (newDelta.getKind()) {
199                         case IResourceDelta.ADDED :
200                             // remove + add = N/A
201
break;
202                         case IResourceDelta.REMOVED :
203                             // remove + remove = N/A
204
break;
205                         case IResourceDelta.CHANGED :
206                             // remove + change = N/A
207
break;
208                     }
209                     break;
210                 case IResourceDelta.CHANGED :
211                     switch (newDelta.getKind()) {
212                         case IResourceDelta.ADDED :
213                             // change + add = N/A
214
break;
215                         case IResourceDelta.REMOVED :
216                             // change + remove = remove
217
// Change the delta kind.
218
oldDelta.setKind(IResourceDelta.REMOVED);
219                             break;
220                         case IResourceDelta.CHANGED :
221                             // change + change = change
222
break;
223                     }
224                     break;
225             }
226         }
227         return oldChanges;
228     }
229
230     private void setKind(int kind) {
231         this.kind = kind;
232     }
233 }
234
Popular Tags