+ public static void setInteractiveMode(boolean interactive)
+ {
+ JvOptionPane.interactiveMode = interactive;
+ }
+
+ private static String getPrefix(int messageType)
+ {
+ String prefix = "";
+
+ // JavaScript only
+ if (Platform.isJS())
+ {
+ switch (messageType)
+ {
+ case JvOptionPane.WARNING_MESSAGE:
+ prefix = "WARNING! ";
+ break;
+ case JvOptionPane.ERROR_MESSAGE:
+ prefix = "ERROR! ";
+ break;
+ default:
+ prefix = "Note: ";
+ }
+ }
+ return prefix;
+ }
+
+ /**
+ * 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()
+ {
+ return new JvOptionPane(null);
+ }
+
+ 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)
+ {
+ showDialog(message, title, optionType, messageType, icon, options,
+ initialValue, true);
+ }
+
+ public void showDialog(Object message, String title, int optionType,
+ 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());
+ return;
+ }
+ // 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
+ //
+
+ ourOptions = Arrays.asList(options);
+
+ 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;
+ }
+
+ // run through buttons for initialValue first so can set a final
+ // timeoutThreadF
+ Thread timeoutThread = null;
+ for (int i = 0; i < options.length; i++)
+ {
+ Object o = options[i];
+ JButton jb = buttons[i];
+ if (o.equals(initialValue))
+ {
+ initialValueButton = jb;
+ if (timeout > 0 && initialValueButton != null
+ && initialValueButton instanceof JButton)
+ {
+ Runnable timeoutClose = () -> {
+ try
+ {
+ Thread.sleep(timeout);
+ } catch (InterruptedException e)
+ {
+ Console.debug(
+ "Dialog timeout interrupted. Probably a button pressed.");
+ }
+ jb.doClick();
+ };
+ timeoutThread = new Thread(timeoutClose);
+ }
+ }
+ }
+ final Thread timeoutThreadF = timeoutThread;
+ timeoutThreadF.start();
+
+ 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];
+
+ int buttonAction = buttonActions[i];
+ Runnable action = callbacks.get(buttonAction);
+ jb.setText((String) o);
+ jb.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ if (timeoutThreadF != null)
+ {
+ timeoutThreadF.interrupt();
+ }
+
+ Object obj = e.getSource();
+ if (obj == null || !(obj instanceof Component))
+ {
+ jalview.bin.Console.debug(
+ "Could not find Component source of event object "
+ + obj);
+ return;
+ }
+ Object joptionpaneObject = SwingUtilities.getAncestorOfClass(
+ JOptionPane.class, (Component) obj);
+ 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)
+ new Thread(action).start();
+ joptionpane.transferFocusBackward();
+ joptionpane.setVisible(false);
+ // put focus and raise parent window if possible, unless cancel or
+ // no 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,
+ useButtons ? buttons : options,
+ useButtons ? initialValueButton : initialValue);
+
+ /*
+ * In Java, the response is returned to this thread and handled here; (for
+ * Javascript, see propertyChange)
+ */
+ if (!Platform.isJS())
+ /**
+ * Java only
+ *
+ * @j2sIgnore
+ */
+ {
+ handleResponse(response);
+ }
+ }
+ else
+ {
+ /*
+ * This is java similar to the swingjs handling, with the callbacks attached to
+ * the button press of the dialog. This means we can use a non-modal JDialog for
+ * the confirmation without blocking the GUI.
+ */
+ JOptionPane joptionpane = new JOptionPane();
+ // Make button options
+ int[] buttonActions = { JvOptionPane.YES_OPTION,
+ JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION };
+
+ // we need the strings to make the buttons with actionEventListener
+ if (options == null)
+ {
+ ArrayList<String> options_default = new ArrayList<>();
+ options_default
+ .add(UIManager.getString("OptionPane.yesButtonText"));
+ if (optionType == JvOptionPane.YES_NO_OPTION
+ || optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
+ {
+ options_default
+ .add(UIManager.getString("OptionPane.noButtonText"));
+ }
+ if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
+ {
+ options_default
+ .add(UIManager.getString("OptionPane.cancelButtonText"));
+ }
+ 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
+ {
+ for (int i = 0; i < options.length && i < 3; i++)
+ {
+ Object o = options[i];
+ int buttonAction = buttonActions[i];
+ Runnable action = callbacks.get(buttonAction);
+ JButton jb = new JButton();
+ jb.setText((String) o);
+ jb.addActionListener(new ActionListener()
+ {
+
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ joptionpane.setValue(buttonAction);
+ if (action != null)
+ new Thread(action).start();
+ // 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 == JvOptionPane.CANCEL_OPTION)
+ raiseParent = false;
+ if (optionType == JvOptionPane.YES_NO_OPTION
+ && buttonAction == JvOptionPane.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);
+ }
+ });
+ options_btns.add(jb);
+ if (o.equals(initialValue))
+ initialValue_btn = jb;
+ }
+ }
+ joptionpane.setMessage(message);
+ joptionpane.setMessageType(messageType);
+ joptionpane.setOptionType(optionType);
+ joptionpane.setIcon(icon);
+ joptionpane.setOptions(
+ Platform.isJS() ? options : options_btns.toArray());
+ joptionpane.setInitialValue(
+ Platform.isJS() ? initialValue : initialValue_btn);
+
+ JDialog dialog = joptionpane.createDialog(parentComponent, title);
+ dialog.setIconImages(ChannelProperties.getIconList());
+ dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL
+ : ModalityType.MODELESS);
+ dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+ dialog.setVisible(true);
+ setDialog(dialog);
+ }
+ }
+
+ public void showInternalDialog(Object mainPanel, String title,
+ int yesNoCancelOption, int questionMessage, Icon icon,
+ Object[] options, String initresponse)
+ {
+ if (!isInteractiveMode())
+ {
+ handleResponse(getMockResponse());
+ }
+
+ // need to set these separately so we can set the title bar icon later
+ this.setOptionType(yesNoCancelOption);
+ this.setMessageType(questionMessage);
+ this.setIcon(icon);
+ this.setInitialValue(initresponse);
+ this.setOptions(options);
+ this.setMessage(mainPanel);
+
+ ourOptions = Arrays.asList(options);
+ if (parentComponent != this
+ && !(parentComponent == null && Desktop.instance == null))
+ {
+ // note the parent goes back to a JRootPane so is probably
+ // Desktop.getDesktop()
+ JInternalFrame jif = this.createInternalFrame(
+ parentComponent != null ? parentComponent : Desktop.instance,
+ title);
+ // connect to the alignFrame using a map in Desktop
+ if (parentComponent instanceof AlignFrame)
+ {
+ Desktop.addModal((AlignFrame) parentComponent, jif);
+ }
+ jif.setFrameIcon(null);
+ jif.addInternalFrameListener(new InternalFrameListener()
+ {
+ @Override
+ public void internalFrameActivated(InternalFrameEvent arg0)
+ {
+ }
+
+ @Override
+ public void internalFrameClosed(InternalFrameEvent arg0)
+ {
+ JvOptionPane.this.internalDialogHandleResponse();
+ }
+
+ @Override
+ public void internalFrameClosing(InternalFrameEvent arg0)
+ {
+ }
+
+ @Override
+ public void internalFrameDeactivated(InternalFrameEvent arg0)
+ {
+ }
+
+ @Override
+ public void internalFrameDeiconified(InternalFrameEvent arg0)
+ {
+ }
+
+ @Override
+ public void internalFrameIconified(InternalFrameEvent arg0)
+ {
+ }
+
+ @Override
+ public void internalFrameOpened(InternalFrameEvent arg0)
+ {
+ }
+ });
+ jif.setVisible(true);
+ startModal(jif);
+ return;
+ }
+ else
+ {
+ JDialog dialog = this.createDialog(parentComponent, title);
+ dialog.setIconImages(ChannelProperties.getIconList());
+ dialog.setVisible(true); // blocking
+ this.internalDialogHandleResponse();
+ return;
+ }
+ }
+
+ private void internalDialogHandleResponse()
+ {
+ Object value = this.getValue();
+ if (value == null
+ || (value instanceof Integer && (Integer) value == -1))
+ {
+ return;
+ }
+ String responseString = value.toString();
+ int response = ourOptions.indexOf(responseString);
+
+ if (!Platform.isJS())
+ /**
+ * Java only
+ *
+ * @j2sIgnore
+ */
+ {
+ handleResponse(response);
+ }
+ }
+
+ /*
+ * @Override public JvOptionPane setResponseHandler(Object response, Runnable
+ * action) { callbacks.put(response, new Callable<Void>() {
+ *
+ * @Override public Void call() { action.run(); return null; } }); return this;
+ * }
+ */
+ @Override
+ public JvOptionPane setResponseHandler(Object response, Runnable action)
+ {
+ if (action == null)
+ {
+ action = NULLCALLABLE;
+ }
+ callbacks.put(response, action);
+ return this;
+ }
+
+ public void setDialog(JDialog d)
+ {
+ dialog = d;
+ }
+
+ public JDialog getDialog()
+ {
+ return dialog;
+ }
+
+ /**
+ * showDialogOnTop will create a dialog that (attempts to) come to top of OS
+ * desktop windows
+ */
+ public static int showDialogOnTop(String label, String actionString,
+ int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
+ {
+ return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
+ JOPTIONPANE_MESSAGETYPE);
+ }
+
+ public static int showDialogOnTop(Component dialogParentComponent,
+ String label, String actionString, int JOPTIONPANE_OPTION,
+ int JOPTIONPANE_MESSAGETYPE)
+ {
+ if (!isInteractiveMode())
+ {
+ return (int) getMockResponse();
+ }
+ // Ensure Jalview window is brought to front (primarily for Quit
+ // confirmation window to be visible)
+
+ // This method of raising the Jalview window is broken in java
+ // jalviewDesktop.setVisible(true);
+ // jalviewDesktop.toFront();
+
+ // A better hack which works is to create a new JFrame parent with
+ // setAlwaysOnTop(true)
+ JFrame dialogParent = new JFrame();
+ if (dialogParentComponent == null)
+ {
+ dialogParent.setIconImages(ChannelProperties.getIconList());
+ dialogParent.setAlwaysOnTop(true);
+ }
+
+ int answer = JOptionPane.showConfirmDialog(
+ dialogParentComponent == null ? dialogParent
+ : dialogParentComponent,
+ label, actionString, JOPTIONPANE_OPTION,
+ JOPTIONPANE_MESSAGETYPE);
+
+ if (dialogParentComponent == null)
+ {
+ dialogParent.setAlwaysOnTop(false);
+ dialogParent.dispose();
+ }
+
+ return answer;
+ }
+
+ public void showDialogOnTopAsync(String label, String actionString,
+ int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
+ Object[] options, Object initialValue, boolean modal)
+ {
+ JFrame frame = new JFrame();
+ frame.setIconImages(ChannelProperties.getIconList());
+ showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
+ JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
+ }
+
+ public void showDialogOnTopAsync(JFrame dialogParent, Object label,
+ String actionString, int JOPTIONPANE_OPTION,
+ 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)
+ {
+ showDialogOnTopAsync(dialogParent, label, actionString,
+ JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
+ initialValue, modal, buttons, true);
+ }
+
+ 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,
+ boolean dispose)
+ {
+ if (!isInteractiveMode())
+ {
+ handleResponse(getMockResponse());
+ return;
+ }
+ // Ensure Jalview window is brought to front (primarily for Quit
+ // confirmation window to be visible)
+
+ // This method of raising the Jalview window is broken in java
+ // jalviewDesktop.setVisible(true);
+ // jalviewDesktop.toFront();
+
+ // A better hack which works is to create a new JFrame parent with
+ // setAlwaysOnTop(true)
+ dialogParent.setAlwaysOnTop(true);
+ parentComponent = dialogParent;
+
+ showDialog(label, actionString, JOPTIONPANE_OPTION,
+ JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
+ buttons);
+
+ if (dispose)
+ {
+ dialogParent.setAlwaysOnTop(false);
+ dialogParent.dispose();
+ }
+ }
+
+ /**
+ * 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)
+ {
+ Object newValue = evt.getNewValue();
+ int ourOption = ourOptions.indexOf(newValue);
+ if (ourOption >= 0)
+ {
+ handleResponse(ourOption);
+ }
+ else
+ {
+ // try our luck..
+ handleResponse(newValue);
+ }
+ }
+
+ @Override
+ public void handleResponse(Object response)
+ {
+ /*
+ * this test is for NaN in Chrome
+ */
+ if (response != null && !response.equals(response))
+ {
+ return;
+ }
+ Runnable action = callbacks.get(response);
+ if (action != null)
+ {
+ try
+ {
+ new Thread(action).start();
+ // action.call();
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ if (parentComponent != null)
+ parentComponent.requestFocus();
+ }
+ }
+
+ /**
+ * Create a non-modal confirm dialog
+ */
+ public JDialog createDialog(Component parentComponent, Object message,
+ 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)