2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.annotatedlineeditor
;
11 import java
.util
.List
;
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
;
32 public class AnnotatedLineEditorReconcilingStrategy
implements IReconcilingStrategy
{
33 private static final Logger logger
= Logger
34 .getLogger(AnnotatedLineEditorReconcilingStrategy
.class);
36 private IDocument document
;
37 private ISourceViewer viewer
;
40 * <p>Constructor for AnnotatedLineEditorReconcilingStrategy.</p>
42 * @param sourceViewer a {@link org.eclipse.jface.text.source.ISourceViewer} object.
44 public AnnotatedLineEditorReconcilingStrategy(ISourceViewer sourceViewer
) {
45 this.viewer
= sourceViewer
;
49 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
52 public void reconcile(IRegion partition
) {
57 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion, org.eclipse.jface.text.IRegion)
60 public void reconcile(DirtyRegion dirtyRegion
, IRegion subRegion
) {
62 doReconcile(dirtyRegion
);
63 } catch (BadLocationException e
) {
64 // TODO Auto-generated catch block
69 synchronized private void doReconcile(DirtyRegion dirtyRegion
) throws BadLocationException
{
70 // Get all affected lines
71 int dirtyOffset
= dirtyRegion
.getOffset();
72 int dirtyLength
= dirtyRegion
.getLength();
74 int lineChangeStart
= document
.getLineOfOffset(dirtyOffset
);
76 if (DirtyRegion
.INSERT
.equals(dirtyRegion
.getType())) {
77 lineChangeEnd
= document
.getLineOfOffset(dirtyOffset
+ dirtyLength
);
79 lineChangeEnd
= lineChangeStart
;
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());
88 List
<LineAnnotation
> annotationsCurrentLine
=
89 ((LineAnnotationModel
) getAnnotationModel()).getUndeletedAnnotations(lineOffset
, lineLength
);
91 if (annotationsCurrentLine
.size() == 0) {
92 logger
.debug("Adding new annotation " + lineText
);
93 addAnnotation(lineText
, lineRegion
);
95 LineAnnotation currentAnnotation
= annotationsCurrentLine
.get(0);
96 int length
= lineLength
;
97 getAnnotationModel().getPosition(currentAnnotation
).setLength(length
);
98 currentAnnotation
.setText(lineText
);
100 for (int j
= 1; j
< annotationsCurrentLine
.size(); j
++) {
101 annotationsCurrentLine
.get(j
).markAsMerged(currentAnnotation
.getEntity());
102 getAnnotationModel().removeAnnotation(annotationsCurrentLine
.get(j
));
107 // Deletes have already taken place
109 ((LineAnnotationModel
) getAnnotationModel()).printAnnotations();
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
);
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);
122 private IAnnotationModel
getAnnotationModel() {
123 return viewer
.getAnnotationModel();
127 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
130 public void setDocument(IDocument document
) {
131 this.document
= document
;