JAL-2047 delay to allow table model initilisation to complete
[jalview.git] / src / jalview / ws / uimodel / PDBRestResponse.java
index fb227ee..3471fab 100644 (file)
 
 package jalview.ws.uimodel;
 
+import jalview.datamodel.SequenceI;
 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
+import jalview.ws.dbsources.PDBRestClient.PDBDocField.Group;
 
 import java.util.Collection;
 import java.util.Objects;
 
+import javax.swing.JTable;
 import javax.swing.table.DefaultTableModel;
 
 import org.json.simple.JSONObject;
@@ -75,10 +78,9 @@ public class PDBRestResponse
     this.searchSummary = searchSummary;
   }
 
-
   /**
-   * Convenience method to obtain a Table model for a given summary List and
-   * request
+   * Convenience method to obtain a Table model for a given summary List based
+   * on the request parameters
    * 
    * @param request
    *          the PDBRestRequest object which holds useful information for
@@ -91,12 +93,37 @@ public class PDBRestResponse
   public static DefaultTableModel getTableModel(PDBRestRequest request,
           Collection<PDBResponseSummary> summariesList)
   {
-    DefaultTableModel tableModel = new DefaultTableModel();
+    final PDBDocField[] cols = request.getWantedFields().toArray(
+            new PDBDocField[0]);
+    final int colOffset = request.getAssociatedSequence() == null ? 0 : 1;
+    DefaultTableModel tableModel = new DefaultTableModel()
+    {
+      @Override
+      public boolean isCellEditable(int row, int column)
+      {
+        return false;
+      }
+
+      @Override
+      public Class<?> getColumnClass(int columnIndex)
+      {
+        if (colOffset == 1 && columnIndex == 0)
+        {
+          return String.class;
+        }
+        if (cols[columnIndex - colOffset].getGroup().getName()
+                .equalsIgnoreCase(Group.QUALITY_MEASURES.getName()))
+        {
+          return Double.class;
+        }
+        return String.class;
+      }
 
+    };
     if (request.getAssociatedSequence() != null)
     {
-      tableModel.addColumn("Sequence"); // Create sequence column header if
-                                        // exists in the request
+      tableModel.addColumn("Ref Sequence"); // Create sequence column header if
+      // exists in the request
     }
     for (PDBDocField field : request.getWantedFields())
     {
@@ -109,56 +136,76 @@ public class PDBRestResponse
       tableModel.addRow(res.getSummaryData()); // Populate table rows with
                                                // summary list
     }
+
     return tableModel;
   }
 
   /**
    * Model for a unique response summary
    * 
-   * @author tcnofoegbu
-   *
    */
   public class PDBResponseSummary
   {
     private String pdbId;
 
-    private String[] summaryRowData;
+    private Object[] summaryRowData;
 
-    private String associatedSequence;
+    private SequenceI associatedSequence;
 
-    public PDBResponseSummary(JSONObject doc, PDBRestRequest request)
+    public PDBResponseSummary(JSONObject pdbJsonDoc, PDBRestRequest request)
     {
       Collection<PDBDocField> diplayFields = request.getWantedFields();
-      String associatedSeq = request.getAssociatedSequence();
+      SequenceI associatedSeq = request.getAssociatedSequence();
       int colCounter = 0;
-      summaryRowData = new String[(associatedSeq != null) ? diplayFields
+      summaryRowData = new Object[(associatedSeq != null) ? diplayFields
               .size() + 1 : diplayFields.size()];
       if (associatedSeq != null)
       {
-        this.associatedSequence = (associatedSeq.length() > 18) ? associatedSeq
-                .substring(0, 18) : associatedSeq;
+        this.associatedSequence = associatedSeq;
         summaryRowData[0] = associatedSequence;
         colCounter = 1;
       }
 
       for (PDBDocField field : diplayFields)
       {
-        String fieldData = (doc.get(field.getCode()) == null) ? "" : doc
-                .get(field.getCode()).toString();
-        if (field.equals(PDBDocField.PDB_ID)
-                && doc.get(PDBDocField.PDB_ID.getCode()) != null)
+        String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
+                : pdbJsonDoc.get(field.getCode()).toString();
+        if (field.equals(PDBDocField.PDB_ID))
         {
           this.pdbId = fieldData;
           summaryRowData[colCounter++] = this.pdbId;
         }
         else
         {
-          summaryRowData[colCounter++] = fieldData;
+          if (field.getGroup().getName()
+                  .equals(Group.QUALITY_MEASURES.getName()))
+          {
+            try
+            {
+              if (fieldData == null || fieldData.isEmpty())
+              {
+                summaryRowData[colCounter++] = null;
+              }
+              else
+              {
+              Double value = Double.valueOf(fieldData);
+              summaryRowData[colCounter++] = value;
+              }
+            } catch (Exception e)
+            {
+              e.printStackTrace();
+              System.out.println("offending value:" + fieldData);
+              summaryRowData[colCounter++] = 0.0;
+            }
+          }else{
+            summaryRowData[colCounter++] = (fieldData == null || fieldData
+                    .isEmpty()) ? null : fieldData;
+          }
         }
       }
     }
 
-    public String getPdbId()
+    public Object getPdbId()
     {
       return pdbId;
     }
@@ -168,33 +215,44 @@ public class PDBRestResponse
       this.pdbId = pdbId;
     }
 
-    public String[] getSummaryData()
+    public Object[] getSummaryData()
     {
       return summaryRowData;
     }
 
-    public void setSummaryData(String[] summaryData)
+    public void setSummaryData(Object[] summaryData)
     {
       this.summaryRowData = summaryData;
     }
 
+    /**
+     * Returns a string representation of this object;
+     */
     @Override
     public String toString()
     {
       StringBuilder summaryFieldValues = new StringBuilder();
-      for (String summaryField : summaryRowData)
+      for (Object summaryField : summaryRowData)
       {
-        summaryFieldValues.append(summaryField).append("\t");
+        summaryFieldValues.append(
+                summaryField == null ? " " : summaryField.toString())
+                .append("\t");
       }
       return summaryFieldValues.toString();
     }
 
+    /**
+     * Returns hash code value for this object
+     */
     @Override
     public int hashCode()
     {
       return Objects.hash(this.pdbId, this.toString());
     }
 
+    /**
+     * Indicates whether some object is equal to this one
+     */
     @Override
     public boolean equals(Object that)
     {
@@ -208,5 +266,49 @@ public class PDBRestResponse
 
   }
 
+  public static void configureTableColumn(JTable tbl_summary,
+          Collection<PDBDocField> wantedFields)
+  {
+    try
+    {
+      // wait for table model initialisation to complete
+      Thread.sleep(1200);
+    } catch (InterruptedException e1)
+    {
+      e1.printStackTrace();
+    }
+    for (PDBDocField wantedField : wantedFields)
+    {
+      try
+      {
+      if (wantedField.equals(PDBDocField.PDB_ID))
+      {
+        tbl_summary.getColumn(wantedField.getName()).setMinWidth(40);
+        tbl_summary.getColumn(wantedField.getName()).setMaxWidth(60);
+        tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(45);
+      }
+      else if (wantedField.equals(PDBDocField.TITLE))
+      {
+        tbl_summary.getColumn(wantedField.getName()).setMinWidth(300);
+        tbl_summary.getColumn(wantedField.getName()).setMaxWidth(1000);
+        tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(400);
+      }
+      else if (wantedField.getGroup() == Group.QUALITY_MEASURES)
+      {
+        tbl_summary.getColumn(wantedField.getName()).setMinWidth(50);
+        tbl_summary.getColumn(wantedField.getName()).setMaxWidth(150);
+        tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(85);
+      }
+      else
+      {
+        tbl_summary.getColumn(wantedField.getName()).setMinWidth(50);
+        tbl_summary.getColumn(wantedField.getName()).setMaxWidth(400);
+        tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(95);
+      }
+      } catch (Exception e)
+      {
+        e.printStackTrace();
+      }
+    }
+  }
 }
-