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.
11 package eu
.etaxonomy
.taxeditor
.annotatedlineeditor
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.jface
.text
.BadLocationException
;
18 import org
.eclipse
.jface
.text
.Document
;
19 import org
.eclipse
.jface
.text
.DocumentEvent
;
20 import org
.eclipse
.jface
.text
.IDocument
;
21 import org
.eclipse
.jface
.text
.IRegion
;
22 import org
.eclipse
.jface
.text
.ITypedRegion
;
23 import org
.eclipse
.jface
.text
.Region
;
24 import org
.eclipse
.jface
.text
.TypedRegion
;
25 import org
.eclipse
.jface
.text
.rules
.FastPartitioner
;
26 import org
.eclipse
.jface
.text
.rules
.RuleBasedPartitionScanner
;
29 * Separates each line into partitions for the main text - usually the
30 * object's title cache - and supplemental text. The supplemental text
31 * partition can then be given a different color and / or font style by
32 * the editor's presentation reconciler.
38 public class AnnotatedLineEditorPartitioner
extends FastPartitioner
{
39 private static final Logger logger
= Logger
40 .getLogger(AnnotatedLineEditorPartitioner
.class);
42 private Document document
;
43 private LineAnnotationModel model
;
44 private ILineDisplayStrategy lineDisplayStrategy
;
47 * <p>Constructor for AnnotatedLineEditorPartitioner.</p>
49 * @param document a {@link org.eclipse.jface.text.Document} object.
50 * @param model a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel} object.
51 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
53 public AnnotatedLineEditorPartitioner(Document document
, LineAnnotationModel model
, ILineDisplayStrategy lineDisplayStrategy
) {
54 super(new RuleBasedPartitionScanner(),
55 new String
[] {ILineDisplayStrategy
.SUPPLEMENTAL_TEXT
});
57 this.document
= document
;
59 this.lineDisplayStrategy
= lineDisplayStrategy
;
63 * @see org.eclipse.jface.text.rules.FastPartitioner#computePartitioning(int, int, boolean)
67 public ITypedRegion
[] computePartitioning(int offset
,
68 int length
, boolean includeZeroLengthPartitions
) {
69 List
<ITypedRegion
> regions
= new ArrayList
<ITypedRegion
>();
71 int lines
= document
.getNumberOfLines(offset
, length
);
72 int startLine
= document
.getLineOfOffset(offset
);
74 // Iterate through all affected lines, separating into regions of DEFAULT_CONTENT_TYPE and SUPPLEMENTAL_TEXT
75 for (int i
= startLine
; i
< startLine
+ lines
; i
++) {
76 LineAnnotation annotation
= (LineAnnotation
) ((LineAnnotationModel
) model
).getAnnotationAtLine(i
, document
);
77 if (annotation
!= null) {
78 IRegion lineInfo
= document
.getLineInformation(i
);
79 String lineText
= document
.get(lineInfo
.getOffset(), lineInfo
.getLength());
81 String supplementalText
= lineDisplayStrategy
.getSupplementalText(annotation
.getEntity());
82 if (supplementalText
== null || supplementalText
.equals("")) {
85 if (lineText
.contains(supplementalText
)) {
86 int startSupplementalText
= lineText
.indexOf(supplementalText
) + lineInfo
.getOffset();
88 if (startSupplementalText
> lineInfo
.getOffset()) {
89 regions
.add(new TypedRegion(lineInfo
.getOffset(), startSupplementalText
- lineInfo
.getOffset(),
90 IDocument
.DEFAULT_CONTENT_TYPE
));
91 logger
.warn("Non supp at " + lineInfo
.getOffset() + ", length " + (startSupplementalText
- lineInfo
.getOffset()));
93 regions
.add(new TypedRegion(startSupplementalText
, supplementalText
.length(),
94 ILineDisplayStrategy
.SUPPLEMENTAL_TEXT
));
95 logger
.warn("Supp at " + startSupplementalText
+ ", length " + supplementalText
.length());
96 if (startSupplementalText
+ supplementalText
.length() < lineInfo
.getOffset() + lineInfo
.getLength()) {
97 regions
.add(new TypedRegion(startSupplementalText
+ supplementalText
.length(),
98 lineInfo
.getLength() - startSupplementalText
- supplementalText
.length(),
99 IDocument
.DEFAULT_CONTENT_TYPE
));
105 } catch (BadLocationException e
) {
106 return super.computePartitioning(offset
, length
, includeZeroLengthPartitions
);
108 return (ITypedRegion
[]) regions
.toArray(new ITypedRegion
[regions
.size()]);
112 * @see org.eclipse.jface.text.rules.FastPartitioner#documentChanged2(org.eclipse.jface.text.DocumentEvent)
116 public IRegion
documentChanged2(DocumentEvent e
) {
117 computePartitioning(e
.getOffset(), e
.getLength());
118 return new Region(e
.getOffset(), e
.getLength());
119 // return super.documentChanged2(e);