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;
43 @SuppressWarnings("serial")
44 public class FTSDataColumnPreferences extends JScrollPane
46 protected JTable tbl_FTSDataColumnPrefs = new JTable();
48 protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
49 tbl_FTSDataColumnPrefs);
51 private HashMap<String, FTSDataColumnI> map = new HashMap<String, FTSDataColumnI>();
53 private Collection<FTSDataColumnI> structSummaryColumns = new LinkedHashSet<FTSDataColumnI>();
55 private Collection<FTSDataColumnI> allFTSDataColumns = new LinkedHashSet<FTSDataColumnI>();
57 public enum PreferenceSource
59 SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
62 private PreferenceSource currentSource;
64 private FTSRestClientI ftsRestClient;
66 public FTSDataColumnPreferences(PreferenceSource source,
67 FTSRestClientI ftsRestClient)
69 this.ftsRestClient = ftsRestClient;
70 if (source.equals(PreferenceSource.STRUCTURE_CHOOSER)
71 || source.equals(PreferenceSource.PREFERENCES))
73 structSummaryColumns = ((PDBFTSRestClient) ftsRestClient)
74 .getAllDefaultDisplayedStructureDataColumns();
76 allFTSDataColumns.addAll(ftsRestClient.getAllFTSDataColumns());
78 tbl_FTSDataColumnPrefs.setAutoCreateRowSorter(true);
79 this.getViewport().add(tbl_FTSDataColumnPrefs);
80 this.currentSource = source;
82 String[] columnNames = null;
86 columnNames = new String[] { "", "Display", "Group" };
88 case STRUCTURE_CHOOSER:
89 columnNames = new String[] { "", "Display", "Group" };
92 columnNames = new String[] { "PDB Field", "Show in search summary",
93 "Show in structure summary" };
99 Object[][] data = new Object[allFTSDataColumns.size() - 1][3];
102 for (FTSDataColumnI field : allFTSDataColumns)
104 if (field.getName().equalsIgnoreCase("all"))
112 data[x++] = new Object[] {
113 ftsRestClient.getAllDefaultDisplayedFTSDataColumns().contains(
114 field), field.getName(), field.getGroup() };
116 case STRUCTURE_CHOOSER:
117 data[x++] = new Object[] { structSummaryColumns.contains(field),
118 field.getName(), field.getGroup() };
121 data[x++] = new Object[] {
123 ftsRestClient.getAllDefaultDisplayedFTSDataColumns().contains(
124 field), structSummaryColumns.contains(field) };
129 map.put(field.getName(), field);
132 FTSDataColumnPrefsTableModel model = new FTSDataColumnPrefsTableModel(
134 tbl_FTSDataColumnPrefs.setModel(model);
139 case STRUCTURE_CHOOSER:
140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0)
141 .setPreferredWidth(30);
142 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0).setMinWidth(20);
143 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0).setMaxWidth(40);
144 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(1)
145 .setPreferredWidth(150);
146 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(1).setMinWidth(150);
147 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(2)
148 .setPreferredWidth(150);
149 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(2).setMinWidth(150);
151 TableRowSorter<TableModel> sorter = new TableRowSorter<>(
152 tbl_FTSDataColumnPrefs.getModel());
153 tbl_FTSDataColumnPrefs.setRowSorter(sorter);
154 List<RowSorter.SortKey> sortKeys = new ArrayList<>();
155 int columnIndexToSort = 2;
156 sortKeys.add(new RowSorter.SortKey(columnIndexToSort,
157 SortOrder.ASCENDING));
158 sorter.setSortKeys(sortKeys);
159 sorter.setComparator(columnIndexToSort,
160 new Comparator<FTSDataColumnGroupI>()
163 public int compare(FTSDataColumnGroupI o1,
164 FTSDataColumnGroupI o2)
166 return o1.getSortOrder() - o2.getSortOrder();
171 tbl_FTSDataColumnPrefs
172 .setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
181 public Collection<FTSDataColumnI> getStructureSummaryFields()
183 return structSummaryColumns;
186 class FTSDataColumnPrefsTableModel extends AbstractTableModel
189 public FTSDataColumnPrefsTableModel(String[] columnNames,
193 this.columnNames = columnNames;
196 private Object[][] data;
198 private String[] columnNames;
201 public int getColumnCount()
203 return columnNames.length;
207 public int getRowCount()
213 public String getColumnName(int col)
215 return columnNames[col];
219 public Object getValueAt(int row, int col)
221 return data[row][col];
225 * JTable uses this method to determine the default renderer/ editor for
226 * each cell. If we didn't implement this method, then the last column would
227 * contain text ("true"/"false"), rather than a check box.
230 public Class getColumnClass(int c)
232 return getValueAt(0, c).getClass();
236 * Don't need to implement this method unless your table's editable.
239 public boolean isCellEditable(int row, int col)
241 // Note that the data/cell address is constant,
242 // no matter where the cell appears onscreen.
243 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
244 // serves as a unique id for each row.
245 // return (col == 1 || col == 2) && !isPDBID(row, col);
246 switch (currentSource)
249 case STRUCTURE_CHOOSER:
250 return (col == 0) && !isPrimaryKeyCell(row, 1);
252 return (col == 1 || col == 2) && !isPrimaryKeyCell(row, 0);
260 * Determines whether the data in a given cell is a PDB ID.
267 public boolean isPrimaryKeyCell(int row, int col)
269 String name = getValueAt(row, col).toString();
270 FTSDataColumnI pdbField = map.get(name);
271 return pdbField.isPrimaryKeyColumn();
275 * Don't need to implement this method unless your table's data can change.
278 public void setValueAt(Object value, int row, int col)
280 data[row][col] = value;
281 fireTableCellUpdated(row, col);
284 switch (currentSource)
287 case STRUCTURE_CHOOSER:
288 name = getValueAt(row, 1).toString();
291 name = getValueAt(row, 0).toString();
296 boolean selected = ((Boolean) value).booleanValue();
298 FTSDataColumnI ftsDataColumn = map.get(name);
300 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
302 updatePrefs(ftsRestClient.getAllDefaultDisplayedFTSDataColumns(),
303 ftsDataColumn, selected);
305 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
307 updatePrefs(structSummaryColumns, ftsDataColumn, selected);
309 else if (currentSource == PreferenceSource.PREFERENCES)
313 updatePrefs(ftsRestClient.getAllDefaultDisplayedFTSDataColumns(),
314 ftsDataColumn, selected);
318 updatePrefs(structSummaryColumns, ftsDataColumn, selected);
323 private void updatePrefs(Collection<FTSDataColumnI> prefConfig,
324 FTSDataColumnI dataColumn, boolean selected)
326 if (prefConfig.contains(dataColumn) && !selected)
328 prefConfig.remove(dataColumn);
331 if (!prefConfig.contains(dataColumn) && selected)
333 prefConfig.add(dataColumn);