JAL-1563 Further increased swing timer for search textfield to 2500ms, removed 1200ms...
[jalview.git] / src / jalview / fts / service / pdb / PDBFTSPanel.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.pdb;
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 PDBFTSPanel extends GFTSPanel
36 {
37   private static String defaultFTSFrameTitle = MessageManager
38           .getString("label.pdb_sequence_fetcher");
39
40   private String ftsFrameTitle = defaultFTSFrameTitle;
41
42   public PDBFTSPanel(SequenceFetcher seqFetcher)
43   {
44     this.seqFetcher = seqFetcher;
45     this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
46             .getProgressIndicator();
47   }
48
49
50   @Override
51   public void searchAction()
52   {
53     new Thread()
54     {
55       @Override
56       public void run()
57       {
58         ftsFrameTitle = defaultFTSFrameTitle;
59         reset();
60         boolean allowEmptySequence = false;
61         if (getTypedText().length() > 0)
62         {
63           setSearchInProgress(true);
64           long startTime = System.currentTimeMillis();
65
66           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
67                   .getSelectedItem()).getCode();
68           wantedFields = PDBFTSRestClient.getInstance()
69                   .getAllDefaulDisplayedDataColumns();
70           String searchTerm = decodeSearchTerm(txt_search.getText(),
71                   searchTarget);
72
73           FTSRestRequest request = new FTSRestRequest();
74           request.setAllowEmptySeq(allowEmptySequence);
75           request.setResponseSize(100);
76           request.setFieldToSearchBy("(" + searchTarget + ":");
77           request.setSearchTerm(searchTerm + ")");
78           request.setWantedFields(wantedFields);
79           FTSRestClientI pdbRestCleint = PDBFTSRestClient.getInstance();
80           FTSRestResponse resultList;
81           try
82           {
83             resultList = pdbRestCleint.executeRequest(request);
84           } catch (Exception e)
85           {
86             setErrorMessage(e.getMessage());
87             checkForErrors();
88             return;
89           }
90
91           if (resultList.getSearchSummary() != null
92                   && resultList.getSearchSummary().size() > 0)
93           {
94             getResultTable().setModel(
95                     FTSRestResponse.getTableModel(request,
96                     resultList.getSearchSummary()));
97             FTSRestResponse.configureTableColumn(getResultTable(),
98                     wantedFields);
99             getResultTable().setVisible(true);
100           }
101
102           long endTime = System.currentTimeMillis();
103           int resultSetCount = resultList.getNumberOfItemsFound();
104           String result = (resultSetCount > 1) ? MessageManager
105                   .getString("label.results") : MessageManager
106                   .getString("label.result");
107           updateSearchFrameTitle(defaultFTSFrameTitle + " - "
108                   + resultSetCount + " " + result + " ("
109                   + (endTime - startTime) + " milli secs)");
110           setSearchInProgress(false);
111         }
112       }
113     }.start();
114   }
115
116   public static String decodeSearchTerm(String enteredText,
117           String targetField)
118   {
119     String foundSearchTerms = enteredText;
120     StringBuilder foundSearchTermsBuilder = new StringBuilder();
121     if (enteredText.contains(";"))
122     {
123       String[] searchTerms = enteredText.split(";");
124       for (String searchTerm : searchTerms)
125       {
126         if (searchTerm.contains(":"))
127         {
128           foundSearchTermsBuilder.append(targetField).append(":")
129                   .append(searchTerm.split(":")[0]).append(" OR ");
130         }
131         else
132         {
133           foundSearchTermsBuilder.append(targetField).append(":")
134                   .append(searchTerm).append(" OR ");
135         }
136       }
137       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
138       foundSearchTerms = foundSearchTermsBuilder.toString();
139       if (foundSearchTerms.contains(" OR "))
140       {
141         foundSearchTerms = foundSearchTerms.substring(
142                 targetField.length() + 1, endIndex);
143       }
144     }
145     else if (enteredText.contains(":"))
146     {
147       foundSearchTerms = foundSearchTerms.split(":")[0];
148     }
149     return foundSearchTerms;
150   }
151
152   @Override
153   public void okAction()
154   {
155     // mainFrame.dispose();
156     disableActionButtons();
157     StringBuilder selectedIds = new StringBuilder();
158     HashSet<String> selectedIdsSet = new HashSet<String>();
159     int primaryKeyColIndex = 0;
160     try
161     {
162       primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
163               wantedFields,
164               false);
165     } catch (Exception e)
166     {
167       // TODO Auto-generated catch block
168       e.printStackTrace();
169     }
170     int[] selectedRows = getResultTable().getSelectedRows();
171     for (int summaryRow : selectedRows)
172     {
173       String idStr = getResultTable().getValueAt(summaryRow,
174               primaryKeyColIndex)
175               .toString();
176       String searchTerm = txt_search.getText();
177       selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
178     }
179
180     for (String selectedId : selectedIdsSet)
181     {
182       selectedIds.append(selectedId).append(";");
183     }
184
185     String ids = selectedIds.toString();
186     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
187     seqFetcher.getTextArea().setText(ids);
188     Thread worker = new Thread(seqFetcher);
189     worker.start();
190     delayAndEnableActionButtons();
191   }
192
193
194   public static String getPDBIdwithSpecifiedChain(String pdbId,
195           String searchTerm)
196   {
197     String pdbIdWithChainCode = "";
198     if (searchTerm.contains(";"))
199     {
200       String[] foundTerms = searchTerm.split(";");
201       for (String foundTerm : foundTerms)
202       {
203         if (foundTerm.contains(pdbId))
204         {
205           pdbIdWithChainCode = foundTerm;
206         }
207       }
208     }
209     else if (searchTerm.contains(pdbId))
210     {
211       pdbIdWithChainCode = searchTerm;
212     }
213     else
214     {
215       pdbIdWithChainCode = pdbId;
216     }
217     return pdbIdWithChainCode;
218   }
219
220
221
222   @Override
223   public FTSRestClientI getFTSRestClient()
224   {
225     return PDBFTSRestClient.getInstance();
226   }
227
228   @Override
229   public String getFTSFrameTitle()
230   {
231     return ftsFrameTitle;
232   }
233
234
235   @Override
236   public void prevPageAction()
237   {
238     // TODO pagination not yet implemented for PDB FTS
239
240   }
241
242   @Override
243   public void nextPageAction()
244   {
245     // TODO pagination not yet implemented for PDB FTS
246
247   }
248
249   @Override
250   public boolean isPaginationEnabled()
251   {
252     return false;
253   }
254
255 }