2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.annotatedlineeditor
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
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
;
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.
37 public class AnnotatedLineEditorPartitioner
extends FastPartitioner
{
38 private static final Logger logger
= Logger
39 .getLogger(AnnotatedLineEditorPartitioner
.class);
41 private Document document
;
42 private LineAnnotationModel model
;
43 private ILineDisplayStrategy lineDisplayStrategy
;
46 * <p>Constructor for AnnotatedLineEditorPartitioner.</p>
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.
52 public AnnotatedLineEditorPartitioner(Document document
, LineAnnotationModel model
, ILineDisplayStrategy lineDisplayStrategy
) {
53 super(new RuleBasedPartitionScanner(),
54 new String
[] {ILineDisplayStrategy
.SUPPLEMENTAL_TEXT
});
56 this.document
= document
;
58 this.lineDisplayStrategy
= lineDisplayStrategy
;
62 * @see org.eclipse.jface.text.rules.FastPartitioner#computePartitioning(int, int, boolean)
66 public ITypedRegion
[] computePartitioning(int offset
,
67 int length
, boolean includeZeroLengthPartitions
) {
68 List
<ITypedRegion
> regions
= new ArrayList
<ITypedRegion
>();
70 int lines
= document
.getNumberOfLines(offset
, length
);
71 int startLine
= document
.getLineOfOffset(offset
);
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());
80 String supplementalText
= lineDisplayStrategy
.getSupplementalText(annotation
.getEntity());
81 if (supplementalText
== null || supplementalText
.equals("")) {
84 if (lineText
.contains(supplementalText
)) {
85 int startSupplementalText
= lineText
.indexOf(supplementalText
) + lineInfo
.getOffset();
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()));
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
));
104 } catch (BadLocationException e
) {
105 return super.computePartitioning(offset
, length
, includeZeroLengthPartitions
);
107 return (ITypedRegion
[]) regions
.toArray(new ITypedRegion
[regions
.size()]);
111 * @see org.eclipse.jface.text.rules.FastPartitioner#documentChanged2(org.eclipse.jface.text.DocumentEvent)
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);