Created
February 6, 2009 00:43
-
-
Save pgoodman/59136 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package minesweeper; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import java.awt.*; | |
| import javax.swing.*; | |
| import minesweeper.functional.*; | |
| /** | |
| * Simplification of the general uses of certain swing objects. Using these | |
| * functions, GUIs can be built recursively, which is convenient because it fits | |
| * well with the underlying tree nature of the architecture. | |
| * | |
| * Part of the idea of this class is that all functions have a return value so | |
| * that even when constructs are created recursively, they can also be accessed | |
| * through the return value. | |
| * | |
| * @author Peter Goodman | |
| */ | |
| abstract public class SimpleGUI { | |
| /** | |
| * Create the GUI. | |
| * @param args | |
| */ | |
| abstract public void create(); | |
| /** | |
| * Create and return a JFrame object. Requires a ComponentCallback to | |
| * initialize the frame with any content before being shown. | |
| * | |
| * @param title The title of the window. | |
| * @param init Delegate to initialize the frame. | |
| * @return JFrame | |
| */ | |
| public static JFrame frame(String title) { | |
| return frame(title, null); | |
| } | |
| public static JFrame frame(String title, Delegate<JFrame> init) { | |
| JFrame frame = new JFrame(title); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| // initialize and display the frame | |
| if(init != null) | |
| init.call(frame); | |
| frame.pack(); | |
| frame.setVisible(true); | |
| return frame; | |
| } | |
| /** | |
| * Create and return a JPanel object. JPanel's are lightweight. | |
| */ | |
| public static JPanel panel() { | |
| return panel(null); | |
| } | |
| public static JPanel panel(Delegate<JPanel> init) { | |
| JPanel panel = new JPanel(); | |
| if(init != null) | |
| init.call(panel); | |
| return panel; | |
| } | |
| /** | |
| * Create a simple button. | |
| */ | |
| public static JButton button() { | |
| return button_hook(new JButton(), null); | |
| } | |
| public static JButton button(Delegate<JButton> on_click) { | |
| return button_hook(new JButton(), on_click); | |
| } | |
| public static JButton button(String name) { | |
| return button(name, null); | |
| } | |
| public static JButton button(String name, Delegate<JButton> on_click) { | |
| return button_hook(new JButton(name), on_click); | |
| } | |
| protected static <B extends AbstractButton> B button_hook(final B btn, final Delegate<B> on_click) { | |
| btn.setEnabled(on_click != null); | |
| // add an on-click event listener if one was passed | |
| if (btn.isEnabled()) { | |
| btn.addActionListener(new ActionListener() { | |
| public void actionPerformed(ActionEvent e) { | |
| on_click.call(btn); | |
| } | |
| }); | |
| } | |
| return btn; | |
| } | |
| /** | |
| * Generic method to add a set of components to another. | |
| * @return | |
| */ | |
| protected static <C extends JComponent, I extends JComponent> C add_items_to_component(C component, I items[]) { | |
| for(I item : items) { | |
| if(item != null) | |
| component.add(item); | |
| } | |
| return component; | |
| } | |
| /** | |
| * Create a menu bar. Accepts a variable number of menus. | |
| * | |
| * @param itms | |
| * @return JMenuBar | |
| */ | |
| public static JMenuBar menu_bar(final JFrame frame, JMenu ... menus) { | |
| JMenuBar bar = add_items_to_component(new JMenuBar(), menus); | |
| frame.setJMenuBar(bar); | |
| return bar; | |
| } | |
| /** | |
| * Create a menu. Accepts a variable number of menu items. | |
| * | |
| * @return JMenu | |
| */ | |
| public static JMenu menu(String name, JMenuItem ... items) { | |
| return add_items_to_component(new JMenu(name), items); | |
| } | |
| /** | |
| * Create a menu item. This takes in a callback which takes as its only | |
| * parameter a JMenuItem. | |
| * | |
| * @param args | |
| */ | |
| public static JMenuItem menu_item(final String text) { | |
| return menu_item(text, null); | |
| } | |
| public static JMenuItem menu_item(String text, final Delegate<JMenuItem> on_click) { | |
| return button_hook(new JMenuItem(text), on_click); | |
| } | |
| /** | |
| * Create a simple label. | |
| * | |
| * @param args | |
| */ | |
| public static JLabel label(final String label_name) { | |
| return label(label_name, JLabel.LEFT); | |
| } | |
| public static JLabel label(final String label_name, int text_align) { | |
| return new JLabel(label_name, text_align); | |
| } | |
| /** | |
| * Set something to the content pane of a frame. | |
| * | |
| * @param args | |
| */ | |
| public static <T extends JComponent> T add_content(Container parent, T child) { | |
| if(parent instanceof JFrame) | |
| ((JFrame) parent).getContentPane().add(child); | |
| else | |
| parent.add(child); | |
| return child; | |
| } | |
| /** | |
| * Create a grid bag layout. | |
| */ | |
| public static JPanel grid(ComponentGridConstraint ... cells) { | |
| JPanel pane = new JPanel(new GridBagLayout()); | |
| for(ComponentGridConstraint cell : cells) | |
| pane.add(cell.first(), cell.second()); | |
| return pane; | |
| } | |
| /** | |
| * Create and return a simple grid cell. A Simple grid cell knows how much it spans | |
| * to the left and right. | |
| */ | |
| public static ComponentGridConstraint grid_cell(Component item) { | |
| return grid_cell(1, item, 1); | |
| } | |
| public static ComponentGridConstraint grid_cell(int width, Component item) { | |
| return grid_cell(width, item, 1); | |
| } | |
| public static ComponentGridConstraint grid_cell(Component item, int height) { | |
| return grid_cell(1, item, height); | |
| } | |
| public static ComponentGridConstraint grid_cell(int width, Component item, int height) { | |
| GridBagConstraints c = new GridBagConstraints(); | |
| c.gridwidth = width; | |
| c.gridheight = height; | |
| return new ComponentGridConstraint(item, c); | |
| } | |
| /** | |
| * Create an alert dialog box. | |
| */ | |
| public static void alert(JFrame frame, String message) { | |
| JOptionPane.showMessageDialog(frame, message); | |
| } | |
| /** | |
| * Set the look and feel. | |
| * | |
| * @param args | |
| */ | |
| public static void set_look_and_feel(JFrame frame, String class_loc) { | |
| try { | |
| UIManager.setLookAndFeel(class_loc); | |
| SwingUtilities.updateComponentTreeUI(frame); | |
| frame.pack(); | |
| // ClassNotFoundException, | |
| // InstantiationException, | |
| // IllegalAccessException, | |
| // UnsupportedLookAndFeelException | |
| } catch(Exception e) { | |
| alert( | |
| frame, | |
| "An error occured while trying to switch the look and feel." | |
| ); | |
| } | |
| } | |
| /** | |
| * Create the GUI in a thread. | |
| * | |
| * @param args | |
| */ | |
| public static void run(final SimpleGUI gui) { | |
| javax.swing.SwingUtilities.invokeLater(new Runnable() { | |
| public void run() { | |
| gui.create(); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment