JAL-1668 Tab for summary config, auto refresh after config and table column sorting
[jalview.git] / src / jalview / jbgui / PDBDocFieldPreferences.java
index 9380380..2021d0b 100644 (file)
@@ -2,7 +2,6 @@ package jalview.jbgui;
 
 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
 
-import java.awt.Rectangle;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -14,7 +13,6 @@ import javax.swing.table.AbstractTableModel;
 @SuppressWarnings("serial")
 public class PDBDocFieldPreferences extends JScrollPane
 {
-
   protected JTable tbl_pdbDocFieldConfig = new JTable();
 
   protected JScrollPane scrl_pdbDocFieldConfig = new JScrollPane(
@@ -26,18 +24,46 @@ public class PDBDocFieldPreferences extends JScrollPane
 
   private static Collection<PDBDocField> structureSummaryFields = new HashSet<PDBDocField>();
 
+  public enum PreferenceSource
+  {
+    SEARCH_SUMMARY, STRUCTURE_CHOOSER, PREFERENCES;
+  }
+
+  private PreferenceSource currentSource;
+
   static
   {
     searchSummaryFields.add(PDBDocField.PDB_ID);
     searchSummaryFields.add(PDBDocField.TITLE);
+
     structureSummaryFields.add(PDBDocField.PDB_ID);
     structureSummaryFields.add(PDBDocField.TITLE);
   }
 
-  public PDBDocFieldPreferences(Rectangle position)
+  public PDBDocFieldPreferences(PreferenceSource source)
   {
-    this.setBounds(position);
+    tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
     this.getViewport().add(tbl_pdbDocFieldConfig);
+    this.currentSource = source;
+
+    String[] columnNames = null;
+    switch (source)
+    {
+    case SEARCH_SUMMARY:
+      columnNames = new String[]
+      { "PDB Feild", "Show in search summary" };
+      break;
+    case STRUCTURE_CHOOSER:
+      columnNames = new String[]
+      { "PDB Feild", "Show in structure summary" };
+      break;
+    case PREFERENCES:
+      columnNames = new String[]
+      { "PDB Feild", "Show in search summary", "Show in structure summary" };
+      break;
+    default:
+      break;
+    }
 
     Object[][] data = new Object[PDBDocField.values().length - 1][3];
     int x = 0;
@@ -48,13 +74,28 @@ public class PDBDocFieldPreferences extends JScrollPane
         continue;
       }
 
-      data[x++] = new Object[]
-      { field.getName(), searchSummaryFields.contains(field),
-          structureSummaryFields.contains(field) };
+      switch (source)
+      {
+      case SEARCH_SUMMARY:
+        data[x++] = new Object[]
+        { field.getName(), searchSummaryFields.contains(field) };
+        break;
+      case STRUCTURE_CHOOSER:
+        data[x++] = new Object[]
+        { field.getName(), structureSummaryFields.contains(field) };
+        break;
+      case PREFERENCES:
+        data[x++] = new Object[]
+        { field.getName(), searchSummaryFields.contains(field),
+            structureSummaryFields.contains(field) };
+        break;
+      default:
+        break;
+      }
       map.put(field.getName(), field);
     }
 
-    PDBFieldTableModel model = new PDBFieldTableModel(data);
+    PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
     tbl_pdbDocFieldConfig.setModel(model);
   }
 
@@ -83,17 +124,16 @@ public class PDBDocFieldPreferences extends JScrollPane
   class PDBFieldTableModel extends AbstractTableModel
   {
 
-    public PDBFieldTableModel(Object[][] data)
+    public PDBFieldTableModel(String[] columnNames, Object[][] data)
     {
       this.data = data;
+      this.columnNames = columnNames;
     }
 
-    private String[] columnNames = new String[]
-    { "PDB Feild", "Show in search summary",
-        "Show in structure chooser summary" };
-
     private Object[][] data;
 
+    private String[] columnNames;
+
     public int getColumnCount()
     {
       return columnNames.length;
@@ -148,31 +188,41 @@ public class PDBDocFieldPreferences extends JScrollPane
 
       PDBDocField pdbField = map.get(name);
 
-      if (col == 1)
+      if (currentSource == PreferenceSource.SEARCH_SUMMARY)
       {
-        if (searchSummaryFields.contains(pdbField) && !selected)
-        {
-          searchSummaryFields.remove(pdbField);
-        }
-
-        if (!searchSummaryFields.contains(pdbField) && selected)
-        {
-          searchSummaryFields.add(pdbField);
-        }
+        updatePrefs(searchSummaryFields, pdbField, selected);
       }
-      else if (col == 2)
+      else if (currentSource == PreferenceSource.STRUCTURE_CHOOSER)
       {
-        if (structureSummaryFields.contains(pdbField) && !selected)
+        updatePrefs(structureSummaryFields, pdbField, selected);
+      }
+      else if (currentSource == PreferenceSource.PREFERENCES)
+      {
+        if (col == 1)
         {
-          structureSummaryFields.remove(pdbField);
+          updatePrefs(searchSummaryFields, pdbField, selected);
         }
-
-        if (!structureSummaryFields.contains(pdbField) && selected)
+        else if (col == 2)
         {
-          structureSummaryFields.add(pdbField);
+          updatePrefs(structureSummaryFields, pdbField, selected);
         }
       }
+    }
+
+    private void updatePrefs(Collection<PDBDocField> prefConfig,
+            PDBDocField pdbField,
+            boolean selected)
+    {
+      if (prefConfig.contains(pdbField) && !selected)
+      {
+        prefConfig.remove(pdbField);
+      }
 
+      if (!prefConfig.contains(pdbField) && selected)
+      {
+        prefConfig.add(pdbField);
+      }
     }
+
   }
 }