import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * An example of an encapsulation of a text editor
 * that separates the GUI from the actual editor.
 * 
 * In this case, the GUI distributes control to individual
 * Action objects.
 * 
 *  @author Prof. David Bernstein, James Madison University
 */
public class TextEditorGUI implements MouseListener
{
  private JFrame             window;
  private JMenu              menu;
  private JMenuBar           menuBar;
  private JPopupMenu         popup;    
  private JToolBar           toolBar;
  private TextEditor         textEditor;
  
  /**
   * The entry point of the application.
   * 
   * @param args  The command line arguments (which are ignored)
   */
  public static void main(String[] args)
  {
    new TextEditorGUI();
  }
  
  /**
   * Default Constructor.
   */
  public TextEditorGUI()
  {
    window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    menuBar = new JMenuBar();
    
    textEditor = new TextEditor();
    textEditor.addMouseListener(this);

    CopyAction  copy  = new CopyAction(textEditor);
    CutAction   cut   = new CutAction(textEditor);
    PasteAction paste = new PasteAction(textEditor); 
    constructControls(copy, cut, paste); 
    
    performLayout();

    window.setVisible(true);
  }
  
  /**
   * Construct the JPopupMenu, JMenu, and JToolBar and
   * add an array of Action objects to them.
   */
  private void constructControls(Action... actions)
  {
    JButton     button;
    String      sd;

    popup   = new JPopupMenu();
    menu    = new JMenu("Edit");
    toolBar = new JToolBar("Edit");

    for (int i=0; i<actions.length; i++)
    {
      popup.add(actions[i]);
      menu.add(actions[i]);
      button = toolBar.add(actions[i]);

      sd  = (String)actions[i].getValue(Action.SHORT_DESCRIPTION);
      if (sd != null) button.setToolTipText(sd);
    }
    
    menuBar.add(menu);
  }
  
  /**
   * Handle mouseClicked messages.
   *
   * @param evt   The event that generated the message
   */
  public void mouseClicked(MouseEvent evt)
  {
  }

  /**
   * Handle mouseEntered messages.
   *
   * @param evt   The event that generated the message
   */
  public void mouseEntered(MouseEvent evt)
  {
  }

  /**
   * Handle mouseExited messages.
   *
   * @param evt   The event that generated the message
   */
  public void mouseExited(MouseEvent evt)
  {
  }

  /**
   * Handle mousePressed messages.
   *
   * @param evt   The event that generated the message
   */
  public void mousePressed(MouseEvent evt)
  {
    if (evt.isPopupTrigger() && !popup.isVisible()) 
    {
        Point    p;

        p = evt.getPoint();           
        popup.show(window, p.x, p.y);
    }
  }

  /**
   * Handle mouseReleased messages.
   *
   * @param evt   The event that generated the message
   */
  public void mouseReleased(MouseEvent evt)
  {
     if (evt.isPopupTrigger() && !popup.isVisible()) 
     {
         Point    p;

         p = evt.getPoint();           
         popup.show(window, p.x, p.y);
     }
  }
  
  /**
   * Layout this component.
   */
  private void performLayout()
  {
    JPanel cp = (JPanel)window.getContentPane();
    cp.setLayout(new BorderLayout());
    window.setSize(400,400);

    cp.add(textEditor, BorderLayout.CENTER);
    
    window.setJMenuBar(menuBar);

    JPanel toolbars = new JPanel(new FlowLayout(FlowLayout.LEFT));
    toolbars.add(toolBar);
    cp.add(toolbars, BorderLayout.NORTH);        
  }
}
