--- /dev/null
+package net.sf.openrocket.util;
+
+import java.awt.Component;
+import java.awt.KeyboardFocusManager;
+import java.awt.Window;
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowEvent;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JButton;
+import javax.swing.JComponent;
+import javax.swing.JDialog;
+import javax.swing.JRootPane;
+import javax.swing.KeyStroke;
+import javax.swing.RootPaneContainer;
+import javax.swing.SwingUtilities;
+
+public class GUIUtil {
+
+ private static final KeyStroke ESCAPE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
+ private static final String CLOSE_ACTION_KEY = "escape:WINDOW_CLOSING";
+
+
+ /**
+ * Add the correct action to close a JDialog when the ESC key is pressed.
+ * The dialog is closed by sending is a WINDOW_CLOSING event.
+ *
+ * @param dialog the dialog for which to install the action.
+ */
+ public static void installEscapeCloseOperation(final JDialog dialog) {
+ Action dispatchClosing = new AbstractAction() {
+ public void actionPerformed(ActionEvent event) {
+ dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
+ }
+ };
+ JRootPane root = dialog.getRootPane();
+ root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE, CLOSE_ACTION_KEY);
+ root.getActionMap().put(CLOSE_ACTION_KEY, dispatchClosing);
+ }
+
+
+ /**
+ * Set the given button as the default button of the frame/dialog it is in. The button
+ * must be first attached to the window component hierarchy.
+ *
+ * @param button the button to set as the default button.
+ */
+ public static void setDefaultButton(JButton button) {
+ Window w = SwingUtilities.windowForComponent(button);
+ if (w == null) {
+ throw new IllegalArgumentException("Attach button to a window first.");
+ }
+ if (!(w instanceof RootPaneContainer)) {
+ throw new IllegalArgumentException("Button not attached to RootPaneContainer, w="+w);
+ }
+ ((RootPaneContainer)w).getRootPane().setDefaultButton(button);
+ }
+
+
+
+ /**
+ * Change the behavior of a component so that TAB and Shift-TAB cycles the focus of
+ * the components. This is necessary for e.g. <code>JTextArea</code>.
+ *
+ * @param c the component to modify
+ */
+ public static void setTabToFocusing(Component c) {
+ Set<KeyStroke> strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("pressed TAB")));
+ c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, strokes);
+ strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("shift pressed TAB")));
+ c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, strokes);
+ }
+
+
+}