refactor to allow distinct StructureSelectionManager instances for
[jalview.git] / src / jalview / structure / StructureSelectionManager.java
index 502c68e..d1bccd8 100644 (file)
@@ -22,11 +22,13 @@ import java.util.*;
 
 import MCview.*;
 import jalview.analysis.*;
+import jalview.api.AlignmentViewPanel;
+import jalview.api.StructureSelectionManagerProvider;
 import jalview.datamodel.*;
 
 public class StructureSelectionManager
 {
-  static StructureSelectionManager instance;
+  static IdentityHashMap<StructureSelectionManagerProvider,StructureSelectionManager> instances;
 
   StructureMapping[] mappings;
 
@@ -47,13 +49,17 @@ public class StructureSelectionManager
   }\r
   Hashtable mappingData = new Hashtable();
 
-  public static StructureSelectionManager getStructureSelectionManager()
+  public static StructureSelectionManager getStructureSelectionManager(StructureSelectionManagerProvider context)
   {
-    if (instance == null)
+    if (instances == null)
     {
-      instance = new StructureSelectionManager();
+      instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider,StructureSelectionManager>();
+    }
+    StructureSelectionManager instance=instances.get(context);
+    if (instance==null)
+    {
+      instances.put(context, instance=new StructureSelectionManager());
     }
-
     return instance;
   }
 
@@ -374,7 +380,7 @@ public class StructureSelectionManager
         }
       }
     }
-    if (results.getSize() > 0)
+    if (results!=null)
     {
       for (int i = 0; i < listeners.size(); i++)
       {
@@ -675,7 +681,7 @@ public class StructureSelectionManager
     modifySeqMappingList(true, codonFrames);
   }
 
-  Vector sel_listeners = new Vector();
+  Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
 
   public void addSelectionListener(SelectionListener selecter)
   {
@@ -712,4 +718,67 @@ public class StructureSelectionManager
       }
     }
   }
+  
+  Vector<AlignmentViewPanelListener> view_listeners=new Vector<AlignmentViewPanelListener>();
+  public synchronized void sendViewPosition(jalview.api.AlignmentViewPanel source, int startRes,
+          int endRes, int startSeq, int endSeq)
+  {
+
+    if (view_listeners != null && view_listeners.size() > 0)
+    {
+      Enumeration<AlignmentViewPanelListener> listeners = view_listeners.elements();
+      while (listeners.hasMoreElements())
+      {
+        AlignmentViewPanelListener slis = listeners
+                .nextElement();
+        if (slis != source)
+        {
+          slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
+        }
+        ;
+      }
+    }
+  }
+  
+
+  public void finalize() throws Throwable {
+    if (listeners!=null) {
+      listeners.clear();
+      listeners=null;
+    }
+    if (mappingData!=null)
+    {
+      mappingData.clear();
+      mappingData=null;
+    }
+    if (sel_listeners!=null)
+    {
+      sel_listeners.clear();
+      sel_listeners=null;
+    }
+    if (view_listeners!=null)
+    {
+      view_listeners.clear();
+      view_listeners=null;
+    }
+    mappings=null;
+    seqmappingrefs=null;
+  }
+
+  /**
+   * release all references associated with this manager provider
+   * @param jalviewLite
+   */
+  public static void release(StructureSelectionManagerProvider jalviewLite)
+  {
+    StructureSelectionManager mnger=(instances.get(jalviewLite));
+    if (mnger!=null)
+    {
+      instances.remove(jalviewLite);
+      try {
+        mnger.finalize();
+      } catch (Throwable x){};
+    }
+  }
+
 }