Project

General

Profile

Download (4.49 KB) Statistics
| Branch: | Tag: | Revision:
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
package eu.etaxonomy.taxeditor.annotatedlineeditor;
10

    
11
import java.util.List;
12

    
13
import org.apache.log4j.Logger;
14
import org.eclipse.jface.text.BadLocationException;
15
import org.eclipse.jface.text.IDocument;
16
import org.eclipse.jface.text.IRegion;
17
import org.eclipse.jface.text.Position;
18
import org.eclipse.jface.text.reconciler.DirtyRegion;
19
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
20
import org.eclipse.jface.text.source.Annotation;
21
import org.eclipse.jface.text.source.IAnnotationModel;
22
import org.eclipse.jface.text.source.ISourceViewer;
23

    
24

    
25
/**
26
 * Doesn't work!
27
 *
28
 * @author p.ciardelli
29
 * @created 25.06.2009
30
 * @version 1.0
31
 */
32
public class AnnotatedLineEditorReconcilingStrategy implements IReconcilingStrategy {
33
	private static final Logger logger = Logger
34
			.getLogger(AnnotatedLineEditorReconcilingStrategy.class);
35

    
36
	private IDocument document;
37
	private ISourceViewer viewer;
38

    
39
	/**
40
	 * <p>Constructor for AnnotatedLineEditorReconcilingStrategy.</p>
41
	 *
42
	 * @param sourceViewer a {@link org.eclipse.jface.text.source.ISourceViewer} object.
43
	 */
44
	public AnnotatedLineEditorReconcilingStrategy(ISourceViewer sourceViewer) {
45
		this.viewer = sourceViewer;
46
	}
47

    
48
	/* (non-Javadoc)
49
	 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
50
	 */
51
	/** {@inheritDoc} */
52
	public void reconcile(IRegion partition) {
53
		// not used
54
	}
55

    
56
	/* (non-Javadoc)
57
	 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion, org.eclipse.jface.text.IRegion)
58
	 */
59
	/** {@inheritDoc} */
60
	public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
61
		try {
62
			doReconcile(dirtyRegion);
63
		} catch (BadLocationException e) {
64
			// TODO Auto-generated catch block
65
			e.printStackTrace();
66
		}
67
	}
68
	
69
	synchronized private void doReconcile(DirtyRegion dirtyRegion) throws BadLocationException {
70
		// Get all affected lines
71
		int dirtyOffset = dirtyRegion.getOffset();
72
		int dirtyLength = dirtyRegion.getLength();
73
		
74
		int lineChangeStart = document.getLineOfOffset(dirtyOffset);
75
		int lineChangeEnd;
76
		if (DirtyRegion.INSERT.equals(dirtyRegion.getType())) {
77
			lineChangeEnd = document.getLineOfOffset(dirtyOffset + dirtyLength);
78
		} else {
79
			lineChangeEnd = lineChangeStart;
80
		}
81
		
82
		for (int i = lineChangeStart; i <= lineChangeEnd; i++) {
83
			int lineOffset = document.getLineOffset(i);
84
			int lineLength = document.getLineLength(i);
85
			IRegion lineRegion = document.getLineInformation(i);
86
			String lineText = document.get(lineRegion.getOffset(), lineRegion.getLength());
87
			
88
			List<LineAnnotation> annotationsCurrentLine = 
89
					((LineAnnotationModel) getAnnotationModel()).getUndeletedAnnotations(lineOffset, lineLength);
90
			
91
			if (annotationsCurrentLine.size() == 0) {
92
				logger.debug("Adding new annotation " + lineText);
93
				addAnnotation(lineText, lineRegion);
94
			} else {
95
				LineAnnotation currentAnnotation = annotationsCurrentLine.get(0);
96
				int length = lineLength;
97
				getAnnotationModel().getPosition(currentAnnotation).setLength(length);
98
				currentAnnotation.setText(lineText);
99
				
100
				for (int j = 1; j < annotationsCurrentLine.size(); j++) {
101
					annotationsCurrentLine.get(j).markAsMerged(currentAnnotation.getEntity());
102
					getAnnotationModel().removeAnnotation(annotationsCurrentLine.get(j));
103
				}
104
			}
105
		}
106
		
107
		// Deletes have already taken place
108
		
109
		((LineAnnotationModel) getAnnotationModel()).printAnnotations();
110
	}
111
		
112
	private void addAnnotation(String text, IRegion region) {
113
		Position position = new Position(region.getOffset(), region.getLength());
114
		Annotation annotation = ((LineAnnotationModel) getAnnotationModel()).createAnnotation(text);
115
		getAnnotationModel().addAnnotation(annotation, position);
116
		
117
//		Position position2 = new Position(region.getOffset() + 1, region.getLength() - 2);
118
//		Annotation annotation2 = new Annotation("org.eclipse.ui.workbench.texteditor.error",true,"asdf");
119
//		getAnnotationModel().addAnnotation(annotation2, position2);
120
	}
121

    
122
	private IAnnotationModel getAnnotationModel() {
123
		return viewer.getAnnotationModel();
124
	}
125
	
126
	/* (non-Javadoc)
127
	 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
128
	 */
129
	/** {@inheritDoc} */
130
	public void setDocument(IDocument document) {
131
		this.document = document;		
132
	}
133
}
(4-4/16)