Merge branch 'JAL-1139_proguard' into develop
[jalview.git] / src / jalview / ext / jmol / JalviewJmolBinding.java
index 025b970..f31272a 100644 (file)
@@ -65,6 +65,12 @@ public abstract class JalviewJmolBinding implements StructureListener,
    * time.
    */
   private boolean loadingFromArchive = false;
+  
+  /**
+   * second flag to indicate if the jmol viewer should ignore sequence colouring
+   * events from the structure manager because the GUI is still setting up
+   */
+  private boolean loadingFinished = true;
 
   /**
    * state flag used to check if the Jmol viewer's paint method can be called
@@ -317,9 +323,55 @@ public abstract class JalviewJmolBinding implements StructureListener,
   public void superposeStructures(AlignmentI[] _alignment,
           int[] _refStructure, ColumnSelection[] _hiddenCols)
   {
+    assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length);
+
     String[] files = getPdbFile();
+    // check to see if we are still waiting for Jmol files
+    long starttime=System.currentTimeMillis();
+    boolean waiting=true;
+    do {
+      waiting=false;
+      for (String file:files)
+      {
+        try {
+          // HACK - in Jalview 2.8 this call may not be threadsafe so we catch
+          // every possible exception
+          StructureMapping[] sm = ssm.getMapping(file);
+          if (sm == null || sm.length == 0)
+          {
+            waiting = true;
+          }
+        } catch (Exception x)
+        {
+          waiting = true;
+        } catch (Error q)
+        {
+          waiting = true;
+        }
+      }
+      // we wait around for a reasonable time before we give up
+    } while (waiting && System.currentTimeMillis()<(10000+1000*files.length+starttime));
+    if (waiting)
+    {
+      System.err.println("RUNTIME PROBLEM: Jmol seems to be taking a long time to process all the structures.");
+      return;
+    }
     StringBuffer selectioncom = new StringBuffer();
-    assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length);
+    // In principle - nSeconds specifies the speed of animation for each
+    // superposition - but is seems to behave weirdly, so we don't specify it.
+    String nSeconds = " ";
+    if (files.length > 10)
+    {
+      nSeconds = " 0.00001 ";
+    }
+    else
+    {
+      nSeconds = " " + (2.0 / files.length) + " ";
+      // if (nSeconds).substring(0,5)+" ";
+    }
+    // see JAL-1345 - should really automatically turn off the animation for
+    // large numbers of structures, but Jmol doesn't seem to allow that.
+    nSeconds = " ";
     // union of all aligned positions are collected together.
     for (int a = 0; a < _alignment.length; a++)
     {
@@ -364,8 +416,9 @@ public abstract class JalviewJmolBinding implements StructureListener,
         // RACE CONDITION - getMapping only returns Jmol loaded filenames once
         // Jmol callback has completed.
         if (mapping == null || mapping.length < 1)
-          continue;
-
+        {
+          throw new Error("Implementation error - Jmol seems to be still working on getting its data - report at http://issues.jalview.org/browse/JAL-1016");
+        }
         int lastPos = -1;
         for (int s = 0; s < sequence[pdbfnum].length; s++)
         {
@@ -430,6 +483,13 @@ public abstract class JalviewJmolBinding implements StructureListener,
           }
         }
       }
+      
+      // TODO: consider bailing if nmatched less than 4 because superposition
+      // not
+      // well defined.
+      // TODO: refactor superposable position search (above) from jmol selection
+      // construction (below)
+
       String[] selcom = new String[files.length];
       int nmatched = 0;
       // generate select statements to select regions to superimpose structures
@@ -495,11 +555,6 @@ public abstract class JalviewJmolBinding implements StructureListener,
           }
         }
       }
-      // TODO: consider bailing if nmatched less than 4 because superposition
-      // not
-      // well defined.
-      // TODO: refactor superposable position search (above) from jmol selection
-      // construction (below)
       for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
       {
         if (pdbfnum == refStructure)
@@ -511,7 +566,7 @@ public abstract class JalviewJmolBinding implements StructureListener,
         command.append(chainNames[pdbfnum]);
         command.append(") against reference (");
         command.append(chainNames[refStructure]);
-        command.append(")\";\ncompare ");
+        command.append(")\";\ncompare "+nSeconds);
         command.append("{");
         command.append(1 + pdbfnum);
         command.append(".1} {");
@@ -566,7 +621,7 @@ public abstract class JalviewJmolBinding implements StructureListener,
   public void colourBySequence(boolean showFeatures,
           jalview.api.AlignmentViewPanel alignmentv)
   {
-    if (!colourBySequence)
+    if (!colourBySequence || !loadingFinished)
       return;
     if (ssm == null)
     {
@@ -1438,10 +1493,24 @@ public abstract class JalviewJmolBinding implements StructureListener,
   {
     this.loadingFromArchive = loadingFromArchive;
   }
-
+  
+  /**
+   * 
+   * @return true if Jmol is still restoring state or loading is still going on (see setFinsihedLoadingFromArchive)
+   */
   public boolean isLoadingFromArchive()
   {
-    return loadingFromArchive;
+    return loadingFromArchive && !loadingFinished;
+  }
+
+  /**
+   * modify flag which controls if sequence colouring events are honoured by the binding. 
+   * Should be true for normal operation
+   * @param finishedLoading
+   */
+  public void setFinishedLoadingFromArchive(boolean finishedLoading)
+  {
+    loadingFinished = finishedLoading;
   }
 
   public void setBackgroundColour(java.awt.Color col)