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.fts.core;
23 import jalview.fts.api.FTSDataColumnI;
24 import jalview.fts.api.FTSDataColumnI.FTSDataColumnGroupI;
25 import jalview.fts.api.FTSRestClientI;
26 import jalview.fts.service.pdb.PDBFTSRestClient;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Comparator;
31 import java.util.HashMap;
32 import java.util.LinkedHashSet;
33 import java.util.List;
35 import javax.swing.JScrollPane;
36 import javax.swing.JTable;
37 import javax.swing.RowSorter;
38 import javax.swing.SortOrder;
39 import javax.swing.table.AbstractTableModel;
40 import javax.swing.table.TableModel;
41 import javax.swing.table.TableRowSorter;
44 @SuppressWarnings("serial")
45 public class FTSDataColumnPreferences extends JScrollPane
47 protected JTable tbl_FTSDataColumnPrefs = new JTable();
49 protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
50 tbl_FTSDataColumnPrefs);
52 private HashMap<String, FTSDataColumnI> map = new HashMap<String, FTSDataColumnI>();
54 private Collection<FTSDataColumnI> structSummaryColumns = new LinkedHashSet<FTSDataColumnI>();
56 private Collection<FTSDataColumnI> allFTSDataColumns = new LinkedHashSet<FTSDataColumnI>();
58 public enum PreferenceSource
60 SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
63 private PreferenceSource currentSource;
65 private FTSRestClientI ftsRestClient;
67 public FTSDataColumnPreferences(PreferenceSource source,
68 FTSRestClientI ftsRestClient)
70 this.ftsRestClient = ftsRestClient;
71 if (source.equals(PreferenceSource.STRUCTURE_CHOOSER)
72 || source.equals(PreferenceSource.PREFERENCES))
74 structSummaryColumns = ((PDBFTSRestClient) ftsRestClient)
75 .getAllDefaultDisplayedStructureDataColumns();
77 allFTSDataColumns.addAll(ftsRestClient.getAllFTSDataColumns());
79 tbl_FTSDataColumnPrefs.setAutoCreateRowSorter(true);
80 this.getViewport().add(tbl_FTSDataColumnPrefs);
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[allFTSDataColumns.size() - 1][3];
103 for (FTSDataColumnI field : allFTSDataColumns)
105 if (field.getName().equalsIgnoreCase("all"))
113 data[x++] = new Object[] {
114 ftsRestClient.getAllDefaultDisplayedFTSDataColumns()
116 field.getName(), field.getGroup() };
118 case STRUCTURE_CHOOSER:
119 data[x++] = new Object[] { structSummaryColumns.contains(field),
120 field.getName(), field.getGroup() };
123 data[x++] = new Object[] { field.getName(),
124 ftsRestClient.getAllDefaultDisplayedFTSDataColumns()
126 structSummaryColumns.contains(field) };
131 map.put(field.getName(), field);
134 FTSDataColumnPrefsTableModel model = new FTSDataColumnPrefsTableModel(columnNames, data);
135 tbl_FTSDataColumnPrefs.setModel(model);
140 case STRUCTURE_CHOOSER:
141 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0)
142 .setPreferredWidth(30);
143 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0).setMinWidth(20);
144 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0).setMaxWidth(40);
145 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(1)
146 .setPreferredWidth(150);
147 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(1).setMinWidth(150);
148 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(2)
149 .setPreferredWidth(150);
150 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(2)
153 TableRowSorter<TableModel> sorter = new TableRowSorter<>(
154 tbl_FTSDataColumnPrefs.getModel());
155 tbl_FTSDataColumnPrefs.setRowSorter(sorter);
156 List<RowSorter.SortKey> sortKeys = new ArrayList<>();
157 int columnIndexToSort = 2;
158 sortKeys.add(new RowSorter.SortKey(columnIndexToSort,
159 SortOrder.ASCENDING));
160 sorter.setSortKeys(sortKeys);
161 sorter.setComparator(
163 new Comparator<FTSDataColumnGroupI>()
166 public int compare(FTSDataColumnGroupI o1,
167 FTSDataColumnGroupI o2)
169 return o1.getSortOrder() - o2.getSortOrder();
174 tbl_FTSDataColumnPrefs
175 .setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
184 public Collection<FTSDataColumnI> getStructureSummaryFields()
186 return structSummaryColumns;
189 class FTSDataColumnPrefsTableModel extends AbstractTableModel
192 public FTSDataColumnPrefsTableModel(String[] columnNames, Object[][] data)
195 this.columnNames = columnNames;
198 private Object[][] data;
200 private String[] columnNames;
203 public int getColumnCount()
205 return columnNames.length;
209 public int getRowCount()
215 public String getColumnName(int col)
217 return columnNames[col];
221 public Object getValueAt(int row, int col)
223 return data[row][col];
227 * JTable uses this method to determine the default renderer/ editor for
228 * each cell. If we didn't implement this method, then the last column would
229 * contain text ("true"/"false"), rather than a check box.
232 public Class getColumnClass(int c)
234 return getValueAt(0, c).getClass();
238 * Don't need to implement this method unless your table's editable.
241 public boolean isCellEditable(int row, int col)
243 // Note that the data/cell address is constant,
244 // no matter where the cell appears onscreen.
245 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
246 // serves as a unique id for each row.
247 // return (col == 1 || col == 2) && !isPDBID(row, col);
248 switch (currentSource)
251 case STRUCTURE_CHOOSER:
252 return (col == 0) && !isPrimaryKeyCell(row, 1);
254 return (col == 1 || col == 2) && !isPrimaryKeyCell(row, 0);
262 * Determines whether the data in a given cell is a PDB ID.
269 public boolean isPrimaryKeyCell(int row, int col)
271 String name = getValueAt(row, col).toString();
272 FTSDataColumnI pdbField = map.get(name);
273 return pdbField.isPrimaryKeyColumn();
277 * Don't need to implement this method unless your table's data can change.
280 public void setValueAt(Object value, int row, int col)
282 data[row][col] = value;
283 fireTableCellUpdated(row, col);
286 switch (currentSource)
289 case STRUCTURE_CHOOSER:
290 name = getValueAt(row, 1).toString();
293 name = getValueAt(row, 0).toString();
298 boolean selected = ((Boolean) value).booleanValue();
300 FTSDataColumnI ftsDataColumn = map.get(name);
302 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
304 updatePrefs(ftsRestClient
305 .getAllDefaultDisplayedFTSDataColumns(), ftsDataColumn,
308 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
310 updatePrefs(structSummaryColumns, ftsDataColumn, selected);
312 else if (currentSource == PreferenceSource.PREFERENCES)
316 updatePrefs(ftsRestClient
317 .getAllDefaultDisplayedFTSDataColumns(), ftsDataColumn,
322 updatePrefs(structSummaryColumns, ftsDataColumn, selected);
327 private void updatePrefs(
328 Collection<FTSDataColumnI> prefConfig,
329 FTSDataColumnI dataColumn, boolean selected)
331 if (prefConfig.contains(dataColumn) && !selected)
333 prefConfig.remove(dataColumn);
336 if (!prefConfig.contains(dataColumn) && selected)
338 prefConfig.add(dataColumn);