8078d43ec540a146773f4776d9469ddd14828f14
[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].getDataType()
111                 .getDataTypeClass();
112       }
113
114     };
115     if (request.getAssociatedSequence() != null)
116     {
117       tableModel.addColumn("Ref Sequence"); // Create sequence column header if
118       // exists in the request
119     }
120     for (FTSDataColumnI field : request
121             .getWantedFields())
122     {
123       tableModel.addColumn(field.getName()); // Create sequence column header if
124                                              // exists in the request
125     }
126
127     for (FTSData res : summariesList)
128     {
129       tableModel.addRow(res.getSummaryData()); // Populate table rows with
130                                                // summary list
131     }
132
133     return tableModel;
134   }
135
136   public static void configureTableColumn(JTable tbl_summary,
137           Collection<FTSDataColumnI> wantedFields)
138   {
139     for (FTSDataColumnI wantedField : wantedFields)
140     {
141       try
142       {
143         tbl_summary.getColumn(wantedField.getName()).setMinWidth(
144                 wantedField.getMinWidth());
145         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(
146                 wantedField.getMaxWidth());
147         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(
148                 wantedField.getPreferredWidth());
149       } catch (Exception e)
150       {
151         e.printStackTrace();
152       }
153       if (wantedField.getDataType().getDataTypeClass() == Double.class)
154       {
155         DecimalFormatTableCellRenderer dfr = new DecimalFormatTableCellRenderer(
156                 wantedField.getDataType().isFormtted(),
157                 wantedField.getDataType().getSignificantFigures());
158         tbl_summary.getColumn(wantedField.getName()).setCellRenderer(dfr);
159       }
160       else if (wantedField.getDataType().getDataTypeClass() == Integer.class)
161       {
162         DecimalFormatTableCellRenderer dfr = new DecimalFormatTableCellRenderer(
163                 wantedField.getDataType().isFormtted(),
164                 wantedField.getDataType().getSignificantFigures());
165         tbl_summary.getColumn(wantedField.getName()).setCellRenderer(dfr);
166       }
167     }
168   }
169
170
171 }