Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / src / jalview / gui / StructureChooser.java
index 07eec2b..4fb9e22 100644 (file)
@@ -35,6 +35,7 @@ import java.util.concurrent.Executors;
 import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JLabel;
+import javax.swing.JMenu;
 import javax.swing.JMenuItem;
 import javax.swing.JPopupMenu;
 import javax.swing.JTable;
@@ -79,13 +80,12 @@ import jalview.ws.sifts.SiftsSettings;
 public class StructureChooser extends GStructureChooser
         implements IProgressIndicator
 {
-  private static final String AUTOSUPERIMPOSE = "AUTOSUPERIMPOSE";
+  protected static final String AUTOSUPERIMPOSE = "AUTOSUPERIMPOSE";
 
   /**
    * warn user if need to fetch more than this many uniprot records at once
    */
   private static final int THRESHOLD_WARN_UNIPROT_FETCH_NEEDED = 20;
-
   private SequenceI selectedSequence;
 
   private SequenceI[] selectedSequences;
@@ -116,7 +116,7 @@ public class StructureChooser extends GStructureChooser
 
   List<SequenceI> seqsWithoutSourceDBRef = null;
 
-  private static StructureViewer lastTargetedView = null;
+  protected static StructureViewer lastTargetedView = null;
 
   public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq,
           AlignmentPanel ap)
@@ -277,7 +277,7 @@ public class StructureChooser extends GStructureChooser
         previousWantedFields = null;
         lastSelected = (FilterOption) cmb_filterOption.getSelectedItem();
         cmb_filterOption.setSelectedItem(null);
-        cachedPDBExists = false; // reset to initial
+        cachedPDBExists=false; // reset to initial
         initialStructureDiscovery();
         if (!isStructuresDiscovered())
         {
@@ -303,7 +303,7 @@ public class StructureChooser extends GStructureChooser
 
     final FetchFinishedListenerI afterDbRefFetch = new FetchFinishedListenerI()
     {
-
+      
       @Override
       public void finished()
       {
@@ -311,12 +311,12 @@ public class StructureChooser extends GStructureChooser
         notQueriedTDBYet = false;
         // new thread to discover structures - via 3d beacons
         Executors.defaultThreadFactory().newThread(strucDiscovery).start();
-
+        
       }
     };
-
+    
     // fetch db refs if OK pressed
-    final Runnable discoverCanonicalDBrefs = new Runnable()
+    final Runnable discoverCanonicalDBrefs = new Runnable() 
     {
       @Override
       public void run()
@@ -334,7 +334,6 @@ public class StructureChooser extends GStructureChooser
                   { new jalview.ws.dbsources.Uniprot() }, null, false);
           dbRefFetcher.addListener(afterDbRefFetch);
           // ideally this would also gracefully run with callbacks
-
           dbRefFetcher.fetchDBRefs(true);
         }
         else
@@ -361,13 +360,11 @@ public class StructureChooser extends GStructureChooser
     Console.debug("Using Uniprot fetch threshold of " + threshold);
     if (ignoreGui || seqsWithoutSourceDBRef.size() < threshold)
     {
-      Executors.defaultThreadFactory().newThread(discoverCanonicalDBrefs)
-              .start();
+      Executors.defaultThreadFactory().newThread(discoverCanonicalDBrefs).start();
       return;
     }
     // need cancel and no to result in the discoverPDB action - mocked is
     // 'cancel' TODO: mock should be OK
-
     StructureChooser thisSC = this;
     JvOptionPane.newOptionDialog(thisSC.getFrame())
             .setResponseHandler(JvOptionPane.OK_OPTION,
@@ -378,7 +375,8 @@ public class StructureChooser extends GStructureChooser
                     MessageManager.formatMessage(
                             "label.fetch_references_for_3dbeacons",
                             seqsWithoutSourceDBRef.size()),
-                    MessageManager.getString("label.3dbeacons"),
+                    MessageManager
+                            .getString("label.3dbeacons"),
                     JvOptionPane.YES_NO_OPTION, JvOptionPane.PLAIN_MESSAGE,
                     null, new Object[]
                     { MessageManager.getString("action.ok"),
@@ -393,7 +391,7 @@ public class StructureChooser extends GStructureChooser
    */
   private void discoverStructureViews()
   {
-    if (Desktop.instance != null)
+    if (Desktop.getInstance() != null)
     {
       targetView.removeAllItems();
       if (lastTargetedView != null && !lastTargetedView.isVisible())
@@ -401,7 +399,7 @@ public class StructureChooser extends GStructureChooser
         lastTargetedView = null;
       }
       int linkedViewsAt = 0;
-      for (StructureViewerBase view : Desktop.instance
+      for (StructureViewerBase view : Desktop.getInstance()
               .getStructureViewers(null, null))
       {
         StructureViewer viewHandler = (lastTargetedView != null
@@ -752,7 +750,7 @@ public class StructureChooser extends GStructureChooser
   {
     FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
             .getSelectedItem());
-
+    
     if (lastSelected == selectedFilterOpt)
     {
       // don't need to do anything, probably
@@ -767,7 +765,7 @@ public class StructureChooser extends GStructureChooser
     String filterTitle = mainFrame.getTitle();
     mainFrame.setTitle(frameTitle);
     chk_invertFilter.setVisible(false);
-
+    
     if (selectedFilterOpt.getView() == VIEWS_FILTER)
     {
       mainFrame.setTitle(filterTitle);
@@ -858,11 +856,9 @@ public class StructureChooser extends GStructureChooser
     FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
             .getSelectedItem());
     String currentView = selectedFilterOpt.getView();
-
     if (currentView == VIEWS_FILTER
             && data instanceof ThreeDBStructureChooserQuerySource)
     {
-
       TDB_FTSData row = ((ThreeDBStructureChooserQuerySource) data)
               .getFTSDataFor(getResultTable(), selectedRow,
                       discoveredStructuresSet);
@@ -891,7 +887,6 @@ public class StructureChooser extends GStructureChooser
     // event not handled by us
     return false;
   }
-
   /**
    * Validates inputs from the Manual PDB entry panel
    */
@@ -963,9 +958,7 @@ public class StructureChooser extends GStructureChooser
   {
     validateSelections();
   }
-
-  private FilterOption lastSelected = null;
-
+  private FilterOption lastSelected=null;
   /**
    * Handles the state change event for the 'filter' combo-box and 'invert'
    * check-box
@@ -1054,7 +1047,8 @@ public class StructureChooser extends GStructureChooser
   {
 
     final StructureSelectionManager ssm = ap.getStructureSelectionManager();
-
+    final StructureViewer theViewer = getTargetedStructureViewer(ssm);
+    boolean superimpose = chk_superpose.isSelected(); 
     final int preferredHeight = pnl_filter.getHeight();
 
     Runnable viewStruc = new Runnable()
@@ -1078,8 +1072,8 @@ public class StructureChooser extends GStructureChooser
 
           SequenceI[] selectedSeqs = selectedSeqsToView
                   .toArray(new SequenceI[selectedSeqsToView.size()]);
-          sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
-                  selectedSeqs);
+          sViewer = StructureViewer.launchStructureViewer(ap, pdbEntriesToView,
+                  selectedSeqs, superimpose, theViewer, progressBar);
         }
         else if (currentView == VIEWS_LOCAL_PDB)
         {
@@ -1103,8 +1097,8 @@ public class StructureChooser extends GStructureChooser
           }
           SequenceI[] selectedSeqs = selectedSeqsToView
                   .toArray(new SequenceI[selectedSeqsToView.size()]);
-          sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
-                  selectedSeqs);
+          sViewer = StructureViewer.launchStructureViewer(ap, pdbEntriesToView,
+                  selectedSeqs, superimpose, theViewer, progressBar);
         }
         else if (currentView == VIEWS_ENTER_ID)
         {
@@ -1122,8 +1116,7 @@ public class StructureChooser extends GStructureChooser
             if (pdbIdStr.split(":").length > 1)
             {
               pdbEntry.setId(pdbIdStr.split(":")[0]);
-              pdbEntry.setChainCode(
-                      pdbIdStr.split(":")[1].toUpperCase(Locale.ROOT));
+              pdbEntry.setChainCode(pdbIdStr.split(":")[1].toUpperCase(Locale.ROOT));
             }
             else
             {
@@ -1134,9 +1127,10 @@ public class StructureChooser extends GStructureChooser
           }
 
           PDBEntry[] pdbEntriesToView = new PDBEntry[] { pdbEntry };
-          sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
-                  new SequenceI[]
-                  { selectedSequence });
+          sViewer = StructureViewer.launchStructureViewer(ap, pdbEntriesToView,
+                   new SequenceI[]
+                  { selectedSequence }, superimpose, theViewer,
+                  progressBar);
         }
         else if (currentView == VIEWS_FROM_FILE)
         {
@@ -1146,14 +1140,12 @@ public class StructureChooser extends GStructureChooser
           {
             selectedSequence = userSelectedSeq;
           }
-          PDBEntry fileEntry = new AssociatePdbFileWithSeq()
-                  .associatePdbWithSeq(selectedPdbFileName,
-                          DataSourceType.FILE, selectedSequence, true,
-                          Desktop.instance);
-
-          sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry },
-                  ap, new SequenceI[]
-                  { selectedSequence });
+          PDBEntry fileEntry =  AssociatePdbFileWithSeq.associatePdbWithSeq(selectedPdbFileName,
+                          DataSourceType.FILE, selectedSequence, true);
+
+          sViewer = StructureViewer.launchStructureViewer(ap,  new PDBEntry[] { fileEntry },
+                 new SequenceI[] { selectedSequence }, superimpose, theViewer,
+                  progressBar);
         }
         SwingUtilities.invokeLater(new Runnable()
         {
@@ -1200,98 +1192,6 @@ public class StructureChooser extends GStructureChooser
   }
 
   /**
-   * Adds PDB structures to a new or existing structure viewer
-   * 
-   * @param ssm
-   * @param pdbEntriesToView
-   * @param alignPanel
-   * @param sequences
-   * @return
-   */
-  private StructureViewer launchStructureViewer(
-          StructureSelectionManager ssm, final PDBEntry[] pdbEntriesToView,
-          final AlignmentPanel alignPanel, SequenceI[] sequences)
-  {
-    long progressId = sequences.hashCode();
-    setProgressBar(MessageManager
-            .getString("status.launching_3d_structure_viewer"), progressId);
-    final StructureViewer theViewer = getTargetedStructureViewer(ssm);
-    boolean superimpose = chk_superpose.isSelected();
-    theViewer.setSuperpose(superimpose);
-
-    /*
-     * remember user's choice of superimpose or not
-     */
-    Cache.setProperty(AUTOSUPERIMPOSE,
-            Boolean.valueOf(superimpose).toString());
-
-    setProgressBar(null, progressId);
-    if (SiftsSettings.isMapWithSifts())
-    {
-      List<SequenceI> seqsWithoutSourceDBRef = new ArrayList<>();
-      int p = 0;
-      // TODO: skip PDBEntry:Sequence pairs where PDBEntry doesn't look like a
-      // real PDB ID. For moment, we can also safely do this if there is already
-      // a known mapping between the PDBEntry and the sequence.
-      for (SequenceI seq : sequences)
-      {
-        PDBEntry pdbe = pdbEntriesToView[p++];
-        if (pdbe != null && pdbe.getFile() != null)
-        {
-          StructureMapping[] smm = ssm.getMapping(pdbe.getFile());
-          if (smm != null && smm.length > 0)
-          {
-            for (StructureMapping sm : smm)
-            {
-              if (sm.getSequence() == seq)
-              {
-                continue;
-              }
-            }
-          }
-        }
-        if (seq.getPrimaryDBRefs().isEmpty())
-        {
-          seqsWithoutSourceDBRef.add(seq);
-          continue;
-        }
-      }
-      if (!seqsWithoutSourceDBRef.isEmpty())
-      {
-        int y = seqsWithoutSourceDBRef.size();
-        setProgressBar(MessageManager.formatMessage(
-                "status.fetching_dbrefs_for_sequences_without_valid_refs",
-                y), progressId);
-        SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef
-                .toArray(new SequenceI[y]);
-        DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef);
-        dbRefFetcher.fetchDBRefs(true);
-
-        setProgressBar("Fetch complete.", progressId); // todo i18n
-      }
-    }
-    if (pdbEntriesToView.length > 1)
-    {
-      setProgressBar(
-              MessageManager.getString(
-                      "status.fetching_3d_structures_for_selected_entries"),
-              progressId);
-      theViewer.viewStructures(pdbEntriesToView, sequences, alignPanel);
-    }
-    else
-    {
-      setProgressBar(MessageManager.formatMessage(
-              "status.fetching_3d_structures_for",
-              pdbEntriesToView[0].getId()), progressId);
-      theViewer.viewStructures(pdbEntriesToView[0], sequences, alignPanel);
-    }
-    setProgressBar(null, progressId);
-    // remember the last viewer we used...
-    lastTargetedView = theViewer;
-    return theViewer;
-  }
-
-  /**
    * Populates the combo-box used in associating manually fetched structures to
    * a unique sequence when more than one sequence selection is made.
    */
@@ -1526,6 +1426,12 @@ public class StructureChooser extends GStructureChooser
   {
     progressBar.setProgressBar(message, id);
   }
+  
+  @Override
+  public void removeProgressBar(long id)
+  {
+    progressBar.removeProgressBar(id);
+  }
 
   @Override
   public void registerHandler(long id, IProgressIndicatorHandler handler)
@@ -1562,7 +1468,6 @@ public class StructureChooser extends GStructureChooser
             && mainFrame.isVisible()
             && cmb_filterOption.getSelectedItem() != null;
   }
-
   /**
    * 
    * @return true if the 3D-Beacons query button will/has been displayed