JAL-1563 Fixed Uniprot FTS result-set count error, added blank image placeholder...
[jalview.git] / src / jalview / fts / service / uniprot / UniprotFTSPanel.java
index 51d7735..32f359e 100644 (file)
@@ -1,14 +1,51 @@
+/*
+ * 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;
+
+  private int resultSetCount;
+
+  private int offSet;
+
+  private static int pageLimit = UniProtFTSRestClient.getInstance()
+          .getDefaultResponsePageSize();
 
   public UniprotFTSPanel(SequenceFetcher seqFetcher)
   {
@@ -18,30 +55,180 @@ public class UniprotFTSPanel extends GFTSPanel
   }
 
   @Override
-  public void txt_search_ActionPerformed()
+  public void searchAction()
   {
-    // TODO Auto-generated method stub
+    offSet = 0;
+    executeSearch();
+  }
+
+  public void executeSearch()
+  {
+    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()).getAltCode();
+
+          wantedFields = UniProtFTSRestClient.getInstance()
+                  .getAllDefaulDisplayedDataColumns();
+          String searchTerm = txt_search.getText();
+
+          FTSRestRequest request = new FTSRestRequest();
+          request.setFieldToSearchBy(searchTarget);
+          request.setSearchTerm(searchTerm);
+          request.setOffSet(offSet);
+          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();
+          resultSetCount = resultList.getNumberOfItemsFound();
+          String result = (resultSetCount > 0) ? MessageManager
+                  .getString("label.results") : MessageManager
+                  .getString("label.result");
+          if (isPaginationEnabled() && resultSetCount > 0)
+          {
+            updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
+                    + " " + (offSet + 1) + " to "
+                    + (offSet + resultSetCount)
+                    + " " + " (" + (endTime - startTime) + " milli secs)");
+          }
+          else
+          {
+            updateSearchFrameTitle(defaultFTSFrameTitle + " - "
+                    + resultSetCount + " " + result + " ("
+                    + (endTime - startTime) + " milli secs)");
+          }
+          setSearchInProgress(false);
+          refreshPaginatorState();
+        }
+      }
+    }.start();
 
   }
 
+
+
+  public void refreshPaginatorState()
+  {
+    // System.out.println("resultSet count : " + resultSetCount);
+    // System.out.println("offSet : " + offSet);
+    // System.out.println("page limit : " + pageLimit);
+    setPrevPageButtonEnabled(false);
+    setNextPageButtonEnabled(false);
+    if (resultSetCount >= pageLimit)
+    {
+      setNextPageButtonEnabled(true);
+    }
+    if (offSet >= pageLimit)
+    {
+      setPrevPageButtonEnabled(true);
+    }
+  }
+
+  @Override
+  public void prevPageAction()
+  {
+    if (offSet >= pageLimit)
+    {
+      offSet = offSet - pageLimit;
+      executeSearch();
+    }else{
+      refreshPaginatorState();
+    }
+  }
+
   @Override
-  public void btn_ok_ActionPerformed()
+  public void nextPageAction()
   {
-    // TODO Auto-generated method stub
+    offSet = offSet + pageLimit;
+    executeSearch();
+
+  }
 
+  @Override
+  public boolean isPaginationEnabled()
+  {
+    return true;
   }
 
+  @Override
+  public void okAction()
+  {
+    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;
   }
 
 }