Project

General

Profile

Download (4.5 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
package eu.etaxonomy.taxeditor.annotatedlineeditor;
11

    
12
import java.util.List;
13

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

    
25

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

    
37
	private IDocument document;
38
	private ISourceViewer viewer;
39

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

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

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

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