3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.bulkeditor
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.eclipse
.jface
.text
.BadLocationException
;
20 import org
.eclipse
.jface
.text
.IDocument
;
21 import org
.eclipse
.jface
.text
.IRegion
;
22 import org
.eclipse
.jface
.text
.Position
;
23 import org
.eclipse
.jface
.text
.source
.Annotation
;
24 import org
.eclipse
.jface
.text
.source
.IAnnotationModel
;
25 import org
.eclipse
.jface
.text
.source
.IAnnotationModelExtension2
;
26 import org
.eclipse
.ui
.IEditorInput
;
33 public class BulkEditorUtil
{
34 private static final Logger logger
= Logger
35 .getLogger(BulkEditorUtil
.class);
37 public static String
getLineWithoutDelimiters(IDocument document
, int lineno
) throws BadLocationException
{
38 int length
= document
.getLineLength(lineno
);
39 int offset
= document
.getLineOffset(lineno
);
40 int delimiterLength
= document
.getLineDelimiter(lineno
) == null ?
41 0 : document
.getLineDelimiter(lineno
).length();
43 return document
.get(offset
, length
- delimiterLength
);
46 public static void removeMergeTargetAnnotations(IAnnotationModel model
) {
47 Iterator iterator
= model
.getAnnotationIterator();
48 while (iterator
.hasNext()) {
49 Annotation annotation
= ((Annotation
) iterator
.next());
50 if (annotation
.getType().equals(BulkEditorAnnotation
.TYPE_MERGE_TARGET
)) {
51 annotation
.setType(Annotation
.TYPE_UNKNOWN
);
56 public static Set
<Annotation
> getMergeCandidateAnnotations(IAnnotationModel model
) {
57 Set
<Annotation
> candidates
= new HashSet
<Annotation
>();
58 Iterator iterator
= model
.getAnnotationIterator();
59 while (iterator
.hasNext()) {
60 Annotation annotation
= ((Annotation
) iterator
.next());
61 if (annotation
.getType().equals(BulkEditorAnnotation
.TYPE_MERGE_CANDIDATE
)) {
62 candidates
.add(annotation
);
68 public static Annotation
getMergeTargetAnnotation(IAnnotationModel model
) {
69 Iterator iterator
= model
.getAnnotationIterator();
70 while (iterator
.hasNext()) {
71 Annotation annotation
= ((Annotation
) iterator
.next());
72 if (annotation
.getType().equals(BulkEditorAnnotation
.TYPE_MERGE_TARGET
)) {
79 public static String
getAnnotationString(Annotation annotation
, IAnnotationModel model
, IDocument document
) {
80 Position position
= model
.getPosition(annotation
);
82 int line
= document
.getLineOfOffset(position
.getOffset());
83 return getLineWithoutDelimiters(document
, line
);
84 } catch (BadLocationException e
) {
85 // TODO Auto-generated catch block
91 public static void removeLineFromEditorInput(int line
,
92 IEditorInput editorInput
) {
93 if (editorInput
instanceof BulkEditorInput
) {
94 ((BulkEditorInput
) editorInput
).getList().remove(line
);
98 public static void removeLineFromDocument(int line
, IDocument document
) {
100 IRegion region
= document
.getLineInformation(line
);
101 document
.replace(region
.getOffset(), region
.getLength(), "");
102 } catch (BadLocationException e
) {
103 // TODO Auto-generated catch block
108 public static void printAnnotations(IAnnotationModel model
) {
109 logger
.debug("--------");
110 List
<Annotation
> list
= new ArrayList
<Annotation
>();
111 Iterator iterator
= model
.getAnnotationIterator();
112 while (iterator
.hasNext()) {
113 boolean added
= false;
114 Annotation annotation
= (Annotation
) iterator
.next();
115 int offset
= model
.getPosition(annotation
).getOffset();
116 int listcount
= list
.size();
117 for (int i
= 0; i
< listcount
; i
++) {
118 if (offset
< model
.getPosition(list
.get(i
)).getOffset()) {
119 list
.add(i
, annotation
);
125 list
.add(annotation
);
129 for (Annotation annotation
: list
) {
130 logger
.debug(annotation
.getClass() + " (" +
131 annotation
.getType() + ") - " +
132 annotation
.getText() + ": o " +
133 model
.getPosition(annotation
).getOffset() + ", l " +
134 model
.getPosition(annotation
).getLength() + ", del? " +
135 model
.getPosition(annotation
).isDeleted());
137 logger
.debug("--------");
141 * When an Annotation's type is changed, the model does not fire a change,
142 * so no re-paint is triggered, and any decorations associated with the type
143 * are not painted. Removing then re-adding triggers the paint.
148 public static void reAddAnnotation(Annotation annotation
,
149 IAnnotationModel model
) {
150 Position position
= model
.getPosition(annotation
);
151 // model.removeAnnotation(annotation);
152 // model.addAnnotation(annotation, position);
155 public static Annotation
getAnnotationAtLine(int line
, IDocument document
,
156 IAnnotationModel model
) {
157 Annotation annotation
= null;
159 int offset
= document
.getLineOffset(line
);
160 int length
= document
.getLineLength(line
);
161 Iterator iterator
= ((IAnnotationModelExtension2
) model
).
162 getAnnotationIterator(offset
, length
, true, true);
163 if (iterator
.hasNext()) {
164 annotation
= (Annotation
) iterator
.next();
166 } catch (BadLocationException e
) {