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