JAL-1270 more fix for failing Functional test
[jalview.git] / src / jalview / jbgui / PDBDocFieldPreferences.java
index 2021d0b..e33cf01 100644 (file)
@@ -1,14 +1,43 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.jbgui;
 
 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
+import jalview.ws.dbsources.PDBRestClient.PDBDocField.Group;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Comparator;
 import java.util.HashMap;
-import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
 
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
+import javax.swing.RowSorter;
+import javax.swing.SortOrder;
 import javax.swing.table.AbstractTableModel;
+import javax.swing.table.TableModel;
+import javax.swing.table.TableRowSorter;
+
 
 @SuppressWarnings("serial")
 public class PDBDocFieldPreferences extends JScrollPane
@@ -20,9 +49,9 @@ public class PDBDocFieldPreferences extends JScrollPane
 
   private HashMap<String, PDBDocField> map = new HashMap<String, PDBDocField>();
 
-  private static Collection<PDBDocField> searchSummaryFields = new HashSet<PDBDocField>();
+  private static Collection<PDBDocField> searchSummaryFields = new LinkedHashSet<PDBDocField>();
 
-  private static Collection<PDBDocField> structureSummaryFields = new HashSet<PDBDocField>();
+  private static Collection<PDBDocField> structureSummaryFields = new LinkedHashSet<PDBDocField>();
 
   public enum PreferenceSource
   {
@@ -35,14 +64,19 @@ public class PDBDocFieldPreferences extends JScrollPane
   {
     searchSummaryFields.add(PDBDocField.PDB_ID);
     searchSummaryFields.add(PDBDocField.TITLE);
+    searchSummaryFields.add(PDBDocField.RESOLUTION);
 
     structureSummaryFields.add(PDBDocField.PDB_ID);
     structureSummaryFields.add(PDBDocField.TITLE);
+    structureSummaryFields.add(PDBDocField.RESOLUTION);
   }
 
   public PDBDocFieldPreferences(PreferenceSource source)
   {
     tbl_pdbDocFieldConfig.setAutoCreateRowSorter(true);
+
+
+
     this.getViewport().add(tbl_pdbDocFieldConfig);
     this.currentSource = source;
 
@@ -50,16 +84,14 @@ public class PDBDocFieldPreferences extends JScrollPane
     switch (source)
     {
     case SEARCH_SUMMARY:
-      columnNames = new String[]
-      { "PDB Feild", "Show in search summary" };
+      columnNames = new String[] { "", "Display", "Group" };
       break;
     case STRUCTURE_CHOOSER:
-      columnNames = new String[]
-      { "PDB Feild", "Show in structure summary" };
+      columnNames = new String[] { "", "Display", "Group" };
       break;
     case PREFERENCES:
-      columnNames = new String[]
-      { "PDB Feild", "Show in search summary", "Show in structure summary" };
+      columnNames = new String[] { "PDB Field", "Show in search summary",
+          "Show in structure summary" };
       break;
     default:
       break;
@@ -77,16 +109,16 @@ public class PDBDocFieldPreferences extends JScrollPane
       switch (source)
       {
       case SEARCH_SUMMARY:
-        data[x++] = new Object[]
-        { field.getName(), searchSummaryFields.contains(field) };
+        data[x++] = new Object[] { searchSummaryFields.contains(field),
+            field.getName(), field.getGroup() };
         break;
       case STRUCTURE_CHOOSER:
-        data[x++] = new Object[]
-        { field.getName(), structureSummaryFields.contains(field) };
+        data[x++] = new Object[] { structureSummaryFields.contains(field),
+            field.getName(), field.getGroup() };
         break;
       case PREFERENCES:
-        data[x++] = new Object[]
-        { field.getName(), searchSummaryFields.contains(field),
+        data[x++] = new Object[] { field.getName(),
+            searchSummaryFields.contains(field),
             structureSummaryFields.contains(field) };
         break;
       default:
@@ -97,6 +129,51 @@ public class PDBDocFieldPreferences extends JScrollPane
 
     PDBFieldTableModel model = new PDBFieldTableModel(columnNames, data);
     tbl_pdbDocFieldConfig.setModel(model);
+
+    switch (source)
+    {
+    case SEARCH_SUMMARY:
+    case STRUCTURE_CHOOSER:
+      tbl_pdbDocFieldConfig.getColumnModel().getColumn(0)
+              .setPreferredWidth(30);
+      tbl_pdbDocFieldConfig.getColumnModel().getColumn(0).setMinWidth(20);
+      tbl_pdbDocFieldConfig.getColumnModel().getColumn(0).setMaxWidth(40);
+      tbl_pdbDocFieldConfig.getColumnModel().getColumn(1)
+              .setPreferredWidth(150);
+      tbl_pdbDocFieldConfig.getColumnModel().getColumn(1).setMinWidth(150);
+      tbl_pdbDocFieldConfig.getColumnModel().getColumn(2)
+              .setPreferredWidth(150);
+      tbl_pdbDocFieldConfig.getColumnModel().getColumn(2)
+.setMinWidth(150);
+
+      TableRowSorter<TableModel> sorter = new TableRowSorter<>(
+              tbl_pdbDocFieldConfig.getModel());
+      tbl_pdbDocFieldConfig.setRowSorter(sorter);
+      List<RowSorter.SortKey> sortKeys = new ArrayList<>();
+      int columnIndexToSort = 2;
+      sortKeys.add(new RowSorter.SortKey(columnIndexToSort,
+              SortOrder.ASCENDING));
+      sorter.setSortKeys(sortKeys);
+      sorter.setComparator(
+              columnIndexToSort,
+              new Comparator<jalview.ws.dbsources.PDBRestClient.PDBDocField.Group>()
+              {
+                @Override
+                public int compare(Group o1, Group o2)
+                {
+                  return o1.getSortOrder() - o2.getSortOrder();
+                }
+              });
+      sorter.sort();
+
+      tbl_pdbDocFieldConfig
+              .setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
+      break;
+    case PREFERENCES:
+    default:
+      break;
+    }
+
   }
 
   public static Collection<PDBDocField> getSearchSummaryFields()
@@ -134,21 +211,25 @@ public class PDBDocFieldPreferences extends JScrollPane
 
     private String[] columnNames;
 
+    @Override
     public int getColumnCount()
     {
       return columnNames.length;
     }
 
+    @Override
     public int getRowCount()
     {
       return data.length;
     }
 
+    @Override
     public String getColumnName(int col)
     {
       return columnNames[col];
     }
 
+    @Override
     public Object getValueAt(int row, int col)
     {
       return data[row][col];
@@ -159,6 +240,7 @@ public class PDBDocFieldPreferences extends JScrollPane
      * each cell. If we didn't implement this method, then the last column would
      * contain text ("true"/"false"), rather than a check box.
      */
+    @Override
     public Class getColumnClass(int c)
     {
       return getValueAt(0, c).getClass();
@@ -167,23 +249,69 @@ public class PDBDocFieldPreferences extends JScrollPane
     /*
      * Don't need to implement this method unless your table's editable.
      */
+    @Override
     public boolean isCellEditable(int row, int col)
     {
       // Note that the data/cell address is constant,
       // no matter where the cell appears onscreen.
-      return col == 1 || col == 2;
+      // !isPDBID(row, col) ensures the PDB_Id cell is never editable as it
+      // serves as a unique id for each row.
+      // return (col == 1 || col == 2) && !isPDBID(row, col);
+      switch (currentSource)
+      {
+      case SEARCH_SUMMARY:
+      case STRUCTURE_CHOOSER:
+        return (col == 0) && !isPDBID(row, 1);
+      case PREFERENCES:
+        return (col == 1 || col == 2) && !isPDBID(row, 0);
+      default:
+        return false;
+      }
+
+    }
+
+    /**
+     * Determines whether the data in a given cell is a PDB ID.
+     * 
+     * @param row
+     * @param col
+     * @return
+     */
 
+    public boolean isPDBID(int row, int col)
+    {
+      boolean matched = false;
+      String name = getValueAt(row, col).toString();
+      PDBDocField pdbField = map.get(name);
+      if (pdbField == PDBDocField.PDB_ID)
+      {
+        matched = true;
+      }
+      return matched;
     }
 
     /*
      * Don't need to implement this method unless your table's data can change.
      */
+    @Override
     public void setValueAt(Object value, int row, int col)
     {
       data[row][col] = value;
       fireTableCellUpdated(row, col);
 
-      String name = getValueAt(row, 0).toString();
+      String name = null;
+      switch (currentSource)
+      {
+      case SEARCH_SUMMARY:
+      case STRUCTURE_CHOOSER:
+        name = getValueAt(row, 1).toString();
+        break;
+      case PREFERENCES:
+        name = getValueAt(row, 0).toString();
+        break;
+      default:
+        break;
+      }
       boolean selected = ((Boolean) value).booleanValue();
 
       PDBDocField pdbField = map.get(name);
@@ -210,8 +338,7 @@ public class PDBDocFieldPreferences extends JScrollPane
     }
 
     private void updatePrefs(Collection<PDBDocField> prefConfig,
-            PDBDocField pdbField,
-            boolean selected)
+            PDBDocField pdbField, boolean selected)
     {
       if (prefConfig.contains(pdbField) && !selected)
       {