1   package debugger.gui.resources;
2   
3   import debugger.resources.ResourcesFactory;
4   import gate.*;
5   import gate.event.CorpusEvent;
6   import gate.event.CorpusListener;
7   import gate.event.CreoleEvent;
8   import gate.event.CreoleListener;
9   
10  import java.util.Iterator;
11  
12  /**
13   * Copyright (c) Ontos AG (http://www.ontosearch.com).
14   * This class is part of JAPE Debugger component for
15   * GATE (Copyright (c) "The University of Sheffield" see http://gate.ac.uk/) <br>
16   * This class contains methods that update resources tree
17   * after user has loaded/unloaded/renamed language or processing
18   * resources in GATE.
19   * @author Oleg Mishchenko, Andrey Shafirin
20   */
21  
22  public class CreoleListenerImpl implements CreoleListener, CorpusListener {
23      private static CreoleListenerImpl ourInstance;
24      private static ResourceTree resourceTree;
25  
26      public synchronized static CreoleListenerImpl getInstance(ResourceTree rt) {
27          if (ourInstance == null) {
28              ourInstance = new CreoleListenerImpl();
29              resourceTree = rt;
30          }
31          return ourInstance;
32      }
33  
34      private CreoleListenerImpl() {
35          // we should add corpus listener to all corpuses already loaded in GATE
36          for (Iterator it = Gate.getCreoleRegister().getLrInstances().iterator(); it.hasNext();) {
37              LanguageResource lr = (LanguageResource) it.next();
38              if (lr instanceof Corpus) {
39                  ((Corpus) lr).addCorpusListener(this);
40              }
41          }
42      }
43  
44      // implementation of CreoleListener methods
45      public void resourceLoaded(CreoleEvent event) {
46          // we don't see document in the tree if it is not added to any corpus,
47          // so on the creation of a document nothing is done
48          if (event.getResource() instanceof ProcessingResource || event.getResource() instanceof Corpus) {
49              ResourcesFactory.updateRoots();
50              resourceTree.refresh();
51              if (event.getResource() instanceof Corpus) {
52                  ((Corpus) event.getResource()).addCorpusListener(this);
53              }
54          }
55      }
56  
57      public void resourceUnloaded(CreoleEvent event) {
58          if (event.getResource() instanceof ProcessingResource || event.getResource() instanceof LanguageResource) {
59              ResourcesFactory.updateRoots();
60              resourceTree.refresh();
61          }
62      }
63  
64      public void datastoreOpened(CreoleEvent event) {
65      }
66  
67      public void datastoreCreated(CreoleEvent event) {
68      }
69  
70      public void datastoreClosed(CreoleEvent event) {
71      }
72  
73      public void resourceRenamed(Resource resource, String s, String s1) {
74          if (resource instanceof ProcessingResource || resource instanceof LanguageResource) {
75              ResourcesFactory.updateRoots();
76              resourceTree.refresh();
77          }
78      }
79  
80      // implementation of CorpusListener methods
81      public void documentAdded(CorpusEvent event) {
82          ResourcesFactory.updateRoots();
83          resourceTree.refresh();
84      }
85  
86      public void documentRemoved(CorpusEvent event) {
87          ResourcesFactory.updateRoots();
88          resourceTree.refresh();
89      }
90  }
91  
92