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.List;
28 import java.util.Objects;
30 import javax.swing.DefaultListModel;
31 import javax.swing.table.DefaultTableModel;
33 import org.json.simple.JSONObject;
36 * Represents the response model produced by the PDBRestClient upon successful
37 * execution of a given request
42 public class PDBRestResponse
44 private int numberOfItemsFound;
46 private String responseTime;
48 private Collection<PDBResponseSummary> searchSummary;
50 public int getNumberOfItemsFound()
52 return numberOfItemsFound;
55 public void setNumberOfItemsFound(int itemFound)
57 this.numberOfItemsFound = itemFound;
60 public String getResponseTime()
65 public void setResponseTime(String responseTime)
67 this.responseTime = responseTime;
70 public Collection<PDBResponseSummary> getSearchSummary()
75 public void setSearchSummary(Collection<PDBResponseSummary> searchSummary)
77 this.searchSummary = searchSummary;
81 * Convenience method to obtain a List model for a given summary list
83 * @param summariesList
84 * the summary list which contains the data for generating the lists
86 * @return the list model generated for the search summary
88 public static DefaultListModel<PDBResponseSummary> getListModel(
89 Collection<PDBResponseSummary> summariesList)
91 DefaultListModel<PDBResponseSummary> defaultListModel = new DefaultListModel<PDBResponseSummary>();
92 for (PDBResponseSummary summaryList : summariesList)
94 defaultListModel.addElement(summaryList);
96 return defaultListModel;
100 * Convenience method to obtain a Table model for a given summary List and
104 * the PDBRestRequest object which holds useful information for
105 * creating a table model
106 * @param summariesList
107 * the summary list which contains the data for populating the
109 * @return the table model which was dynamically generated
111 public static DefaultTableModel getTableModel(PDBRestRequest request,
112 Collection<PDBResponseSummary> summariesList)
114 DefaultTableModel model = new DefaultTableModel();
116 if (request.getAssociatedSequence() != null)
118 model.addColumn("Sequence");
120 for (PDBDocField field : request.getWantedFields())
122 model.addColumn(field.getName());
125 for (PDBResponseSummary res : summariesList)
127 model.addRow(res.getSummaryData());
133 * Model for a unique response summary
138 public class PDBResponseSummary
140 private String pdbId;
142 private String concatenatedSummaryData;
144 private String[] summaryData;
146 private String associatedSequence;
148 private int width = 480;
150 public PDBResponseSummary(JSONObject doc, PDBRestRequest request)
152 StringBuilder summaryBuilder = new StringBuilder();
153 List<PDBDocField> diplayFields = request.getWantedFields();
154 String associatedSeq = request.getAssociatedSequence();
156 summaryData = new String[(associatedSeq != null) ? diplayFields
157 .size() + 1 : diplayFields.size()];
158 if (associatedSeq != null)
160 this.associatedSequence = (associatedSeq.length() > 18) ? associatedSeq
161 .substring(0, 18) : associatedSeq;
162 summaryData[0] = associatedSequence;
166 for (PDBDocField field : diplayFields)
168 if (field.equals(PDBDocField.MOLECULE_TYPE)
169 && doc.get(PDBDocField.MOLECULE_TYPE.getCode()) != null)
171 String moleculeType = doc
172 .get(PDBDocField.MOLECULE_TYPE.getCode()).toString();
173 if (moleculeType.equalsIgnoreCase("protein"))
175 summaryBuilder.append("<img src=\""
176 + getClass().getResource("/images/protein.png")
177 .toString() + "\">");
179 if (moleculeType.equalsIgnoreCase("dna"))
181 summaryBuilder.append("<img src=\""
182 + getClass().getResource("/images/dna.png").toString()
185 if (moleculeType.equalsIgnoreCase("rna"))
187 summaryBuilder.append("<img src=\""
188 + getClass().getResource("/images/dna.png").toString()
191 if (moleculeType.equalsIgnoreCase("sugar"))
193 summaryBuilder.append("<img src=\""
194 + getClass().getResource("/images/sugar.png")
195 .toString() + "\">");
197 summaryData[colCounter++] = moleculeType;
199 else if (field.equals(PDBDocField.PDB_ID)
200 && doc.get(PDBDocField.PDB_ID.getCode()) != null)
202 this.pdbId = doc.get(PDBDocField.PDB_ID.getCode()).toString();
203 summaryBuilder.append(this.pdbId).append(" | ");
204 summaryData[colCounter++] = this.pdbId;
206 else if (doc.get(field.getCode()) != null)
208 summaryBuilder.append(field.getName()).append(": ")
209 .append(doc.get(field.getCode())).append(" | ");
210 summaryData[colCounter++] = doc.get(field.getCode()).toString();
213 int endIndex = summaryBuilder.lastIndexOf(" | ");
214 String fSummary = summaryBuilder.toString().substring(0, endIndex);
215 this.concatenatedSummaryData = fSummary.trim();
216 summaryBuilder = null;
219 public String getPdbId()
224 public void setPdbId(String pdbId)
229 public String getConcatenatedSummaryData()
231 return concatenatedSummaryData;
234 public void setConcatenatedSummaryData(String concatenatedSummaryData)
236 this.concatenatedSummaryData = concatenatedSummaryData;
239 public String[] getSummaryData()
244 public void setSummaryData(String[] summaryData)
246 this.summaryData = summaryData;
249 public String toString()
251 StringBuilder html = new StringBuilder();
252 html.append("<html><div style=\"width:" + width
253 + "; word-wrap: break-word; border-bottom-style: dotted;\"> ");
254 html.append(concatenatedSummaryData);
255 html.append("</div></html>");
256 return html.toString();
260 public int hashCode()
262 return Objects.hash(this.pdbId, this.concatenatedSummaryData);
266 public boolean equals(Object other)
268 if (!(other instanceof PDBResponseSummary))
273 PDBResponseSummary that = (PDBResponseSummary) other;
275 // Custom equality check here.
276 return this.pdbId.equals(that.pdbId)
277 && this.concatenatedSummaryData
278 .equals(that.concatenatedSummaryData);