1   package debugger.resources.pr;
2   
3   import gate.Annotation;
4   import gate.AnnotationSet;
5   import gate.Document;
6   import gate.annotation.AnnotationSetImpl;
7   import gate.event.AnnotationSetEvent;
8   import gate.event.AnnotationSetListener;
9   
10  import java.util.ArrayList;
11  import java.util.Iterator;
12  
13  /**
14   * Copyright (c) Ontos AG (http://www.ontosearch.com).
15   * This class is part of JAPE Debugger component for
16   * GATE (Copyright (c) "The University of Sheffield" see http://gate.ac.uk/) <br>
17   * @author Andrey Shafirin
18   */
19  
20  public class RuleAnnotationHistory implements AnnotationSetListener {
21      private ArrayList annSetsByDocument;
22  
23      public RuleAnnotationHistory() {
24          this.annSetsByDocument = new ArrayList();
25      }
26  
27      public Iterator getAllAnnotationSets() {
28          return annSetsByDocument.iterator();
29      }
30  
31      public AnnotationSet getAnnotationSet(Document document) {
32          synchronized (annSetsByDocument) {
33              for (Iterator iterator = annSetsByDocument.iterator(); iterator.hasNext();) {
34                  AnnotationSet annotationSet = (AnnotationSet) iterator.next();
35                  if (annotationSet.getDocument().equals(document)) {
36                      return annotationSet;
37                  }
38              }
39              return null;
40          }
41      }
42  
43      public boolean addAnnotationSet(AnnotationSet annSet) {
44          if (null == annSet) {
45              return false;
46          }
47          AnnotationSet sourceAnnSet = getAnnotationSet(annSet.getDocument());
48          boolean flag = true;
49          if (null != sourceAnnSet) {
50              for (Iterator annItr = annSet.iterator(); annItr.hasNext();) {
51                  Annotation ann = (Annotation) annItr.next();
52                  if (!sourceAnnSet.add(ann)) {
53                      flag = false;
54                  }
55              }
56              return flag;
57          } else {
58              return annSetsByDocument.add(annSet);
59          }
60      }
61  
62      public boolean addAnnotation(Annotation annotation, Document document) {
63          if (null != getAnnotationSet(document)) {
64              return getAnnotationSet(document).add(annotation);
65          } else {
66              AnnotationSet annSet = new AnnotationSetImpl(document);
67              annSet.add(annotation);
68              return addAnnotationSet(annSet);
69          }
70      }
71  
72      public void annotationAdded(AnnotationSetEvent e) {
73      }
74  
75      public void annotationRemoved(AnnotationSetEvent e) {
76          AnnotationSet annSet = getAnnotationSet(e.getSourceDocument());
77          if (null != annSet) {
78              annSet.remove(e.getAnnotation());
79          }
80      }
81  }
82  
83