package jalview.gui;
import java.awt.Component;
+import java.awt.Container;
import java.awt.Dialog.ModalityType;
import java.awt.HeadlessException;
import java.awt.Window;
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());
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;
* 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)");
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;
}
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)
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();
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,
}
options = options_default.toArray();
}
- ArrayList<JButton> 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()
{
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(
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;
+ }
}