3 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.LinkedHashSet;
9 import javax.swing.JScrollPane;
10 import javax.swing.JTable;
11 import javax.swing.table.AbstractTableModel;
13 @SuppressWarnings("serial")
14 public class PDBDocFieldPreferences extends JScrollPane
16 protected JTable tbl_pdbDocFieldConfig = new JTable();
18 protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
19 tbl_pdbDocFieldConfig);
21 private HashMap<String, PDBDocField> map = new HashMap<String, PDBDocField>();
23 private static Collection<PDBDocField> searchSummaryFields = new LinkedHashSet<PDBDocField>();
25 private static Collection<PDBDocField> structureSummaryFields = new LinkedHashSet<PDBDocField>();
27 public enum PreferenceSource
29 SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
32 private PreferenceSource currentSource;
36 searchSummaryFields.add(PDBDocField.PDB_ID);
37 searchSummaryFields.add(PDBDocField.TITLE);
39 structureSummaryFields.add(PDBDocField.PDB_ID);
40 structureSummaryFields.add(PDBDocField.TITLE);
43 public PDBDocFieldPreferences(PreferenceSource source)
45 tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
46 this.getViewport().add(tbl_pdbDocFieldConfig);
47 this.currentSource = source;
49 String[] columnNames = null;
53 columnNames = new String[] { "PDB Field", "Show in search summary" };
55 case STRUCTURE_CHOOSER:
56 columnNames = new String[] { "PDB Field", "Show in structure summary" };
59 columnNames = new String[] { "PDB Field", "Show in search summary",
60 "Show in structure summary" };
66 Object[][] data = new Object[PDBDocField.values().length - 1][3];
68 for (PDBDocField field : PDBDocField.values())
70 if (field.getName().equalsIgnoreCase("all"))
78 data[x++] = new Object[] { field.getName(),
79 searchSummaryFields.contains(field) };
81 case STRUCTURE_CHOOSER:
82 data[x++] = new Object[] { field.getName(),
83 structureSummaryFields.contains(field) };
86 data[x++] = new Object[] { field.getName(),
87 searchSummaryFields.contains(field),
88 structureSummaryFields.contains(field) };
93 map.put(field.getName(), field);
96 PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
97 tbl_pdbDocFieldConfig.setModel(model);
100 public static Collection<PDBDocField> getSearchSummaryFields()
102 return searchSummaryFields;
105 public static void setSearchSummaryFields(
106 Collection<PDBDocField> searchSummaryFields)
108 PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
111 public static Collection<PDBDocField> getStructureSummaryFields()
113 return structureSummaryFields;
116 public static void setStructureSummaryFields(
117 Collection<PDBDocField> structureSummaryFields)
119 PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
122 class PDBFieldTableModel extends AbstractTableModel
125 public PDBFieldTableModel(String[] columnNames, Object[][] data)
128 this.columnNames = columnNames;
131 private Object[][] data;
133 private String[] columnNames;
135 public int getColumnCount()
137 return columnNames.length;
140 public int getRowCount()
145 public String getColumnName(int col)
147 return columnNames[col];
150 public Object getValueAt(int row, int col)
152 return data[row][col];
156 * JTable uses this method to determine the default renderer/ editor for
157 * each cell. If we didn't implement this method, then the last column would
158 * contain text ("true"/"false"), rather than a check box.
160 public Class getColumnClass(int c)
162 return getValueAt(0, c).getClass();
166 * Don't need to implement this method unless your table's editable.
168 public boolean isCellEditable(int row, int col)
170 // Note that the data/cell address is constant,
171 // no matter where the cell appears onscreen.
172 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
173 // serves as a unique id for each row.
174 return (col == 1 || col == 2) && !isPDBID(row, col);
179 * Determines whether the data in a given cell is a PDB ID.
186 public boolean isPDBID(int row, int col)
188 boolean matched = false;
189 String name = getValueAt(row, 0).toString();
190 PDBDocField pdbField = map.get(name);
191 if (pdbField == PDBDocField.PDB_ID)
199 * Don't need to implement this method unless your table's data can change.
201 public void setValueAt(Object value, int row, int col)
203 data[row][col] = value;
204 fireTableCellUpdated(row, col);
206 String name = getValueAt(row, 0).toString();
207 boolean selected = ((Boolean) value).booleanValue();
209 PDBDocField pdbField = map.get(name);
211 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
213 updatePrefs(searchSummaryFields, pdbField, selected);
215 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
217 updatePrefs(structureSummaryFields, pdbField, selected);
219 else if (currentSource == PreferenceSource.PREFERENCES)
223 updatePrefs(searchSummaryFields, pdbField, selected);
227 updatePrefs(structureSummaryFields, pdbField, selected);
232 private void updatePrefs(Collection<PDBDocField> prefConfig,
233 PDBDocField pdbField, boolean selected)
235 if (prefConfig.contains(pdbField) && !selected)
237 prefConfig.remove(pdbField);
240 if (!prefConfig.contains(pdbField) && selected)
242 prefConfig.add(pdbField);