package nxm.ucl.libg;

import java.util.Calendar;

/** A class that creates a simple text/combo box that allows the user to
    type in and/or spin a year value.  The selected year is used to update
    the given calendar, which will add the specific year value to its
    currently selected date, and re-render the calendar.

    @author crs
    @since NeXtMidas 2.9.1
 */
public class MJYearPicker extends MJSpinField {
  /** NOTE: this should be changed if the structure/fields of this class changes. */
  private static final long serialVersionUID = 2012122810331L; // for Serializable interface

  /** The previous year when the Year selection changes, used mainly for reporting the property change */
  protected int oldYear;
  /** The start, minimum, or earliest selectable year */
  protected int startYear;
  /** The end, maximum, or latest selectable year */
  protected int endYear;

  /** GDatePicker Month Object */
  private MJMonthPicker monthPicker;
  /** GDatePicker Day Object */
  private MJDayPicker   dayPicker;

  /**
    Default constructor.
   */
  public MJYearPicker () {
    setName("MJYearPicker");
    Calendar calendar = Calendar.getInstance();
    setMinimum(calendar.getMinimum(Calendar.YEAR));
    setMaximum(calendar.getMaximum(Calendar.YEAR));
    setValue(calendar.get(Calendar.YEAR));
  }

  /**
    Sets the year on this component and fires an event, "year", to any
    listening component that the year has changed.
    @param year The new year value
   */
  public void setYear (int year) {
    super.setValue(year, true, false);

    if (dayPicker != null) {
      dayPicker.setYear(year);
    }
    spinner.setValue(Integer.valueOf(value)); // Updated in 3.9.0 to use valueOf instead of new instance
    firePropertyChange("year", oldYear, value);
    oldYear = value;
  }

  /**
    Returns the current year value.
    @return Integer year value
   */
  public int getYear () {
    return super.getValue();
  }

  /**
    Sets the year value.
    @param value The new year value
   */
  public void setValue (int value) {
    setYear(value);
  }

  /**
    Sets the start year, or minimum year.
    @param startYear The minimum start year
   */
  public void setStartYear (int startYear) {
    setMinimum(startYear);
  }

  /** Accessory Methods **/

  /**
    Sets this object's MonthPicker reference.
    @param monthPicker The MonthPicker object to reference.
   */
  public void setMonthPicker (MJMonthPicker monthPicker) {
    this.monthPicker = monthPicker;
  }

  /**
    Sets this object's DayPicker reference.
    @param dayPicker The DayPicker object to reference.
   */
  public void setDayPicker (MJDayPicker dayPicker) {
    this.dayPicker = dayPicker;
  }

}