updates for 0.9.3
[debian/openrocket] / src / net / sf / openrocket / util / GUIUtil.java
1 package net.sf.openrocket.util;
2
3 import java.awt.Component;
4 import java.awt.Image;
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;
16 import java.util.Set;
17
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;
28
29 public class GUIUtil {
30
31         private static final KeyStroke ESCAPE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
32         private static final String CLOSE_ACTION_KEY =  "escape:WINDOW_CLOSING"; 
33         
34     private static final List<Image> images = new ArrayList<Image>();
35     static {
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");
41     }
42     
43     private static void loadImage(String file) {
44         InputStream is;
45  
46         is = ClassLoader.getSystemResourceAsStream(file);
47         if (is == null)
48                 return;
49         
50         try {
51                 Image image = ImageIO.read(is);
52                 images.add(image);
53         } catch (IOException ignore) {
54                 ignore.printStackTrace();
55         }
56     }
57     
58
59         
60         
61         /**
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.
64          * 
65          * @param dialog        the dialog for which to install the action.
66          */
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)); 
71                 } 
72             }; 
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); 
76         }
77         
78         
79         /**
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.
82          * 
83          * @param button        the button to set as the default button.
84          */
85         public static void setDefaultButton(JButton button) {
86                 Window w = SwingUtilities.windowForComponent(button);
87                 if (w == null) {
88                         throw new IllegalArgumentException("Attach button to a window first.");
89                 }
90                 if (!(w instanceof RootPaneContainer)) {
91                         throw new IllegalArgumentException("Button not attached to RootPaneContainer, w="+w);
92                 }
93                 ((RootPaneContainer)w).getRootPane().setDefaultButton(button);
94         }
95
96         
97         
98         /**
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>.
101          * 
102          * @param c             the component to modify
103          */
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);
109     }
110
111     
112     public static void setWindowIcons(Window window) {
113         window.setIconImages(images);
114     }
115         
116 }