X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FJvOptionPane.java;fp=src%2Fjalview%2Fgui%2FJvOptionPane.java;h=10dbfcf402acd47bea4e8b373889273480f26fb7;hb=62d4f5d6877f5f36c67f472dabebef3129600191;hp=02bca5ad9411537774462580328be7438f0981b6;hpb=3a3f08c3625510708606106bfcd3cc68b3a017cc;p=jalview.git diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index 02bca5a..10dbfcf 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -22,6 +22,7 @@ package jalview.gui; import java.awt.Component; +import java.awt.Container; import java.awt.Dialog.ModalityType; import java.awt.HeadlessException; import java.awt.Window; @@ -781,6 +782,14 @@ public class JvOptionPane extends JOptionPane int messageType, Icon icon, Object[] options, Object initialValue, boolean modal) { + showDialog(message, title, optionType, messageType, icon, options, + initialValue, modal, null); + } + + public void showDialog(Object message, String title, int optionType, + int messageType, Icon icon, Object[] options, Object initialValue, + boolean modal, JButton[] buttons) + { if (!isInteractiveMode()) { handleResponse(getMockResponse()); @@ -804,9 +813,92 @@ public class JvOptionPane extends JOptionPane if (modal) { + boolean useButtons = false; + Object initialValueButton = null; + NOTNULL: if (buttons != null) + { + if (buttons.length != options.length) + { + jalview.bin.Console.error( + "Supplied buttons array not the same length as supplied options array."); + break NOTNULL; + } + int[] buttonActions = { JOptionPane.YES_OPTION, + JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION }; + for (int i = 0; i < options.length; i++) + { + Object o = options[i]; + jalview.bin.Console.debug( + "Setting button " + i + " to '" + o.toString() + "'"); + JButton jb = buttons[i]; + + if (o.equals(initialValue)) + initialValueButton = jb; + + int buttonAction = buttonActions[i]; + Callable action = callbacks.get(buttonAction); + jb.setText((String) o); + jb.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + + Object obj = e.getSource(); + Object joptionpaneObject = getAncestorClass(obj, + JOptionPane.class); + if (joptionpaneObject == null + || !(joptionpaneObject instanceof JOptionPane)) + { + jalview.bin.Console.debug( + "Could not find JOptionPane ancestor of event object " + + obj); + return; + } + JOptionPane joptionpane = (JOptionPane) joptionpaneObject; + joptionpane.setValue(buttonAction); + if (action != null) + Executors.newSingleThreadExecutor().submit(action); + // joptionpane.transferFocusBackward(); + joptionpane.transferFocusBackward(); + joptionpane.setVisible(false); + // put focus and raise parent window if possible, unless cancel + // button pressed + boolean raiseParent = (parentComponent != null); + if (buttonAction == JOptionPane.CANCEL_OPTION) + raiseParent = false; + if (optionType == JOptionPane.YES_NO_OPTION + && buttonAction == JOptionPane.NO_OPTION) + raiseParent = false; + if (raiseParent) + { + parentComponent.requestFocus(); + if (parentComponent instanceof JInternalFrame) + { + JInternalFrame jif = (JInternalFrame) parentComponent; + jif.show(); + jif.moveToFront(); + jif.grabFocus(); + } + else if (parentComponent instanceof Window) + { + Window w = (Window) parentComponent; + w.toFront(); + w.requestFocus(); + } + } + joptionpane.setVisible(false); + } + }); + + } + useButtons = true; + } // use a JOptionPane as usual int response = JOptionPane.showOptionDialog(parentComponent, message, - title, optionType, messageType, icon, options, initialValue); + title, optionType, messageType, icon, + useButtons ? buttons : options, + useButtons ? initialValueButton : initialValue); /* * In Java, the response is returned to this thread and handled here; @@ -830,8 +922,9 @@ public class JvOptionPane extends JOptionPane * a non-modal JDialog for the confirmation without blocking the GUI. */ - JDialog dialog = this.createDialog(parentComponent, message, title, - optionType, messageType, icon, options, initialValue, modal); + JDialog dialog = createDialog(parentComponent, message, title, + optionType, messageType, icon, options, initialValue, modal, + buttons); jalview.bin.Console.debug("About to setVisible(true)"); dialog.setVisible(true); jalview.bin.Console.debug("Just setVisible(true)"); @@ -918,9 +1011,7 @@ public class JvOptionPane extends JOptionPane actionString, JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE); dialogParent.setAlwaysOnTop(false); - jalview.bin.Console.debug("*********** BEFORE dialogParent.dispose()"); dialogParent.dispose(); - jalview.bin.Console.debug("*********** BEFORE dialogParent.dispose()"); return answer; } @@ -939,6 +1030,16 @@ public class JvOptionPane extends JOptionPane int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, Object initialValue, boolean modal) { + showDialogOnTopAsync(dialogParent, label, actionString, + JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options, + initialValue, modal, null); + } + + public void showDialogOnTopAsync(JFrame dialogParent, Object label, + String actionString, int JOPTIONPANE_OPTION, + int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, + Object initialValue, boolean modal, JButton[] buttons) + { // Ensure Jalview window is brought to front (primarily for Quit // confirmation window to be visible) @@ -952,7 +1053,8 @@ public class JvOptionPane extends JOptionPane parentComponent = dialogParent; showDialog(label, actionString, JOPTIONPANE_OPTION, - JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal); + JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal, + buttons); dialogParent.setAlwaysOnTop(false); dialogParent.dispose(); @@ -1013,6 +1115,17 @@ public class JvOptionPane extends JOptionPane String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue, boolean modal) { + return createDialog(parentComponent, message, title, optionType, + messageType, icon, options, initialValue, modal, null); + } + + public JDialog createDialog(Component parentComponent, Object message, + String title, int optionType, int messageType, Icon icon, + Object[] options, Object initialValue, boolean modal, + JButton[] buttons) + { + JButton[] optionsButtons = null; + Object initialValueButton = null; JOptionPane joptionpane = new JOptionPane(); // Make button options int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, @@ -1035,26 +1148,39 @@ public class JvOptionPane extends JOptionPane } options = options_default.toArray(); } - ArrayList options_btns = new ArrayList<>(); - Object initialValue_btn = null; if (!Platform.isJS()) // JalviewJS already uses callback, don't need to // add them here { - if (((optionType == JOptionPane.YES_NO_OPTION - || optionType == JOptionPane.OK_CANCEL_OPTION) - && options.length < 2) + if (((optionType == JOptionPane.YES_OPTION + || optionType == JOptionPane.NO_OPTION + || optionType == JOptionPane.CANCEL_OPTION + || optionType == JOptionPane.OK_OPTION + || optionType == JOptionPane.DEFAULT_OPTION) + && options.length < 1) + || ((optionType == JOptionPane.YES_NO_OPTION + || optionType == JOptionPane.OK_CANCEL_OPTION) + && options.length < 2) || (optionType == JOptionPane.YES_NO_CANCEL_OPTION && options.length < 3)) { jalview.bin.Console .debug("JvOptionPane: not enough options for dialog type"); } + optionsButtons = new JButton[options.length]; for (int i = 0; i < options.length && i < 3; i++) { Object o = options[i]; int buttonAction = buttonActions[i]; Callable action = callbacks.get(buttonAction); - JButton jb = new JButton(); + JButton jb; + if (buttons != null && buttons.length > i && buttons[i] != null) + { + jb = buttons[i]; + } + else + { + jb = new JButton(); + } jb.setText((String) o); jb.addActionListener(new ActionListener() { @@ -1095,19 +1221,18 @@ public class JvOptionPane extends JOptionPane joptionpane.setVisible(false); } }); - options_btns.add(jb); + optionsButtons[i] = jb; if (o.equals(initialValue)) - initialValue_btn = jb; + initialValueButton = jb; } } joptionpane.setMessage(message); joptionpane.setMessageType(messageType); joptionpane.setOptionType(optionType); joptionpane.setIcon(icon); - joptionpane - .setOptions(Platform.isJS() ? options : options_btns.toArray()); + joptionpane.setOptions(Platform.isJS() ? options : optionsButtons); joptionpane.setInitialValue( - Platform.isJS() ? initialValue : initialValue_btn); + Platform.isJS() ? initialValue : initialValueButton); JDialog dialog = joptionpane.createDialog(parentComponent, title); dialog.setModalityType( @@ -1126,4 +1251,28 @@ public class JvOptionPane extends JOptionPane return false; } + + /** + * Get parent JOptionPane if there is one + */ + public static Container getAncestorClass(Object o, Class cl) + { + Container c; + if (o instanceof Container) + c = (Container) o; + else + return null; + Container p = c.getParent(); + if (p == null) + return null; + if (p.getClass() == cl) + { + return p; + } + else if (p instanceof Component) + { + return getAncestorClass((Component) p, cl); + } + return null; + } }