JAL-1988 JAL-3772 Tidying of unspecified Callable declarations. Use SwingUtilities...
[jalview.git] / src / jalview / gui / JvOptionPane.java
index 10dbfcf..b58ea41 100644 (file)
@@ -22,7 +22,6 @@
 package jalview.gui;
 
 import java.awt.Component;
-import java.awt.Container;
 import java.awt.Dialog.ModalityType;
 import java.awt.HeadlessException;
 import java.awt.Window;
@@ -45,6 +44,7 @@ import javax.swing.JFrame;
 import javax.swing.JInternalFrame;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 
 import jalview.util.Platform;
@@ -61,7 +61,7 @@ public class JvOptionPane extends JOptionPane
 
   private Component parentComponent;
 
-  private Map<Object, Callable> callbacks = new HashMap<>();
+  private Map<Object, Callable<Void>> callbacks = new HashMap<>();
 
   /*
    * JalviewJS reports user choice in the dialog as the selected
@@ -836,7 +836,7 @@ public class JvOptionPane extends JOptionPane
             initialValueButton = jb;
 
           int buttonAction = buttonActions[i];
-          Callable action = callbacks.get(buttonAction);
+          Callable<Void> action = callbacks.get(buttonAction);
           jb.setText((String) o);
           jb.addActionListener(new ActionListener()
           {
@@ -845,8 +845,15 @@ public class JvOptionPane extends JOptionPane
             {
 
               Object obj = e.getSource();
-              Object joptionpaneObject = getAncestorClass(obj,
-                      JOptionPane.class);
+              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))
               {
@@ -859,11 +866,10 @@ public class JvOptionPane extends JOptionPane
               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
+              // 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;
@@ -982,7 +988,8 @@ public class JvOptionPane extends JOptionPane
   }
   */
   @Override
-  public JvOptionPane setResponseHandler(Object response, Callable action)
+  public JvOptionPane setResponseHandler(Object response,
+          Callable<Void> action)
   {
     callbacks.put(response, action);
     return this;
@@ -1171,7 +1178,7 @@ public class JvOptionPane extends JOptionPane
       {
         Object o = options[i];
         int buttonAction = buttonActions[i];
-        Callable action = callbacks.get(buttonAction);
+        Callable<Void> action = callbacks.get(buttonAction);
         JButton jb;
         if (buttons != null && buttons.length > i && buttons[i] != null)
         {
@@ -1251,28 +1258,4 @@ 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;
-  }
 }