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.ws.dbsources.PDBRestClient.PDBDocField;
26 import java.util.Collection;
27 import java.util.Objects;
29 import javax.swing.DefaultListModel;
30 import javax.swing.table.DefaultTableModel;
32 import org.json.simple.JSONObject;
35 * Represents the response model produced by the PDBRestClient upon successful
36 * execution of a given request
41 public class PDBRestResponse
43 private int numberOfItemsFound;
45 private String responseTime;
47 private Collection<PDBResponseSummary> searchSummary;
49 public int getNumberOfItemsFound()
51 return numberOfItemsFound;
54 public void setNumberOfItemsFound(int itemFound)
56 this.numberOfItemsFound = itemFound;
59 public String getResponseTime()
64 public void setResponseTime(String responseTime)
66 this.responseTime = responseTime;
69 public Collection<PDBResponseSummary> getSearchSummary()
74 public void setSearchSummary(Collection<PDBResponseSummary> searchSummary)
76 this.searchSummary = searchSummary;
80 * Convenience method to obtain a List model for a given summary list
82 * @param summariesList
83 * the summary list which contains the data for generating the lists
85 * @return the list model generated for the search summary
87 public static DefaultListModel<PDBResponseSummary> getListModel(
88 Collection<PDBResponseSummary> summariesList)
90 DefaultListModel<PDBResponseSummary> defaultListModel = new DefaultListModel<PDBResponseSummary>();
91 for (PDBResponseSummary summaryList : summariesList)
93 defaultListModel.addElement(summaryList);
95 return defaultListModel;
99 * Convenience method to obtain a Table model for a given summary List and
103 * the PDBRestRequest object which holds useful information for
104 * creating a table model
105 * @param summariesList
106 * the summary list which contains the data for populating the
108 * @return the table model which was dynamically generated
110 public static DefaultTableModel getTableModel(PDBRestRequest request,
111 Collection<PDBResponseSummary> summariesList)
113 DefaultTableModel model = new DefaultTableModel();
115 if (request.getAssociatedSequence() != null)
117 model.addColumn("Sequence");
119 for (PDBDocField field : request.getWantedFields())
121 model.addColumn(field.getName());
124 for (PDBResponseSummary res : summariesList)
126 model.addRow(res.getSummaryData());
132 * Model for a unique response summary
137 public class PDBResponseSummary
139 private String pdbId;
141 private String concatenatedSummaryData;
143 private String[] summaryData;
145 private String associatedSequence;
147 private int width = 480;
149 public PDBResponseSummary(JSONObject doc, PDBRestRequest request)
151 StringBuilder summaryBuilder = new StringBuilder();
152 Collection<PDBDocField> diplayFields = request.getWantedFields();
153 String associatedSeq = request.getAssociatedSequence();
155 summaryData = new String[(associatedSeq != null) ? diplayFields
156 .size() + 1 : diplayFields.size()];
157 if (associatedSeq != null)
159 this.associatedSequence = (associatedSeq.length() > 18) ? associatedSeq
160 .substring(0, 18) : associatedSeq;
161 summaryData[0] = associatedSequence;
165 for (PDBDocField field : diplayFields)
167 if (field.equals(PDBDocField.PDB_ID)
168 && doc.get(PDBDocField.PDB_ID.getCode()) != null)
170 this.pdbId = doc.get(PDBDocField.PDB_ID.getCode()).toString();
171 summaryData[colCounter++] = this.pdbId;
175 String value = (doc.get(field.getCode()) == null) ? "" : doc.get(
176 field.getCode()).toString();
177 summaryBuilder.append(field.getName()).append(": ").append(value)
179 summaryData[colCounter++] = value;
182 this.concatenatedSummaryData = summaryBuilder.toString();
183 summaryBuilder = null;
186 public String getPdbId()
191 public void setPdbId(String pdbId)
196 public String getConcatenatedSummaryData()
198 return concatenatedSummaryData;
201 public void setConcatenatedSummaryData(String concatenatedSummaryData)
203 this.concatenatedSummaryData = concatenatedSummaryData;
206 public String[] getSummaryData()
211 public void setSummaryData(String[] summaryData)
213 this.summaryData = summaryData;
216 public String toString()
218 StringBuilder html = new StringBuilder();
219 html.append("<html><div style=\"width:" + width
220 + "; word-wrap: break-word; border-bottom-style: dotted;\"> ");
221 html.append(concatenatedSummaryData);
222 html.append("</div></html>");
223 return html.toString();
227 public int hashCode()
229 return Objects.hash(this.pdbId, this.concatenatedSummaryData);
233 public boolean equals(Object other)
235 if (!(other instanceof PDBResponseSummary))
240 PDBResponseSummary that = (PDBResponseSummary) other;
242 // Custom equality check here.
243 return this.pdbId.equals(that.pdbId)
244 && this.concatenatedSummaryData
245 .equals(that.concatenatedSummaryData);