Project

General

Profile

Download (4.87 KB) Statistics
| Branch: | Tag: | Revision:
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
		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
		this.document = (Document) document; 
103
		super.setPartialRange(document, offset, length, contentType, partitionOffset);
104
	}
105
	
106
	/* (non-Javadoc)
107
	 * @see org.eclipse.jface.text.rules.RuleBasedPartitionScanner#nextToken()
108
	 */
109
	/** {@inheritDoc} */
110
	@Override
111
	public IToken nextToken() {
112
		try {
113
			int line = document.getLineOfOffset(getTokenOffset());
114
			if (line != lastLine) {
115
				rule.setStartSequence(supplementalTexts.get(line));
116
			}
117
			lastLine = line;
118
		} catch (BadLocationException e) {
119
			logger.debug("Ignoring", e);
120
		}
121
		return super.nextToken();
122
	}
123
	
124
	class SupplementalTextRule extends SingleLineRule {
125

    
126
		static final String DUMMY_SEQUENCE = "_dummy_sequence";
127
		
128
		/**
129
		 * @param startSequence
130
		 * @param endSequence
131
		 * @param token
132
		 * @param escapeCharacter
133
		 * @param breaksOnEOL
134
		 */
135
		public SupplementalTextRule(IToken token) {
136
			super(DUMMY_SEQUENCE, null, token, '\\', true);
137
		}
138
				
139
		/**
140
		 * Set pattern match for rule.
141
		 * 
142
		 * @param sequence
143
		 */
144
		protected void setStartSequence(String sequence) {
145
			if (sequence != null && sequence.length() > 0) {
146
				fStartSequence = sequence.toCharArray();
147
			}
148
		}
149
		
150
	}
151
}
(18-18/18)