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[]
54 { "PDB Field", "Show in search summary" };
56 case STRUCTURE_CHOOSER:
57 columnNames = new String[]
58 { "PDB Field", "Show in structure summary" };
61 columnNames = new String[]
62 { "PDB Field", "Show in search summary", "Show in structure summary" };
68 Object[][] data = new Object[PDBDocField.values().length - 1][3];
70 for (PDBDocField field : PDBDocField.values())
72 if (field.getName().equalsIgnoreCase("all"))
80 data[x++] = new Object[]
81 { field.getName(), searchSummaryFields.contains(field) };
83 case STRUCTURE_CHOOSER:
84 data[x++] = new Object[]
85 { field.getName(), structureSummaryFields.contains(field) };
88 data[x++] = new Object[]
89 { field.getName(), searchSummaryFields.contains(field),
90 structureSummaryFields.contains(field) };
95 map.put(field.getName(), field);
98 PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
99 tbl_pdbDocFieldConfig.setModel(model);
102 public static Collection<PDBDocField> getSearchSummaryFields()
104 return searchSummaryFields;
107 public static void setSearchSummaryFields(
108 Collection<PDBDocField> searchSummaryFields)
110 PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
113 public static Collection<PDBDocField> getStructureSummaryFields()
115 return structureSummaryFields;
118 public static void setStructureSummaryFields(
119 Collection<PDBDocField> structureSummaryFields)
121 PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
124 class PDBFieldTableModel extends AbstractTableModel
127 public PDBFieldTableModel(String[] columnNames, Object[][] data)
130 this.columnNames = columnNames;
133 private Object[][] data;
135 private String[] columnNames;
137 public int getColumnCount()
139 return columnNames.length;
142 public int getRowCount()
147 public String getColumnName(int col)
149 return columnNames[col];
152 public Object getValueAt(int row, int col)
154 return data[row][col];
158 * JTable uses this method to determine the default renderer/ editor for
159 * each cell. If we didn't implement this method, then the last column would
160 * contain text ("true"/"false"), rather than a check box.
162 public Class getColumnClass(int c)
164 return getValueAt(0, c).getClass();
168 * Don't need to implement this method unless your table's editable.
170 public boolean isCellEditable(int row, int col)
172 // Note that the data/cell address is constant,
173 // no matter where the cell appears onscreen.
174 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
175 // serves as a unique id for each row.
176 return (col == 1 || col == 2) && !isPDBID(row, col);
181 * Determines whether the data in a given cell is a PDB ID.
188 public boolean isPDBID(int row, int col)
190 boolean matched = false;
191 String name = getValueAt(row, 0).toString();
192 PDBDocField pdbField = map.get(name);
193 if (pdbField == PDBDocField.PDB_ID)
201 * Don't need to implement this method unless your table's data can change.
203 public void setValueAt(Object value, int row, int col)
205 data[row][col] = value;
206 fireTableCellUpdated(row, col);
208 String name = getValueAt(row, 0).toString();
209 boolean selected = ((Boolean) value).booleanValue();
211 PDBDocField pdbField = map.get(name);
213 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
215 updatePrefs(searchSummaryFields, pdbField, selected);
217 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
219 updatePrefs(structureSummaryFields, pdbField, selected);
221 else if (currentSource == PreferenceSource.PREFERENCES)
225 updatePrefs(searchSummaryFields, pdbField, selected);
229 updatePrefs(structureSummaryFields, pdbField, selected);
234 private void updatePrefs(Collection<PDBDocField> prefConfig,
235 PDBDocField pdbField,
238 if (prefConfig.contains(pdbField) && !selected)
240 prefConfig.remove(pdbField);
243 if (!prefConfig.contains(pdbField) && selected)
245 prefConfig.add(pdbField);