ref #6190 removing svn property place holder in first line of code - java files
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / annotatedlineeditor / AnnotatedLineEditorPartitioner.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.annotatedlineeditor;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.Document;
18 import org.eclipse.jface.text.DocumentEvent;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.ITypedRegion;
22 import org.eclipse.jface.text.Region;
23 import org.eclipse.jface.text.TypedRegion;
24 import org.eclipse.jface.text.rules.FastPartitioner;
25 import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
26
27 /**
28 * Separates each line into partitions for the main text - usually the
29 * object's title cache - and supplemental text. The supplemental text
30 * partition can then be given a different color and / or font style by
31 * the editor's presentation reconciler.
32 *
33 * @author p.ciardelli
34 * @created 28.10.2009
35 * @version 1.0
36 */
37 public class AnnotatedLineEditorPartitioner extends FastPartitioner {
38 private static final Logger logger = Logger
39 .getLogger(AnnotatedLineEditorPartitioner.class);
40
41 private Document document;
42 private LineAnnotationModel model;
43 private ILineDisplayStrategy lineDisplayStrategy;
44
45 /**
46 * <p>Constructor for AnnotatedLineEditorPartitioner.</p>
47 *
48 * @param document a {@link org.eclipse.jface.text.Document} object.
49 * @param model a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel} object.
50 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
51 */
52 public AnnotatedLineEditorPartitioner(Document document, LineAnnotationModel model, ILineDisplayStrategy lineDisplayStrategy) {
53 super(new RuleBasedPartitionScanner(),
54 new String[] {ILineDisplayStrategy.SUPPLEMENTAL_TEXT});
55
56 this.document = document;
57 this.model = model;
58 this.lineDisplayStrategy = lineDisplayStrategy;
59 }
60
61 /* (non-Javadoc)
62 * @see org.eclipse.jface.text.rules.FastPartitioner#computePartitioning(int, int, boolean)
63 */
64 /** {@inheritDoc} */
65 @Override
66 public ITypedRegion[] computePartitioning(int offset,
67 int length, boolean includeZeroLengthPartitions) {
68 List<ITypedRegion> regions = new ArrayList<ITypedRegion>();
69 try {
70 int lines = document.getNumberOfLines(offset, length);
71 int startLine = document.getLineOfOffset(offset);
72
73 // Iterate through all affected lines, separating into regions of DEFAULT_CONTENT_TYPE and SUPPLEMENTAL_TEXT
74 for (int i = startLine; i < startLine + lines; i++) {
75 LineAnnotation annotation = (LineAnnotation) ((LineAnnotationModel) model).getAnnotationAtLine(i, document);
76 if (annotation != null) {
77 IRegion lineInfo = document.getLineInformation(i);
78 String lineText = document.get(lineInfo.getOffset(), lineInfo.getLength());
79
80 String supplementalText = lineDisplayStrategy.getSupplementalText(annotation.getEntity());
81 if (supplementalText == null || supplementalText.equals("")) {
82 continue;
83 }
84 if (lineText.contains(supplementalText)) {
85 int startSupplementalText = lineText.indexOf(supplementalText) + lineInfo.getOffset();
86
87 if (startSupplementalText > lineInfo.getOffset()) {
88 regions.add(new TypedRegion(lineInfo.getOffset(), startSupplementalText - lineInfo.getOffset(),
89 IDocument.DEFAULT_CONTENT_TYPE));
90 logger.warn("Non supp at " + lineInfo.getOffset() + ", length " + (startSupplementalText - lineInfo.getOffset()));
91 }
92 regions.add(new TypedRegion(startSupplementalText, supplementalText.length(),
93 ILineDisplayStrategy.SUPPLEMENTAL_TEXT));
94 logger.warn("Supp at " + startSupplementalText + ", length " + supplementalText.length());
95 if (startSupplementalText + supplementalText.length() < lineInfo.getOffset() + lineInfo.getLength()) {
96 regions.add(new TypedRegion(startSupplementalText + supplementalText.length(),
97 lineInfo.getLength() - startSupplementalText - supplementalText.length(),
98 IDocument.DEFAULT_CONTENT_TYPE));
99 }
100 }
101
102 }
103 }
104 } catch (BadLocationException e) {
105 return super.computePartitioning(offset, length, includeZeroLengthPartitions);
106 }
107 return (ITypedRegion[]) regions.toArray(new ITypedRegion[regions.size()]);
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.jface.text.rules.FastPartitioner#documentChanged2(org.eclipse.jface.text.DocumentEvent)
112 */
113 /** {@inheritDoc} */
114 @Override
115 public IRegion documentChanged2(DocumentEvent e) {
116 computePartitioning(e.getOffset(), e.getLength());
117 return new Region(e.getOffset(), e.getLength());
118 // return super.documentChanged2(e);
119 }
120 }