001package horstmann.ch10_command;
002import java.awt.BorderLayout;
003
004import javax.swing.Action;
005import javax.swing.ImageIcon;
006import javax.swing.JFrame;
007import javax.swing.JMenu;
008import javax.swing.JMenuBar;
009import javax.swing.JTextArea;
010import javax.swing.JToolBar;
011
012/**
013   This program demonstrates action objects. Two actions
014   insert greetings into a text area. Each action can be
015   triggered by a menu item or toolbar button. When an
016   action is carried out, the opposite action becomes enabled.
017 */
018public class CommandTester
019{
020        public static void main(String[] args)
021        {
022                JFrame frame = new JFrame();
023                JMenuBar bar = new JMenuBar();
024                frame.setJMenuBar(bar);
025                JMenu menu = new JMenu("Say");
026                bar.add(menu);
027                JToolBar toolBar = new JToolBar();
028                frame.add(toolBar, BorderLayout.NORTH);
029                JTextArea textArea = new JTextArea(10, 40);
030                frame.add(textArea, BorderLayout.CENTER);
031
032                GreetingAction helloAction = new GreetingAction(
033                                "Hello, World", textArea);
034                helloAction.putValue(Action.NAME, "Hello");
035                helloAction.putValue(Action.SMALL_ICON,
036                                new ImageIcon("hello.png"));
037
038                GreetingAction goodbyeAction = new GreetingAction(
039                                "Goodbye, World", textArea);
040                goodbyeAction.putValue(Action.NAME, "Goodbye");
041                goodbyeAction.putValue(Action.SMALL_ICON,
042                                new ImageIcon("goodbye.png"));
043
044                helloAction.setOpposite(goodbyeAction);
045                goodbyeAction.setOpposite(helloAction);
046                goodbyeAction.setEnabled(false);
047
048                menu.add(helloAction);
049                menu.add(goodbyeAction);
050
051                toolBar.add(helloAction);
052                toolBar.add(goodbyeAction);
053
054                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
055                frame.pack();
056                frame.setVisible(true);
057        }
058}