
import java.beans.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;

public class JMonthChooser extends JComboBox implements ItemListener {

	public JMonthChooser() {
		super();
		addItemListener( this );
		dayChooser = null;
		locale = Locale.getDefault();
		initNames();
		setMonth( Calendar.getInstance().get( Calendar.MONTH ) );
	}

	public void initNames() {
		DateFormatSymbols dateFormatSymbols = new DateFormatSymbols( locale );
		String[] monthNames = dateFormatSymbols.getMonths();
		if( getItemCount() == 12 )
			removeAllItems();
		for( int i=0; i<12; i++ )
			addItem( monthNames[i] );
		setSelectedIndex( month );
	}

	public void itemStateChanged( ItemEvent iEvt ) {
		int index = getSelectedIndex();
		if( index >= 0 )
		setMonth( index, false );
	}

	private void setMonth( int newMonth, boolean select ) {
		int oldMonth = month;
		month = newMonth;
		if( select )
			setSelectedIndex( month );
		if( dayChooser != null )
			dayChooser.setMonth( month );
		firePropertyChange( "month", oldMonth, month );
	}

	public void setMonth( int newMonth ) {
		setMonth( newMonth, true );
	}

	public int getMonth() {
		return month;
	}

	public void setDayChooser( JDayChooser dayChooser ) {
		this.dayChooser = dayChooser;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale( Locale l ) {
		locale = l;
		initNames();
	}

	private Locale      locale;
	private int         month;
	private JDayChooser dayChooser;
}
