3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.annotatedlineeditor
;
12 import java
.util
.List
;
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
;
33 public class AnnotatedLineEditorReconcilingStrategy
implements IReconcilingStrategy
{
34 private static final Logger logger
= Logger
35 .getLogger(AnnotatedLineEditorReconcilingStrategy
.class);
37 private IDocument document
;
38 private ISourceViewer viewer
;
41 * <p>Constructor for AnnotatedLineEditorReconcilingStrategy.</p>
43 * @param sourceViewer a {@link org.eclipse.jface.text.source.ISourceViewer} object.
45 public AnnotatedLineEditorReconcilingStrategy(ISourceViewer sourceViewer
) {
46 this.viewer
= sourceViewer
;
50 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
53 public void reconcile(IRegion partition
) {
58 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion, org.eclipse.jface.text.IRegion)
61 public void reconcile(DirtyRegion dirtyRegion
, IRegion subRegion
) {
63 doReconcile(dirtyRegion
);
64 } catch (BadLocationException e
) {
65 // TODO Auto-generated catch block
70 synchronized private void doReconcile(DirtyRegion dirtyRegion
) throws BadLocationException
{
71 // Get all affected lines
72 int dirtyOffset
= dirtyRegion
.getOffset();
73 int dirtyLength
= dirtyRegion
.getLength();
75 int lineChangeStart
= document
.getLineOfOffset(dirtyOffset
);
77 if (DirtyRegion
.INSERT
.equals(dirtyRegion
.getType())) {
78 lineChangeEnd
= document
.getLineOfOffset(dirtyOffset
+ dirtyLength
);
80 lineChangeEnd
= lineChangeStart
;
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());
89 List
<LineAnnotation
> annotationsCurrentLine
=
90 ((LineAnnotationModel
) getAnnotationModel()).getUndeletedAnnotations(lineOffset
, lineLength
);
92 if (annotationsCurrentLine
.size() == 0) {
93 logger
.debug("Adding new annotation " + lineText
);
94 addAnnotation(lineText
, lineRegion
);
96 LineAnnotation currentAnnotation
= annotationsCurrentLine
.get(0);
97 int length
= lineLength
;
98 getAnnotationModel().getPosition(currentAnnotation
).setLength(length
);
99 currentAnnotation
.setText(lineText
);
101 for (int j
= 1; j
< annotationsCurrentLine
.size(); j
++) {
102 annotationsCurrentLine
.get(j
).markAsMerged(currentAnnotation
.getEntity());
103 getAnnotationModel().removeAnnotation(annotationsCurrentLine
.get(j
));
108 // Deletes have already taken place
110 ((LineAnnotationModel
) getAnnotationModel()).printAnnotations();
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
);
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);
123 private IAnnotationModel
getAnnotationModel() {
124 return viewer
.getAnnotationModel();
128 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
131 public void setDocument(IDocument document
) {
132 this.document
= document
;