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;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.LinkedHashSet;
29 import java.util.List;
31 import javax.swing.JScrollPane;
32 import javax.swing.JTable;
33 import javax.swing.RowSorter;
34 import javax.swing.SortOrder;
35 import javax.swing.table.AbstractTableModel;
36 import javax.swing.table.TableModel;
37 import javax.swing.table.TableRowSorter;
39 @SuppressWarnings("serial")
40 public class PDBDocFieldPreferences extends JScrollPane
42 protected JTable tbl_pdbDocFieldConfig = new JTable();
44 protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
45 tbl_pdbDocFieldConfig);
47 private HashMap<String, PDBDocField> map = new HashMap<String, PDBDocField>();
49 private static Collection<PDBDocField> searchSummaryFields = new LinkedHashSet<PDBDocField>();
51 private static Collection<PDBDocField> structureSummaryFields = new LinkedHashSet<PDBDocField>();
53 public enum PreferenceSource
55 SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
58 private PreferenceSource currentSource;
62 searchSummaryFields.add(PDBDocField.PDB_ID);
63 searchSummaryFields.add(PDBDocField.TITLE);
65 structureSummaryFields.add(PDBDocField.PDB_ID);
66 structureSummaryFields.add(PDBDocField.TITLE);
69 public PDBDocFieldPreferences(PreferenceSource source)
71 tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
75 this.getViewport().add(tbl_pdbDocFieldConfig);
76 this.currentSource = source;
78 String[] columnNames = null;
82 columnNames = new String[] { "", "Display", "Group" };
84 case STRUCTURE_CHOOSER:
85 columnNames = new String[] { " ", "Display", "Group" };
88 columnNames = new String[] { "PDB Field", "Show in search summary",
89 "Show in structure summary" };
95 Object[][] data = new Object[PDBDocField.values().length - 1][3];
97 for (PDBDocField field : PDBDocField.values())
99 if (field.getName().equalsIgnoreCase("all"))
107 data[x++] = new Object[] { searchSummaryFields.contains(field),
108 field.getName(), field.getGroup() };
110 case STRUCTURE_CHOOSER:
111 data[x++] = new Object[] { structureSummaryFields.contains(field),
112 field.getName(), field.getGroup() };
115 data[x++] = new Object[] { field.getName(),
116 searchSummaryFields.contains(field),
117 structureSummaryFields.contains(field) };
122 map.put(field.getName(), field);
125 PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
126 tbl_pdbDocFieldConfig.setModel(model);
128 // DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
129 // rightRenderer.setHorizontalAlignment(SwingConstants.LEFT);
130 // tbl_pdbDocFieldConfig.getColumn("Show in structure summary")
131 // .setCellRenderer(rightRenderer);
135 case STRUCTURE_CHOOSER:
136 tbl_pdbDocFieldConfig.getColumnModel().getColumn(0)
137 .setPreferredWidth(5);
138 tbl_pdbDocFieldConfig.getColumnModel().getColumn(1)
139 .setPreferredWidth(195);
140 tbl_pdbDocFieldConfig.getColumnModel().getColumn(2)
141 .setPreferredWidth(200);
143 TableRowSorter<TableModel> sorter = new TableRowSorter<>(
144 tbl_pdbDocFieldConfig.getModel());
145 tbl_pdbDocFieldConfig.setRowSorter(sorter);
146 List<RowSorter.SortKey> sortKeys = new ArrayList<>();
147 int columnIndexToSort = 2;
148 sortKeys.add(new RowSorter.SortKey(columnIndexToSort,
149 SortOrder.ASCENDING));
150 sorter.setSortKeys(sortKeys);
161 public static Collection<PDBDocField> getSearchSummaryFields()
163 return searchSummaryFields;
166 public static void setSearchSummaryFields(
167 Collection<PDBDocField> searchSummaryFields)
169 PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
172 public static Collection<PDBDocField> getStructureSummaryFields()
174 return structureSummaryFields;
177 public static void setStructureSummaryFields(
178 Collection<PDBDocField> structureSummaryFields)
180 PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
183 class PDBFieldTableModel extends AbstractTableModel
186 public PDBFieldTableModel(String[] columnNames, Object[][] data)
189 this.columnNames = columnNames;
192 private Object[][] data;
194 private String[] columnNames;
197 public int getColumnCount()
199 return columnNames.length;
203 public int getRowCount()
209 public String getColumnName(int col)
211 return columnNames[col];
215 public Object getValueAt(int row, int col)
217 return data[row][col];
221 * JTable uses this method to determine the default renderer/ editor for
222 * each cell. If we didn't implement this method, then the last column would
223 * contain text ("true"/"false"), rather than a check box.
226 public Class getColumnClass(int c)
228 return getValueAt(0, c).getClass();
232 * Don't need to implement this method unless your table's editable.
235 public boolean isCellEditable(int row, int col)
237 // Note that the data/cell address is constant,
238 // no matter where the cell appears onscreen.
239 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
240 // serves as a unique id for each row.
241 // return (col == 1 || col == 2) && !isPDBID(row, col);
242 switch (currentSource)
245 case STRUCTURE_CHOOSER:
246 return (col == 0) && !isPDBID(row, 1);
248 return (col == 1 || col == 2) && !isPDBID(row, 0);
256 * Determines whether the data in a given cell is a PDB ID.
263 public boolean isPDBID(int row, int col)
265 boolean matched = false;
266 String name = getValueAt(row, col).toString();
267 PDBDocField pdbField = map.get(name);
268 if (pdbField == PDBDocField.PDB_ID)
276 * Don't need to implement this method unless your table's data can change.
279 public void setValueAt(Object value, int row, int col)
281 data[row][col] = value;
282 fireTableCellUpdated(row, col);
285 switch (currentSource)
288 case STRUCTURE_CHOOSER:
289 name = getValueAt(row, 1).toString();
292 name = getValueAt(row, 0).toString();
297 boolean selected = ((Boolean) value).booleanValue();
299 PDBDocField pdbField = map.get(name);
301 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
303 updatePrefs(searchSummaryFields, pdbField, selected);
305 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
307 updatePrefs(structureSummaryFields, pdbField, selected);
309 else if (currentSource == PreferenceSource.PREFERENCES)
313 updatePrefs(searchSummaryFields, pdbField, selected);
317 updatePrefs(structureSummaryFields, pdbField, selected);
322 private void updatePrefs(Collection<PDBDocField> prefConfig,
323 PDBDocField pdbField, boolean selected)
325 if (prefConfig.contains(pdbField) && !selected)
327 prefConfig.remove(pdbField);
330 if (!prefConfig.contains(pdbField) && selected)
332 prefConfig.add(pdbField);