package caltool.schedule_ui;

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

/****
 *
 * Class OKScheduleEventButtonListener defines the action
 * listener that is attached to the OK button in the
 * schedule appointment dialog.  When the class is
 * constructed, it is passed references to the classes
 * that need to be accessed in the actionPerformed method.
 * In this case, the required classes are the Schedule
 * model class and the ScheduleEventDialog view class.
 *                                                     <p>
 * Access to the model is for calling the scheduleEvent
 * method.  Access to the view is for gathering the data
 * values that are sent to scheduleEvent.
 *
 * @author Gene Fisher (gfisher@calpoly.edu)
 * @version 6feb15
 *
 */
public class OKScheduleEventButtonListener
        implements ActionListener {

    /**
     * Construct this with the given Schedule model and
     * parent dialog view.  Access to the model is for
     * calling its scheduleEvent method.  Access to the
     * parent view is for gathering data to be sent to
     * scheduleEvent.
     */
    public OKScheduleEventButtonListener(Schedule schedule,
            ScheduleEventDialog dialog) {
        this.schedule = schedule;
        this.dialog = dialog;
    }

    /**
     * Respond to a press of the OK button by calling
     * ScheduleEvent with a new Event.  The Event data are
     * gathered from the JTextFields and JComboBox in the
     * parent dialog.
     */
    public void actionPerformed(ActionEvent e) {

        try {
            schedule.scheduleEvent(
                new caltool.schedule.Event(
                    dialog.getTitle(),
                    dialog.getStartDate(),
                    dialog.getEndDate(),
                    dialog.getCategory(),
                    dialog.getSecurity()
                )
            );
        }
        catch (ScheduleEventPrecondViolation errors) {
            dialog.displayErrors(errors);
        }

    }

    /** The companion model */
    protected Schedule schedule;

    /** The parent view */
    protected ScheduleEventDialog dialog;

}