JAL-3739 (combined) commits from feature/JAL-3739_useFormFieldText
[jalview.git] / src / jalview / gui / StructureChooser.java
index ea2d50d..6bcac24 100644 (file)
@@ -38,8 +38,8 @@ import jalview.fts.service.pdb.PDBFTSRestClient;
 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
 import jalview.gui.structurechooser.StructureChooserQuerySource;
 import jalview.io.DataSourceType;
+import jalview.jbgui.FilterOption;
 import jalview.jbgui.GStructureChooser;
-import jalview.jbgui.GStructureChooser.FilterOption;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureSelectionManager;
 import jalview.util.MessageManager;
@@ -97,6 +97,8 @@ public class StructureChooser extends GStructureChooser
 
   private boolean cachedPDBExists;
 
+  private Collection<FTSData> lastDiscoveredStructuresSet;
+
   private static StructureViewer lastTargetedView = null;
 
   public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq,
@@ -104,7 +106,7 @@ public class StructureChooser extends GStructureChooser
   {
     // which FTS engine to use
     data = StructureChooserQuerySource
-            .getTDBfts();
+            .getQuerySourceFor(selectedSeqs);
     initDialog();
     
     this.ap = ap;
@@ -134,6 +136,14 @@ public class StructureChooser extends GStructureChooser
       @Override
       public void run()
       {
+        // looks for any existing structures already loaded 
+        // for the sequences (the cached ones) 
+        // then queries the StructureChooserQuerySource to 
+        // discover more structures.
+        // 
+        // Possible optimisation is to only begin querying
+        // the structure chooser if there are no cached structures.
+        
         long startTime = System.currentTimeMillis();
         updateProgressIndicator(MessageManager
                 .getString("status.loading_cached_pdb_entries"), startTime);
@@ -272,7 +282,9 @@ public class StructureChooser extends GStructureChooser
     {
       getResultTable()
               .setModel(data.getTableModel(discoveredStructuresSet));
+      
       noOfStructuresFound = discoveredStructuresSet.size();
+      lastDiscoveredStructuresSet=discoveredStructuresSet;
       mainFrame.setTitle(MessageManager.formatMessage(
               "label.structure_chooser_no_of_structures",
               noOfStructuresFound, totalTime));
@@ -328,6 +340,7 @@ public class StructureChooser extends GStructureChooser
   {
     Thread filterThread = new Thread(new Runnable()
     {
+
       @Override
       public void run()
       {
@@ -344,7 +357,7 @@ public class StructureChooser extends GStructureChooser
           FTSRestResponse resultList;
           try
           {
-            resultList = data.selectFirstRankedQuery(seq, wantedFields,
+            resultList = data.selectFirstRankedQuery(seq, discoveredStructuresSet,wantedFields,
                     fieldToFilterBy, !chk_invertFilter.isSelected());
 
           } catch (Exception e)
@@ -370,7 +383,7 @@ public class StructureChooser extends GStructureChooser
           reorderedStructuresSet.addAll(discoveredStructuresSet);
           getResultTable()
                   .setModel(data.getTableModel(reorderedStructuresSet));
-
+          
           FTSRestResponse.configureTableColumn(getResultTable(),
                   wantedFields, tempUserPrefs);
           getResultTable().getColumn("Ref Sequence").setPreferredWidth(120);
@@ -442,46 +455,57 @@ public class StructureChooser extends GStructureChooser
   protected void populateFilterComboBox(boolean haveData,
           boolean cachedPDBExist)
   {
+    populateFilterComboBox(haveData, cachedPDBExist, null);
+  }
+  /**
+   * Populates the filter combo-box options dynamically depending on discovered
+   * structures
+   */
+  protected void populateFilterComboBox(boolean haveData,
+          boolean cachedPDBExist, FilterOption lastSel)
+  {
+    
     /*
      * temporarily suspend the change listener behaviour
      */
     cmb_filterOption.removeItemListener(this);
-
+    int selSet=-1;
     cmb_filterOption.removeAllItems();
     if (haveData)
     {
-      cmb_filterOption.addItem(new FilterOption(
-              MessageManager.getString("label.best_quality"),
-              "overall_quality", VIEWS_FILTER, false));
-      cmb_filterOption.addItem(new FilterOption(
-              MessageManager.getString("label.best_resolution"),
-              "resolution", VIEWS_FILTER, false));
-      cmb_filterOption.addItem(new FilterOption(
-              MessageManager.getString("label.most_protein_chain"),
-              "number_of_protein_chains", VIEWS_FILTER, false));
-      cmb_filterOption.addItem(new FilterOption(
-              MessageManager.getString("label.most_bound_molecules"),
-              "number_of_bound_molecules", VIEWS_FILTER, false));
-      cmb_filterOption.addItem(new FilterOption(
-              MessageManager.getString("label.most_polymer_residues"),
-              "number_of_polymer_residues", VIEWS_FILTER, true));
+      List<FilterOption> filters = data.getAvailableFilterOptions(VIEWS_FILTER);
+      data.updateAvailableFilterOptions(VIEWS_FILTER, filters, lastDiscoveredStructuresSet);
+      int p=0;
+      for (FilterOption filter:filters)
+      {
+        if (lastSel!=null && filter.equals(lastSel)) {
+          selSet=p;
+        }
+        p++;
+        cmb_filterOption.addItem(filter);
+      }
     }
     cmb_filterOption.addItem(
             new FilterOption(MessageManager.getString("label.enter_pdb_id"),
-                    "-", VIEWS_ENTER_ID, false));
+                    "-", VIEWS_ENTER_ID, false,null));
     cmb_filterOption.addItem(
             new FilterOption(MessageManager.getString("label.from_file"),
-                    "-", VIEWS_FROM_FILE, false));
+                    "-", VIEWS_FROM_FILE, false,null));
 
     if (cachedPDBExist)
     {
       FilterOption cachedOption = new FilterOption(
               MessageManager.getString("label.cached_structures"), "-",
-              VIEWS_LOCAL_PDB, false);
+              VIEWS_LOCAL_PDB, false,null);
       cmb_filterOption.addItem(cachedOption);
-      cmb_filterOption.setSelectedItem(cachedOption);
+      if (selSet==-1) {
+        cmb_filterOption.setSelectedItem(cachedOption);
+      }
+    }
+    if (selSet>-1)
+    {
+      cmb_filterOption.setSelectedIndex(selSet);
     }
-
     cmb_filterOption.addItemListener(this);
   }
 
@@ -500,8 +524,18 @@ public class StructureChooser extends GStructureChooser
     if (selectedFilterOpt.getView() == VIEWS_FILTER)
     {
       mainFrame.setTitle(filterTitle);
-      chk_invertFilter.setVisible(true);
-      filterResultSet(selectedFilterOpt.getValue());
+      // TDB Query has no invert as yet
+      chk_invertFilter.setVisible(selectedFilterOpt.getQuerySource() instanceof PDBStructureChooserQuerySource);
+      
+      if (data!=selectedFilterOpt.getQuerySource() || data.needsRefetch(selectedFilterOpt)) 
+      {
+        data = selectedFilterOpt.getQuerySource();
+        // rebuild the views completely, since prefs will also change
+        tabRefresh();
+        return;
+      } else {
+        filterResultSet(selectedFilterOpt.getValue());
+      }
     }
     else if (selectedFilterOpt.getView() == VIEWS_ENTER_ID
             || selectedFilterOpt.getView() == VIEWS_FROM_FILE)
@@ -1067,6 +1101,8 @@ public class StructureChooser extends GStructureChooser
         public void run()
         {
           fetchStructuresMetaData();
+          //populateFilterComboBox(true, cachedPDBExists);
+
           filterResultSet(
                   ((FilterOption) cmb_filterOption.getSelectedItem())
                           .getValue());