JAL-3626 from JAL-3253-applet CalculationChooser
authorBobHanson <hansonr@stolaf.edu>
Mon, 1 Jun 2020 22:25:24 +0000 (17:25 -0500)
committerBobHanson <hansonr@stolaf.edu>
Mon, 1 Jun 2020 22:25:24 +0000 (17:25 -0500)
-- dissociated from panel for static access by JavaScript

src/jalview/gui/CalculationChooser.java
src/jalview/gui/Preferences.java

index 097a5a0..5248306 100644 (file)
@@ -62,8 +62,13 @@ import javax.swing.event.InternalFrameAdapter;
 import javax.swing.event.InternalFrameEvent;
 
 /**
- * A dialog where a user can choose and action Tree or PCA calculation options
+ * A dialog where a user can choose and action Tree or PCA calculation options.
+ * 
+ * Allows also for dialog-free static methods openPCAPanel(...) and
+ * openTreePanel(...) for scripted use.
+ * 
  */
+@SuppressWarnings("serial")
 public class CalculationChooser extends JPanel
 {
   /*
@@ -74,7 +79,7 @@ public class CalculationChooser extends JPanel
    */
   private static boolean treeMatchGaps = true;
 
-  private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
+  private static Font VERDANA_11PT;
 
   private static final int MIN_TREE_SELECTION = 3;
 
@@ -102,7 +107,7 @@ public class CalculationChooser extends JPanel
 
   private JCheckBox shorterSequence;
 
-  final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
+  private static ComboBoxTooltipRenderer renderer; // BH was not static
 
   List<String> tips = new ArrayList<>();
 
@@ -112,6 +117,37 @@ public class CalculationChooser extends JPanel
   private PCAPanel pcaPanel;
 
   /**
+   * Open a new Tree panel on the desktop statically. Params are standard (not
+   * set by Groovy). No dialog is opened.
+   * 
+   * @param af
+   * @param treeType
+   * @param modelName
+   * @return null if successful; the string
+   *         "label.you_need_at_least_n_sequences" if number of sequences
+   *         selected is inappropriate
+   */
+  public static Object openTreePanel(AlignFrame af, String treeType,
+          String modelName)
+  {
+    return openTreePanel(af, treeType, modelName, null);
+  }
+
+  /**
+   * public static method for JalviewJS API to open a PCAPanel without
+   * necessarily using a dialog.
+   * 
+   * @param af
+   * @param modelName
+   * @return the PCAPanel, or the string "label.you_need_at_least_n_sequences"
+   *         if number of sequences selected is inappropriate
+   */
+  public static Object openPcaPanel(AlignFrame af, String modelName)
+  {
+    return openPcaPanel(af, modelName, null);
+  }
+
+  /**
    * Constructor
    * 
    * @param af
@@ -232,6 +268,10 @@ public class CalculationChooser extends JPanel
     paramsPanel.add(includeGappedColumns);
     paramsPanel.add(shorterSequence);
 
+    if (VERDANA_11PT == null)
+    {
+      VERDANA_11PT = new Font("Verdana", 0, 11);
+    }
     /*
      * OK / Cancel buttons
      */
@@ -380,7 +420,11 @@ public class CalculationChooser extends JPanel
    */
   protected JComboBox<String> buildModelOptionsList()
   {
-    final JComboBox<String> scoreModelsCombo = new JComboBox<>();
+    JComboBox<String> scoreModelsCombo = new JComboBox<>();
+    if (renderer == null)
+    {
+      renderer = new ComboBoxTooltipRenderer();
+    }
     scoreModelsCombo.setRenderer(renderer);
 
     /*
@@ -500,7 +544,8 @@ public class CalculationChooser extends JPanel
      * for backwards compatibility with Jalview < 2.8 (JAL-2962)
      */
     if (nucleotide && forPca
-            && Cache.getDefault("BLOSUM62_PCA_FOR_NUCLEOTIDE", false))
+            && Cache.getDefault(Preferences.BLOSUM62_PCA_FOR_NUCLEOTIDE,
+                    false))
     {
       filtered.add(scoreModels.getBlosum62());
     }
@@ -537,6 +582,63 @@ public class CalculationChooser extends JPanel
    */
   protected void openTreePanel(String modelName, SimilarityParamsI params)
   {
+    Object ret = openTreePanel(af,
+            neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
+                    : TreeBuilder.AVERAGE_DISTANCE,
+            modelName, params);
+    if (ret instanceof String)
+    {
+      JvOptionPane.showMessageDialog(this, // was opening on Desktop?
+              MessageManager.formatMessage(
+                      (String) ret,
+                      MIN_TREE_SELECTION),
+              MessageManager.getString("label.not_enough_sequences"),
+              JvOptionPane.WARNING_MESSAGE);
+
+    }
+  }
+
+  /**
+   * Open a new PCA panel on the desktop
+   * 
+   * @param modelName
+   * @param params
+   */
+  protected void openPcaPanel(String modelName, SimilarityParamsI params)
+  {
+    Object ret = openPcaPanel(af, modelName, params);
+    if (ret instanceof String)
+    {
+      JvOptionPane.showInternalMessageDialog(this,
+              MessageManager.formatMessage(
+                      (String) ret,
+                      MIN_PCA_SELECTION),
+              MessageManager
+                      .getString("label.sequence_selection_insufficient"),
+              JvOptionPane.WARNING_MESSAGE);
+    }
+    else
+    {
+      // only used for test suite
+      pcaPanel = (PCAPanel) ret;
+    }
+
+  }
+
+  /**
+   * Open a new Tree panel on the desktop statically
+   * 
+   * @param af
+   * @param treeType
+   * @param modelName
+   * @param params
+   * @return null, or the string "label.you_need_at_least_n_sequences" if number
+   *         of sequences selected is inappropriate
+   */
+  public static Object openTreePanel(AlignFrame af, String treeType,
+          String modelName, SimilarityParamsI params)
+  {
+
     /*
      * gui validation shouldn't allow insufficient sequences here, but leave
      * this check in in case this method gets exposed programmatically in future
@@ -545,56 +647,58 @@ public class CalculationChooser extends JPanel
     SequenceGroup sg = viewport.getSelectionGroup();
     if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
     {
-      JvOptionPane.showMessageDialog(Desktop.desktop,
-              MessageManager.formatMessage(
-                      "label.you_need_at_least_n_sequences",
-                      MIN_TREE_SELECTION),
-              MessageManager.getString("label.not_enough_sequences"),
-              JvOptionPane.WARNING_MESSAGE);
-      return;
+      return "label.you_need_at_least_n_sequences";
+    }
+
+    if (params == null)
+    {
+      params = getSimilarityParameters(false);
     }
 
-    String treeType = neighbourJoining.isSelected()
-            ? TreeBuilder.NEIGHBOUR_JOINING
-            : TreeBuilder.AVERAGE_DISTANCE;
     af.newTreePanel(treeType, modelName, params);
+    return null;
   }
 
   /**
-   * Open a new PCA panel on the desktop
+   * public static method for JalviewJS API
    * 
+   * @param af
    * @param modelName
    * @param params
+   * @return the PCAPanel, or null if number of sequences selected is
+   *         inappropriate
    */
-  protected void openPcaPanel(String modelName, SimilarityParamsI params)
+  public static Object openPcaPanel(AlignFrame af, String modelName,
+          SimilarityParamsI params)
   {
+
     AlignViewport viewport = af.getViewport();
 
     /*
      * gui validation shouldn't allow insufficient sequences here, but leave
      * this check in in case this method gets exposed programmatically in future
+     * 
+     * 
      */
     if (((viewport.getSelectionGroup() != null)
             && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
             && (viewport.getSelectionGroup().getSize() > 0))
             || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
     {
-      JvOptionPane.showInternalMessageDialog(this,
-              MessageManager.formatMessage(
-                      "label.you_need_at_least_n_sequences",
-                      MIN_PCA_SELECTION),
-              MessageManager
-                      .getString("label.sequence_selection_insufficient"),
-              JvOptionPane.WARNING_MESSAGE);
-      return;
+      return "label.you_need_at_least_n_sequences";
+    }
+
+    if (params == null)
+    {
+      params = getSimilarityParameters(true);
     }
 
     /*
      * construct the panel and kick off its calculation thread
      */
-    pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
-    new Thread(pcaPanel).start();
-
+    PCAPanel pcap = new PCAPanel(af.alignPanel, modelName, params);
+    new Thread(pcap).start();
+    return pcap;
   }
 
   /**
@@ -610,6 +714,7 @@ public class CalculationChooser extends JPanel
     }
   }
 
+
   /**
    * Returns a data bean holding parameters for similarity (or distance) model
    * calculation
@@ -617,7 +722,8 @@ public class CalculationChooser extends JPanel
    * @param doPCA
    * @return
    */
-  protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
+  public static SimilarityParamsI getSimilarityParameters(
+          boolean doPCA)
   {
     // commented out: parameter choices read from gui widgets
     // SimilarityParamsI params = new SimilarityParams(
@@ -638,6 +744,7 @@ public class CalculationChooser extends JPanel
 
     return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
             matchOnShortestLength);
+
   }
 
   /**
index 66962d6..bd795af 100755 (executable)
@@ -100,6 +100,8 @@ public class Preferences extends GPreferences
 
   public static final String AUTOASSOCIATE_PDBANDSEQS = "AUTOASSOCIATE_PDBANDSEQS";
 
+  public static final String BLOSUM62_PCA_FOR_NUCLEOTIDE = "BLOSUM62_PCA_FOR_NUCLEOTIDE";
+
   public static final String CENTRE_COLUMN_LABELS = "CENTRE_COLUMN_LABELS";
 
   public static final String CHIMERA_PATH = "CHIMERA_PATH";