JAL-1563 Modified Uniprot data column configurations and improved implementation...
[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   public UniprotFTSPanel(SequenceFetcher seqFetcher)
44   {
45     this.seqFetcher = seqFetcher;
46     this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
47             .getProgressIndicator();
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         if (getTypedText().length() > 0)
61         {
62           setSearchInProgress(true);
63           long startTime = System.currentTimeMillis();
64
65           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
66                   .getSelectedItem()).getAltCode();
67
68           wantedFields = UniProtFTSRestClient.getInstance()
69                   .getAllDefaulDisplayedDataColumns();
70           String searchTerm = txt_search.getText();
71
72           FTSRestRequest request = new FTSRestRequest();
73           request.setFieldToSearchBy(searchTarget);
74           request.setSearchTerm(searchTerm);
75           request.setWantedFields(wantedFields);
76           FTSRestClientI uniProtRestCleint = UniProtFTSRestClient
77                   .getInstance();
78           FTSRestResponse resultList;
79           try
80           {
81             resultList = uniProtRestCleint.executeRequest(request);
82           } catch (Exception e)
83           {
84             e.printStackTrace();
85             setErrorMessage(e.getMessage());
86             checkForErrors();
87             return;
88           }
89
90           if (resultList.getSearchSummary() != null
91                   && resultList.getSearchSummary().size() > 0)
92           {
93             getResultTable().setModel(
94                     FTSRestResponse.getTableModel(request,
95                     resultList.getSearchSummary()));
96             FTSRestResponse.configureTableColumn(getResultTable(),
97                     wantedFields);
98             getResultTable().setVisible(true);
99           }
100
101           long endTime = System.currentTimeMillis();
102           int resultSetCount = resultList.getNumberOfItemsFound();
103           String result = (resultSetCount > 1) ? MessageManager
104                   .getString("label.results") : MessageManager
105                   .getString("label.result");
106           updateSearchFrameTitle(defaultFTSFrameTitle + " - "
107                   + resultSetCount + " " + result + " ("
108                   + (endTime - startTime) + " milli secs)");
109           setSearchInProgress(false);
110         }
111       }
112     }.start();
113
114   }
115
116   @Override
117   public void okAction()
118   {
119     disableActionButtons();
120     StringBuilder selectedIds = new StringBuilder();
121     HashSet<String> selectedIdsSet = new HashSet<String>();
122     int primaryKeyColIndex = 0;
123     try
124     {
125       primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
126               wantedFields, false);
127     } catch (Exception e)
128     {
129       e.printStackTrace();
130     }
131     int[] selectedRows = getResultTable().getSelectedRows();
132     for (int summaryRow : selectedRows)
133     {
134       String idStr = getResultTable().getValueAt(summaryRow,
135               primaryKeyColIndex).toString();
136       selectedIdsSet.add(idStr);
137     }
138
139     for (String selectedId : selectedIdsSet)
140     {
141       selectedIds.append(selectedId).append(";");
142     }
143
144     String ids = selectedIds.toString();
145     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
146     seqFetcher.getTextArea().setText(ids);
147     Thread worker = new Thread(seqFetcher);
148     worker.start();
149     delayAndEnableActionButtons();
150   }
151
152   @Override
153   public FTSRestClientI getFTSRestClient()
154   {
155     return UniProtFTSRestClient.getInstance();
156   }
157
158   @Override
159   public String getFTSFrameTitle()
160   {
161     return ftsFrameTitle;
162   }
163
164 }