JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / jbgui / PDBDocFieldPreferences.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.LinkedHashSet;
28
29 import javax.swing.JScrollPane;
30 import javax.swing.JTable;
31 import javax.swing.table.AbstractTableModel;
32
33 @SuppressWarnings("serial")
34 public class PDBDocFieldPreferences extends JScrollPane
35 {
36   protected JTable tbl_pdbDocFieldConfig = new JTable();
37
38   protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
39           tbl_pdbDocFieldConfig);
40
41   private HashMap<String, PDBDocField> map = new HashMap<String, PDBDocField>();
42
43   private static Collection<PDBDocField> searchSummaryFields = new LinkedHashSet<PDBDocField>();
44
45   private static Collection<PDBDocField> structureSummaryFields = new LinkedHashSet<PDBDocField>();
46
47   public enum PreferenceSource
48   {
49     SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
50   }
51
52   private PreferenceSource currentSource;
53
54   static
55   {
56     searchSummaryFields.add(PDBDocField.PDB_ID);
57     searchSummaryFields.add(PDBDocField.TITLE);
58
59     structureSummaryFields.add(PDBDocField.PDB_ID);
60     structureSummaryFields.add(PDBDocField.TITLE);
61   }
62
63   public PDBDocFieldPreferences(PreferenceSource source)
64   {
65     tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
66     this.getViewport().add(tbl_pdbDocFieldConfig);
67     this.currentSource = source;
68
69     String[] columnNames = null;
70     switch (source)
71     {
72     case SEARCH_SUMMARY:
73       columnNames = new String[] { "PDB Field", "Show in search summary" };
74       break;
75     case STRUCTURE_CHOOSER:
76       columnNames = new String[] { "PDB Field", "Show in structure summary" };
77       break;
78     case PREFERENCES:
79       columnNames = new String[] { "PDB Field", "Show in search summary",
80           "Show in structure summary" };
81       break;
82     default:
83       break;
84     }
85
86     Object[][] data = new Object[PDBDocField.values().length - 1][3];
87     int x = 0;
88     for (PDBDocField field : PDBDocField.values())
89     {
90       if (field.getName().equalsIgnoreCase("all"))
91       {
92         continue;
93       }
94
95       switch (source)
96       {
97       case SEARCH_SUMMARY:
98         data[x++] = new Object[] { field.getName(),
99             searchSummaryFields.contains(field) };
100         break;
101       case STRUCTURE_CHOOSER:
102         data[x++] = new Object[] { field.getName(),
103             structureSummaryFields.contains(field) };
104         break;
105       case PREFERENCES:
106         data[x++] = new Object[] { field.getName(),
107             searchSummaryFields.contains(field),
108             structureSummaryFields.contains(field) };
109         break;
110       default:
111         break;
112       }
113       map.put(field.getName(), field);
114     }
115
116     PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
117     tbl_pdbDocFieldConfig.setModel(model);
118   }
119
120   public static Collection<PDBDocField> getSearchSummaryFields()
121   {
122     return searchSummaryFields;
123   }
124
125   public static void setSearchSummaryFields(
126           Collection<PDBDocField> searchSummaryFields)
127   {
128     PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
129   }
130
131   public static Collection<PDBDocField> getStructureSummaryFields()
132   {
133     return structureSummaryFields;
134   }
135
136   public static void setStructureSummaryFields(
137           Collection<PDBDocField> structureSummaryFields)
138   {
139     PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
140   }
141
142   class PDBFieldTableModel extends AbstractTableModel
143   {
144
145     public PDBFieldTableModel(String[] columnNames, Object[][] data)
146     {
147       this.data = data;
148       this.columnNames = columnNames;
149     }
150
151     private Object[][] data;
152
153     private String[] columnNames;
154
155     public int getColumnCount()
156     {
157       return columnNames.length;
158     }
159
160     public int getRowCount()
161     {
162       return data.length;
163     }
164
165     public String getColumnName(int col)
166     {
167       return columnNames[col];
168     }
169
170     public Object getValueAt(int row, int col)
171     {
172       return data[row][col];
173     }
174
175     /*
176      * JTable uses this method to determine the default renderer/ editor for
177      * each cell. If we didn't implement this method, then the last column would
178      * contain text ("true"/"false"), rather than a check box.
179      */
180     public Class getColumnClass(int c)
181     {
182       return getValueAt(0, c).getClass();
183     }
184
185     /*
186      * Don't need to implement this method unless your table's editable.
187      */
188     public boolean isCellEditable(int row, int col)
189     {
190       // Note that the data/cell address is constant,
191       // no matter where the cell appears onscreen.
192       // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
193       // serves as a unique id for each row.
194       return (col == 1 || col == 2) && !isPDBID(row, col);
195
196     }
197
198     /**
199      * Determines whether the data in a given cell is a PDB ID.
200      * 
201      * @param row
202      * @param col
203      * @return
204      */
205
206     public boolean isPDBID(int row, int col)
207     {
208       boolean matched = false;
209       String name = getValueAt(row, 0).toString();
210       PDBDocField pdbField = map.get(name);
211       if (pdbField == PDBDocField.PDB_ID)
212       {
213         matched = true;
214       }
215       return matched;
216     }
217
218     /*
219      * Don't need to implement this method unless your table's data can change.
220      */
221     public void setValueAt(Object value, int row, int col)
222     {
223       data[row][col] = value;
224       fireTableCellUpdated(row, col);
225
226       String name = getValueAt(row, 0).toString();
227       boolean selected = ((Boolean) value).booleanValue();
228
229       PDBDocField pdbField = map.get(name);
230
231       if (currentSource == PreferenceSource.SEARCH_SUMMARY)
232       {
233         updatePrefs(searchSummaryFields, pdbField, selected);
234       }
235       else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
236       {
237         updatePrefs(structureSummaryFields, pdbField, selected);
238       }
239       else if (currentSource == PreferenceSource.PREFERENCES)
240       {
241         if (col == 1)
242         {
243           updatePrefs(searchSummaryFields, pdbField, selected);
244         }
245         else if (col == 2)
246         {
247           updatePrefs(structureSummaryFields, pdbField, selected);
248         }
249       }
250     }
251
252     private void updatePrefs(Collection<PDBDocField> prefConfig,
253             PDBDocField pdbField, boolean selected)
254     {
255       if (prefConfig.contains(pdbField) && !selected)
256       {
257         prefConfig.remove(pdbField);
258       }
259
260       if (!prefConfig.contains(pdbField) && selected)
261       {
262         prefConfig.add(pdbField);
263       }
264     }
265
266   }
267 }