JAL-1563 Fixed Uniprot FTS result-set count error, added blank image placeholder...
[jalview.git] / src / jalview / fts / service / uniprot / UniprotFTSPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21
22 package jalview.fts.service.uniprot;
23
24 import jalview.fts.api.FTSDataColumnI;
25 import jalview.fts.api.FTSRestClientI;
26 import jalview.fts.core.FTSRestRequest;
27 import jalview.fts.core.FTSRestResponse;
28 import jalview.fts.core.GFTSPanel;
29 import jalview.gui.SequenceFetcher;
30 import jalview.util.MessageManager;
31
32 import java.util.HashSet;
33
34 @SuppressWarnings("serial")
35 public class UniprotFTSPanel extends GFTSPanel
36 {
37
38   private static String defaultFTSFrameTitle = MessageManager
39           .getString("label.uniprot_sequence_fetcher");
40
41   private String ftsFrameTitle = defaultFTSFrameTitle;
42
43   private int resultSetCount;
44
45   private int offSet;
46
47   private static int pageLimit = UniProtFTSRestClient.getInstance()
48           .getDefaultResponsePageSize();
49
50   public UniprotFTSPanel(SequenceFetcher seqFetcher)
51   {
52     this.seqFetcher = seqFetcher;
53     this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
54             .getProgressIndicator();
55   }
56
57   @Override
58   public void searchAction()
59   {
60     offSet = 0;
61     executeSearch();
62   }
63
64   public void executeSearch()
65   {
66     new Thread()
67   {
68       @Override
69       public void run()
70       {
71         ftsFrameTitle = defaultFTSFrameTitle;
72         reset();
73         if (getTypedText().length() > 0)
74         {
75           setSearchInProgress(true);
76           long startTime = System.currentTimeMillis();
77
78           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
79                   .getSelectedItem()).getAltCode();
80
81           wantedFields = UniProtFTSRestClient.getInstance()
82                   .getAllDefaulDisplayedDataColumns();
83           String searchTerm = txt_search.getText();
84
85           FTSRestRequest request = new FTSRestRequest();
86           request.setFieldToSearchBy(searchTarget);
87           request.setSearchTerm(searchTerm);
88           request.setOffSet(offSet);
89           request.setWantedFields(wantedFields);
90           FTSRestClientI uniProtRestCleint = UniProtFTSRestClient
91                   .getInstance();
92           FTSRestResponse resultList;
93           try
94           {
95             resultList = uniProtRestCleint.executeRequest(request);
96           } catch (Exception e)
97           {
98             e.printStackTrace();
99             setErrorMessage(e.getMessage());
100             checkForErrors();
101             return;
102           }
103
104           if (resultList.getSearchSummary() != null
105                   && resultList.getSearchSummary().size() > 0)
106           {
107             getResultTable().setModel(
108                     FTSRestResponse.getTableModel(request,
109                             resultList.getSearchSummary()));
110             FTSRestResponse.configureTableColumn(getResultTable(),
111                     wantedFields);
112             getResultTable().setVisible(true);
113           }
114
115           long endTime = System.currentTimeMillis();
116           resultSetCount = resultList.getNumberOfItemsFound();
117           String result = (resultSetCount > 0) ? MessageManager
118                   .getString("label.results") : MessageManager
119                   .getString("label.result");
120           if (isPaginationEnabled() && resultSetCount > 0)
121           {
122             updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
123                     + " " + (offSet + 1) + " to "
124                     + (offSet + resultSetCount)
125                     + " " + " (" + (endTime - startTime) + " milli secs)");
126           }
127           else
128           {
129             updateSearchFrameTitle(defaultFTSFrameTitle + " - "
130                     + resultSetCount + " " + result + " ("
131                     + (endTime - startTime) + " milli secs)");
132           }
133           setSearchInProgress(false);
134           refreshPaginatorState();
135         }
136       }
137     }.start();
138
139   }
140
141
142
143   public void refreshPaginatorState()
144   {
145     // System.out.println("resultSet count : " + resultSetCount);
146     // System.out.println("offSet : " + offSet);
147     // System.out.println("page limit : " + pageLimit);
148     setPrevPageButtonEnabled(false);
149     setNextPageButtonEnabled(false);
150     if (resultSetCount >= pageLimit)
151     {
152       setNextPageButtonEnabled(true);
153     }
154     if (offSet >= pageLimit)
155     {
156       setPrevPageButtonEnabled(true);
157     }
158   }
159
160   @Override
161   public void prevPageAction()
162   {
163     if (offSet >= pageLimit)
164     {
165       offSet = offSet - pageLimit;
166       executeSearch();
167     }else{
168       refreshPaginatorState();
169     }
170   }
171
172   @Override
173   public void nextPageAction()
174   {
175     offSet = offSet + pageLimit;
176     executeSearch();
177
178   }
179
180   @Override
181   public boolean isPaginationEnabled()
182   {
183     return true;
184   }
185
186   @Override
187   public void okAction()
188   {
189     disableActionButtons();
190     StringBuilder selectedIds = new StringBuilder();
191     HashSet<String> selectedIdsSet = new HashSet<String>();
192     int primaryKeyColIndex = 0;
193     try
194     {
195       primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
196               wantedFields, false);
197     } catch (Exception e)
198     {
199       e.printStackTrace();
200     }
201     int[] selectedRows = getResultTable().getSelectedRows();
202     for (int summaryRow : selectedRows)
203     {
204       String idStr = getResultTable().getValueAt(summaryRow,
205               primaryKeyColIndex).toString();
206       selectedIdsSet.add(idStr);
207     }
208
209     for (String selectedId : selectedIdsSet)
210     {
211       selectedIds.append(selectedId).append(";");
212     }
213
214     String ids = selectedIds.toString();
215     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
216     seqFetcher.getTextArea().setText(ids);
217     Thread worker = new Thread(seqFetcher);
218     worker.start();
219     delayAndEnableActionButtons();
220   }
221
222   @Override
223   public FTSRestClientI getFTSRestClient()
224   {
225     return UniProtFTSRestClient.getInstance();
226   }
227
228   @Override
229   public String getFTSFrameTitle()
230   {
231     return ftsFrameTitle;
232   }
233
234 }