JAL-3875 fully reset filter combo-box when fetch 3d-beacons button is pressed, and...
[jalview.git] / src / jalview / gui / StructureChooser.java
index 9f9de6e..ce57373 100644 (file)
 
 package jalview.gui;
 
-import java.util.Locale;
-
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Locale;
 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;
 import javax.swing.SwingUtilities;
 import javax.swing.table.AbstractTableModel;
@@ -50,6 +54,7 @@ import jalview.fts.core.FTSDataColumnPreferences;
 import jalview.fts.core.FTSRestRequest;
 import jalview.fts.core.FTSRestResponse;
 import jalview.fts.service.pdb.PDBFTSRestClient;
+import jalview.fts.service.threedbeacons.TDB_FTSData;
 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
 import jalview.gui.structurechooser.StructureChooserQuerySource;
 import jalview.gui.structurechooser.ThreeDBStructureChooserQuerySource;
@@ -76,11 +81,6 @@ public class StructureChooser extends GStructureChooser
 {
   private static final String AUTOSUPERIMPOSE = "AUTOSUPERIMPOSE";
 
-  /**
-   * transient combo box choice for initiating 3db fetch
-   */
-  private static final String VIEWS_QUERYING_TDB = "QUERY_3DB";
-
   private SequenceI selectedSequence;
 
   private SequenceI[] selectedSequences;
@@ -168,7 +168,8 @@ public class StructureChooser extends GStructureChooser
       canQueryTDB = true;
       if (needCanonical)
       {
-        notQueriedTDBYet = false;
+        // triggers display of the 'Query TDB' button
+        notQueriedTDBYet = true;
       }
     }
   };
@@ -184,6 +185,15 @@ public class StructureChooser extends GStructureChooser
     }
 
     chk_superpose.setSelected(Cache.getDefault(AUTOSUPERIMPOSE, true));
+    btn_queryTDB.addActionListener(new ActionListener()
+    {
+
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        promptForTDBFetch(false);
+      }
+    });
 
     Executors.defaultThreadFactory().newThread(new Runnable()
     {
@@ -232,7 +242,11 @@ public class StructureChooser extends GStructureChooser
     updateCurrentView();
   }
 
-  private void promptForTDBFetch()
+  /**
+   * raises dialog for Uniprot fetch followed by 3D beacons search
+   * @param ignoreGui - when true, don't ask, just fetch 
+   */
+  public void promptForTDBFetch(boolean ignoreGui)
   {
     final long progressId = System.currentTimeMillis();
 
@@ -242,13 +256,30 @@ public class StructureChooser extends GStructureChooser
       @Override
       public void run()
       {
+        mainFrame.setEnabled(false);
+        cmb_filterOption.setEnabled(false);
+        progressBar.setProgressBar(MessageManager.getString("status.searching_3d_beacons"), progressId);
         // TODO: warn if no accessions discovered
         populateSeqsWithoutSourceDBRef();
         // redo initial discovery - this time with 3d beacons
         // Executors.
         previousWantedFields=null;
-        
+        lastSelected=(FilterOption) cmb_filterOption.getSelectedItem();
+        cmb_filterOption.setSelectedItem(null);
+        cachedPDBExists=false; // reset to initial
         initialStructureDiscovery();
+        if (!isStructuresDiscovered())
+        {
+          progressBar.setProgressBar(MessageManager.getString("status.no_structures_discovered_from_3d_beacons"), progressId);
+          btn_queryTDB.setToolTipText(MessageManager.getString("status.no_structures_discovered_from_3d_beacons"));
+          btn_queryTDB.setEnabled(false);
+        } else {
+          cmb_filterOption.setSelectedIndex(0); // select 'best'
+          btn_queryTDB.setVisible(false);
+          progressBar.setProgressBar(null, progressId);
+        }
+        mainFrame.setEnabled(true);
+        cmb_filterOption.setEnabled(true);
       }
     };
 
@@ -299,8 +330,13 @@ public class StructureChooser extends GStructureChooser
         }
       };
     };
+    if (ignoreGui)
+    {
+      Executors.defaultThreadFactory().newThread(discoverCanonicalDBrefs).start();
+      return;
+    }
     // need cancel and no to result in the discoverPDB action - mocked is
-    // 'cancel'
+    // 'cancel' TODO: mock should be OK
     JvOptionPane.newOptionDialog(this)
             .setResponseHandler(JvOptionPane.OK_OPTION,
                     discoverCanonicalDBrefs)
@@ -656,10 +692,7 @@ public class StructureChooser extends GStructureChooser
                     "-", VIEWS_FROM_FILE, false, null));
     if (canQueryTDB && notQueriedTDBYet)
     {
-      FilterOption queryTDBOption = new FilterOption(
-              MessageManager.getString("label.search_3dbeacons"), "-",
-              VIEWS_QUERYING_TDB, false, null);
-      cmb_filterOption.addItem(queryTDBOption);
+      btn_queryTDB.setVisible(true);
     }
 
     if (cachedPDBExist)
@@ -688,12 +721,6 @@ public class StructureChooser extends GStructureChooser
     FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
             .getSelectedItem());
     
-    // first check if we need to rebuild dialog
-    if (selectedFilterOpt.getView() == VIEWS_QUERYING_TDB)
-    {
-      promptForTDBFetch();
-      return;
-    }
     if (lastSelected == selectedFilterOpt)
     {
       // don't need to do anything, probably
@@ -793,6 +820,38 @@ public class StructureChooser extends GStructureChooser
             .setEnabled(selectedCount > 1 || targetView.getItemCount() > 0);
   }
 
+  @Override
+  protected boolean showPopupFor(int selectedRow, int x, int y)
+  {
+    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);
+      String pageUrl = row.getModelViewUrl(); 
+      JPopupMenu popup = new JPopupMenu("3D Beacons");
+      JMenuItem viewUrl = new JMenuItem("View model web page");
+      viewUrl.addActionListener(
+              new ActionListener() {
+                @Override
+                public void actionPerformed(ActionEvent e)
+                {
+                  Desktop.showUrl(pageUrl);
+                }
+              }
+              );
+      popup.add(viewUrl);
+      SwingUtilities.invokeLater(new Runnable()  {
+        public void run() { popup.show(getResultTable(), x, y); }
+      });
+      return true;
+    }
+    // event not handled by us
+    return false;
+  }
   /**
    * Validates inputs from the Manual PDB entry panel
    */
@@ -1458,4 +1517,16 @@ public class StructureChooser extends GStructureChooser
             && mainFrame.isVisible()
             && cmb_filterOption.getSelectedItem() != null;
   }
+  /**
+   * 
+   * @return true if the 3D-Beacons query button will/has been displayed
+   */
+  public boolean isCanQueryTDB() {
+         return canQueryTDB;
+  }
+
+  public boolean isNotQueriedTDBYet()
+  {
+    return notQueriedTDBYet;
+  }
 }