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