/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * published by the Free Software Foundation; either version 2.1 of
 * the license, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA  02111-1307, USA.
 * 
 */
package com.k_int.AdminApp.widgets;

import java.util.Calendar;
import java.util.Date;

import javax.swing.Box;
import javax.swing.BoxLayout;

import javax.swing.JLabel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



/**
 * Title:		CalendarAgeComboBox
 * @author:		rob
 * @version:	$Id: CalendarAgeComboBox.java,v 1.1 2004/11/15 13:47:50 rob Exp $
 * Copyright:	Copyright 1999-2004 Knowledge Integration Ltd
 * Company: 	Knowledge Integration Ltd
 * Description:
 * 
 * 
 * Created:		23-Aug-2004  10:57:38
 * 
 * 
 * History:		$Log: CalendarAgeComboBox.java,v $
 * History:		Revision 1.1  2004/11/15 13:47:50  rob
 * History:		*** empty log message ***
 * History:		
 *
 */
public class CalendarAgeComboBox extends CalendarComboBox //implements ActionListener
{
    private JLabel age_label;
    protected void buildInputPanel(){
        inputPanel.setLayout( new BoxLayout( inputPanel, BoxLayout.X_AXIS ) );

        input.setColumns( 12 );
        inputPanel.add(input);
       
        inputPanel.add(Box.createHorizontalStrut(10));
        age_label= new JLabel("Age:   ");
        inputPanel.add(age_label);
    
        input.addFocusListener(this);
        comboBtn.setActionCommand( "combo" );
        inputPanel.add( comboBtn );    
        //input.addActionListener(this);
       
    }
    
    public void setDate(Date newDate) 
    { 
        if(newDate!=null)
        {
            current.setTime(newDate);       
            input.setText(sf.format(newDate).toString());
            performAdditionalFunction();
            //age_label.setText("Age: "+Integer.toString((new Date()).getYear()-newDate.getYear())+"  ");           
        }
        else
        {
            input.setText("");
            age_label.setText("Age:   ");
        }
    }
    
    protected void performAdditionalFunction()
    {
        if(input.getText().equals("")==false)
        {
            int b_month=current.getTime().getMonth();
            int b_day = current.getTime().getDay();
            int b_year = current.getTime().getYear();
            
           
            Date now = new Date();
            int current_month = now.getMonth();
            int current_day = now.getDay();
            
            int age= now.getYear()-b_year;
            
            if(current_month<b_month)
            {
                age=age-1;
            }
            else if (current_month==b_month)
            {
                if(current_day<b_day)
                    age=age-1;
            }
                
            age_label.setText("Age: "+age+"  ");
        }
    }
 
}


