1 package net.sf.openrocket.util;
3 import java.awt.Component;
5 import java.awt.KeyboardFocusManager;
6 import java.awt.Window;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.KeyEvent;
9 import java.awt.event.WindowEvent;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.HashSet;
15 import java.util.List;
18 import javax.imageio.ImageIO;
19 import javax.swing.AbstractAction;
20 import javax.swing.Action;
21 import javax.swing.JButton;
22 import javax.swing.JComponent;
23 import javax.swing.JDialog;
24 import javax.swing.JRootPane;
25 import javax.swing.KeyStroke;
26 import javax.swing.RootPaneContainer;
27 import javax.swing.SwingUtilities;
29 public class GUIUtil {
31 private static final KeyStroke ESCAPE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
32 private static final String CLOSE_ACTION_KEY = "escape:WINDOW_CLOSING";
34 private static final List<Image> images = new ArrayList<Image>();
36 loadImage("pix/icon/icon-256.png");
37 loadImage("pix/icon/icon-064.png");
38 loadImage("pix/icon/icon-048.png");
39 loadImage("pix/icon/icon-032.png");
40 loadImage("pix/icon/icon-016.png");
43 private static void loadImage(String file) {
46 is = ClassLoader.getSystemResourceAsStream(file);
51 Image image = ImageIO.read(is);
53 } catch (IOException ignore) {
54 ignore.printStackTrace();
62 * Add the correct action to close a JDialog when the ESC key is pressed.
63 * The dialog is closed by sending is a WINDOW_CLOSING event.
65 * @param dialog the dialog for which to install the action.
67 public static void installEscapeCloseOperation(final JDialog dialog) {
68 Action dispatchClosing = new AbstractAction() {
69 public void actionPerformed(ActionEvent event) {
70 dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
73 JRootPane root = dialog.getRootPane();
74 root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE, CLOSE_ACTION_KEY);
75 root.getActionMap().put(CLOSE_ACTION_KEY, dispatchClosing);
80 * Set the given button as the default button of the frame/dialog it is in. The button
81 * must be first attached to the window component hierarchy.
83 * @param button the button to set as the default button.
85 public static void setDefaultButton(JButton button) {
86 Window w = SwingUtilities.windowForComponent(button);
88 throw new IllegalArgumentException("Attach button to a window first.");
90 if (!(w instanceof RootPaneContainer)) {
91 throw new IllegalArgumentException("Button not attached to RootPaneContainer, w="+w);
93 ((RootPaneContainer)w).getRootPane().setDefaultButton(button);
99 * Change the behavior of a component so that TAB and Shift-TAB cycles the focus of
100 * the components. This is necessary for e.g. <code>JTextArea</code>.
102 * @param c the component to modify
104 public static void setTabToFocusing(Component c) {
105 Set<KeyStroke> strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("pressed TAB")));
106 c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, strokes);
107 strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("shift pressed TAB")));
108 c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, strokes);
112 public static void setWindowIcons(Window window) {
113 window.setIconImages(images);