JAL-1270 more fix for failing Functional test
[jalview.git] / src / jalview / jbgui / PDBDocFieldPreferences.java
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.jbgui;
22
23 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
24 import jalview.ws.dbsources.PDBRestClient.PDBDocField.Group;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Comparator;
29 import java.util.HashMap;
30 import java.util.LinkedHashSet;
31 import java.util.List;
32
33 import javax.swing.JScrollPane;
34 import javax.swing.JTable;
35 import javax.swing.RowSorter;
36 import javax.swing.SortOrder;
37 import javax.swing.table.AbstractTableModel;
38 import javax.swing.table.TableModel;
39 import javax.swing.table.TableRowSorter;
40
41
42 @SuppressWarnings("serial")
43 public class PDBDocFieldPreferences extends JScrollPane
44 {
45   protected JTable tbl_pdbDocFieldConfig = new JTable();
46
47   protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
48           tbl_pdbDocFieldConfig);
49
50   private HashMap<String, PDBDocField> map = new HashMap<String, PDBDocField>();
51
52   private static Collection<PDBDocField> searchSummaryFields = new LinkedHashSet<PDBDocField>();
53
54   private static Collection<PDBDocField> structureSummaryFields = new LinkedHashSet<PDBDocField>();
55
56   public enum PreferenceSource
57   {
58     SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
59   }
60
61   private PreferenceSource currentSource;
62
63   static
64   {
65     searchSummaryFields.add(PDBDocField.PDB_ID);
66     searchSummaryFields.add(PDBDocField.TITLE);
67     searchSummaryFields.add(PDBDocField.RESOLUTION);
68
69     structureSummaryFields.add(PDBDocField.PDB_ID);
70     structureSummaryFields.add(PDBDocField.TITLE);
71     structureSummaryFields.add(PDBDocField.RESOLUTION);
72   }
73
74   public PDBDocFieldPreferences(PreferenceSource source)
75   {
76     tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
77
78
79
80     this.getViewport().add(tbl_pdbDocFieldConfig);
81     this.currentSource = source;
82
83     String[] columnNames = null;
84     switch (source)
85     {
86     case SEARCH_SUMMARY:
87       columnNames = new String[] { "", "Display", "Group" };
88       break;
89     case STRUCTURE_CHOOSER:
90       columnNames = new String[] { "", "Display", "Group" };
91       break;
92     case PREFERENCES:
93       columnNames = new String[] { "PDB Field", "Show in search summary",
94           "Show in structure summary" };
95       break;
96     default:
97       break;
98     }
99
100     Object[][] data = new Object[PDBDocField.values().length - 1][3];
101     int x = 0;
102     for (PDBDocField field : PDBDocField.values())
103     {
104       if (field.getName().equalsIgnoreCase("all"))
105       {
106         continue;
107       }
108
109       switch (source)
110       {
111       case SEARCH_SUMMARY:
112         data[x++] = new Object[] { searchSummaryFields.contains(field),
113             field.getName(), field.getGroup() };
114         break;
115       case STRUCTURE_CHOOSER:
116         data[x++] = new Object[] { structureSummaryFields.contains(field),
117             field.getName(), field.getGroup() };
118         break;
119       case PREFERENCES:
120         data[x++] = new Object[] { field.getName(),
121             searchSummaryFields.contains(field),
122             structureSummaryFields.contains(field) };
123         break;
124       default:
125         break;
126       }
127       map.put(field.getName(), field);
128     }
129
130     PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
131     tbl_pdbDocFieldConfig.setModel(model);
132
133     switch (source)
134     {
135     case SEARCH_SUMMARY:
136     case STRUCTURE_CHOOSER:
137       tbl_pdbDocFieldConfig.getColumnModel().getColumn(0)
138               .setPreferredWidth(30);
139       tbl_pdbDocFieldConfig.getColumnModel().getColumn(0).setMinWidth(20);
140       tbl_pdbDocFieldConfig.getColumnModel().getColumn(0).setMaxWidth(40);
141       tbl_pdbDocFieldConfig.getColumnModel().getColumn(1)
142               .setPreferredWidth(150);
143       tbl_pdbDocFieldConfig.getColumnModel().getColumn(1).setMinWidth(150);
144       tbl_pdbDocFieldConfig.getColumnModel().getColumn(2)
145               .setPreferredWidth(150);
146       tbl_pdbDocFieldConfig.getColumnModel().getColumn(2)
147 .setMinWidth(150);
148
149       TableRowSorter<TableModel> sorter = new TableRowSorter<>(
150               tbl_pdbDocFieldConfig.getModel());
151       tbl_pdbDocFieldConfig.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.setSortKeys(sortKeys);
157       sorter.setComparator(
158               columnIndexToSort,
159               new Comparator<jalview.ws.dbsources.PDBRestClient.PDBDocField.Group>()
160               {
161                 @Override
162                 public int compare(Group o1, Group o2)
163                 {
164                   return o1.getSortOrder() - o2.getSortOrder();
165                 }
166               });
167       sorter.sort();
168
169       tbl_pdbDocFieldConfig
170               .setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
171       break;
172     case PREFERENCES:
173     default:
174       break;
175     }
176
177   }
178
179   public static Collection<PDBDocField> getSearchSummaryFields()
180   {
181     return searchSummaryFields;
182   }
183
184   public static void setSearchSummaryFields(
185           Collection<PDBDocField> searchSummaryFields)
186   {
187     PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
188   }
189
190   public static Collection<PDBDocField> getStructureSummaryFields()
191   {
192     return structureSummaryFields;
193   }
194
195   public static void setStructureSummaryFields(
196           Collection<PDBDocField> structureSummaryFields)
197   {
198     PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
199   }
200
201   class PDBFieldTableModel extends AbstractTableModel
202   {
203
204     public PDBFieldTableModel(String[] columnNames, Object[][] data)
205     {
206       this.data = data;
207       this.columnNames = columnNames;
208     }
209
210     private Object[][] data;
211
212     private String[] columnNames;
213
214     @Override
215     public int getColumnCount()
216     {
217       return columnNames.length;
218     }
219
220     @Override
221     public int getRowCount()
222     {
223       return data.length;
224     }
225
226     @Override
227     public String getColumnName(int col)
228     {
229       return columnNames[col];
230     }
231
232     @Override
233     public Object getValueAt(int row, int col)
234     {
235       return data[row][col];
236     }
237
238     /*
239      * JTable uses this method to determine the default renderer/ editor for
240      * each cell. If we didn't implement this method, then the last column would
241      * contain text ("true"/"false"), rather than a check box.
242      */
243     @Override
244     public Class getColumnClass(int c)
245     {
246       return getValueAt(0, c).getClass();
247     }
248
249     /*
250      * Don't need to implement this method unless your table's editable.
251      */
252     @Override
253     public boolean isCellEditable(int row, int col)
254     {
255       // Note that the data/cell address is constant,
256       // no matter where the cell appears onscreen.
257       // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
258       // serves as a unique id for each row.
259       // return (col == 1 || col == 2) && !isPDBID(row, col);
260       switch (currentSource)
261       {
262       case SEARCH_SUMMARY:
263       case STRUCTURE_CHOOSER:
264         return (col == 0) && !isPDBID(row, 1);
265       case PREFERENCES:
266         return (col == 1 || col == 2) && !isPDBID(row, 0);
267       default:
268         return false;
269       }
270
271     }
272
273     /**
274      * Determines whether the data in a given cell is a PDB ID.
275      * 
276      * @param row
277      * @param col
278      * @return
279      */
280
281     public boolean isPDBID(int row, int col)
282     {
283       boolean matched = false;
284       String name = getValueAt(row, col).toString();
285       PDBDocField pdbField = map.get(name);
286       if (pdbField == PDBDocField.PDB_ID)
287       {
288         matched = true;
289       }
290       return matched;
291     }
292
293     /*
294      * Don't need to implement this method unless your table's data can change.
295      */
296     @Override
297     public void setValueAt(Object value, int row, int col)
298     {
299       data[row][col] = value;
300       fireTableCellUpdated(row, col);
301
302       String name = null;
303       switch (currentSource)
304       {
305       case SEARCH_SUMMARY:
306       case STRUCTURE_CHOOSER:
307         name = getValueAt(row, 1).toString();
308         break;
309       case PREFERENCES:
310         name = getValueAt(row, 0).toString();
311         break;
312       default:
313         break;
314       }
315       boolean selected = ((Boolean) value).booleanValue();
316
317       PDBDocField pdbField = map.get(name);
318
319       if (currentSource == PreferenceSource.SEARCH_SUMMARY)
320       {
321         updatePrefs(searchSummaryFields, pdbField, selected);
322       }
323       else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
324       {
325         updatePrefs(structureSummaryFields, pdbField, selected);
326       }
327       else if (currentSource == PreferenceSource.PREFERENCES)
328       {
329         if (col == 1)
330         {
331           updatePrefs(searchSummaryFields, pdbField, selected);
332         }
333         else if (col == 2)
334         {
335           updatePrefs(structureSummaryFields, pdbField, selected);
336         }
337       }
338     }
339
340     private void updatePrefs(Collection<PDBDocField> prefConfig,
341             PDBDocField pdbField, boolean selected)
342     {
343       if (prefConfig.contains(pdbField) && !selected)
344       {
345         prefConfig.remove(pdbField);
346       }
347
348       if (!prefConfig.contains(pdbField) && selected)
349       {
350         prefConfig.add(pdbField);
351       }
352     }
353
354   }
355 }