129
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.source.AnnotationModelEvent;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 import java.util.HashMap;
|
|
19 import java.util.HashSet;
|
|
20 import java.util.Map;
|
|
21 import java.util.Set;
|
|
22
|
|
23 import dwtx.jface.text.Position;
|
|
24
|
|
25
|
|
26 /**
|
|
27 * Specification of changes applied to annotation models. The event carries the
|
|
28 * changed annotation model as well as added, removed, and modified annotations.
|
|
29 * <p>
|
|
30 * An event can be sealed. Afterwards it can not be modified. Thus, the normal
|
|
31 * process is that an empty event is created, filled with the changed
|
|
32 * information, and before it is sent to the listeners, the event is sealed.
|
|
33 *
|
|
34 * @see dwtx.jface.text.source.IAnnotationModel
|
|
35 * @see dwtx.jface.text.source.IAnnotationModelListenerExtension
|
|
36 * @since 2.0
|
|
37 */
|
|
38 public class AnnotationModelEvent {
|
|
39
|
|
40 /** The model this event refers to. */
|
|
41 private IAnnotationModel fAnnotationModel;
|
|
42 /**
|
|
43 * The added annotations.
|
|
44 * @since 3.0
|
|
45 */
|
|
46 private Set fAddedAnnotations= new HashSet();
|
|
47 /**
|
|
48 * The removed annotations.
|
|
49 * @since 3.0
|
|
50 */
|
|
51 private Map fRemovedAnnotations= new HashMap();
|
|
52 /**
|
|
53 * The changed annotations.
|
|
54 * @since 3.0
|
|
55 */
|
|
56 private Set fChangedAnnotations= new HashSet();
|
|
57 /**
|
|
58 * Indicates that this event does not contain detailed information.
|
|
59 * @since 3.0
|
|
60 */
|
|
61 private bool fIsWorldChange;
|
|
62 /**
|
|
63 * The modification stamp.
|
|
64 * @since 3.0
|
|
65 */
|
|
66 private Object fModificationStamp;
|
|
67
|
|
68 /**
|
|
69 * Creates a new annotation model event for the given model.
|
|
70 *
|
|
71 * @param model the model
|
|
72 */
|
|
73 public AnnotationModelEvent(IAnnotationModel model) {
|
|
74 this(model, true);
|
|
75 }
|
|
76
|
|
77 /**
|
|
78 * Creates a new annotation model event for the given model.
|
|
79 *
|
|
80 * @param model the model
|
|
81 * @param isWorldChange <code>true</code> if world change
|
|
82 * @since 3.0
|
|
83 */
|
|
84 public AnnotationModelEvent(IAnnotationModel model, bool isWorldChange) {
|
|
85 fAnnotationModel= model;
|
|
86 fIsWorldChange= isWorldChange;
|
|
87 }
|
|
88
|
|
89 /**
|
|
90 * Returns the model this event refers to.
|
|
91 *
|
|
92 * @return the model this events belongs to
|
|
93 */
|
|
94 public IAnnotationModel getAnnotationModel() {
|
|
95 return fAnnotationModel;
|
|
96 }
|
|
97
|
|
98 /**
|
|
99 * Adds the given annotation to the set of annotations that are reported as
|
|
100 * being added from the model. If this event is considered a world change,
|
|
101 * it is no longer so after this method has successfully finished.
|
|
102 *
|
|
103 * @param annotation the added annotation
|
|
104 * @since 3.0
|
|
105 */
|
|
106 public void annotationAdded(Annotation annotation) {
|
|
107 fAddedAnnotations.add(annotation);
|
|
108 fIsWorldChange= false;
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * Returns the added annotations.
|
|
113 *
|
|
114 * @return the added annotations
|
|
115 * @since 3.0
|
|
116 */
|
|
117 public Annotation[] getAddedAnnotations() {
|
|
118 int size= fAddedAnnotations.size();
|
|
119 Annotation[] added= new Annotation[size];
|
|
120 fAddedAnnotations.toArray(added);
|
|
121 return added;
|
|
122 }
|
|
123
|
|
124 /**
|
|
125 * Adds the given annotation to the set of annotations that are reported as
|
|
126 * being removed from the model. If this event is considered a world
|
|
127 * change, it is no longer so after this method has successfully finished.
|
|
128 *
|
|
129 * @param annotation the removed annotation
|
|
130 * @since 3.0
|
|
131 */
|
|
132 public void annotationRemoved(Annotation annotation) {
|
|
133 annotationRemoved(annotation, null);
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Adds the given annotation to the set of annotations that are reported as
|
|
138 * being removed from the model. If this event is considered a world
|
|
139 * change, it is no longer so after this method has successfully finished.
|
|
140 *
|
|
141 * @param annotation the removed annotation
|
|
142 * @param position the position of the removed annotation
|
|
143 * @since 3.0
|
|
144 */
|
|
145 public void annotationRemoved(Annotation annotation, Position position) {
|
|
146 fRemovedAnnotations.put(annotation, position);
|
|
147 fIsWorldChange= false;
|
|
148 }
|
|
149
|
|
150 /**
|
|
151 * Returns the removed annotations.
|
|
152 *
|
|
153 * @return the removed annotations
|
|
154 * @since 3.0
|
|
155 */
|
|
156 public Annotation[] getRemovedAnnotations() {
|
|
157 int size= fRemovedAnnotations.size();
|
|
158 Annotation[] removed= new Annotation[size];
|
|
159 fRemovedAnnotations.keySet().toArray(removed);
|
|
160 return removed;
|
|
161 }
|
|
162
|
|
163 /**
|
|
164 * Returns the position of the removed annotation at that point in time
|
|
165 * when the annotation has been removed.
|
|
166 *
|
|
167 * @param annotation the removed annotation
|
|
168 * @return the position of the removed annotation or <code>null</code>
|
|
169 * @since 3.0
|
|
170 */
|
|
171 public Position getPositionOfRemovedAnnotation(Annotation annotation) {
|
|
172 return (Position) fRemovedAnnotations.get(annotation);
|
|
173 }
|
|
174
|
|
175 /**
|
|
176 * Adds the given annotation to the set of annotations that are reported as
|
|
177 * being changed from the model. If this event is considered a world
|
|
178 * change, it is no longer so after this method has successfully finished.
|
|
179 *
|
|
180 * @param annotation the changed annotation
|
|
181 * @since 3.0
|
|
182 */
|
|
183 public void annotationChanged(Annotation annotation) {
|
|
184 fChangedAnnotations.add(annotation);
|
|
185 fIsWorldChange= false;
|
|
186 }
|
|
187
|
|
188 /**
|
|
189 * Returns the changed annotations.
|
|
190 *
|
|
191 * @return the changed annotations
|
|
192 * @since 3.0
|
|
193 */
|
|
194 public Annotation[] getChangedAnnotations() {
|
|
195 int size= fChangedAnnotations.size();
|
|
196 Annotation[] changed= new Annotation[size];
|
|
197 fChangedAnnotations.toArray(changed);
|
|
198 return changed;
|
|
199 }
|
|
200
|
|
201 /**
|
|
202 * Returns whether this annotation model event is empty or not. If this
|
|
203 * event represents a world change, this method returns <code>false</code>
|
|
204 * although the event does not carry any added, removed, or changed
|
|
205 * annotations.
|
|
206 *
|
|
207 * @return <code>true</code> if this event is empty
|
|
208 * @since 3.0
|
|
209 */
|
|
210 public bool isEmpty() {
|
|
211 return !fIsWorldChange && fAddedAnnotations.isEmpty() && fRemovedAnnotations.isEmpty() && fChangedAnnotations.isEmpty();
|
|
212 }
|
|
213
|
|
214 /**
|
|
215 * Returns whether this annotation model events contains detailed
|
|
216 * information about the modifications applied to the event annotation
|
|
217 * model or whether it represents a world change. I.e. everything in the
|
|
218 * model might have changed.
|
|
219 *
|
|
220 * @return <code>true</code> if world change, <code>false</code> otherwise
|
|
221 * @since 3.0
|
|
222 */
|
|
223 public bool isWorldChange() {
|
|
224 return fIsWorldChange;
|
|
225 }
|
|
226
|
|
227 /**
|
|
228 * Marks this event as world change according to the given flag.
|
|
229 *
|
|
230 * @param isWorldChange <code>true</code> if this event is a world change, <code>false</code> otherwise
|
|
231 * @since 3.0
|
|
232 */
|
|
233 void markWorldChange(bool isWorldChange) {
|
|
234 fIsWorldChange= isWorldChange;
|
|
235 }
|
|
236
|
|
237 /**
|
|
238 * Returns whether this annotation model event is still valid.
|
|
239 *
|
|
240 * @return <code>true</code> if this event is still valid, <code>false</code> otherwise
|
|
241 * @since 3.0
|
|
242 */
|
|
243 public bool isValid() {
|
|
244 if (fModificationStamp !is null && fAnnotationModel instanceof IAnnotationModelExtension) {
|
|
245 IAnnotationModelExtension extension= (IAnnotationModelExtension) fAnnotationModel;
|
|
246 return fModificationStamp is extension.getModificationStamp();
|
|
247 }
|
|
248 return true;
|
|
249 }
|
|
250
|
|
251 /**
|
|
252 * Seals this event. Any direct modification to the annotation model after the event has been sealed
|
|
253 * invalidates this event.
|
|
254 *
|
|
255 * @since 3.0
|
|
256 */
|
|
257 public void markSealed() {
|
|
258 if (fAnnotationModel instanceof IAnnotationModelExtension) {
|
|
259 IAnnotationModelExtension extension= (IAnnotationModelExtension) fAnnotationModel;
|
|
260 fModificationStamp= extension.getModificationStamp();
|
|
261 }
|
|
262 }
|
|
263 }
|