JAL-3048 remove copied todo
[jalview.git] / src / jalview / gui / JvOptionPane.java
index e1d76f1..89899e2 100644 (file)
 
 package jalview.gui;
 
+import jalview.util.dialogrunner.DialogRunner;
+import jalview.util.dialogrunner.DialogRunnerI;
+import jalview.util.dialogrunner.RunResponse;
+
 import java.awt.Component;
 import java.awt.HeadlessException;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.Arrays;
+import java.util.List;
 
 import javax.swing.Icon;
 import javax.swing.JOptionPane;
+import javax.swing.JPanel;
 
 public class JvOptionPane extends JOptionPane
+        implements DialogRunnerI, PropertyChangeListener
 {
   // BH 2018 no changes needed here.
 
@@ -37,6 +47,14 @@ public class JvOptionPane extends JOptionPane
 
   private static boolean interactiveMode = true;
 
+  private Component parentComponent;
+
+  public JvOptionPane(final Component parentComponent)
+  {
+    
+    this.parentComponent = parentComponent;
+  }
+
   public static int showConfirmDialog(Component parentComponent,
           Object message) throws HeadlessException
   {
@@ -61,7 +79,9 @@ public class JvOptionPane extends JOptionPane
           throws HeadlessException
   {
     if (!isInteractiveMode())
+    {
       return (int) getMockResponse();
+    }
     switch (optionType)
     {
     case JvOptionPane.YES_NO_CANCEL_OPTION:
@@ -155,7 +175,9 @@ public class JvOptionPane extends JOptionPane
           String message, String title, int optionType)
   {
     if (!isInteractiveMode())
+    {
       return (int) getMockResponse();
+    }
     switch (optionType)
     {
     case JvOptionPane.YES_NO_CANCEL_OPTION:
@@ -188,7 +210,9 @@ public class JvOptionPane extends JOptionPane
           Object message, String title, int optionType, int messageType)
   {
     if (!isInteractiveMode())
+    {
       return (int) getMockResponse();
+    }
     switch (optionType)
     {
     case JvOptionPane.YES_NO_CANCEL_OPTION:
@@ -220,7 +244,9 @@ public class JvOptionPane extends JOptionPane
           Icon icon)
   {
     if (!isInteractiveMode())
+    {
       return (int) getMockResponse();
+    }
     switch (optionType)
     {
     case JvOptionPane.YES_NO_CANCEL_OPTION:
@@ -255,7 +281,9 @@ public class JvOptionPane extends JOptionPane
           throws HeadlessException
   {
     if (!isInteractiveMode())
+    {
       return (int) getMockResponse();
+    }
     // two uses:
     //
     // TODO
@@ -666,4 +694,139 @@ 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
+   * 
+   * @param desktop
+   * @param question
+   * @param string
+   * @param defaultOption
+   * @param plainMessage
+   * @param object
+   * @param options
+   * @param string2
+   * @return
+   */
+  public static JvOptionPane newOptionDialog(Component parentComponent)
+  {
+    return new JvOptionPane(parentComponent);
+  }
+
+  public void showDialog(
+          String message, String title, int optionType, int messageType,
+          Icon icon, Object[] options, Object initialValue)
+  {
+
+    if (!isInteractiveMode())
+    {
+      runner.firstRun((int) getMockResponse());
+    }
+    // two uses:
+    //
+    // TODO
+    //
+    // 1) AlignViewport for openLinkedAlignment
+    //
+    // Show a dialog with the option to open and link (cDNA <-> protein) as a
+    // new
+    // alignment, either as a standalone alignment or in a split frame. Returns
+    // true if the new alignment was opened, false if not, because the user
+    // declined the offer.
+    //
+    // 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,
+            optionType, messageType, icon, options, initialValue);
+    /**
+     * @j2sNative
+     */
+    {
+      runner.firstRun(response);
+    }
+
+  }
+
+  public void showInternalDialog(JPanel mainPanel, String title,
+          int yesNoCancelOption, int questionMessage, Icon icon,
+          Object[] options, String initresponse)
+  {
+    if (!isInteractiveMode())
+    {
+      runner.firstRun((int) getMockResponse());
+    }
+    Component parent;
+    /**
+     * @j2sNative parent = this;
+     */
+    {
+      parent = parentComponent;
+    }
+    ;
+    ourOptions = Arrays.asList(options);
+    
+    int response;
+    if (parent!=this) {
+
+      response = JOptionPane.showInternalOptionDialog(parent, mainPanel,
+              title, yesNoCancelOption, questionMessage, icon, options,
+              initresponse);
+    }
+    else
+    {
+      response = JOptionPane.showOptionDialog(parent, mainPanel, title,
+              yesNoCancelOption, questionMessage, icon, options,
+              initresponse);
+    }
+    /**
+     * @j2sNative
+     */
+    {
+      runner.firstRun(response);
+    }
+    
+  }
+  @Override
+  public JvOptionPane response(RunResponse action)
+  {
+
+    runner.response(action);
+    return this;
+  }
+
+  public JvOptionPane defaultResponse(Runnable runnable)
+  {
+    runner.setDefaultResponse(runnable);
+    return this;
+  }
+
+  @Override
+  public void propertyChange(PropertyChangeEvent evt)
+  {
+    int ourOption = ourOptions.indexOf(evt.getNewValue());
+    if (ourOption == -1)
+    {
+      // try our luck..
+      runner.run(evt.getNewValue());
+    }
+    else
+    {
+      runner.run(ourOption);
+    }
+  }
+
+
 }