JAL-2071 further refactoring, optimisation, and house keeping for the generic Free...
[jalview.git] / src / jalview / fts / core / FTSRestResponse.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.core;
23
24 import jalview.fts.api.FTSData;
25 import jalview.fts.api.FTSDataColumnI;
26
27 import java.util.Collection;
28
29 import javax.swing.JTable;
30 import javax.swing.table.DefaultTableModel;
31
32 /**
33  * Represents the response model generated by the FTSRestClient upon successful
34  * execution of a given FTS request
35  * 
36  * @author tcnofoegbu
37  *
38  */
39 public class FTSRestResponse
40 {
41   private int numberOfItemsFound;
42
43   private String responseTime;
44
45   private Collection<FTSData> searchSummary;
46
47   public int getNumberOfItemsFound()
48   {
49     return numberOfItemsFound;
50   }
51
52   public void setNumberOfItemsFound(int itemFound)
53   {
54     this.numberOfItemsFound = itemFound;
55   }
56
57   public String getResponseTime()
58   {
59     return responseTime;
60   }
61
62   public void setResponseTime(String responseTime)
63   {
64     this.responseTime = responseTime;
65   }
66
67   public Collection<FTSData> getSearchSummary()
68   {
69     return searchSummary;
70   }
71
72   public void setSearchSummary(Collection<FTSData> searchSummary)
73   {
74     this.searchSummary = searchSummary;
75   }
76
77   /**
78    * Convenience method to obtain a Table model for a given summary List based
79    * on the request parameters
80    * 
81    * @param request
82    *          the FTSRestRequest object which holds useful information for
83    *          creating a table model
84    * @param summariesList
85    *          the summary list which contains the data for populating the
86    *          table's rows
87    * @return the table model which was dynamically generated
88    */
89   public static DefaultTableModel getTableModel(FTSRestRequest request,
90           Collection<FTSData> summariesList)
91   {
92     final FTSDataColumnI[] cols = request.getWantedFields()
93             .toArray(new FTSDataColumnI[0]);
94     final int colOffset = request.getAssociatedSequence() == null ? 0 : 1;
95     DefaultTableModel tableModel = new DefaultTableModel()
96     {
97       @Override
98       public boolean isCellEditable(int row, int column)
99       {
100         return false;
101       }
102
103       @Override
104       public Class<?> getColumnClass(int columnIndex)
105       {
106         if (colOffset == 1 && columnIndex == 0)
107         {
108           return String.class;
109         }
110         return cols[columnIndex - colOffset].getDataColumnClass();
111       }
112
113     };
114     if (request.getAssociatedSequence() != null)
115     {
116       tableModel.addColumn("Ref Sequence"); // Create sequence column header if
117       // exists in the request
118     }
119     for (FTSDataColumnI field : request
120             .getWantedFields())
121     {
122       tableModel.addColumn(field.getName()); // Create sequence column header if
123                                              // exists in the request
124     }
125
126     for (FTSData res : summariesList)
127     {
128       tableModel.addRow(res.getSummaryData()); // Populate table rows with
129                                                // summary list
130     }
131
132     return tableModel;
133   }
134
135   public static void configureTableColumn(JTable tbl_summary,
136           Collection<FTSDataColumnI> wantedFields)
137   {
138     try
139     {
140       // wait for table model initialisation to complete
141       Thread.sleep(1200);
142     } catch (InterruptedException e1)
143     {
144       e1.printStackTrace();
145     }
146     for (FTSDataColumnI wantedField : wantedFields)
147     {
148       try
149       {
150         tbl_summary.getColumn(wantedField.getName()).setMinWidth(
151                 wantedField.getMinWidth());
152         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(
153                 wantedField.getMaxWidth());
154         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(
155                 wantedField.getPreferredWidth());
156       } catch (Exception e)
157       {
158         e.printStackTrace();
159       }
160     }
161   }
162
163
164 }