X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FJvOptionPane.java;h=0a7c49ea2a1d758c7daa174d58499bc555cddc8d;hb=1213d49ca3df8f48360da857121805d088c13aab;hp=e62244579bf496cd940e0af5efbcc63d278d7dc2;hpb=49c595b1a5d841a27e6c01367da99d278152b976;p=jalview.git diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index e622445..0a7c49e 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -32,10 +32,12 @@ import java.awt.Toolkit; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseMotionAdapter; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.beans.PropertyVetoException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -50,8 +52,11 @@ import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLayeredPane; +import javax.swing.JMenu; +import javax.swing.JMenuBar; import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.JRootPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.event.InternalFrameEvent; @@ -83,6 +88,13 @@ public class JvOptionPane extends JOptionPane private Map callbacks = new HashMap<>(); + private int timeout = -1; + + public void setTimeout(int i) + { + timeout = i; + } + /* * JalviewJS reports user choice in the dialog as the selected option (text); * this list allows conversion to index (int) @@ -717,7 +729,8 @@ public class JvOptionPane extends JOptionPane private static void outputMessage(Object message) { - System.out.println(">>> JOption Message : " + message.toString()); + jalview.bin.Console + .outPrintln(">>> JOption Message : " + message.toString()); } public static Object getMockResponse() @@ -845,6 +858,38 @@ public class JvOptionPane extends JOptionPane "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++) @@ -854,9 +899,6 @@ public class JvOptionPane extends JOptionPane "Setting button " + i + " to '" + o.toString() + "'"); JButton jb = buttons[i]; - if (o.equals(initialValue)) - initialValueButton = jb; - int buttonAction = buttonActions[i]; Runnable action = callbacks.get(buttonAction); jb.setText((String) o); @@ -865,6 +907,10 @@ public class JvOptionPane extends JOptionPane @Override public void actionPerformed(ActionEvent e) { + if (timeoutThreadF != null) + { + timeoutThreadF.interrupt(); + } Object obj = e.getSource(); if (obj == null || !(obj instanceof Component)) @@ -1071,9 +1117,16 @@ public class JvOptionPane extends JOptionPane 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() { @@ -1252,6 +1305,17 @@ public class JvOptionPane extends JOptionPane 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()); @@ -1273,8 +1337,11 @@ public class JvOptionPane extends JOptionPane JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal, buttons); - dialogParent.setAlwaysOnTop(false); - dialogParent.dispose(); + if (dispose) + { + dialogParent.setAlwaysOnTop(false); + dialogParent.dispose(); + } } /** @@ -1505,11 +1572,26 @@ public class JvOptionPane extends JOptionPane lp.add(modalInterceptor); f.toFront(); + // disable the main menu bar if in Linux + JMenuBar menubar = null; + if (Platform.isLinux()) + { + JRootPane rootpane = Desktop.getDesktop().getRootPane(); + menubar = rootpane.getJMenuBar(); + } + // We need to explicitly dispatch events when we are blocking the event // dispatch thread. EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); try { + if (menubar != null) + { + // don't allow clicks on main menu on linux due to a hanging bug. + // see JAL-4214. + setMenusEnabled(menubar, false); + } + while (!f.isClosed()) { if (EventQueue.isDispatchThread()) @@ -1521,11 +1603,25 @@ public class JvOptionPane extends JOptionPane // EventQueue.dispatchEvent() directly, because it is // protected, unfortunately. if (ev instanceof ActiveEvent) + { ((ActiveEvent) ev).dispatch(); - else if (ev.getSource() instanceof Component) + } + else if (ev instanceof KeyEvent && ((KeyEvent) ev).isControlDown() + && menubar != null) + { + // temporarily enable menus to send Ctrl+? KeyEvents + setMenusEnabled(menubar, true); ((Component) ev.getSource()).dispatchEvent(ev); + setMenusEnabled(menubar, false); + } else if (ev.getSource() instanceof MenuComponent) + { ((MenuComponent) ev.getSource()).dispatchEvent(ev); + } + else if (ev.getSource() instanceof Component) + { + ((Component) ev.getSource()).dispatchEvent(ev); + } // Other events are ignored as per spec in // EventQueue.dispatchEvent } @@ -1540,14 +1636,34 @@ public class JvOptionPane extends JOptionPane // If we get interrupted, then leave the modal state. } finally { + // re-enable the main menu bar + if (menubar != null) + { + setMenusEnabled(menubar, true); + } + // Clean up the modal interceptor. lp.remove(modalInterceptor); + // unpaint the frame + f.setVisible(false); + + // close the frame + try + { + f.setClosed(true); + } catch (PropertyVetoException e) + { + f.doDefaultCloseAction(); + } + // Remove the internal frame from its parent, so it is no longer // lurking around and clogging memory. Container parent = f.getParent(); if (parent != null) + { parent.remove(f); + } } } @@ -1618,4 +1734,14 @@ public class JvOptionPane extends JOptionPane return jvop; } + + private static void setMenusEnabled(JMenuBar menubar, boolean b) + { + for (int i = 0; i < menubar.getMenuCount(); i++) + { + JMenu menu = menubar.getMenu(i); + menu.setEnabled(b); + } + } + }