JAL-2071 architectural improvement for Plugable Free Text Search Services
[jalview.git] / src / jalview / fts / core / FTSRestResponse.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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 produced by the PDBRestClient upon successful
34  * execution of a given 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 PDBRestRequest 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         if (cols[columnIndex - colOffset].getGroup().getName()
111                 .equalsIgnoreCase("Quality Measures"))
112         {
113           return Double.class;
114         }
115         return String.class;
116       }
117
118     };
119     if (request.getAssociatedSequence() != null)
120     {
121       tableModel.addColumn("Ref Sequence"); // Create sequence column header if
122       // exists in the request
123     }
124     for (FTSDataColumnI field : request
125             .getWantedFields())
126     {
127       tableModel.addColumn(field.getName()); // Create sequence column header if
128                                              // exists in the request
129     }
130
131     for (FTSData res : summariesList)
132     {
133       tableModel.addRow(res.getSummaryData()); // Populate table rows with
134                                                // summary list
135     }
136
137     return tableModel;
138   }
139
140   public static void configureTableColumn(JTable tbl_summary,
141           Collection<FTSDataColumnI> wantedFields)
142   {
143     try
144     {
145       // wait for table model initialisation to complete
146       Thread.sleep(1200);
147     } catch (InterruptedException e1)
148     {
149       e1.printStackTrace();
150     }
151     for (FTSDataColumnI wantedField : wantedFields)
152     {
153       try
154       {
155         tbl_summary.getColumn(wantedField.getName()).setMinWidth(
156                 wantedField.getMinWidth());
157         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(
158                 wantedField.getMaxWidth());
159         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(
160                 wantedField.getPreferredWidth());
161       } catch (Exception e)
162       {
163         e.printStackTrace();
164       }
165     }
166   }
167
168
169 }