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.Collection;
26 import java.util.HashMap;
27 import java.util.LinkedHashSet;
29 import javax.swing.JScrollPane;
30 import javax.swing.JTable;
31 import javax.swing.table.AbstractTableModel;
33 @SuppressWarnings("serial")
34 public class PDBDocFieldPreferences extends JScrollPane
36 protected JTable tbl_pdbDocFieldConfig = new JTable();
38 protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
39 tbl_pdbDocFieldConfig);
41 private HashMap<String, PDBDocField> map = new HashMap<String, PDBDocField>();
43 private static Collection<PDBDocField> searchSummaryFields = new LinkedHashSet<PDBDocField>();
45 private static Collection<PDBDocField> structureSummaryFields = new LinkedHashSet<PDBDocField>();
47 public enum PreferenceSource
49 SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
52 private PreferenceSource currentSource;
56 searchSummaryFields.add(PDBDocField.PDB_ID);
57 searchSummaryFields.add(PDBDocField.TITLE);
59 structureSummaryFields.add(PDBDocField.PDB_ID);
60 structureSummaryFields.add(PDBDocField.TITLE);
63 public PDBDocFieldPreferences(PreferenceSource source)
65 tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
66 this.getViewport().add(tbl_pdbDocFieldConfig);
67 this.currentSource = source;
69 String[] columnNames = null;
73 columnNames = new String[] { "PDB Field", "Show in search summary" };
75 case STRUCTURE_CHOOSER:
76 columnNames = new String[] { "PDB Field", "Show in structure summary" };
79 columnNames = new String[] { "PDB Field", "Show in search summary",
80 "Show in structure summary" };
86 Object[][] data = new Object[PDBDocField.values().length - 1][3];
88 for (PDBDocField field : PDBDocField.values())
90 if (field.getName().equalsIgnoreCase("all"))
98 data[x++] = new Object[] { field.getName(),
99 searchSummaryFields.contains(field) };
101 case STRUCTURE_CHOOSER:
102 data[x++] = new Object[] { field.getName(),
103 structureSummaryFields.contains(field) };
106 data[x++] = new Object[] { field.getName(),
107 searchSummaryFields.contains(field),
108 structureSummaryFields.contains(field) };
113 map.put(field.getName(), field);
116 PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
117 tbl_pdbDocFieldConfig.setModel(model);
120 public static Collection<PDBDocField> getSearchSummaryFields()
122 return searchSummaryFields;
125 public static void setSearchSummaryFields(
126 Collection<PDBDocField> searchSummaryFields)
128 PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
131 public static Collection<PDBDocField> getStructureSummaryFields()
133 return structureSummaryFields;
136 public static void setStructureSummaryFields(
137 Collection<PDBDocField> structureSummaryFields)
139 PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
142 class PDBFieldTableModel extends AbstractTableModel
145 public PDBFieldTableModel(String[] columnNames, Object[][] data)
148 this.columnNames = columnNames;
151 private Object[][] data;
153 private String[] columnNames;
155 public int getColumnCount()
157 return columnNames.length;
160 public int getRowCount()
165 public String getColumnName(int col)
167 return columnNames[col];
170 public Object getValueAt(int row, int col)
172 return data[row][col];
176 * JTable uses this method to determine the default renderer/ editor for
177 * each cell. If we didn't implement this method, then the last column would
178 * contain text ("true"/"false"), rather than a check box.
180 public Class getColumnClass(int c)
182 return getValueAt(0, c).getClass();
186 * Don't need to implement this method unless your table's editable.
188 public boolean isCellEditable(int row, int col)
190 // Note that the data/cell address is constant,
191 // no matter where the cell appears onscreen.
192 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
193 // serves as a unique id for each row.
194 return (col == 1 || col == 2) && !isPDBID(row, col);
199 * Determines whether the data in a given cell is a PDB ID.
206 public boolean isPDBID(int row, int col)
208 boolean matched = false;
209 String name = getValueAt(row, 0).toString();
210 PDBDocField pdbField = map.get(name);
211 if (pdbField == PDBDocField.PDB_ID)
219 * Don't need to implement this method unless your table's data can change.
221 public void setValueAt(Object value, int row, int col)
223 data[row][col] = value;
224 fireTableCellUpdated(row, col);
226 String name = getValueAt(row, 0).toString();
227 boolean selected = ((Boolean) value).booleanValue();
229 PDBDocField pdbField = map.get(name);
231 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
233 updatePrefs(searchSummaryFields, pdbField, selected);
235 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
237 updatePrefs(structureSummaryFields, pdbField, selected);
239 else if (currentSource == PreferenceSource.PREFERENCES)
243 updatePrefs(searchSummaryFields, pdbField, selected);
247 updatePrefs(structureSummaryFields, pdbField, selected);
252 private void updatePrefs(Collection<PDBDocField> prefConfig,
253 PDBDocField pdbField, boolean selected)
255 if (prefConfig.contains(pdbField) && !selected)
257 prefConfig.remove(pdbField);
260 if (!prefConfig.contains(pdbField) && selected)
262 prefConfig.add(pdbField);