JAL-2944 open selected structures in existing or a new view, and by default offer...
authorJim Procter <jprocter@issues.jalview.org>
Thu, 5 Apr 2018 13:31:56 +0000 (14:31 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 5 Apr 2018 13:31:56 +0000 (14:31 +0100)
src/jalview/gui/StructureChooser.java

index 198aa62..1c99f02 100644 (file)
@@ -125,6 +125,7 @@ public class StructureChooser extends GStructureChooser
         fetchStructuresMetaData();
         // revise filter options if no results were found
         populateFilterComboBox(isStructuresDiscovered(), cachedPDBExists);
+        discoverStructureViews();
         updateProgressIndicator(null, startTime);
         mainFrame.setVisible(true);
         updateCurrentView();
@@ -133,6 +134,47 @@ public class StructureChooser extends GStructureChooser
     discoverPDBStructuresThread.start();
   }
 
+  private void discoverStructureViews()
+  {
+    if (Desktop.instance != null)
+    {
+      targetView.removeAllItems();
+      targetView.addItem(
+              new StructureViewer(ap.getStructureSelectionManager()));
+      if (lastTargetedView != null && !lastTargetedView.isVisible())
+      {
+        lastTargetedView = null;
+      }
+      int linkedViewsAt = 1;
+      for (StructureViewerBase view : Desktop.instance
+              .getStructureViewers(null, null))
+      {
+        StructureViewer viewHandler = (lastTargetedView != null
+                && lastTargetedView.sview == view) ? lastTargetedView
+                        : StructureViewer.reconfigure(view);
+
+        if (view.isLinkedWith(ap))
+        {
+          targetView.insertItemAt(viewHandler,
+                  linkedViewsAt++);
+        }
+        else
+        {
+          targetView.addItem(viewHandler);
+        }
+      }
+      targetView.setVisible(targetView.getItemCount() > 1);
+
+      // finally, restore last targeted view by default.
+      if (lastTargetedView != null)
+      {
+        targetView.setSelectedItem(lastTargetedView);
+      }
+    }
+  }
+
+  private static StructureViewer lastTargetedView = null;
+
   /**
    * Updates the progress indicator with the specified message
    * 
@@ -957,6 +999,18 @@ public class StructureChooser extends GStructureChooser
     return foundEntry;
   }
 
+  public StructureViewer getTargetedStructureViewer(
+          StructureSelectionManager ssm)
+  {
+    Object _sv = targetView.getSelectedItem();
+
+    if (_sv == null)
+    {
+      return new StructureViewer(ssm);
+    } else {
+      return (StructureViewer) _sv;
+    }
+  }
   private StructureViewer launchStructureViewer(
           StructureSelectionManager ssm,
           final PDBEntry[] pdbEntriesToView,
@@ -965,9 +1019,9 @@ public class StructureChooser extends GStructureChooser
     long progressId = sequences.hashCode();
     setProgressBar(MessageManager
             .getString("status.launching_3d_structure_viewer"), progressId);
-    final StructureViewer sViewer = new StructureViewer(ssm);
-    setProgressBar(null, progressId);
+    final StructureViewer sViewer = getTargetedStructureViewer(ssm);
 
+    setProgressBar(null, progressId);
     if (SiftsSettings.isMapWithSifts())
     {
       List<SequenceI> seqsWithoutSourceDBRef = new ArrayList<>();
@@ -1032,6 +1086,8 @@ public class StructureChooser extends GStructureChooser
       sViewer.viewStructures(pdbEntriesToView[0], sequences, alignPanel);
     }
     setProgressBar(null, progressId);
+    // remember the last viewer we used...
+    lastTargetedView = sViewer;
     return sViewer;
   }