1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.annotatedlineeditor;
|
12
|
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.Map;
|
16
|
|
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;
|
30
|
|
31
|
/**
|
32
|
* Gives a different color and font style to all text identified as supplemental text by
|
33
|
* its corresponding LineAnnotation.
|
34
|
*
|
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.
|
38
|
*
|
39
|
* @author p.ciardelli
|
40
|
* @created 28.10.2009
|
41
|
* @see CopyOfSupplementalTextScanner
|
42
|
* @version 1.0
|
43
|
*/
|
44
|
public class SupplementalTextScanner extends RuleBasedPartitionScanner {
|
45
|
private static final Logger logger = Logger
|
46
|
.getLogger(SupplementalTextScanner.class);
|
47
|
|
48
|
private ILineDisplayStrategy lineDisplayStrategy;
|
49
|
private Document document;
|
50
|
private ISourceViewer sourceViewer;
|
51
|
|
52
|
private Map<Integer, String> supplementalTexts;
|
53
|
private SupplementalTextRule rule;
|
54
|
private int lastLine;
|
55
|
|
56
|
/**
|
57
|
* <p>Constructor for SupplementalTextScanner.</p>
|
58
|
*
|
59
|
* @param sourceViewer a {@link org.eclipse.jface.text.source.ISourceViewer} object.
|
60
|
* @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
|
61
|
*/
|
62
|
public SupplementalTextScanner(ISourceViewer sourceViewer, ILineDisplayStrategy lineDisplayStrategy) {
|
63
|
this.sourceViewer = sourceViewer;
|
64
|
this.lineDisplayStrategy = lineDisplayStrategy;
|
65
|
|
66
|
TextAttribute textAttribute = new TextAttribute(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY), null, SWT.ITALIC);
|
67
|
IToken supplementalTextToken = new Token(textAttribute);
|
68
|
|
69
|
rule = new SupplementalTextRule(supplementalTextToken);
|
70
|
setPredicateRules(new IPredicateRule[] {rule});
|
71
|
}
|
72
|
|
73
|
/* (non-Javadoc)
|
74
|
* @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
|
75
|
*/
|
76
|
/** {@inheritDoc} */
|
77
|
@Override
|
78
|
public void setPartialRange(IDocument document, int offset, int length,
|
79
|
String contentType, int partitionOffset) {
|
80
|
|
81
|
// Get all supplemental texts in the partial range
|
82
|
supplementalTexts = new HashMap<Integer, String>();
|
83
|
LineAnnotationModel model = (LineAnnotationModel) sourceViewer.getAnnotationModel();
|
84
|
if(model != null){
|
85
|
Iterator iter = model.getAnnotationIterator(offset, length, true, true);
|
86
|
while (iter.hasNext()) {
|
87
|
Object next = iter.next();
|
88
|
if (next instanceof LineAnnotation) {
|
89
|
Object entity = ((LineAnnotation) next).getEntity();
|
90
|
String supplementalText = lineDisplayStrategy.getSupplementalText(entity);
|
91
|
if (supplementalText != null && supplementalText.length() > 0) {
|
92
|
try {
|
93
|
int line = document.getLineOfOffset(model.getPosition((LineAnnotation) next).getOffset());
|
94
|
supplementalTexts.put(line, supplementalText);
|
95
|
} catch (BadLocationException e) {
|
96
|
// TODO Auto-generated catch block
|
97
|
e.printStackTrace();
|
98
|
}
|
99
|
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
this.document = (Document) document;
|
105
|
super.setPartialRange(document, offset, length, contentType, partitionOffset);
|
106
|
}
|
107
|
|
108
|
/* (non-Javadoc)
|
109
|
* @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#nextToken()
|
110
|
*/
|
111
|
/** {@inheritDoc} */
|
112
|
@Override
|
113
|
public IToken nextToken() {
|
114
|
try {
|
115
|
int line = document.getLineOfOffset(getTokenOffset());
|
116
|
if (line != lastLine) {
|
117
|
rule.setStartSequence(supplementalTexts.get(line));
|
118
|
}
|
119
|
lastLine = line;
|
120
|
} catch (BadLocationException e) {
|
121
|
logger.debug("Ignoring", e);
|
122
|
}
|
123
|
return super.nextToken();
|
124
|
}
|
125
|
|
126
|
class SupplementalTextRule extends SingleLineRule {
|
127
|
|
128
|
static final String DUMMY_SEQUENCE = "_dummy_sequence";
|
129
|
|
130
|
/**
|
131
|
* @param startSequence
|
132
|
* @param endSequence
|
133
|
* @param token
|
134
|
* @param escapeCharacter
|
135
|
* @param breaksOnEOL
|
136
|
*/
|
137
|
public SupplementalTextRule(IToken token) {
|
138
|
super(DUMMY_SEQUENCE, null, token, '\\', true);
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Set pattern match for rule.
|
143
|
*
|
144
|
* @param sequence
|
145
|
*/
|
146
|
protected void setStartSequence(String sequence) {
|
147
|
if (sequence != null && sequence.length() > 0) {
|
148
|
fStartSequence = sequence.toCharArray();
|
149
|
}
|
150
|
}
|
151
|
|
152
|
}
|
153
|
}
|