import java.awt.*;
import javax.swing.*;

/**
 * A GUI Component that can be used to edit Course information.
 * 
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CourseField extends JPanel
{
  private static final long serialVersionUID = 1L;
  private static final String[] DEPARTMENTS = {"CS", "ENG", "MATH", "PHYS"};
  
  private JComboBox<String> departmentField;
  private JTextField        numberField;
  
  /**
   * Default Constructor.
   */
  public CourseField()
  {
    super();
    departmentField = new JComboBox<String>(DEPARTMENTS);
    numberField = new JTextField();
    
    setLayout(new BorderLayout());
    add(departmentField, BorderLayout.WEST);
    add(numberField, BorderLayout.CENTER);
  }
  
  /**
   * Get a Course object from this CourseField.
   * 
   * @return A new Course object with the appropriate attributes
   */
  public Course getCourse()
  {
    int selected = departmentField.getSelectedIndex();
    return new Course(departmentField.getItemAt(selected), numberField.getText());
  }
  
  /**
   * Use the attributes of a Course object to populate this CourseField.
   * 
   * @param course  The Course object to use
   */
  public void setCourse(Course course)
  {
    departmentField.setSelectedItem(course.getDepartment());
    numberField.setText(course.getNumber());
  }
}
