Project

General

Profile

Download (4.83 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.ArrayList;
14
import java.util.List;
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.DocumentEvent;
20
import org.eclipse.jface.text.IDocument;
21
import org.eclipse.jface.text.IRegion;
22
import org.eclipse.jface.text.ITypedRegion;
23
import org.eclipse.jface.text.Region;
24
import org.eclipse.jface.text.TypedRegion;
25
import org.eclipse.jface.text.rules.FastPartitioner;
26
import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
27

    
28
/**
29
 * Separates each line into partitions for the main text - usually the
30
 * object's title cache - and supplemental text. The supplemental text
31
 * partition can then be given a different color and / or font style by
32
 * the editor's presentation reconciler.
33
 *
34
 * @author p.ciardelli
35
 * @created 28.10.2009
36
 * @version 1.0
37
 */
38
public class AnnotatedLineEditorPartitioner extends FastPartitioner {
39
	private static final Logger logger = Logger
40
			.getLogger(AnnotatedLineEditorPartitioner.class);
41
	
42
	private Document document;
43
	private LineAnnotationModel model;
44
	private ILineDisplayStrategy lineDisplayStrategy;
45
	
46
	/**
47
	 * <p>Constructor for AnnotatedLineEditorPartitioner.</p>
48
	 *
49
	 * @param document a {@link org.eclipse.jface.text.Document} object.
50
	 * @param model a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel} object.
51
	 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
52
	 */
53
	public AnnotatedLineEditorPartitioner(Document document, LineAnnotationModel model, ILineDisplayStrategy lineDisplayStrategy) {
54
		super(new RuleBasedPartitionScanner(), 
55
				new String[] {ILineDisplayStrategy.SUPPLEMENTAL_TEXT});
56
		
57
		this.document = document;
58
		this.model = model;
59
		this.lineDisplayStrategy = lineDisplayStrategy;
60
	}
61
	
62
	/* (non-Javadoc)
63
	 * @see org.eclipse.jface.text.rules.FastPartitioner#computePartitioning(int, int, boolean)
64
	 */
65
	/** {@inheritDoc} */
66
	@Override
67
	public ITypedRegion[] computePartitioning(int offset,
68
			int length, boolean includeZeroLengthPartitions) {
69
		List<ITypedRegion> regions = new ArrayList<ITypedRegion>(); 
70
		try {
71
			int lines = document.getNumberOfLines(offset, length);
72
			int startLine = document.getLineOfOffset(offset);
73
			
74
			// Iterate through all affected lines, separating into regions of DEFAULT_CONTENT_TYPE and SUPPLEMENTAL_TEXT
75
			for (int i = startLine; i < startLine + lines; i++) {
76
				LineAnnotation annotation = (LineAnnotation) ((LineAnnotationModel) model).getAnnotationAtLine(i, document);
77
				if (annotation != null) {
78
					IRegion lineInfo = document.getLineInformation(i);
79
					String lineText = document.get(lineInfo.getOffset(), lineInfo.getLength());
80
					
81
					String supplementalText = lineDisplayStrategy.getSupplementalText(annotation.getEntity());
82
					if (supplementalText == null || supplementalText.equals("")) {
83
						continue;
84
					}
85
					if (lineText.contains(supplementalText)) {
86
						int startSupplementalText = lineText.indexOf(supplementalText) + lineInfo.getOffset();
87
						
88
						if (startSupplementalText > lineInfo.getOffset()) {
89
							regions.add(new TypedRegion(lineInfo.getOffset(), startSupplementalText - lineInfo.getOffset(), 
90
															IDocument.DEFAULT_CONTENT_TYPE));
91
							logger.warn("Non supp at " + lineInfo.getOffset() + ", length " + (startSupplementalText - lineInfo.getOffset()));
92
						}														
93
						regions.add(new TypedRegion(startSupplementalText, supplementalText.length(), 
94
														ILineDisplayStrategy.SUPPLEMENTAL_TEXT));
95
						logger.warn("Supp at " + startSupplementalText + ", length " + supplementalText.length());
96
						if (startSupplementalText + supplementalText.length() < lineInfo.getOffset() + lineInfo.getLength()) {
97
							regions.add(new TypedRegion(startSupplementalText + supplementalText.length(), 
98
															lineInfo.getLength() - startSupplementalText - supplementalText.length(), 
99
															IDocument.DEFAULT_CONTENT_TYPE));
100
						}									
101
					}
102
					
103
				}
104
			}
105
		} catch (BadLocationException e) {
106
			return super.computePartitioning(offset, length, includeZeroLengthPartitions);
107
		}
108
		return (ITypedRegion[]) regions.toArray(new ITypedRegion[regions.size()]);
109
	}
110
	
111
	/* (non-Javadoc)
112
	 * @see org.eclipse.jface.text.rules.FastPartitioner#documentChanged2(org.eclipse.jface.text.DocumentEvent)
113
	 */
114
	/** {@inheritDoc} */
115
	@Override
116
	public IRegion documentChanged2(DocumentEvent e) {
117
		computePartitioning(e.getOffset(), e.getLength());
118
		return new Region(e.getOffset(), e.getLength());
119
//		return super.documentChanged2(e);
120
	}
121
}
(3-3/16)