Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 13:01:17 GMT
  2. Package jalview.fts.core

File FTSDataColumnPreferences.java

 

Coverage histogram

../../../img/srcFileCovDistChart5.png
43% of files have more coverage

Code metrics

18
98
13
3
336
237
37
0.38
7.54
4.33
2.85

Classes

Class Line # Actions
FTSDataColumnPreferences 52 52 12
0.8644067686.4%
FTSDataColumnPreferences.PreferenceSource 65 0 0
-1.0 -
FTSDataColumnPreferences.FTSDataColumnPrefsTableModel 184 46 25
0.1857142918.6%
 

Contributing tests

This file is covered by 44 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.fts.core;
22   
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;
28   
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;
35   
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;
43   
44    /**
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
47    *
48    * @author tcofoegbu
49    *
50    */
51    @SuppressWarnings("serial")
 
52    public class FTSDataColumnPreferences extends JScrollPane
53    {
54    protected JTable tbl_FTSDataColumnPrefs = new JTable();
55   
56    protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
57    tbl_FTSDataColumnPrefs);
58   
59    private HashMap<String, FTSDataColumnI> map = new HashMap<String, FTSDataColumnI>();
60   
61    private Collection<FTSDataColumnI> structSummaryColumns = new LinkedHashSet<FTSDataColumnI>();
62   
63    private Collection<FTSDataColumnI> allFTSDataColumns = new LinkedHashSet<FTSDataColumnI>();
64   
 
65    public enum PreferenceSource
66    {
67    SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
68    }
69   
70    private PreferenceSource currentSource;
71   
72    private FTSRestClientI ftsRestClient;
73   
 
74  140 toggle public FTSDataColumnPreferences(PreferenceSource source,
75    FTSRestClientI ftsRestClient)
76    {
77  140 this.ftsRestClient = ftsRestClient;
78  140 if (source.equals(PreferenceSource.STRUCTURE_CHOOSER)
79    || source.equals(PreferenceSource.PREFERENCES))
80    {
81  138 structSummaryColumns = ((StructureFTSRestClientI) ftsRestClient)
82    .getAllDefaultDisplayedStructureDataColumns();
83    }
84  140 allFTSDataColumns.addAll(ftsRestClient.getAllFTSDataColumns());
85   
86  140 tbl_FTSDataColumnPrefs.setAutoCreateRowSorter(true);
87  140 this.getViewport().add(tbl_FTSDataColumnPrefs);
88  140 this.currentSource = source;
89   
90  140 String[] columnNames = ftsRestClient.getPreferencesColumnsFor(source);
91   
92  140 Object[][] data = new Object[allFTSDataColumns.size()][3];
93   
94  140 int x = 0;
95  140 for (FTSDataColumnI field : allFTSDataColumns)
96    {
97    // jalview.bin.Console.outPrintln("allFTSDataColumns==" +
98    // allFTSDataColumns);
99  7147 if (field.getName().equalsIgnoreCase("all"))
100    {
101  69 continue;
102    }
103   
104  7078 switch (source)
105    {
106  102 case SEARCH_SUMMARY:
107  102 data[x++] = new Object[] { ftsRestClient
108    .getAllDefaultDisplayedFTSDataColumns().contains(field),
109    field.getName(), field.getGroup() };
110    // jalview.bin.Console.outPrintln(" PUIS " + field.getName() + " ET
111    // AUSSI " +
112    // field.getGroup() + "X = " + x);
113  102 break;
114  6976 case STRUCTURE_CHOOSER:
115  6976 data[x++] = new Object[] { structSummaryColumns.contains(field),
116    field.getName(), field.getGroup() };
117  6976 break;
118  0 case PREFERENCES:
119  0 data[x++] = new Object[] {
120    field.getName(), ftsRestClient
121    .getAllDefaultDisplayedFTSDataColumns().contains(field),
122    structSummaryColumns.contains(field) };
123  0 break;
124  0 default:
125  0 break;
126    }
127  7078 map.put(field.getName(), field);
128    }
129   
130  140 FTSDataColumnPrefsTableModel model = new FTSDataColumnPrefsTableModel(
131    columnNames, data);
132  140 tbl_FTSDataColumnPrefs.setModel(model);
133   
134  140 switch (source)
135    {
136  2 case SEARCH_SUMMARY:
137  138 case STRUCTURE_CHOOSER:
138  140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0)
139    .setPreferredWidth(30);
140  140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0).setMinWidth(20);
141  140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(0).setMaxWidth(40);
142  140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(1)
143    .setPreferredWidth(150);
144  140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(1).setMinWidth(150);
145  140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(2)
146    .setPreferredWidth(150);
147  140 tbl_FTSDataColumnPrefs.getColumnModel().getColumn(2).setMinWidth(150);
148   
149  140 TableRowSorter<TableModel> sorter = new TableRowSorter<>(
150    tbl_FTSDataColumnPrefs.getModel());
151  140 tbl_FTSDataColumnPrefs.setRowSorter(sorter);
152  140 List<RowSorter.SortKey> sortKeys = new ArrayList<>();
153  140 int columnIndexToSort = 2;
154  140 sortKeys.add(new RowSorter.SortKey(columnIndexToSort,
155    SortOrder.ASCENDING));
156  140 sorter.setComparator(columnIndexToSort,
157    new Comparator<FTSDataColumnGroupI>()
158    {
 
159  30338 toggle @Override
160    public int compare(FTSDataColumnGroupI o1,
161    FTSDataColumnGroupI o2)
162    {
163  30338 return o1.getSortOrder() - o2.getSortOrder();
164    }
165    });
166  140 sorter.setSortKeys(sortKeys);
167    // BH 2018 setSortKeys does a sort sorter.sort();
168   
169  140 tbl_FTSDataColumnPrefs
170    .setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
171  140 break;
172  0 case PREFERENCES:
173  0 default:
174  0 break;
175    }
176   
177    }
178   
 
179  52 toggle public Collection<FTSDataColumnI> getStructureSummaryFields()
180    {
181  52 return structSummaryColumns;
182    }
183   
 
184    class FTSDataColumnPrefsTableModel extends AbstractTableModel
185    {
186   
 
187  140 toggle public FTSDataColumnPrefsTableModel(String[] columnNames,
188    Object[][] data)
189    {
190  140 this.data = data;
191  140 this.columnNames = columnNames;
192    }
193   
194    private Object[][] data;
195   
196    private String[] columnNames;
197   
 
198  1680 toggle @Override
199    public int getColumnCount()
200    {
201  1680 return columnNames.length;
202    }
203   
 
204  700 toggle @Override
205    public int getRowCount()
206    {
207  700 return data.length;
208    }
209   
 
210  420 toggle @Override
211    public String getColumnName(int col)
212    {
213  420 return columnNames[col];
214    }
215   
 
216  61922 toggle @Override
217    public Object getValueAt(int row, int col)
218    {
219  61922 return data[row][col];
220    }
221   
222    /*
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.
226    */
 
227  280 toggle @Override
228    public Class getColumnClass(int c)
229    {
230  280 return getValueAt(0, c).getClass();
231    }
232   
233    /*
234    * Don't need to implement this method unless your table's editable.
235    */
 
236  0 toggle @Override
237    public boolean isCellEditable(int row, int col)
238    {
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  0 switch (currentSource)
245    {
246  0 case SEARCH_SUMMARY:
247  0 case STRUCTURE_CHOOSER:
248  0 return (col == 0) && !isPrimaryKeyCell(row, 1);
249  0 case PREFERENCES:
250  0 return (col == 1 || col == 2) && !isPrimaryKeyCell(row, 0);
251  0 default:
252  0 return false;
253    }
254   
255    }
256   
257    /**
258    * Determines whether the data in a given cell is a PDB ID.
259    *
260    * @param row
261    * @param col
262    * @return
263    */
264   
 
265  0 toggle public boolean isPrimaryKeyCell(int row, int col)
266    {
267  0 String name = getValueAt(row, col).toString();
268  0 FTSDataColumnI pdbField = map.get(name);
269  0 return pdbField.isPrimaryKeyColumn();
270    }
271   
272    /*
273    * Don't need to implement this method unless your table's data can change.
274    */
 
275  0 toggle @Override
276    public void setValueAt(Object value, int row, int col)
277    {
278  0 data[row][col] = value;
279  0 fireTableCellUpdated(row, col);
280   
281  0 String name = null;
282  0 switch (currentSource)
283    {
284  0 case SEARCH_SUMMARY:
285  0 case STRUCTURE_CHOOSER:
286  0 name = getValueAt(row, 1).toString();
287  0 break;
288  0 case PREFERENCES:
289  0 name = getValueAt(row, 0).toString();
290  0 break;
291  0 default:
292  0 break;
293    }
294  0 boolean selected = ((Boolean) value).booleanValue();
295   
296  0 FTSDataColumnI ftsDataColumn = map.get(name);
297   
298  0 if (currentSource == PreferenceSource.SEARCH_SUMMARY)
299    {
300  0 updatePrefs(ftsRestClient.getAllDefaultDisplayedFTSDataColumns(),
301    ftsDataColumn, selected);
302    }
303  0 else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
304    {
305  0 updatePrefs(structSummaryColumns, ftsDataColumn, selected);
306    }
307  0 else if (currentSource == PreferenceSource.PREFERENCES)
308    {
309  0 if (col == 1)
310    {
311  0 updatePrefs(ftsRestClient.getAllDefaultDisplayedFTSDataColumns(),
312    ftsDataColumn, selected);
313    }
314  0 else if (col == 2)
315    {
316  0 updatePrefs(structSummaryColumns, ftsDataColumn, selected);
317    }
318    }
319    }
320   
 
321  0 toggle private void updatePrefs(Collection<FTSDataColumnI> prefConfig,
322    FTSDataColumnI dataColumn, boolean selected)
323    {
324  0 if (prefConfig.contains(dataColumn) && !selected)
325    {
326  0 prefConfig.remove(dataColumn);
327    }
328   
329  0 if (!prefConfig.contains(dataColumn) && selected)
330    {
331  0 prefConfig.add(dataColumn);
332    }
333    }
334   
335    }
336    }