2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
21 package jalview.jbgui;
23 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
24 import jalview.ws.dbsources.PDBRestClient.PDBDocField.Group;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Comparator;
29 import java.util.HashMap;
30 import java.util.LinkedHashSet;
31 import java.util.List;
33 import javax.swing.JScrollPane;
34 import javax.swing.JTable;
35 import javax.swing.RowSorter;
36 import javax.swing.SortOrder;
37 import javax.swing.table.AbstractTableModel;
38 import javax.swing.table.TableModel;
39 import javax.swing.table.TableRowSorter;
42 @SuppressWarnings("serial")
43 public class PDBDocFieldPreferences extends JScrollPane
45 protected JTable tbl_pdbDocFieldConfig = new JTable();
47 protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
48 tbl_pdbDocFieldConfig);
50 private HashMap<String, PDBDocField> map = new HashMap<String, PDBDocField>();
52 private static Collection<PDBDocField> searchSummaryFields = new LinkedHashSet<PDBDocField>();
54 private static Collection<PDBDocField> structureSummaryFields = new LinkedHashSet<PDBDocField>();
56 public enum PreferenceSource
58 SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
61 private PreferenceSource currentSource;
65 searchSummaryFields.add(PDBDocField.PDB_ID);
66 searchSummaryFields.add(PDBDocField.TITLE);
68 structureSummaryFields.add(PDBDocField.PDB_ID);
69 structureSummaryFields.add(PDBDocField.TITLE);
72 public PDBDocFieldPreferences(PreferenceSource source)
74 tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
78 this.getViewport().add(tbl_pdbDocFieldConfig);
79 this.currentSource = source;
81 String[] columnNames = null;
85 columnNames = new String[] { "", "Display", "Group" };
87 case STRUCTURE_CHOOSER:
88 columnNames = new String[] { "", "Display", "Group" };
91 columnNames = new String[] { "PDB Field", "Show in search summary",
92 "Show in structure summary" };
98 Object[][] data = new Object[PDBDocField.values().length - 1][3];
100 for (PDBDocField field : PDBDocField.values())
102 if (field.getName().equalsIgnoreCase("all"))
110 data[x++] = new Object[] { searchSummaryFields.contains(field),
111 field.getName(), field.getGroup() };
113 case STRUCTURE_CHOOSER:
114 data[x++] = new Object[] { structureSummaryFields.contains(field),
115 field.getName(), field.getGroup() };
118 data[x++] = new Object[] { field.getName(),
119 searchSummaryFields.contains(field),
120 structureSummaryFields.contains(field) };
125 map.put(field.getName(), field);
128 PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
129 tbl_pdbDocFieldConfig.setModel(model);
131 // DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
132 // rightRenderer.setHorizontalAlignment(SwingConstants.LEFT);
133 // tbl_pdbDocFieldConfig.getColumn("Show in structure summary")
134 // .setCellRenderer(rightRenderer);
138 case STRUCTURE_CHOOSER:
139 tbl_pdbDocFieldConfig.getColumnModel().getColumn(0)
140 .setPreferredWidth(5);
141 tbl_pdbDocFieldConfig.getColumnModel().getColumn(1)
142 .setPreferredWidth(195);
143 tbl_pdbDocFieldConfig.getColumnModel().getColumn(2)
144 .setPreferredWidth(200);
146 TableRowSorter<TableModel> sorter = new TableRowSorter<>(
147 tbl_pdbDocFieldConfig.getModel());
148 tbl_pdbDocFieldConfig.setRowSorter(sorter);
149 List<RowSorter.SortKey> sortKeys = new ArrayList<>();
150 int columnIndexToSort = 2;
151 sortKeys.add(new RowSorter.SortKey(columnIndexToSort,
152 SortOrder.ASCENDING));
153 sorter.setSortKeys(sortKeys);
154 sorter.setComparator(
156 new Comparator<jalview.ws.dbsources.PDBRestClient.PDBDocField.Group>()
159 public int compare(Group o1, Group o2)
161 return o1.getSortOrder() - o2.getSortOrder();
174 public static Collection<PDBDocField> getSearchSummaryFields()
176 return searchSummaryFields;
179 public static void setSearchSummaryFields(
180 Collection<PDBDocField> searchSummaryFields)
182 PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
185 public static Collection<PDBDocField> getStructureSummaryFields()
187 return structureSummaryFields;
190 public static void setStructureSummaryFields(
191 Collection<PDBDocField> structureSummaryFields)
193 PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
196 class PDBFieldTableModel extends AbstractTableModel
199 public PDBFieldTableModel(String[] columnNames, Object[][] data)
202 this.columnNames = columnNames;
205 private Object[][] data;
207 private String[] columnNames;
210 public int getColumnCount()
212 return columnNames.length;
216 public int getRowCount()
222 public String getColumnName(int col)
224 return columnNames[col];
228 public Object getValueAt(int row, int col)
230 return data[row][col];
234 * JTable uses this method to determine the default renderer/ editor for
235 * each cell. If we didn't implement this method, then the last column would
236 * contain text ("true"/"false"), rather than a check box.
239 public Class getColumnClass(int c)
241 return getValueAt(0, c).getClass();
245 * Don't need to implement this method unless your table's editable.
248 public boolean isCellEditable(int row, int col)
250 // Note that the data/cell address is constant,
251 // no matter where the cell appears onscreen.
252 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
253 // serves as a unique id for each row.
254 // return (col == 1 || col == 2) && !isPDBID(row, col);
255 switch (currentSource)
258 case STRUCTURE_CHOOSER:
259 return (col == 0) && !isPDBID(row, 1);
261 return (col == 1 || col == 2) && !isPDBID(row, 0);
269 * Determines whether the data in a given cell is a PDB ID.
276 public boolean isPDBID(int row, int col)
278 boolean matched = false;
279 String name = getValueAt(row, col).toString();
280 PDBDocField pdbField = map.get(name);
281 if (pdbField == PDBDocField.PDB_ID)
289 * Don't need to implement this method unless your table's data can change.
292 public void setValueAt(Object value, int row, int col)
294 data[row][col] = value;
295 fireTableCellUpdated(row, col);
298 switch (currentSource)
301 case STRUCTURE_CHOOSER:
302 name = getValueAt(row, 1).toString();
305 name = getValueAt(row, 0).toString();
310 boolean selected = ((Boolean) value).booleanValue();
312 PDBDocField pdbField = map.get(name);
314 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
316 updatePrefs(searchSummaryFields, pdbField, selected);
318 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
320 updatePrefs(structureSummaryFields, pdbField, selected);
322 else if (currentSource == PreferenceSource.PREFERENCES)
326 updatePrefs(searchSummaryFields, pdbField, selected);
330 updatePrefs(structureSummaryFields, pdbField, selected);
335 private void updatePrefs(Collection<PDBDocField> prefConfig,
336 PDBDocField pdbField, boolean selected)
338 if (prefConfig.contains(pdbField) && !selected)
340 prefConfig.remove(pdbField);
343 if (!prefConfig.contains(pdbField) && selected)
345 prefConfig.add(pdbField);