import javax.swing.*;
import javax.swing.event.*;


/**
 * An abstract action for an Editor
 *
 * @author  Prof. David Bernstein, James Madison University
 */
public abstract class EditorAction extends AbstractAction 
    implements CaretListener
{
  private static final long serialVersionUID = 1L;

  private TextEditor    editor;

  /**
   * Explicit Value Constructor.
   *
   * @param editor  The Editor that is the target of this command.
   */
  public EditorAction(TextEditor editor, String name, Icon icon)
  {
    super(name, icon);
    this.editor = editor;
    setEnabled(false);
    editor.addCaretListener(this);
  }

  /**
   * Handle caretUpdate messages.
   *
   * @param evt   The event that caused the message to be generated
   */
  public void caretUpdate(CaretEvent evt)
  {
    setEnabled(editor.getSelectedText() != null, editor.getClipboard() != null);
  }

  /**
   * Get the Editor associated with this EditorAction.
   *
   * @return  The Editor
   */
  protected TextEditor getEditor()
  {
    return this.editor;
  }

  /**
   * Set the enabled state of this Action based on whether
   * text is selected and whether the "clipboard" is populated.
   *
   * @param textIsSelected     true if there is text selected
   * @param textIsOnClipboard  true if the "clipboard" is populated
   */
  protected abstract void setEnabled(boolean textIsSelected, boolean textIsOnClipboard);

}
