Initial check-in of Bulk Editor code.
[taxeditor.git] / taxeditor-bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / BulkEditorUtil.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 package eu.etaxonomy.taxeditor.bulkeditor;
11
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Set;
17
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;
27
28 /**
29 * @author p.ciardelli
30 * @created 25.06.2009
31 * @version 1.0
32 */
33 public class BulkEditorUtil {
34 private static final Logger logger = Logger
35 .getLogger(BulkEditorUtil.class);
36
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();
42
43 return document.get(offset, length - delimiterLength);
44 }
45
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);
52 }
53 }
54 }
55
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);
63 }
64 }
65 return candidates;
66 }
67
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)) {
73 return annotation;
74 }
75 }
76 return null;
77 }
78
79 public static String getAnnotationString(Annotation annotation, IAnnotationModel model, IDocument document) {
80 Position position = model.getPosition(annotation);
81 try {
82 int line = document.getLineOfOffset(position.getOffset());
83 return getLineWithoutDelimiters(document, line);
84 } catch (BadLocationException e) {
85 // TODO Auto-generated catch block
86 e.printStackTrace();
87 }
88 return "";
89 }
90
91 public static void removeLineFromEditorInput(int line,
92 IEditorInput editorInput) {
93 if (editorInput instanceof BulkEditorInput) {
94 ((BulkEditorInput) editorInput).getList().remove(line);
95 }
96 }
97
98 public static void removeLineFromDocument(int line, IDocument document) {
99 try {
100 IRegion region = document.getLineInformation(line);
101 document.replace(region.getOffset(), region.getLength(), "");
102 } catch (BadLocationException e) {
103 // TODO Auto-generated catch block
104 e.printStackTrace();
105 }
106 }
107
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);
120 added = true;
121 break;
122 }
123 }
124 if (!added) {
125 list.add(annotation);
126 }
127 }
128
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());
136 }
137 logger.debug("--------");
138 }
139
140 /**
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.
144 *
145 * @param annotation
146 * @param model
147 */
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);
153 }
154
155 public static Annotation getAnnotationAtLine(int line, IDocument document,
156 IAnnotationModel model) {
157 Annotation annotation = null;
158 try {
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();
165 }
166 } catch (BadLocationException e) {
167 // do nothing
168 }
169 return annotation;
170 }
171 }