JAL-1563 JAL-2071 Update and completed implementation for UniProt FTS support
[jalview.git] / src / jalview / fts / service / uniprot / UniprotFTSPanel.java
index 51d7735..dcef358 100644 (file)
@@ -1,14 +1,44 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+
 package jalview.fts.service.uniprot;
 
+import jalview.fts.api.FTSDataColumnI;
 import jalview.fts.api.FTSRestClientI;
+import jalview.fts.core.FTSRestRequest;
+import jalview.fts.core.FTSRestResponse;
 import jalview.fts.core.GFTSPanel;
 import jalview.gui.SequenceFetcher;
 import jalview.util.MessageManager;
 
+import java.util.HashSet;
+
 @SuppressWarnings("serial")
 public class UniprotFTSPanel extends GFTSPanel
 {
 
+  private static String defaultFTSFrameTitle = MessageManager
+          .getString("label.uniprot_sequence_fetcher");
+
+  private String ftsFrameTitle = defaultFTSFrameTitle;
 
   public UniprotFTSPanel(SequenceFetcher seqFetcher)
   {
@@ -18,30 +48,117 @@ public class UniprotFTSPanel extends GFTSPanel
   }
 
   @Override
-  public void txt_search_ActionPerformed()
+  public void searchAction()
   {
-    // TODO Auto-generated method stub
+    new Thread()
+  {
+      @Override
+      public void run()
+      {
+        ftsFrameTitle = defaultFTSFrameTitle;
+        reset();
+        if (getTypedText().length() > 0)
+        {
+          setSearchInProgress(true);
+          long startTime = System.currentTimeMillis();
+
+          String searchTarget = ((FTSDataColumnI) cmb_searchTarget
+                  .getSelectedItem()).getCode();
+
+          wantedFields = UniProtFTSRestClient.getInstance()
+                  .getAllDefaulDisplayedDataColumns();
+          String searchTerm = txt_search.getText();
+
+          FTSRestRequest request = new FTSRestRequest();
+          request.setFieldToSearchBy(searchTarget);
+          request.setSearchTerm(searchTerm);
+          request.setWantedFields(wantedFields);
+          FTSRestClientI uniProtRestCleint = UniProtFTSRestClient
+                  .getInstance();
+          FTSRestResponse resultList;
+          try
+          {
+            resultList = uniProtRestCleint.executeRequest(request);
+          } catch (Exception e)
+          {
+            e.printStackTrace();
+            setErrorMessage(e.getMessage());
+            checkForErrors();
+            return;
+          }
+
+          if (resultList.getSearchSummary() != null
+                  && resultList.getSearchSummary().size() > 0)
+          {
+            getResultTable().setModel(
+                    FTSRestResponse.getTableModel(request,
+                    resultList.getSearchSummary()));
+            FTSRestResponse.configureTableColumn(getResultTable(),
+                    wantedFields);
+            getResultTable().setVisible(true);
+          }
+
+          long endTime = System.currentTimeMillis();
+          int resultSetCount = resultList.getNumberOfItemsFound();
+          String result = (resultSetCount > 1) ? MessageManager
+                  .getString("label.results") : MessageManager
+                  .getString("label.result");
+          updateSearchFrameTitle(defaultFTSFrameTitle + " - "
+                  + resultSetCount + " " + result + " ("
+                  + (endTime - startTime) + " milli secs)");
+          setSearchInProgress(false);
+        }
+      }
+    }.start();
 
   }
 
   @Override
-  public void btn_ok_ActionPerformed()
+  public void okAction()
   {
-    // TODO Auto-generated method stub
+    disableActionButtons();
+    StringBuilder selectedIds = new StringBuilder();
+    HashSet<String> selectedIdsSet = new HashSet<String>();
+    int primaryKeyColIndex = 0;
+    try
+    {
+      primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
+              wantedFields, false);
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+    }
+    int[] selectedRows = getResultTable().getSelectedRows();
+    for (int summaryRow : selectedRows)
+    {
+      String idStr = getResultTable().getValueAt(summaryRow,
+              primaryKeyColIndex).toString();
+      selectedIdsSet.add(idStr);
+    }
 
-  }
+    for (String selectedId : selectedIdsSet)
+    {
+      selectedIds.append(selectedId).append(";");
+    }
 
+    String ids = selectedIds.toString();
+    // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
+    seqFetcher.getTextArea().setText(ids);
+    Thread worker = new Thread(seqFetcher);
+    worker.start();
+    delayAndEnableActionButtons();
+  }
 
   @Override
   public FTSRestClientI getFTSRestClient()
   {
-    return UniProtRestClient.getInstance();
+    return UniProtFTSRestClient.getInstance();
   }
 
   @Override
   public String getFTSFrameTitle()
   {
-    return MessageManager.getString("label.uniprot_sequence_fetcher");
+    return ftsFrameTitle;
   }
 
 }