JAL-3048 DialogRunner changes (wip)
[jalview.git] / src / jalview / gui / JvOptionPane.java
index a141e14..92a57ca 100644 (file)
@@ -21,6 +21,7 @@
 
 package jalview.gui;
 
+import jalview.bin.Jalview;
 import jalview.util.dialogrunner.DialogRunner;
 import jalview.util.dialogrunner.DialogRunnerI;
 import jalview.util.dialogrunner.RunResponse;
@@ -36,11 +37,9 @@ import javax.swing.Icon;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 
-public class JvOptionPane extends JOptionPane
-        implements DialogRunnerI, PropertyChangeListener
+public class JvOptionPane extends JOptionPane implements DialogRunnerI,
+    PropertyChangeListener
 {
-  // BH 2018 no changes needed here.
-
   private static final long serialVersionUID = -3019167117756785229L;
 
   private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
@@ -49,10 +48,17 @@ public class JvOptionPane extends JOptionPane
 
   private Component parentComponent;
 
-  public JvOptionPane(final Component parentComponent)
+  private DialogRunnerI runner = new DialogRunner();
+
+  /*
+   * JalviewJS reports user choice in the dialog as the selected
+   * option (text); this list allows conversion to index (int)
+   */
+  List<Object> ourOptions;
+  
+  public JvOptionPane(final Component parent)
   {
-    
-    this.parentComponent = parentComponent;
+    this.parentComponent = Jalview.isJS() ? this : parent;
   }
 
   public static int showConfirmDialog(Component parentComponent,
@@ -704,18 +710,17 @@ public class JvOptionPane extends JOptionPane
     return interactiveMode;
   }
 
-  public static void setInteractiveMode(boolean interactiveMode)
+  public static void setInteractiveMode(boolean interactive)
   {
-    JvOptionPane.interactiveMode = interactiveMode;
+    JvOptionPane.interactiveMode = interactive;
   }
 
-  @SuppressWarnings("unused")
   private static String getPrefix(int messageType)
   {
-    String prefix = ""; // JavaScript only
-
-    if (/** @j2sNative true || */
-    false)
+    String prefix = ""; 
+    
+    // JavaScript only
+    if (Jalview.isJS())
     {
       switch (messageType)
       {
@@ -732,9 +737,6 @@ public class JvOptionPane extends JOptionPane
     return prefix;
   }
 
-  DialogRunner<JvOptionPane> runner = new DialogRunner(this);
-
-  private List<Object> ourOptions;
   /**
    * create a new option dialog that can be used to register responses - along
    * lines of showOptionDialog
@@ -761,7 +763,7 @@ public class JvOptionPane extends JOptionPane
 
     if (!isInteractiveMode())
     {
-      runner.firstRun((int) getMockResponse());
+      runner.handleResponse(getMockResponse());
     }
     // two uses:
     //
@@ -777,25 +779,20 @@ public class JvOptionPane extends JOptionPane
     //
     // 2) UserDefinedColors warning about saving over a name already defined
     //
-    Component parent;
-    /**
-     * @j2sNative
-     * parent = this;
-     */
-    {
-      parent = parentComponent;
-    }
-    ;
+    
     ourOptions = Arrays.asList(options);
-    int response = JOptionPane.showOptionDialog(parent, message, title,
+    
+    int response = JOptionPane.showOptionDialog(parentComponent, message, title,
             optionType, messageType, icon, options, initialValue);
-    /**
-     * @j2sNative
+    
+    /*
+     * In Java, the response is returned to this thread and handled here;
+     * (for Javascript, see propertyChange)
      */
+    if (!Jalview.isJS())
     {
-      runner.firstRun(response);
+      runner.handleResponse(response);
     }
-
   }
 
   public void showInternalDialog(JPanel mainPanel, String title,
@@ -804,66 +801,56 @@ public class JvOptionPane extends JOptionPane
   {
     if (!isInteractiveMode())
     {
-      runner.firstRun((int) getMockResponse());
-    }
-    Component parent;
-    /**
-     * @j2sNative parent = this;
-     */
-    {
-      parent = parentComponent;
+      runner.handleResponse(getMockResponse());
     }
-    ourOptions = Arrays.asList(options);
     
+    ourOptions = Arrays.asList(options);
     int response;
-    if (parent!=this) {
-
-      response = JOptionPane.showInternalOptionDialog(parent, mainPanel,
+    if (parentComponent != this) 
+    {
+      response = JOptionPane.showInternalOptionDialog(parentComponent, mainPanel,
               title, yesNoCancelOption, questionMessage, icon, options,
               initresponse);
     }
     else
     {
-      response = JOptionPane.showOptionDialog(parent, mainPanel, title,
+      response = JOptionPane.showOptionDialog(parentComponent, mainPanel, title,
               yesNoCancelOption, questionMessage, icon, options,
               initresponse);
     }
-    /**
-     * @j2sNative
-     */
+    if (!Jalview.isJS())
     {
-      runner.firstRun(response);
+      runner.handleResponse(response);
     }
     
   }
   @Override
   public JvOptionPane addResponse(RunResponse action)
   {
-
     runner.addResponse(action);
     return this;
   }
 
-  public JvOptionPane defaultResponse(Runnable runnable)
-  {
-    runner.setDefaultResponse(runnable);
-    return this;
-  }
-
+  /**
+   * JalviewJS signals option selection by a property change event
+   * for the option e.g. "OK".  This methods responds to that by
+   * running the response action that corresponds to that option.
+   * 
+   * @param evt
+   */
   @Override
   public void propertyChange(PropertyChangeEvent evt)
   {
-    int ourOption = ourOptions.indexOf(evt.getNewValue());
-    if (ourOption == -1)
+    Object newValue = evt.getNewValue();
+       int ourOption = ourOptions.indexOf(newValue);
+    if (ourOption >= 0)
     {
-      // try our luck..
-      runner.run(evt.getNewValue());
+      runner.handleResponse(ourOption);
     }
     else
     {
-      runner.run(ourOption);
+      // try our luck..
+      runner.handleResponse(newValue);
     }
   }
-
-
 }