JAL-1668 added PDBDocFieldPreference for configuring rest response summary fields
[jalview.git] / src / jalview / jbgui / PDBDocFieldPreferences.java
1 package jalview.jbgui;
2
3 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
4
5 import java.awt.Rectangle;
6 import java.util.Collection;
7 import java.util.HashMap;
8 import java.util.HashSet;
9
10 import javax.swing.JScrollPane;
11 import javax.swing.JTable;
12 import javax.swing.table.AbstractTableModel;
13
14 @SuppressWarnings("serial")
15 public class PDBDocFieldPreferences extends JScrollPane
16 {
17
18   protected JTable tbl_pdbDocFieldConfig = new JTable();
19
20   protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
21           tbl_pdbDocFieldConfig);
22
23   private HashMap<String, PDBDocField> map = new HashMap<String, PDBDocField>();
24
25   private static Collection<PDBDocField> searchSummaryFields = new HashSet<PDBDocField>();
26
27   private static Collection<PDBDocField> structureSummaryFields = new HashSet<PDBDocField>();
28
29   static
30   {
31     searchSummaryFields.add(PDBDocField.PDB_ID);
32     searchSummaryFields.add(PDBDocField.TITLE);
33     structureSummaryFields.add(PDBDocField.PDB_ID);
34     structureSummaryFields.add(PDBDocField.TITLE);
35   }
36
37   public PDBDocFieldPreferences(Rectangle position)
38   {
39     this.setBounds(position);
40     this.getViewport().add(tbl_pdbDocFieldConfig);
41
42     Object[][] data = new Object[PDBDocField.values().length - 1][3];
43     int x = 0;
44     for (PDBDocField field : PDBDocField.values())
45     {
46       if (field.getName().equalsIgnoreCase("all"))
47       {
48         continue;
49       }
50
51       data[x++] = new Object[]
52       { field.getName(), searchSummaryFields.contains(field),
53           structureSummaryFields.contains(field) };
54       map.put(field.getName(), field);
55     }
56
57     PDBFieldTableModel model = new PDBFieldTableModel(data);
58     tbl_pdbDocFieldConfig.setModel(model);
59   }
60
61   public static Collection<PDBDocField> getSearchSummaryFields()
62   {
63     return searchSummaryFields;
64   }
65
66   public static void setSearchSummaryFields(
67           Collection<PDBDocField> searchSummaryFields)
68   {
69     PDBDocFieldPreferences.searchSummaryFields = searchSummaryFields;
70   }
71
72   public static Collection<PDBDocField> getStructureSummaryFields()
73   {
74     return structureSummaryFields;
75   }
76
77   public static void setStructureSummaryFields(
78           Collection<PDBDocField> structureSummaryFields)
79   {
80     PDBDocFieldPreferences.structureSummaryFields = structureSummaryFields;
81   }
82
83   class PDBFieldTableModel extends AbstractTableModel
84   {
85
86     public PDBFieldTableModel(Object[][] data)
87     {
88       this.data = data;
89     }
90
91     private String[] columnNames = new String[]
92     { "PDB Feild", "Show in search summary",
93         "Show in structure chooser summary" };
94
95     private Object[][] data;
96
97     public int getColumnCount()
98     {
99       return columnNames.length;
100     }
101
102     public int getRowCount()
103     {
104       return data.length;
105     }
106
107     public String getColumnName(int col)
108     {
109       return columnNames[col];
110     }
111
112     public Object getValueAt(int row, int col)
113     {
114       return data[row][col];
115     }
116
117     /*
118      * JTable uses this method to determine the default renderer/ editor for
119      * each cell. If we didn't implement this method, then the last column would
120      * contain text ("true"/"false"), rather than a check box.
121      */
122     public Class getColumnClass(int c)
123     {
124       return getValueAt(0, c).getClass();
125     }
126
127     /*
128      * Don't need to implement this method unless your table's editable.
129      */
130     public boolean isCellEditable(int row, int col)
131     {
132       // Note that the data/cell address is constant,
133       // no matter where the cell appears onscreen.
134       return col == 1 || col == 2;
135
136     }
137
138     /*
139      * Don't need to implement this method unless your table's data can change.
140      */
141     public void setValueAt(Object value, int row, int col)
142     {
143       data[row][col] = value;
144       fireTableCellUpdated(row, col);
145
146       String name = getValueAt(row, 0).toString();
147       boolean selected = ((Boolean) value).booleanValue();
148
149       PDBDocField pdbField = map.get(name);
150
151       if (col == 1)
152       {
153         if (searchSummaryFields.contains(pdbField) && !selected)
154         {
155           searchSummaryFields.remove(pdbField);
156         }
157
158         if (!searchSummaryFields.contains(pdbField) && selected)
159         {
160           searchSummaryFields.add(pdbField);
161         }
162       }
163       else if (col == 2)
164       {
165         if (structureSummaryFields.contains(pdbField) && !selected)
166         {
167           structureSummaryFields.remove(pdbField);
168         }
169
170         if (!structureSummaryFields.contains(pdbField) && selected)
171         {
172           structureSummaryFields.add(pdbField);
173         }
174       }
175
176     }
177   }
178 }