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.api.StructureFTSRestClientI;
27 import jalview.fts.service.pdb.PDBFTSRestClient;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Comparator;
32 import java.util.HashMap;
33 import java.util.LinkedHashSet;
34 import java.util.List;
36 import javax.swing.JScrollPane;
37 import javax.swing.JTable;
38 import javax.swing.RowSorter;
39 import javax.swing.SortOrder;
40 import javax.swing.table.AbstractTableModel;
41 import javax.swing.table.TableModel;
42 import javax.swing.table.TableRowSorter;
45 * Helps render GUI allowing control of which columns to show for entries
46 * returned from an FTS query. TODO: push down FTSClient specific code
51 @SuppressWarnings("serial")
52 public class FTSDataColumnPreferences extends JScrollPane
54 protected JTable tbl_FTSDataColumnPrefs = new JTable();
56 protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
57 tbl_FTSDataColumnPrefs);
59 private HashMap<String, FTSDataColumnI> map = new HashMap<String, FTSDataColumnI>();
61 private Collection<FTSDataColumnI> structSummaryColumns = new LinkedHashSet<FTSDataColumnI>();
63 private Collection<FTSDataColumnI> allFTSDataColumns = new LinkedHashSet<FTSDataColumnI>();
65 public enum PreferenceSource
67 SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
70 private PreferenceSource currentSource;
72 private FTSRestClientI ftsRestClient;
74 public FTSDataColumnPreferences(PreferenceSource source,
75 FTSRestClientI ftsRestClient)
77 this.ftsRestClient = ftsRestClient;
78 if (source.equals(PreferenceSource.STRUCTURE_CHOOSER)
79 || source.equals(PreferenceSource.PREFERENCES))
81 structSummaryColumns = ((StructureFTSRestClientI) ftsRestClient)
82 .getAllDefaultDisplayedStructureDataColumns();
84 allFTSDataColumns.addAll(ftsRestClient.getAllFTSDataColumns());
86 tbl_FTSDataColumnPrefs.setAutoCreateRowSorter(true);
87 this.getViewport().add(tbl_FTSDataColumnPrefs);
88 this.currentSource = source;
90 String[] columnNames = ftsRestClient.getPreferencesColumnsFor(source);
92 Object[][] data = new Object[allFTSDataColumns.size()][3];
95 for (FTSDataColumnI field : allFTSDataColumns)
97 // jalview.bin.Console.outPrintln("allFTSDataColumns==" +
98 // allFTSDataColumns);
99 if (field.getName().equalsIgnoreCase("all"))
107 data[x++] = new Object[] { ftsRestClient
108 .getAllDefaultDisplayedFTSDataColumns().contains(field),
109 field.getName(), field.getGroup() };
110 // jalview.bin.Console.outPrintln(" PUIS " + field.getName() + " ET
112 // field.getGroup() + "X = " + x);
114 case STRUCTURE_CHOOSER:
115 data[x++] = new Object[] { structSummaryColumns.contains(field),
116 field.getName(), field.getGroup() };
119 data[x++] = new Object[] {
120 field.getName(), ftsRestClient
121 .getAllDefaultDisplayedFTSDataColumns().contains(field),
122 structSummaryColumns.contains(field) };
127 map.put(field.getName(), field);
130 FTSDataColumnPrefsTableModel model = new FTSDataColumnPrefsTableModel(
132 tbl_FTSDataColumnPrefs.setModel(model);
137 case STRUCTURE_CHOOSER:
138 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0)
139 .setPreferredWidth(30);
140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0).setMinWidth(20);
141 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0).setMaxWidth(40);
142 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(1)
143 .setPreferredWidth(150);
144 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(1).setMinWidth(150);
145 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(2)
146 .setPreferredWidth(150);
147 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(2).setMinWidth(150);
149 TableRowSorter<TableModel> sorter = new TableRowSorter<>(
150 tbl_FTSDataColumnPrefs.getModel());
151 tbl_FTSDataColumnPrefs.setRowSorter(sorter);
152 List<RowSorter.SortKey> sortKeys = new ArrayList<>();
153 int columnIndexToSort = 2;
154 sortKeys.add(new RowSorter.SortKey(columnIndexToSort,
155 SortOrder.ASCENDING));
156 sorter.setComparator(columnIndexToSort,
157 new Comparator<FTSDataColumnGroupI>()
160 public int compare(FTSDataColumnGroupI o1,
161 FTSDataColumnGroupI o2)
163 return o1.getSortOrder() - o2.getSortOrder();
166 sorter.setSortKeys(sortKeys);
167 // BH 2018 setSortKeys does a sort sorter.sort();
169 tbl_FTSDataColumnPrefs
170 .setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
179 public Collection<FTSDataColumnI> getStructureSummaryFields()
181 return structSummaryColumns;
184 class FTSDataColumnPrefsTableModel extends AbstractTableModel
187 public FTSDataColumnPrefsTableModel(String[] columnNames,
191 this.columnNames = columnNames;
194 private Object[][] data;
196 private String[] columnNames;
199 public int getColumnCount()
201 return columnNames.length;
205 public int getRowCount()
211 public String getColumnName(int col)
213 return columnNames[col];
217 public Object getValueAt(int row, int col)
219 return data[row][col];
223 * JTable uses this method to determine the default renderer/ editor for
224 * each cell. If we didn't implement this method, then the last column would
225 * contain text ("true"/"false"), rather than a check box.
228 public Class getColumnClass(int c)
230 return getValueAt(0, c).getClass();
234 * Don't need to implement this method unless your table's editable.
237 public boolean isCellEditable(int row, int col)
239 // Note that the data/cell address is constant,
240 // no matter where the cell appears onscreen.
241 // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
242 // serves as a unique id for each row.
243 // return (col == 1 || col == 2) && !isPDBID(row, col);
244 switch (currentSource)
247 case STRUCTURE_CHOOSER:
248 return (col == 0) && !isPrimaryKeyCell(row, 1);
250 return (col == 1 || col == 2) && !isPrimaryKeyCell(row, 0);
258 * Determines whether the data in a given cell is a PDB ID.
265 public boolean isPrimaryKeyCell(int row, int col)
267 String name = getValueAt(row, col).toString();
268 FTSDataColumnI pdbField = map.get(name);
269 return pdbField.isPrimaryKeyColumn();
273 * Don't need to implement this method unless your table's data can change.
276 public void setValueAt(Object value, int row, int col)
278 data[row][col] = value;
279 fireTableCellUpdated(row, col);
282 switch (currentSource)
285 case STRUCTURE_CHOOSER:
286 name = getValueAt(row, 1).toString();
289 name = getValueAt(row, 0).toString();
294 boolean selected = ((Boolean) value).booleanValue();
296 FTSDataColumnI ftsDataColumn = map.get(name);
298 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
300 updatePrefs(ftsRestClient.getAllDefaultDisplayedFTSDataColumns(),
301 ftsDataColumn, selected);
303 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
305 updatePrefs(structSummaryColumns, ftsDataColumn, selected);
307 else if (currentSource == PreferenceSource.PREFERENCES)
311 updatePrefs(ftsRestClient.getAllDefaultDisplayedFTSDataColumns(),
312 ftsDataColumn, selected);
316 updatePrefs(structSummaryColumns, ftsDataColumn, selected);
321 private void updatePrefs(Collection<FTSDataColumnI> prefConfig,
322 FTSDataColumnI dataColumn, boolean selected)
324 if (prefConfig.contains(dataColumn) && !selected)
326 prefConfig.remove(dataColumn);
329 if (!prefConfig.contains(dataColumn) && selected)
331 prefConfig.add(dataColumn);