001package horstmann.ch10_command;
002import java.awt.event.ActionEvent;
003
004import javax.swing.AbstractAction;
005import javax.swing.Action;
006import javax.swing.JTextArea;
007
008/**
009   This action places a greeting into a text field
010   and afterwards disables itself and enables its
011   opposite action.
012 */
013@SuppressWarnings("serial")
014public class GreetingAction extends AbstractAction
015{
016        /**
017      Constructs a greeting action.
018      @param greeting the string to add to the text area
019      @param textArea the text area to which to add the greeting
020         */
021        public GreetingAction(String greeting, JTextArea textArea)
022        {
023                this.greeting = greeting;
024                this.textArea = textArea;
025        }
026
027        /**
028      Sets the opposite action.
029      @param action the action to be enabled after this action was
030      carried out
031         */
032        public void setOpposite(Action action)
033        {
034                oppositeAction = action;
035        }
036
037        public void actionPerformed(ActionEvent event)
038        {
039                textArea.append(greeting);
040                textArea.append("\n");
041                if (oppositeAction != null)
042                {
043                        setEnabled(false);
044                        oppositeAction.setEnabled(true);
045                }
046        }
047
048        private String greeting;
049        private JTextArea textArea;
050        private Action oppositeAction;
051}