From: Jim Procter Date: Tue, 3 Jul 2018 16:27:30 +0000 (+0100) Subject: JAL-3048 first pass at JvOptionDialog as property listener for dialog runner rather... X-Git-Tag: Release_2_11_4_0~45^2~18^2~595^2~3 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=a622f6142714e1dcddcaa89b970c99fbe676a121;p=jalview.git JAL-3048 first pass at JvOptionDialog as property listener for dialog runner rather than Desktop.dialogdata based method --- diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index e1d76f1..4e443e7 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -21,13 +21,22 @@ 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; public class JvOptionPane extends JOptionPane + implements DialogRunnerI, PropertyChangeListener { // BH 2018 no changes needed here. @@ -37,6 +46,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 +78,9 @@ public class JvOptionPane extends JOptionPane throws HeadlessException { if (!isInteractiveMode()) + { return (int) getMockResponse(); + } switch (optionType) { case JvOptionPane.YES_NO_CANCEL_OPTION: @@ -155,7 +174,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 +209,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 +243,9 @@ public class JvOptionPane extends JOptionPane Icon icon) { if (!isInteractiveMode()) + { return (int) getMockResponse(); + } switch (optionType) { case JvOptionPane.YES_NO_CANCEL_OPTION: @@ -255,7 +280,9 @@ public class JvOptionPane extends JOptionPane throws HeadlessException { if (!isInteractiveMode()) + { return (int) getMockResponse(); + } // two uses: // // TODO @@ -666,4 +693,99 @@ public class JvOptionPane extends JOptionPane return prefix; } + DialogRunner runner = new DialogRunner(this); + + private List 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); + } + + } + + @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); + } + } + }