2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
22 package jalview.ws.uimodel;
24 import jalview.datamodel.SequenceI;
25 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
26 import jalview.ws.dbsources.PDBRestClient.PDBDocField.Group;
28 import java.util.Collection;
29 import java.util.Objects;
31 import javax.swing.JTable;
32 import javax.swing.table.DefaultTableModel;
34 import org.json.simple.JSONObject;
37 * Represents the response model produced by the PDBRestClient upon successful
38 * execution of a given request
43 public class PDBRestResponse
45 private int numberOfItemsFound;
47 private String responseTime;
49 private Collection<PDBResponseSummary> searchSummary;
51 public int getNumberOfItemsFound()
53 return numberOfItemsFound;
56 public void setNumberOfItemsFound(int itemFound)
58 this.numberOfItemsFound = itemFound;
61 public String getResponseTime()
66 public void setResponseTime(String responseTime)
68 this.responseTime = responseTime;
71 public Collection<PDBResponseSummary> getSearchSummary()
76 public void setSearchSummary(Collection<PDBResponseSummary> searchSummary)
78 this.searchSummary = searchSummary;
82 * Convenience method to obtain a Table model for a given summary List based
83 * on the request parameters
86 * the PDBRestRequest object which holds useful information for
87 * creating a table model
88 * @param summariesList
89 * the summary list which contains the data for populating the
91 * @return the table model which was dynamically generated
93 public static DefaultTableModel getTableModel(PDBRestRequest request,
94 Collection<PDBResponseSummary> summariesList)
96 final PDBDocField[] cols = request.getWantedFields().toArray(
98 final int colOffset = request.getAssociatedSequence() == null ? 0 : 1;
99 DefaultTableModel tableModel = new DefaultTableModel()
102 public boolean isCellEditable(int row, int column)
108 public Class<?> getColumnClass(int columnIndex)
110 if (colOffset == 1 && columnIndex == 0)
114 if (cols[columnIndex - colOffset].getGroup().getName()
115 .equalsIgnoreCase(Group.QUALITY_MEASURES.getName()))
123 if (request.getAssociatedSequence() != null)
125 tableModel.addColumn("Ref Sequence"); // Create sequence column header if
126 // exists in the request
128 for (PDBDocField field : request.getWantedFields())
130 tableModel.addColumn(field.getName()); // Create sequence column header if
131 // exists in the request
134 for (PDBResponseSummary res : summariesList)
136 tableModel.addRow(res.getSummaryData()); // Populate table rows with
144 * Model for a unique response summary
147 public class PDBResponseSummary
149 private String pdbId;
151 private Object[] summaryRowData;
153 private SequenceI associatedSequence;
155 public PDBResponseSummary(JSONObject pdbJsonDoc, PDBRestRequest request)
157 Collection<PDBDocField> diplayFields = request.getWantedFields();
158 SequenceI associatedSeq = request.getAssociatedSequence();
160 summaryRowData = new Object[(associatedSeq != null) ? diplayFields
161 .size() + 1 : diplayFields.size()];
162 if (associatedSeq != null)
164 this.associatedSequence = associatedSeq;
165 summaryRowData[0] = associatedSequence;
169 for (PDBDocField field : diplayFields)
171 String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
172 : pdbJsonDoc.get(field.getCode()).toString();
173 if (field.equals(PDBDocField.PDB_ID))
175 this.pdbId = fieldData;
176 summaryRowData[colCounter++] = this.pdbId;
180 if (field.getGroup().getName()
181 .equals(Group.QUALITY_MEASURES.getName()))
185 if (fieldData == null || fieldData.isEmpty())
187 summaryRowData[colCounter++] = null;
191 Double value = Double.valueOf(fieldData);
192 summaryRowData[colCounter++] = value;
194 } catch (Exception e)
197 System.out.println("offending value:" + fieldData);
198 summaryRowData[colCounter++] = 0.0;
201 summaryRowData[colCounter++] = (fieldData == null || fieldData
202 .isEmpty()) ? null : fieldData;
208 public Object getPdbId()
213 public void setPdbId(String pdbId)
218 public Object[] getSummaryData()
220 return summaryRowData;
223 public void setSummaryData(Object[] summaryData)
225 this.summaryRowData = summaryData;
229 * Returns a string representation of this object;
232 public String toString()
234 StringBuilder summaryFieldValues = new StringBuilder();
235 for (Object summaryField : summaryRowData)
237 summaryFieldValues.append(
238 summaryField == null ? " " : summaryField.toString())
241 return summaryFieldValues.toString();
245 * Returns hash code value for this object
248 public int hashCode()
250 return Objects.hash(this.pdbId, this.toString());
254 * Indicates whether some object is equal to this one
257 public boolean equals(Object that)
259 if (!(that instanceof PDBResponseSummary))
263 PDBResponseSummary another = (PDBResponseSummary) that;
264 return this.toString().equals(another.toString());
269 public static void configureTableColumn(JTable tbl_summary,
270 Collection<PDBDocField> wantedFields)
272 for (PDBDocField wantedField : wantedFields)
276 if (wantedField.equals(PDBDocField.PDB_ID))
278 tbl_summary.getColumn(wantedField.getName()).setMinWidth(40);
279 tbl_summary.getColumn(wantedField.getName()).setMaxWidth(60);
280 tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(45);
282 else if (wantedField.equals(PDBDocField.TITLE))
284 tbl_summary.getColumn(wantedField.getName()).setMinWidth(300);
285 tbl_summary.getColumn(wantedField.getName()).setMaxWidth(1000);
286 tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(400);
288 else if (wantedField.getGroup() == Group.QUALITY_MEASURES)
290 tbl_summary.getColumn(wantedField.getName()).setMinWidth(50);
291 tbl_summary.getColumn(wantedField.getName()).setMaxWidth(150);
292 tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(85);
296 tbl_summary.getColumn(wantedField.getName()).setMinWidth(50);
297 tbl_summary.getColumn(wantedField.getName()).setMaxWidth(400);
298 tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(95);
300 } catch (Exception e)