KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > AnnotationModelEvent


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.jface.text.source;
12
13
14 import org.eclipse.jface.text.Position;
15
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21
22 /**
23  * Specification of changes applied to annotation models. The event carries the
24  * changed annotation model as well as added, removed, and modified annotations.
25  * <p>
26  * An event can be sealed. Afterwards it can not be modified. Thus, the normal
27  * process is that an empty event is created, filled with the changed
28  * information, and before it is sent to the listeners, the event is sealed.
29  *
30  * @see org.eclipse.jface.text.source.IAnnotationModel
31  * @see org.eclipse.jface.text.source.IAnnotationModelListenerExtension
32  * @since 2.0
33  */

34 public class AnnotationModelEvent {
35
36     /** The model this event refers to. */
37     private IAnnotationModel fAnnotationModel;
38     /**
39      * The added annotations.
40      * @since 3.0
41      */

42     private Set JavaDoc fAddedAnnotations= new HashSet JavaDoc();
43     /**
44      * The removed annotations.
45      * @since 3.0
46      */

47     private Map JavaDoc fRemovedAnnotations= new HashMap JavaDoc();
48     /**
49      * The changed annotations.
50      * @since 3.0
51      */

52     private Set JavaDoc fChangedAnnotations= new HashSet JavaDoc();
53     /**
54      * Indicates that this event does not contain detailed information.
55      * @since 3.0
56      */

57     private boolean fIsWorldChange;
58     /**
59      * The modification stamp.
60      * @since 3.0
61      */

62     private Object JavaDoc fModificationStamp;
63
64     /**
65      * Creates a new annotation model event for the given model.
66      *
67      * @param model the model
68      */

69     public AnnotationModelEvent(IAnnotationModel model) {
70         this(model, true);
71     }
72
73     /**
74      * Creates a new annotation model event for the given model.
75      *
76      * @param model the model
77      * @param isWorldChange <code>true</code> if world change
78      * @since 3.0
79      */

80     public AnnotationModelEvent(IAnnotationModel model, boolean isWorldChange) {
81         fAnnotationModel= model;
82         fIsWorldChange= isWorldChange;
83     }
84
85     /**
86      * Returns the model this event refers to.
87      *
88      * @return the model this events belongs to
89      */

90     public IAnnotationModel getAnnotationModel() {
91         return fAnnotationModel;
92     }
93
94     /**
95      * Adds the given annotation to the set of annotations that are reported as
96      * being added from the model. If this event is considered a world change,
97      * it is no longer so after this method has successfully finished.
98      *
99      * @param annotation the added annotation
100      * @since 3.0
101      */

102     public void annotationAdded(Annotation annotation) {
103         fAddedAnnotations.add(annotation);
104         fIsWorldChange= false;
105     }
106
107     /**
108      * Returns the added annotations.
109      *
110      * @return the added annotations
111      * @since 3.0
112      */

113     public Annotation[] getAddedAnnotations() {
114         int size= fAddedAnnotations.size();
115         Annotation[] added= new Annotation[size];
116         fAddedAnnotations.toArray(added);
117         return added;
118     }
119
120     /**
121      * Adds the given annotation to the set of annotations that are reported as
122      * being removed from the model. If this event is considered a world
123      * change, it is no longer so after this method has successfully finished.
124      *
125      * @param annotation the removed annotation
126      * @since 3.0
127      */

128     public void annotationRemoved(Annotation annotation) {
129         annotationRemoved(annotation, null);
130     }
131
132     /**
133      * Adds the given annotation to the set of annotations that are reported as
134      * being removed from the model. If this event is considered a world
135      * change, it is no longer so after this method has successfully finished.
136      *
137      * @param annotation the removed annotation
138      * @param position the position of the removed annotation
139      * @since 3.0
140      */

141     public void annotationRemoved(Annotation annotation, Position position) {
142         fRemovedAnnotations.put(annotation, position);
143         fIsWorldChange= false;
144     }
145
146     /**
147      * Returns the removed annotations.
148      *
149      * @return the removed annotations
150      * @since 3.0
151      */

152     public Annotation[] getRemovedAnnotations() {
153         int size= fRemovedAnnotations.size();
154         Annotation[] removed= new Annotation[size];
155         fRemovedAnnotations.keySet().toArray(removed);
156         return removed;
157     }
158
159     /**
160      * Returns the position of the removed annotation at that point in time
161      * when the annotation has been removed.
162      *
163      * @param annotation the removed annotation
164      * @return the position of the removed annotation or <code>null</code>
165      * @since 3.0
166      */

167     public Position getPositionOfRemovedAnnotation(Annotation annotation) {
168         return (Position) fRemovedAnnotations.get(annotation);
169     }
170
171     /**
172      * Adds the given annotation to the set of annotations that are reported as
173      * being changed from the model. If this event is considered a world
174      * change, it is no longer so after this method has successfully finished.
175      *
176      * @param annotation the changed annotation
177      * @since 3.0
178      */

179     public void annotationChanged(Annotation annotation) {
180         fChangedAnnotations.add(annotation);
181         fIsWorldChange= false;
182     }
183
184     /**
185      * Returns the changed annotations.
186      *
187      * @return the changed annotations
188      * @since 3.0
189      */

190     public Annotation[] getChangedAnnotations() {
191         int size= fChangedAnnotations.size();
192         Annotation[] changed= new Annotation[size];
193         fChangedAnnotations.toArray(changed);
194         return changed;
195     }
196
197     /**
198      * Returns whether this annotation model event is empty or not. If this
199      * event represents a world change, this method returns <code>false</code>
200      * although the event does not carry any added, removed, or changed
201      * annotations.
202      *
203      * @return <code>true</code> if this event is empty
204      * @since 3.0
205      */

206     public boolean isEmpty() {
207         return !fIsWorldChange && fAddedAnnotations.isEmpty() && fRemovedAnnotations.isEmpty() && fChangedAnnotations.isEmpty();
208     }
209
210     /**
211      * Returns whether this annotation model events contains detailed
212      * information about the modifications applied to the event annotation
213      * model or whether it represents a world change. I.e. everything in the
214      * model might have changed.
215      *
216      * @return <code>true</code> if world change, <code>false</code> otherwise
217      * @since 3.0
218      */

219     public boolean isWorldChange() {
220         return fIsWorldChange;
221     }
222
223     /**
224      * Marks this event as world change according to the given flag.
225      *
226      * @param isWorldChange <code>true</code> if this event is a world change, <code>false</code> otherwise
227      * @since 3.0
228      */

229     void markWorldChange(boolean isWorldChange) {
230         fIsWorldChange= isWorldChange;
231     }
232
233     /**
234      * Returns whether this annotation model event is still valid.
235      *
236      * @return <code>true</code> if this event is still valid, <code>false</code> otherwise
237      * @since 3.0
238      */

239     public boolean isValid() {
240         if (fModificationStamp != null && fAnnotationModel instanceof IAnnotationModelExtension) {
241             IAnnotationModelExtension extension= (IAnnotationModelExtension) fAnnotationModel;
242             return fModificationStamp == extension.getModificationStamp();
243         }
244         return true;
245     }
246
247     /**
248      * Seals this event. Any direct modification to the annotation model after the event has been sealed
249      * invalidates this event.
250      *
251      * @since 3.0
252      */

253     public void markSealed() {
254         if (fAnnotationModel instanceof IAnnotationModelExtension) {
255             IAnnotationModelExtension extension= (IAnnotationModelExtension) fAnnotationModel;
256             fModificationStamp= extension.getModificationStamp();
257         }
258     }
259 }
260
Popular Tags