Revision 3be6ef3e
Added by Niels Hoffmann over 13 years ago
taxeditor-bulkeditor/src/main/java/eu/etaxonomy/taxeditor/annotatedlineeditor/LineAnnotationModel.java | ||
---|---|---|
1 |
// $Id$ |
|
2 |
/** |
|
3 |
* Copyright (C) 2007 EDIT |
|
4 |
* European Distributed Institute of Taxonomy |
|
5 |
* http://www.e-taxonomy.eu |
|
6 |
* |
|
7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
9 |
*/ |
|
10 |
|
|
11 |
package eu.etaxonomy.taxeditor.annotatedlineeditor; |
|
12 |
|
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.Collections; |
|
15 |
import java.util.Comparator; |
|
16 |
import java.util.HashSet; |
|
17 |
import java.util.Iterator; |
|
18 |
import java.util.List; |
|
19 |
import java.util.Set; |
|
20 |
|
|
21 |
import org.apache.log4j.Logger; |
|
22 |
import org.eclipse.jface.text.BadLocationException; |
|
23 |
import org.eclipse.jface.text.IDocument; |
|
24 |
import org.eclipse.jface.text.Position; |
|
25 |
import org.eclipse.jface.text.source.Annotation; |
|
26 |
import org.eclipse.jface.text.source.AnnotationModel; |
|
27 |
import org.eclipse.jface.text.source.AnnotationModelEvent; |
|
28 |
|
|
29 |
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil; |
|
30 |
|
|
31 |
/** |
|
32 |
* An <code>AnnotationModel</code> which holds <code>LineAnnotation</code>'s. |
|
33 |
* <p> |
|
34 |
* Major difference with <code>AnnotationModel</code> is that <code>removeAnnotation(Annotation annotation, |
|
35 |
* boolean fireModelChanged)</code> adds annotations marked for removal to a collection for further |
|
36 |
* processing, i.e. when the document is saved. This collection is accessed via <code>getDeletedAnnotations()</code> |
|
37 |
* and <code>clearDeletedAnnotations()</code>. |
|
38 |
* |
|
39 |
* @author p.ciardelli |
|
40 |
* @created 25.06.2009 |
|
41 |
* @version 1.0 |
|
42 |
*/ |
|
43 |
public class LineAnnotationModel extends AnnotationModel { |
|
44 |
|
|
45 |
private Comparator<Annotation> comparator; |
|
46 |
private IEntityCreator<?> entityCreator; |
|
47 |
private Set<LineAnnotation> deletedAnnotations = new HashSet<LineAnnotation>(); |
|
48 |
|
|
49 |
private ILineDisplayStrategy lineDisplayStrategy; |
|
50 |
|
|
51 |
public LineAnnotationModel(ILineDisplayStrategy lineDisplayStrategy) { |
|
52 |
super(); |
|
53 |
this.lineDisplayStrategy = lineDisplayStrategy; |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* Changes the annotation's type and fires model changed |
|
58 |
* notification. Resulting redraw will cause any change |
|
59 |
* in annotation's decoration to be displayed. |
|
60 |
* |
|
61 |
* @param annotation |
|
62 |
* @param type |
|
63 |
*/ |
|
64 |
public void changeAnnotationType(Annotation annotation, String type) { |
|
65 |
annotation.setType(type); |
|
66 |
fireModelChanged(new AnnotationModelEvent(this)); |
|
67 |
} |
|
68 |
|
|
69 |
/* (non-Javadoc) |
|
70 |
* @see org.eclipse.jface.text.source.AnnotationModel#removeAnnotation(org.eclipse.jface.text.source.Annotation, boolean) |
|
71 |
*/ |
|
72 |
protected void removeAnnotation(Annotation annotation, |
|
73 |
boolean fireModelChanged) { |
|
74 |
// Set data model element for deletion |
|
75 |
annotation.markDeleted(true); |
|
76 |
if (annotation instanceof LineAnnotation) { |
|
77 |
deletedAnnotations.add((LineAnnotation) annotation); |
|
78 |
} |
|
79 |
super.removeAnnotation(annotation, fireModelChanged); |
|
80 |
} |
|
81 |
|
|
82 |
/** |
|
83 |
* @param type |
|
84 |
*/ |
|
85 |
public void removeTypeFromAllAnnotations(String type) { |
|
86 |
Iterator<?> iterator = getAnnotationIterator(); |
|
87 |
while (iterator.hasNext()) { |
|
88 |
Annotation annotation = ((Annotation) iterator.next()); |
|
89 |
if (annotation.getType().equals(type)) { |
|
90 |
annotation.setType(Annotation.TYPE_UNKNOWN); |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* @param type |
|
97 |
* @return |
|
98 |
*/ |
|
99 |
public Set<LineAnnotation> getAllAnnotationsOfType(String type) { |
|
100 |
Set<LineAnnotation> candidates = new HashSet<LineAnnotation>(); |
|
101 |
Iterator<?> iterator = getAnnotationIterator(); |
|
102 |
while (iterator.hasNext()) { |
|
103 |
LineAnnotation annotation = ((LineAnnotation) iterator.next()); |
|
104 |
if (annotation.getType().equals(type)) { |
|
105 |
candidates.add(annotation); |
|
106 |
} |
|
107 |
} |
|
108 |
return candidates; |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* @param type |
|
113 |
* @return |
|
114 |
*/ |
|
115 |
public Annotation getFirstAnnotationOfType(String type) { |
|
116 |
Iterator<?> iterator = getAnnotationIterator(); |
|
117 |
while (iterator.hasNext()) { |
|
118 |
Annotation annotation = ((Annotation) iterator.next()); |
|
119 |
if (annotation.getType().equals(type)) { |
|
120 |
return annotation; |
|
121 |
} |
|
122 |
} |
|
123 |
return null; |
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* Gets first LineAnnotation in line |
|
128 |
* |
|
129 |
* @param line |
|
130 |
* @param document |
|
131 |
* @param model |
|
132 |
* @return |
|
133 |
*/ |
|
134 |
public Annotation getAnnotationAtLine(int line, IDocument document) { |
|
135 |
// Annotation annotation = null; |
|
136 |
try { |
|
137 |
int offset = document.getLineOffset(line); |
|
138 |
int length = document.getLineLength(line); |
|
139 |
|
|
140 |
Iterator<?> iterator = getAnnotationIterator(offset, length, true, true); |
|
141 |
while (iterator.hasNext()) { |
|
142 |
Object next = iterator.next(); |
|
143 |
if (next instanceof LineAnnotation) { |
|
144 |
return (LineAnnotation) next; |
|
145 |
} |
|
146 |
// annotation = (Annotation) iterator.next(); |
|
147 |
} |
|
148 |
} catch (BadLocationException e) { |
|
149 |
// do nothing |
|
150 |
} |
|
151 |
return null; |
|
152 |
} |
|
153 |
|
|
154 |
/** |
|
155 |
* @return |
|
156 |
*/ |
|
157 |
public List<Annotation> getOrderedAnnotations() { |
|
158 |
List<Annotation> list = new ArrayList<Annotation>(); |
|
159 |
Iterator<?> iterator = getAnnotationIterator(); |
|
160 |
while (iterator.hasNext()) { |
|
161 |
list.add((Annotation) iterator.next()); |
|
162 |
} |
|
163 |
Collections.sort(list, getAnnotationComparator()); |
|
164 |
return list; |
|
165 |
} |
|
166 |
|
|
167 |
public void printAnnotations() { |
|
168 |
Logger logger = BulkEditorUtil.getLog4JLogger(getClass()); |
|
169 |
logger.debug("------------------------"); |
|
170 |
logger.debug("Active annotations"); |
|
171 |
logger.debug("------------------------"); |
|
172 |
List<Annotation> list = getOrderedAnnotations(); |
|
173 |
|
|
174 |
for (Annotation annotation : list) { |
|
175 |
logger.debug( |
|
176 |
(annotation.isMarkedDeleted() ? "DELETED " : "") + |
|
177 |
(((LineAnnotation) annotation).isMarkedAsMerged() ? "MERGED " : "") + |
|
178 |
(((LineAnnotation) annotation).isDirty() ? "DIRTY " : "") + |
|
179 |
(((LineAnnotation) annotation).isMarkedAsNew() ? "NEW " : "") + |
|
180 |
annotation.getText() + ": o " + |
|
181 |
getPosition(annotation).getOffset() + ", l " + |
|
182 |
getPosition(annotation).getLength()); |
|
183 |
} |
|
184 |
logger.debug("------------------------"); |
|
185 |
logger.debug("Deleted annotations"); |
|
186 |
logger.debug("------------------------"); |
|
187 |
for (LineAnnotation annotation : deletedAnnotations) { |
|
188 |
logger.debug( |
|
189 |
(annotation.isMarkedDeleted() ? "DELETED " : "") + |
|
190 |
(((LineAnnotation) annotation).isMarkedAsMerged() ? "MERGED " : "") + |
|
191 |
(((LineAnnotation) annotation).isDirty() ? "DIRTY " : "") + |
|
192 |
(((LineAnnotation) annotation).isMarkedAsNew() ? "NEW " : "") + |
|
193 |
annotation.getText() + ": o "); |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
/** |
|
198 |
* @param offset |
|
199 |
* @param length |
|
200 |
* @return |
|
201 |
*/ |
|
202 |
public List<LineAnnotation> getUndeletedAnnotations(int offset, int length) { |
|
203 |
|
|
204 |
List<LineAnnotation> list = new ArrayList<LineAnnotation>(); |
|
205 |
|
|
206 |
Iterator<?> iterator = getAnnotationIterator(offset, length, true, true); |
|
207 |
while (iterator.hasNext()) { |
|
208 |
Object next = iterator.next(); |
|
209 |
if (next instanceof LineAnnotation && !((Annotation) next).isMarkedDeleted()) { |
|
210 |
list.add((LineAnnotation) next); |
|
211 |
} |
|
212 |
} |
|
213 |
Collections.sort(list, getAnnotationComparator()); |
|
214 |
return list; |
|
215 |
} |
|
216 |
|
|
217 |
public Set<LineAnnotation> getDeletedAnnotations() { |
|
218 |
return deletedAnnotations; |
|
219 |
} |
|
220 |
|
|
221 |
public void clearDeletedAnnotations() { |
|
222 |
deletedAnnotations.clear(); |
|
223 |
} |
|
224 |
|
|
225 |
private Comparator<Annotation> getAnnotationComparator() { |
|
226 |
if (comparator == null) { |
|
227 |
this.comparator = new AnnotationComparator(); |
|
228 |
} |
|
229 |
return comparator; |
|
230 |
} |
|
231 |
|
|
232 |
class AnnotationComparator implements Comparator<Annotation> { |
|
233 |
|
|
234 |
/* (non-Javadoc) |
|
235 |
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
|
236 |
*/ |
|
237 |
public int compare(Annotation annotation1, Annotation annotation2) { |
|
238 |
if (annotation1.isMarkedDeleted() && !annotation2.isMarkedDeleted()) { |
|
239 |
return 1; |
|
240 |
} |
|
241 |
if (!annotation1.isMarkedDeleted() && annotation2.isMarkedDeleted()) { |
|
242 |
return -1; |
|
243 |
} |
|
244 |
int offset1 = LineAnnotationModel.this. |
|
245 |
getPosition(annotation1).getOffset(); |
|
246 |
int offset2 = LineAnnotationModel.this. |
|
247 |
getPosition(annotation2).getOffset(); |
|
248 |
if (offset1 > offset2) { |
|
249 |
return 1; |
|
250 |
} else if (offset1 < offset2) { |
|
251 |
return -1; |
|
252 |
} else { |
|
253 |
return 0; |
|
254 |
} |
|
255 |
} |
|
256 |
} |
|
257 |
|
|
258 |
/** |
|
259 |
* The annotation model is assigned responsibility for creating new containers |
|
260 |
* from blocks of text. |
|
261 |
* |
|
262 |
* @param entityCreator |
|
263 |
*/ |
|
264 |
public void setEntityCreator(IEntityCreator<?> entityCreator) { |
|
265 |
this.entityCreator = entityCreator; |
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Creates an annotation without adding it to the model. |
|
270 |
* |
|
271 |
* This object must have its <code>cdmEntityCreator</code> set. |
|
272 |
* |
|
273 |
* @param text |
|
274 |
* @return |
|
275 |
*/ |
|
276 |
public Annotation createAnnotation(String text) { |
|
277 |
LineAnnotation annotation = null; |
|
278 |
if (entityCreator != null) { |
|
279 |
annotation = new LineAnnotation(entityCreator.createEntity(text), lineDisplayStrategy); |
|
280 |
annotation.markAsNew(true); |
|
281 |
} |
|
282 |
return annotation; |
|
283 |
} |
|
284 |
|
|
285 |
/** |
|
286 |
* Creates an annotation without adding it to the model. |
|
287 |
* |
|
288 |
* @param text |
|
289 |
* @return |
|
290 |
*/ |
|
291 |
public Annotation createAnnotation(Object entity) { |
|
292 |
LineAnnotation annotation = new LineAnnotation(entity, lineDisplayStrategy); |
|
293 |
annotation.markAsNew(true); |
|
294 |
return annotation; |
|
295 |
} |
|
296 |
|
|
297 |
/* (non-Javadoc) |
|
298 |
* @see org.eclipse.jface.text.source.AnnotationModel#addAnnotation(org.eclipse.jface.text.source.Annotation, org.eclipse.jface.text.Position) |
|
299 |
*/ |
|
300 |
@Override |
|
301 |
public void addAnnotation(Annotation annotation, Position position) { |
|
302 |
super.addAnnotation(annotation, position); |
|
303 |
} |
|
304 |
|
|
305 |
public Annotation getAnnotation(Object entity) { |
|
306 |
if (entity != null) { |
|
307 |
Iterator iterator = getAnnotationIterator(); |
|
308 |
while (iterator.hasNext()) { |
|
309 |
LineAnnotation annotation = (LineAnnotation) iterator.next(); |
|
310 |
if (entity.equals(annotation.getEntity())) { |
|
311 |
return annotation; |
|
312 |
} |
|
313 |
} |
|
314 |
} |
|
315 |
return null; |
|
316 |
} |
|
317 |
} |
|
1 |
// $Id$ |
|
2 |
/** |
|
3 |
* Copyright (C) 2007 EDIT |
|
4 |
* European Distributed Institute of Taxonomy |
|
5 |
* http://www.e-taxonomy.eu |
|
6 |
* |
|
7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
9 |
*/ |
|
10 |
|
|
11 |
package eu.etaxonomy.taxeditor.annotatedlineeditor; |
|
12 |
|
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.Collections; |
|
15 |
import java.util.Comparator; |
|
16 |
import java.util.HashSet; |
|
17 |
import java.util.Iterator; |
|
18 |
import java.util.List; |
|
19 |
import java.util.Set; |
|
20 |
|
|
21 |
import org.apache.log4j.Logger; |
|
22 |
import org.eclipse.jface.text.BadLocationException; |
|
23 |
import org.eclipse.jface.text.IDocument; |
|
24 |
import org.eclipse.jface.text.Position; |
|
25 |
import org.eclipse.jface.text.source.Annotation; |
|
26 |
import org.eclipse.jface.text.source.AnnotationModel; |
|
27 |
import org.eclipse.jface.text.source.AnnotationModelEvent; |
|
28 |
|
|
29 |
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil; |
|
30 |
|
|
31 |
/** |
|
32 |
* An <code>AnnotationModel</code> which holds <code>LineAnnotation</code>'s. |
|
33 |
* <p> |
|
34 |
* Major difference with <code>AnnotationModel</code> is that <code>removeAnnotation(Annotation annotation, |
|
35 |
* boolean fireModelChanged)</code> adds annotations marked for removal to a collection for further |
|
36 |
* processing, i.e. when the document is saved. This collection is accessed via <code>getDeletedAnnotations()</code> |
|
37 |
* and <code>clearDeletedAnnotations()</code>. |
|
38 |
* |
|
39 |
* @author p.ciardelli |
|
40 |
* @created 25.06.2009 |
|
41 |
* @version 1.0 |
|
42 |
*/ |
|
43 |
public class LineAnnotationModel extends AnnotationModel { |
|
44 |
|
|
45 |
private Comparator<Annotation> comparator; |
|
46 |
private IEntityCreator<?> entityCreator; |
|
47 |
private Set<LineAnnotation> deletedAnnotations = new HashSet<LineAnnotation>(); |
|
48 |
|
|
49 |
private ILineDisplayStrategy lineDisplayStrategy; |
|
50 |
|
|
51 |
/** |
|
52 |
* <p>Constructor for LineAnnotationModel.</p> |
|
53 |
* |
|
54 |
* @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object. |
|
55 |
*/ |
|
56 |
public LineAnnotationModel(ILineDisplayStrategy lineDisplayStrategy) { |
|
57 |
super(); |
|
58 |
this.lineDisplayStrategy = lineDisplayStrategy; |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Changes the annotation's type and fires model changed |
|
63 |
* notification. Resulting redraw will cause any change |
|
64 |
* in annotation's decoration to be displayed. |
|
65 |
* |
|
66 |
* @param annotation a {@link org.eclipse.jface.text.source.Annotation} object. |
|
67 |
* @param type a {@link java.lang.String} object. |
|
68 |
*/ |
|
69 |
public void changeAnnotationType(Annotation annotation, String type) { |
|
70 |
annotation.setType(type); |
|
71 |
fireModelChanged(new AnnotationModelEvent(this)); |
|
72 |
} |
|
73 |
|
|
74 |
/* (non-Javadoc) |
|
75 |
* @see org.eclipse.jface.text.source.AnnotationModel#removeAnnotation(org.eclipse.jface.text.source.Annotation, boolean) |
|
76 |
*/ |
|
77 |
/** {@inheritDoc} */ |
|
78 |
protected void removeAnnotation(Annotation annotation, |
|
79 |
boolean fireModelChanged) { |
|
80 |
// Set data model element for deletion |
|
81 |
annotation.markDeleted(true); |
|
82 |
if (annotation instanceof LineAnnotation) { |
|
83 |
deletedAnnotations.add((LineAnnotation) annotation); |
|
84 |
} |
|
85 |
super.removeAnnotation(annotation, fireModelChanged); |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* <p>removeTypeFromAllAnnotations</p> |
|
90 |
* |
|
91 |
* @param type a {@link java.lang.String} object. |
|
92 |
*/ |
|
93 |
public void removeTypeFromAllAnnotations(String type) { |
|
94 |
Iterator<?> iterator = getAnnotationIterator(); |
|
95 |
while (iterator.hasNext()) { |
|
96 |
Annotation annotation = ((Annotation) iterator.next()); |
|
97 |
if (annotation.getType().equals(type)) { |
|
98 |
annotation.setType(Annotation.TYPE_UNKNOWN); |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
/** |
|
104 |
* <p>getAllAnnotationsOfType</p> |
|
105 |
* |
|
106 |
* @param type a {@link java.lang.String} object. |
|
107 |
* @return a {@link java.util.Set} object. |
|
108 |
*/ |
|
109 |
public Set<LineAnnotation> getAllAnnotationsOfType(String type) { |
|
110 |
Set<LineAnnotation> candidates = new HashSet<LineAnnotation>(); |
|
111 |
Iterator<?> iterator = getAnnotationIterator(); |
|
112 |
while (iterator.hasNext()) { |
|
113 |
LineAnnotation annotation = ((LineAnnotation) iterator.next()); |
|
114 |
if (annotation.getType().equals(type)) { |
|
115 |
candidates.add(annotation); |
|
116 |
} |
|
117 |
} |
|
118 |
return candidates; |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* <p>getFirstAnnotationOfType</p> |
|
123 |
* |
|
124 |
* @param type a {@link java.lang.String} object. |
|
125 |
* @return a {@link org.eclipse.jface.text.source.Annotation} object. |
|
126 |
*/ |
|
127 |
public Annotation getFirstAnnotationOfType(String type) { |
|
128 |
Iterator<?> iterator = getAnnotationIterator(); |
|
129 |
while (iterator.hasNext()) { |
|
130 |
Annotation annotation = ((Annotation) iterator.next()); |
|
131 |
if (annotation.getType().equals(type)) { |
|
132 |
return annotation; |
|
133 |
} |
|
134 |
} |
|
135 |
return null; |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* Gets first LineAnnotation in line |
|
140 |
* |
|
141 |
* @param line a int. |
|
142 |
* @param document a {@link org.eclipse.jface.text.IDocument} object. |
|
143 |
* @return a {@link org.eclipse.jface.text.source.Annotation} object. |
|
144 |
*/ |
|
145 |
public Annotation getAnnotationAtLine(int line, IDocument document) { |
|
146 |
// Annotation annotation = null; |
|
147 |
try { |
|
148 |
int offset = document.getLineOffset(line); |
|
149 |
int length = document.getLineLength(line); |
|
150 |
|
|
151 |
Iterator<?> iterator = getAnnotationIterator(offset, length, true, true); |
|
152 |
while (iterator.hasNext()) { |
|
153 |
Object next = iterator.next(); |
|
154 |
if (next instanceof LineAnnotation) { |
|
155 |
return (LineAnnotation) next; |
|
156 |
} |
|
157 |
// annotation = (Annotation) iterator.next(); |
|
158 |
} |
|
159 |
} catch (BadLocationException e) { |
|
160 |
// do nothing |
|
161 |
} |
|
162 |
return null; |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
* <p>getOrderedAnnotations</p> |
|
167 |
* |
|
168 |
* @return a {@link java.util.List} object. |
|
169 |
*/ |
|
170 |
public List<Annotation> getOrderedAnnotations() { |
|
171 |
List<Annotation> list = new ArrayList<Annotation>(); |
|
172 |
Iterator<?> iterator = getAnnotationIterator(); |
|
173 |
while (iterator.hasNext()) { |
|
174 |
list.add((Annotation) iterator.next()); |
|
175 |
} |
|
176 |
Collections.sort(list, getAnnotationComparator()); |
|
177 |
return list; |
|
178 |
} |
|
179 |
|
|
180 |
/** |
|
181 |
* <p>printAnnotations</p> |
|
182 |
*/ |
|
183 |
public void printAnnotations() { |
|
184 |
Logger logger = BulkEditorUtil.getLog4JLogger(getClass()); |
|
185 |
logger.debug("------------------------"); |
|
186 |
logger.debug("Active annotations"); |
|
187 |
logger.debug("------------------------"); |
|
188 |
List<Annotation> list = getOrderedAnnotations(); |
|
189 |
|
|
190 |
for (Annotation annotation : list) { |
|
191 |
logger.debug( |
|
192 |
(annotation.isMarkedDeleted() ? "DELETED " : "") + |
|
193 |
(((LineAnnotation) annotation).isMarkedAsMerged() ? "MERGED " : "") + |
|
194 |
(((LineAnnotation) annotation).isDirty() ? "DIRTY " : "") + |
|
195 |
(((LineAnnotation) annotation).isMarkedAsNew() ? "NEW " : "") + |
|
196 |
annotation.getText() + ": o " + |
|
197 |
getPosition(annotation).getOffset() + ", l " + |
|
198 |
getPosition(annotation).getLength()); |
|
199 |
} |
|
200 |
logger.debug("------------------------"); |
|
201 |
logger.debug("Deleted annotations"); |
|
202 |
logger.debug("------------------------"); |
|
203 |
for (LineAnnotation annotation : deletedAnnotations) { |
|
204 |
logger.debug( |
|
205 |
(annotation.isMarkedDeleted() ? "DELETED " : "") + |
|
206 |
(((LineAnnotation) annotation).isMarkedAsMerged() ? "MERGED " : "") + |
|
207 |
(((LineAnnotation) annotation).isDirty() ? "DIRTY " : "") + |
|
208 |
(((LineAnnotation) annotation).isMarkedAsNew() ? "NEW " : "") + |
|
209 |
annotation.getText() + ": o "); |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
214 |
* <p>getUndeletedAnnotations</p> |
|
215 |
* |
|
216 |
* @param offset a int. |
|
217 |
* @param length a int. |
|
218 |
* @return a {@link java.util.List} object. |
|
219 |
*/ |
|
220 |
public List<LineAnnotation> getUndeletedAnnotations(int offset, int length) { |
|
221 |
|
|
222 |
List<LineAnnotation> list = new ArrayList<LineAnnotation>(); |
|
223 |
|
|
224 |
Iterator<?> iterator = getAnnotationIterator(offset, length, true, true); |
|
225 |
while (iterator.hasNext()) { |
|
226 |
Object next = iterator.next(); |
|
227 |
if (next instanceof LineAnnotation && !((Annotation) next).isMarkedDeleted()) { |
|
228 |
list.add((LineAnnotation) next); |
|
229 |
} |
|
230 |
} |
|
231 |
Collections.sort(list, getAnnotationComparator()); |
|
232 |
return list; |
|
233 |
} |
|
234 |
|
|
235 |
/** |
|
236 |
* <p>Getter for the field <code>deletedAnnotations</code>.</p> |
|
237 |
* |
|
238 |
* @return a {@link java.util.Set} object. |
|
239 |
*/ |
|
240 |
public Set<LineAnnotation> getDeletedAnnotations() { |
|
241 |
return deletedAnnotations; |
|
242 |
} |
|
243 |
|
|
244 |
/** |
|
245 |
* <p>clearDeletedAnnotations</p> |
|
246 |
*/ |
|
247 |
public void clearDeletedAnnotations() { |
|
248 |
deletedAnnotations.clear(); |
|
249 |
} |
|
250 |
|
|
251 |
private Comparator<Annotation> getAnnotationComparator() { |
|
252 |
if (comparator == null) { |
|
253 |
this.comparator = new AnnotationComparator(); |
|
254 |
} |
|
255 |
return comparator; |
|
256 |
} |
|
257 |
|
|
258 |
class AnnotationComparator implements Comparator<Annotation> { |
|
259 |
|
|
260 |
/* (non-Javadoc) |
|
261 |
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
|
262 |
*/ |
|
263 |
public int compare(Annotation annotation1, Annotation annotation2) { |
|
264 |
if (annotation1.isMarkedDeleted() && !annotation2.isMarkedDeleted()) { |
|
265 |
return 1; |
|
266 |
} |
|
267 |
if (!annotation1.isMarkedDeleted() && annotation2.isMarkedDeleted()) { |
|
268 |
return -1; |
|
269 |
} |
|
270 |
int offset1 = LineAnnotationModel.this. |
|
271 |
getPosition(annotation1).getOffset(); |
|
272 |
int offset2 = LineAnnotationModel.this. |
|
273 |
getPosition(annotation2).getOffset(); |
|
274 |
if (offset1 > offset2) { |
|
275 |
return 1; |
|
276 |
} else if (offset1 < offset2) { |
|
277 |
return -1; |
|
278 |
} else { |
|
279 |
return 0; |
|
280 |
} |
|
281 |
} |
|
282 |
} |
|
283 |
|
|
284 |
/** |
|
285 |
* The annotation model is assigned responsibility for creating new containers |
|
286 |
* from blocks of text. |
|
287 |
* |
|
288 |
* @param entityCreator a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator} object. |
|
289 |
*/ |
|
290 |
public void setEntityCreator(IEntityCreator<?> entityCreator) { |
|
291 |
this.entityCreator = entityCreator; |
|
292 |
} |
|
293 |
|
|
294 |
/** |
|
295 |
* Creates an annotation without adding it to the model. |
|
296 |
* |
|
297 |
* This object must have its <code>cdmEntityCreator</code> set. |
|
298 |
* |
|
299 |
* @param text a {@link java.lang.String} object. |
|
300 |
* @return a {@link org.eclipse.jface.text.source.Annotation} object. |
|
301 |
*/ |
|
302 |
public Annotation createAnnotation(String text) { |
|
303 |
LineAnnotation annotation = null; |
|
304 |
if (entityCreator != null) { |
|
305 |
annotation = new LineAnnotation(entityCreator.createEntity(text), lineDisplayStrategy); |
|
306 |
annotation.markAsNew(true); |
|
307 |
} |
|
308 |
return annotation; |
|
309 |
} |
|
310 |
|
|
311 |
/** |
|
312 |
* Creates an annotation without adding it to the model. |
|
313 |
* |
|
314 |
* @param entity a {@link java.lang.Object} object. |
|
315 |
* @return a {@link org.eclipse.jface.text.source.Annotation} object. |
|
316 |
*/ |
|
317 |
public Annotation createAnnotation(Object entity) { |
|
318 |
LineAnnotation annotation = new LineAnnotation(entity, lineDisplayStrategy); |
|
319 |
annotation.markAsNew(true); |
|
320 |
return annotation; |
|
321 |
} |
|
322 |
|
|
323 |
/* (non-Javadoc) |
|
324 |
* @see org.eclipse.jface.text.source.AnnotationModel#addAnnotation(org.eclipse.jface.text.source.Annotation, org.eclipse.jface.text.Position) |
|
325 |
*/ |
|
326 |
/** {@inheritDoc} */ |
|
327 |
@Override |
|
328 |
public void addAnnotation(Annotation annotation, Position position) { |
|
329 |
super.addAnnotation(annotation, position); |
|
330 |
} |
|
331 |
|
|
332 |
/** |
|
333 |
* <p>getAnnotation</p> |
|
334 |
* |
|
335 |
* @param entity a {@link java.lang.Object} object. |
|
336 |
* @return a {@link org.eclipse.jface.text.source.Annotation} object. |
|
337 |
*/ |
|
338 |
public Annotation getAnnotation(Object entity) { |
|
339 |
if (entity != null) { |
|
340 |
Iterator iterator = getAnnotationIterator(); |
|
341 |
while (iterator.hasNext()) { |
|
342 |
LineAnnotation annotation = (LineAnnotation) iterator.next(); |
|
343 |
if (entity.equals(annotation.getEntity())) { |
|
344 |
return annotation; |
|
345 |
} |
|
346 |
} |
|
347 |
} |
|
348 |
return null; |
|
349 |
} |
|
350 |
} |
Also available in: Unified diff
performed javacscript:fix and worked on documentation