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
.HashMap
;
13 import java
.util
.Iterator
;
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
.IDocument
;
20 import org
.eclipse
.jface
.text
.TextAttribute
;
21 import org
.eclipse
.jface
.text
.rules
.IPredicateRule
;
22 import org
.eclipse
.jface
.text
.rules
.IToken
;
23 import org
.eclipse
.jface
.text
.rules
.RuleBasedPartitionScanner
;
24 import org
.eclipse
.jface
.text
.rules
.SingleLineRule
;
25 import org
.eclipse
.jface
.text
.rules
.Token
;
26 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.widgets
.Display
;
31 * Gives a different color and font style to all text identified as supplemental text by
32 * its corresponding LineAnnotation.
34 * Note: implementing IPartitionScanner in a straightforward fashion proved buggy, so this more obtuse
35 * extension of RuleBasedPartitionScanner should be used instead. Uses a matching IRule whose pattern changes
36 * depending on the current line.
40 * @see CopyOfSupplementalTextScanner
43 public class SupplementalTextScanner
extends RuleBasedPartitionScanner
{
44 private static final Logger logger
= Logger
45 .getLogger(SupplementalTextScanner
.class);
47 private ILineDisplayStrategy lineDisplayStrategy
;
48 private Document document
;
49 private ISourceViewer sourceViewer
;
51 private Map
<Integer
, String
> supplementalTexts
;
52 private SupplementalTextRule rule
;
56 * <p>Constructor for SupplementalTextScanner.</p>
58 * @param sourceViewer a {@link org.eclipse.jface.text.source.ISourceViewer} object.
59 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
61 public SupplementalTextScanner(ISourceViewer sourceViewer
, ILineDisplayStrategy lineDisplayStrategy
) {
62 this.sourceViewer
= sourceViewer
;
63 this.lineDisplayStrategy
= lineDisplayStrategy
;
65 TextAttribute textAttribute
= new TextAttribute(Display
.getCurrent().getSystemColor(SWT
.COLOR_DARK_GRAY
), null, SWT
.ITALIC
);
66 IToken supplementalTextToken
= new Token(textAttribute
);
68 rule
= new SupplementalTextRule(supplementalTextToken
);
69 setPredicateRules(new IPredicateRule
[] {rule
});
73 * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
77 public void setPartialRange(IDocument document
, int offset
, int length
,
78 String contentType
, int partitionOffset
) {
80 // Get all supplemental texts in the partial range
81 supplementalTexts
= new HashMap
<Integer
, String
>();
82 LineAnnotationModel model
= (LineAnnotationModel
) sourceViewer
.getAnnotationModel();
84 Iterator iter
= model
.getAnnotationIterator(offset
, length
, true, true);
85 while (iter
.hasNext()) {
86 Object next
= iter
.next();
87 if (next
instanceof LineAnnotation
) {
88 Object entity
= ((LineAnnotation
) next
).getEntity();
89 String supplementalText
= lineDisplayStrategy
.getSupplementalText(entity
);
90 if (supplementalText
!= null && supplementalText
.length() > 0) {
92 int line
= document
.getLineOfOffset(model
.getPosition((LineAnnotation
) next
).getOffset());
93 supplementalTexts
.put(line
, supplementalText
);
94 } catch (BadLocationException e
) {
95 // TODO Auto-generated catch block
103 this.document
= (Document
) document
;
104 super.setPartialRange(document
, offset
, length
, contentType
, partitionOffset
);
108 * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#nextToken()
112 public IToken
nextToken() {
114 int line
= document
.getLineOfOffset(getTokenOffset());
115 if (line
!= lastLine
) {
116 rule
.setStartSequence(supplementalTexts
.get(line
));
119 } catch (BadLocationException e
) {
120 logger
.debug("Ignoring", e
);
122 return super.nextToken();
125 class SupplementalTextRule
extends SingleLineRule
{
127 static final String DUMMY_SEQUENCE
= "_dummy_sequence";
130 * @param startSequence
133 * @param escapeCharacter
136 public SupplementalTextRule(IToken token
) {
137 super(DUMMY_SEQUENCE
, null, token
, '\\', true);
141 * Set pattern match for rule.
145 protected void setStartSequence(String sequence
) {
146 if (sequence
!= null && sequence
.length() > 0) {
147 fStartSequence
= sequence
.toCharArray();