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
.HashMap
;
14 import java
.util
.Iterator
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.jface
.text
.BadLocationException
;
19 import org
.eclipse
.jface
.text
.Document
;
20 import org
.eclipse
.jface
.text
.IDocument
;
21 import org
.eclipse
.jface
.text
.TextAttribute
;
22 import org
.eclipse
.jface
.text
.rules
.IPredicateRule
;
23 import org
.eclipse
.jface
.text
.rules
.IToken
;
24 import org
.eclipse
.jface
.text
.rules
.RuleBasedPartitionScanner
;
25 import org
.eclipse
.jface
.text
.rules
.SingleLineRule
;
26 import org
.eclipse
.jface
.text
.rules
.Token
;
27 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.widgets
.Display
;
32 * Gives a different color and font style to all text identified as supplemental text by
33 * its corresponding LineAnnotation.
35 * Note: implementing IPartitionScanner in a straightforward fashion proved buggy, so this more obtuse
36 * extension of RuleBasedPartitionScanner should be used instead. Uses a matching IRule whose pattern changes
37 * depending on the current line.
41 * @see CopyOfSupplementalTextScanner
44 public class SupplementalTextScanner
extends RuleBasedPartitionScanner
{
45 private static final Logger logger
= Logger
46 .getLogger(SupplementalTextScanner
.class);
48 private ILineDisplayStrategy lineDisplayStrategy
;
49 private Document document
;
50 private ISourceViewer sourceViewer
;
52 private Map
<Integer
, String
> supplementalTexts
;
53 private SupplementalTextRule rule
;
57 * <p>Constructor for SupplementalTextScanner.</p>
59 * @param sourceViewer a {@link org.eclipse.jface.text.source.ISourceViewer} object.
60 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
62 public SupplementalTextScanner(ISourceViewer sourceViewer
, ILineDisplayStrategy lineDisplayStrategy
) {
63 this.sourceViewer
= sourceViewer
;
64 this.lineDisplayStrategy
= lineDisplayStrategy
;
66 TextAttribute textAttribute
= new TextAttribute(Display
.getCurrent().getSystemColor(SWT
.COLOR_DARK_GRAY
), null, SWT
.ITALIC
);
67 IToken supplementalTextToken
= new Token(textAttribute
);
69 rule
= new SupplementalTextRule(supplementalTextToken
);
70 setPredicateRules(new IPredicateRule
[] {rule
});
74 * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
78 public void setPartialRange(IDocument document
, int offset
, int length
,
79 String contentType
, int partitionOffset
) {
81 // Get all supplemental texts in the partial range
82 supplementalTexts
= new HashMap
<Integer
, String
>();
83 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
102 this.document
= (Document
) document
;
103 super.setPartialRange(document
, offset
, length
, contentType
, partitionOffset
);
107 * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#nextToken()
111 public IToken
nextToken() {
113 int line
= document
.getLineOfOffset(getTokenOffset());
114 if (line
!= lastLine
) {
115 rule
.setStartSequence(supplementalTexts
.get(line
));
118 } catch (BadLocationException e
) {
119 logger
.debug("Ignoring", e
);
121 return super.nextToken();
124 class SupplementalTextRule
extends SingleLineRule
{
126 static final String DUMMY_SEQUENCE
= "_dummy_sequence";
129 * @param startSequence
132 * @param escapeCharacter
135 public SupplementalTextRule(IToken token
) {
136 super(DUMMY_SEQUENCE
, null, token
, '\\', true);
140 * Set pattern match for rule.
144 protected void setStartSequence(String sequence
) {
145 if (sequence
!= null && sequence
.length() > 0) {
146 fStartSequence
= sequence
.toCharArray();