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);
67 searchSummaryFields.add(PDBDocField.RESOLUTION);
69 structureSummaryFields.add(PDBDocField.PDB_ID);
70 structureSummaryFields.add(PDBDocField.TITLE);
71 structureSummaryFields.add(PDBDocField.RESOLUTION);
74 public PDBDocFieldPreferences(PreferenceSource source)
76 tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
80 this.getViewport().add(tbl_pdbDocFieldConfig);
81 this.currentSource = source;
83 String[] columnNames = null;
87 columnNames = new String[] { "", "Display", "Group" };
89 case STRUCTURE_CHOOSER:
90 columnNames = new String[] { "", "Display", "Group" };
93 columnNames = new String[] { "PDB Field", "Show in search summary",
94 "Show in structure summary" };
100 Object[][] data = new Object[PDBDocField.values().length - 1][3];
102 for (PDBDocField field : PDBDocField.values())
104 if (field.getName().equalsIgnoreCase("all"))
112 data[x++] = new Object[] { searchSummaryFields.contains(field),
113 field.getName(), field.getGroup() };
115 case STRUCTURE_CHOOSER:
116 data[x++] = new Object[] { structureSummaryFields.contains(field),
117 field.getName(), field.getGroup() };
120 data[x++] = new Object[] { field.getName(),
121 searchSummaryFields.contains(field),
122 structureSummaryFields.contains(field) };
127 map.put(field.getName(), field);
130 PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
131 tbl_pdbDocFieldConfig.setModel(model);
136 case STRUCTURE_CHOOSER:
137 tbl_pdbDocFieldConfig.getColumnModel().getColumn(0)
138 .setPreferredWidth(30);
139 tbl_pdbDocFieldConfig.getColumnModel().getColumn(0).setMinWidth(20);
140 tbl_pdbDocFieldConfig.getColumnModel().getColumn(0).setMaxWidth(40);
141 tbl_pdbDocFieldConfig.getColumnModel().getColumn(1)
142 .setPreferredWidth(150);
143 tbl_pdbDocFieldConfig.getColumnModel().getColumn(1).setMinWidth(150);
144 tbl_pdbDocFieldConfig.getColumnModel().getColumn(2)
145 .setPreferredWidth(150);
146 tbl_pdbDocFieldConfig.getColumnModel().getColumn(2)
149 TableRowSorter<TableModel> sorter = new TableRowSorter<>(
150 tbl_pdbDocFieldConfig.getModel());
151 tbl_pdbDocFieldConfig.setRowSorter(sorter);
152 List<RowSorter.SortKey> sortKeys = new ArrayList<>();
153 int columnIndexToSort = 2;
154 sortKeys.add(new RowSorter.SortKey(columnIndexToSort,
155 SortOrder.ASCENDING));
156 sorter.setSortKeys(sortKeys);
157 sorter.setComparator(
159 new Comparator<jalview.ws.dbsources.PDBRestClient.PDBDocField.Group>()
162 public int compare(Group o1, Group o2)
164 return o1.getSortOrder() - o2.getSortOrder();
169 tbl_pdbDocFieldConfig
170 .setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
179 public static Collection<PDBDocField> getSearchSummaryFields()
181 return searchSummaryFields;
184 public static void setSearchSummaryFields(
185 Collection<PDBDocField> searchSummaryFields)
187 PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
190 public static Collection<PDBDocField> getStructureSummaryFields()
192 return structureSummaryFields;
195 public static void setStructureSummaryFields(
196 Collection<PDBDocField> structureSummaryFields)
198 PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
201 class PDBFieldTableModel extends AbstractTableModel
204 public PDBFieldTableModel(String[] columnNames, Object[][] data)
207 this.columnNames = columnNames;
210 private Object[][] data;
212 private String[] columnNames;
215 public int getColumnCount()
217 return columnNames.length;
221 public int getRowCount()
227 public String getColumnName(int col)
229 return columnNames[col];
233 public Object getValueAt(int row, int col)
235 return data[row][col];
239 * JTable uses this method to determine the default renderer/ editor for
240 * each cell. If we didn't implement this method, then the last column would
241 * contain text ("true"/"false"), rather than a check box.
244 public Class getColumnClass(int c)
246 return getValueAt(0, c).getClass();
250 * Don't need to implement this method unless your table's editable.
253 public boolean isCellEditable(int row, int col)
255 // Note that the data/cell address is constant,
256 // no matter where the cell appears onscreen.
257 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
258 // serves as a unique id for each row.
259 // return (col == 1 || col == 2) && !isPDBID(row, col);
260 switch (currentSource)
263 case STRUCTURE_CHOOSER:
264 return (col == 0) && !isPDBID(row, 1);
266 return (col == 1 || col == 2) && !isPDBID(row, 0);
274 * Determines whether the data in a given cell is a PDB ID.
281 public boolean isPDBID(int row, int col)
283 boolean matched = false;
284 String name = getValueAt(row, col).toString();
285 PDBDocField pdbField = map.get(name);
286 if (pdbField == PDBDocField.PDB_ID)
294 * Don't need to implement this method unless your table's data can change.
297 public void setValueAt(Object value, int row, int col)
299 data[row][col] = value;
300 fireTableCellUpdated(row, col);
303 switch (currentSource)
306 case STRUCTURE_CHOOSER:
307 name = getValueAt(row, 1).toString();
310 name = getValueAt(row, 0).toString();
315 boolean selected = ((Boolean) value).booleanValue();
317 PDBDocField pdbField = map.get(name);
319 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
321 updatePrefs(searchSummaryFields, pdbField, selected);
323 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
325 updatePrefs(structureSummaryFields, pdbField, selected);
327 else if (currentSource == PreferenceSource.PREFERENCES)
331 updatePrefs(searchSummaryFields, pdbField, selected);
335 updatePrefs(structureSummaryFields, pdbField, selected);
340 private void updatePrefs(Collection<PDBDocField> prefConfig,
341 PDBDocField pdbField, boolean selected)
343 if (prefConfig.contains(pdbField) && !selected)
345 prefConfig.remove(pdbField);
348 if (!prefConfig.contains(pdbField) && selected)
350 prefConfig.add(pdbField);