01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package horstmann.ch10_command;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextArea;

/**
   This action places a greeting into a text field
   and afterwards disables itself and enables its
   opposite action.
 */
@SuppressWarnings("serial")
public class GreetingAction extends AbstractAction
{
  /**
      Constructs a greeting action.
      @param greeting the string to add to the text area
      @param textArea the text area to which to add the greeting
   */
  public GreetingAction(String greeting, JTextArea textArea)
  {
    this.greeting = greeting;
    this.textArea = textArea;
  }

  /**
      Sets the opposite action.
      @param action the action to be enabled after this action was
      carried out
   */
  public void setOpposite(Action action)
  {
    oppositeAction = action;
  }

  public void actionPerformed(ActionEvent event)
  {
    textArea.append(greeting);
    textArea.append("\n");
    if (oppositeAction != null)
    {
      setEnabled(false);
      oppositeAction.setEnabled(true);
    }
  }

  private String greeting;
  private JTextArea textArea;
  private Action oppositeAction;
}