Revision: 17179
          http://sourceforge.net/p/gate/code/17179
Author:   markagreenwood
Date:     2013-12-18 12:51:20 +0000 (Wed, 18 Dec 2013)
Log Message:
-----------
removed the clear events and replaced with standard delete relation events

Modified Paths:
--------------
    gate/trunk/src/main/gate/event/RelationSetEvent.java
    gate/trunk/src/main/gate/event/RelationSetListener.java
    gate/trunk/src/main/gate/gui/docview/RelationSetView.java
    gate/trunk/src/main/gate/relations/RelationSet.java

Modified: gate/trunk/src/main/gate/event/RelationSetEvent.java
===================================================================
--- gate/trunk/src/main/gate/event/RelationSetEvent.java        2013-12-18 
10:51:17 UTC (rev 17178)
+++ gate/trunk/src/main/gate/event/RelationSetEvent.java        2013-12-18 
12:51:20 UTC (rev 17179)
@@ -42,7 +42,7 @@
     super(source, type);
     this.relation = relation;
   }
-
+  
   /**
    * Gets the document that has had an annotation added or removed.
    * @return a {@link gate.Document}

Modified: gate/trunk/src/main/gate/event/RelationSetListener.java
===================================================================
--- gate/trunk/src/main/gate/event/RelationSetListener.java     2013-12-18 
10:51:17 UTC (rev 17178)
+++ gate/trunk/src/main/gate/event/RelationSetListener.java     2013-12-18 
12:51:20 UTC (rev 17179)
@@ -14,5 +14,4 @@
   /**Called when an {@link gate.relations.Relation} has been removed*/
   public void relationRemoved(RelationSetEvent e);
 
-
 }
\ No newline at end of file

Modified: gate/trunk/src/main/gate/gui/docview/RelationSetView.java
===================================================================
--- gate/trunk/src/main/gate/gui/docview/RelationSetView.java   2013-12-18 
10:51:17 UTC (rev 17178)
+++ gate/trunk/src/main/gate/gui/docview/RelationSetView.java   2013-12-18 
12:51:20 UTC (rev 17179)
@@ -13,11 +13,18 @@
  */
 package gate.gui.docview;
 
+import gate.AnnotationSet;
 import gate.Document;
 import gate.Resource;
 import gate.creole.AbstractVisualResource;
 import gate.creole.metadata.CreoleResource;
 import gate.creole.metadata.GuiType;
+import gate.event.AnnotationSetEvent;
+import gate.event.AnnotationSetListener;
+import gate.event.DocumentEvent;
+import gate.event.DocumentListener;
+import gate.event.RelationSetEvent;
+import gate.event.RelationSetListener;
 import gate.gui.MainFrame;
 import gate.relations.RelationSet;
 
@@ -31,12 +38,16 @@
 import javax.swing.JToolBar;
 
 @CreoleResource(name = "Relation Viewer", guiType = GuiType.LARGE, 
resourceDisplayed = "gate.Document")
-public class RelationSetView extends AbstractVisualResource {
+public class RelationSetView extends AbstractVisualResource
+                                                           implements
+                                                           DocumentListener,
+                                                           
AnnotationSetListener,
+                                                           RelationSetListener 
{
 
   private static final long serialVersionUID = 2976754146115707386L;
 
   private JTextPane text = new JTextPane();
-  
+
   private Document doc = null;
 
   @Override
@@ -46,36 +57,39 @@
     text.setEditable(false);
 
     add(text, BorderLayout.CENTER);
-    
-    JButton btnRefresh = new JButton("Refresh",MainFrame.getIcon("Refresh"));
+
+    JButton btnRefresh = new JButton("Refresh", MainFrame.getIcon("Refresh"));
     btnRefresh.addActionListener(new ActionListener() {
-      
+
       @Override
       public void actionPerformed(ActionEvent arg0) {
         refresh();
       }
     });
-    
+
     JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
     toolbar.setFloatable(false);
-    
+
     toolbar.add(btnRefresh);
     toolbar.addSeparator();
     toolbar.add(new JLabel("Currently this view is not automatically 
updated"));
-    
-    add(toolbar, BorderLayout.NORTH);
 
+    // not currently need as we now support the listners properly
+    // add(toolbar, BorderLayout.NORTH);
+
     return this;
   }
-  
+
   private void refresh() {
+    if(doc == null) return;
+
     StringBuilder builder = new StringBuilder();
-    
+
     RelationSet relations = doc.getAnnotations().getRelations();
     if(relations.size() > 0) {
       builder.append(relations).append("\n\n");
     }
-    
+
     for(String name : doc.getAnnotationSetNames()) {
       relations = doc.getAnnotations(name).getRelations();
       if(relations.size() > 0) {
@@ -87,8 +101,69 @@
   }
 
   @Override
-  public void setTarget(Object target) {    
+  public void setTarget(Object target) {
+    if(doc != null) {
+      doc.removeDocumentListener(this);
+      doc.getAnnotations().removeAnnotationSetListener(this);
+      doc.getAnnotations().getRelations().removeRelationSetListener(this);
+      for(String name : doc.getAnnotationSetNames()) {
+        AnnotationSet as = doc.getAnnotations(name);
+        as.removeAnnotationSetListener(this);
+        as.getRelations().removeRelationSetListener(this);
+      }
+    }
+
     doc = (Document)target;
+    doc.addDocumentListener(this);
+    doc.getAnnotations().addAnnotationSetListener(this);
+    doc.getAnnotations().getRelations().addRelationSetListener(this);
+
+    for(String name : doc.getAnnotationSetNames()) {
+      AnnotationSet as = doc.getAnnotations(name);
+      as.addAnnotationSetListener(this);
+      as.getRelations().addRelationSetListener(this);
+    }
+
     refresh();
   }
+
+  @Override
+  public void annotationSetAdded(DocumentEvent e) {
+    
doc.getAnnotations(e.getAnnotationSetName()).addAnnotationSetListener(this);
+    refresh();
+  }
+
+  @Override
+  public void annotationSetRemoved(DocumentEvent e) {
+    doc.getAnnotations(e.getAnnotationSetName()).removeAnnotationSetListener(
+            this);
+    refresh();
+  }
+
+  @Override
+  public void contentEdited(DocumentEvent e) {
+    // we don't care about changes in content, as hopefully any changes
+    // that cause annotations to be deleted will be handled through the
+    // appropriate handlers
+  }
+
+  @Override
+  public void annotationAdded(AnnotationSetEvent e) {
+    refresh();
+  }
+
+  @Override
+  public void annotationRemoved(AnnotationSetEvent e) {
+    refresh();
+  }
+
+  @Override
+  public void relationAdded(RelationSetEvent e) {
+    refresh();
+  }
+
+  @Override
+  public void relationRemoved(RelationSetEvent e) {
+    refresh();
+  }
 }

Modified: gate/trunk/src/main/gate/relations/RelationSet.java
===================================================================
--- gate/trunk/src/main/gate/relations/RelationSet.java 2013-12-18 10:51:17 UTC 
(rev 17178)
+++ gate/trunk/src/main/gate/relations/RelationSet.java 2013-12-18 12:51:20 UTC 
(rev 17179)
@@ -124,6 +124,17 @@
    * Empties the relation set
    */
   public void clear() {
+
+    // clearing the indexes won't fire the events so we fire the events
+    // first and then clear the indexes, hopefully we won't end up with
+    // any weird side effects from this but I can't think of a
+    // safer/better way of doing it other than calling delete on each
+    // relation which would be horribly inefficient
+    for(Relation r : indexById.values()) {
+      fireRelationRemoved(new RelationSetEvent(this,
+              RelationSetEvent.RELATION_REMOVED, r));
+    }
+
     indexByType.clear();
     indexesByMember.clear();
     indexById.clear();
@@ -439,7 +450,7 @@
     }
   }
 
-  public synchronized void addAnnotationSetListener(RelationSetListener l) {
+  public synchronized void addRelationSetListener(RelationSetListener l) {
     @SuppressWarnings("unchecked")
     Vector<RelationSetListener> v =
             listeners == null

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to