JAL-2780 JAL-2781 JAL-2902 refactor ‘OK’ action to optionally blocking method for...
authorJim Procter <jprocter@issues.jalview.org>
Wed, 7 Feb 2018 12:55:30 +0000 (12:55 +0000)
committerJim Procter <jprocter@issues.jalview.org>
Wed, 7 Feb 2018 12:56:25 +0000 (12:56 +0000)
src/jalview/gui/StructureChooser.java

index 6e73724..5de3082 100644 (file)
@@ -21,6 +21,7 @@
 
 package jalview.gui;
 
+import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Jalview;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.DBRefSource;
@@ -728,31 +729,43 @@ public class StructureChooser extends GStructureChooser
 
   }
 
-  public void selectStructure(String...pdbids)
+  /**
+   * select structures for viewing by their PDB IDs
+   * 
+   * @param pdbids
+   * @return true if structures were found and marked as selected
+   */
+  public boolean selectStructure(String... pdbids)
   {
+    boolean found = false;
+
     FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
             .getSelectedItem());
     String currentView = selectedFilterOpt.getView();
     JTable restable = (currentView == VIEWS_FILTER) ? getResultTable()
-            : tbl_local_pdb;
+            : (currentView == VIEWS_LOCAL_PDB) ? tbl_local_pdb : null;
 
-    if (currentView == VIEWS_FILTER)
+    if (restable == null)
     {
-     
-      int pdbIdColIndex = restable.getColumn("PDB Id")
-              .getModelIndex();
-      for (int r = 0; r < restable.getRowCount(); r++)
+      // can't select (enter PDB ID, or load file - need to also select which
+      // sequence to associate with)
+      return false;
+    }
+
+    int pdbIdColIndex = restable.getColumn("PDB Id").getModelIndex();
+    for (int r = 0; r < restable.getRowCount(); r++)
+    {
+      for (int p = 0; p < pdbids.length; p++)
       {
-        for (int p=0;p<pdbids.length;p++)
-        {
-          if (String.valueOf(restable.getValueAt(r, pdbIdColIndex))
-                  .equalsIgnoreCase(pdbids[p]))
+        if (String.valueOf(restable.getValueAt(r, pdbIdColIndex))
+                .equalsIgnoreCase(pdbids[p]))
         {
-            restable.setRowSelectionInterval(r, r);
-        }
+          restable.setRowSelectionInterval(r, r);
+          found = true;
         }
       }
     }
+    return found;
   }
   /**
    * Handles action event for btn_ok
@@ -760,11 +773,22 @@ public class StructureChooser extends GStructureChooser
   @Override
   public void ok_ActionPerformed()
   {
+    showStructures(false);
+  }
+
+  /**
+   * structure viewer opened by this dialog, or null
+   */
+  private StructureViewer sViewer = null;
+
+  public void showStructures(boolean waitUntilFinished)
+  {
+
     final StructureSelectionManager ssm = ap.getStructureSelectionManager();
 
     final int preferredHeight = pnl_filter.getHeight();
 
-    new Thread(new Runnable()
+    Runnable viewStruc = new Runnable()
     {
       @Override
       public void run()
@@ -810,7 +834,8 @@ public class StructureChooser extends GStructureChooser
           }
           SequenceI[] selectedSeqs = selectedSeqsToView
                   .toArray(new SequenceI[selectedSeqsToView.size()]);
-          launchStructureViewer(ssm, pdbEntriesToView, ap, selectedSeqs);
+          sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
+                  selectedSeqs);
         }
         else if (currentView == VIEWS_LOCAL_PDB)
         {
@@ -833,7 +858,8 @@ public class StructureChooser extends GStructureChooser
           }
           SequenceI[] selectedSeqs = selectedSeqsToView
                   .toArray(new SequenceI[selectedSeqsToView.size()]);
-          launchStructureViewer(ssm, pdbEntriesToView, ap, selectedSeqs);
+          sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
+                  selectedSeqs);
         }
         else if (currentView == VIEWS_ENTER_ID)
         {
@@ -862,7 +888,7 @@ public class StructureChooser extends GStructureChooser
           }
 
           PDBEntry[] pdbEntriesToView = new PDBEntry[] { pdbEntry };
-          launchStructureViewer(ssm, pdbEntriesToView, ap,
+          sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
                   new SequenceI[]
                   { selectedSequence });
         }
@@ -879,14 +905,33 @@ public class StructureChooser extends GStructureChooser
                           DataSourceType.FILE, selectedSequence, true,
                           Desktop.instance);
 
-          launchStructureViewer(ssm, new PDBEntry[] { fileEntry }, ap,
+          sViewer = launchStructureViewer(
+                  ssm, new PDBEntry[]
+                  { fileEntry }, ap,
                   new SequenceI[]
                   { selectedSequence });
         }
         closeAction(preferredHeight);
         mainFrame.dispose();
       }
-    }).start();
+    };
+    Thread runner = new Thread(viewStruc);
+    runner.start();
+    if (waitUntilFinished)
+    {
+      while (sViewer == null ? runner.isAlive()
+              : (sViewer.sview == null ? true
+                      : !sViewer.sview.hasMapping()))
+      {
+        try
+        {
+          Thread.sleep(300);
+        } catch (InterruptedException ie)
+        {
+
+        }
+      }
+    }
   }
 
   private PDBEntry getFindEntry(String id, Vector<PDBEntry> pdbEntries)
@@ -904,7 +949,8 @@ public class StructureChooser extends GStructureChooser
     return foundEntry;
   }
 
-  private void launchStructureViewer(StructureSelectionManager ssm,
+  private StructureViewer launchStructureViewer(
+          StructureSelectionManager ssm,
           final PDBEntry[] pdbEntriesToView,
           final AlignmentPanel alignPanel, SequenceI[] sequences)
   {
@@ -978,6 +1024,7 @@ public class StructureChooser extends GStructureChooser
       sViewer.viewStructures(pdbEntriesToView[0], sequences, alignPanel);
     }
     setProgressBar(null, progressId);
+    return sViewer;
   }
 
   /**
@@ -1208,4 +1255,9 @@ public class StructureChooser extends GStructureChooser
   {
     return progressBar.operationInProgress();
   }
+
+  public JalviewStructureDisplayI getOpenedStructureViewer()
+  {
+    return sViewer == null ? null : sViewer.sview;
+  }
 }