/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package com.sun.dtv.lwuit;
import com.sun.dtv.lwuit.events.ActionListener;
import com.sun.dtv.lwuit.plaf.Style;
import com.sun.dtv.lwuit.layouts.BorderLayout;
import com.sun.dtv.lwuit.layouts.BoxLayout;
import com.sun.dtv.lwuit.layouts.FlowLayout;
import com.sun.dtv.lwuit.plaf.UIManager;
import java.util.Date;
import java.util.Hashtable;
/**
 * Date widget for selecting a date/time value. 
 * <p>To localize strings for month names
 * use the values "Calendar.Month" in the resource localization e.g. "Calendar.Jan", "Calendar.Feb" etc...
 *
 * @author Iddo Ari
 */
public class Calendar extends Container {
    protected Label month;
    protected Label year;
    private MonthView mv;
    private static String[] MONTHS = new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    /**
     * Creates a new instance of Calendar set to the given date based on time
     * since epoch (the java.util.Date convention)
     * 
     * @param time time since epoch
     */
    public Calendar(long time) {
        super(new BorderLayout());
        Container upper = new Container(new FlowLayout(Component.CENTER));
        mv = new MonthView(time, this);
        
        month = new Label(MONTHS[mv.getMonth()]);
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.setTime(new java.util.Date(time));
        month.getStyle().setBgTransparency(0);
        int y = cal.get(java.util.Calendar.YEAR);
        year = new Label("" + y);
        year.getStyle().setBgTransparency(0);
        
        Container cnt = new Container(new BoxLayout(BoxLayout.X_AXIS));
        cnt.addComponent(month);
        cnt.addComponent(year);
        upper.addComponent(cnt);
        
        addComponent(BorderLayout.NORTH, upper);
        addComponent(BorderLayout.CENTER, mv);
    }
    
    /**
     * Constructs a calendar with the current date and time
     */
    public Calendar() {
        this(System.currentTimeMillis());
    }
    /**
     * @inheritDoc
     */
    protected String getUIID() {
        return "Calendar";
    }
    /**
     * Returns the time for the current calendar.
     * 
     * @return the time for the current calendar.
     */
    public long getSelectedDay() {
        return mv.getSelectedDay();
    }
    void componentChanged() {
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.set(java.util.Calendar.YEAR, mv.getYear());
        cal.set(java.util.Calendar.MONTH, mv.getMonth());
        cal.set(java.util.Calendar.DAY_OF_MONTH, mv.getDayOfMonth());
        Hashtable t =  UIManager.getInstance().getResourceBundle();
        String text = MONTHS[mv.getMonth()];
        if(t != null) {
            Object o = t.get("Calendar." + text);
            if(o != null) {
                text = (String)o;
            }
        } 
        month.setText(text);
        year.setText("" + mv.getYear());
        month.getParent().revalidate();
    }
    /**
     * Return the date object matching the current selection
     * 
     * @return the date object matching the current selection
     */
    public Date getDate() {
        return new Date(mv.getSelectedDay());
    }
    /**
     * @inheritDoc
     */
    public void paint(Graphics g) {
        super.paint(g);
    }
    /**
     * @inheritDoc
     */
    public void refreshTheme() {
        mv.refreshTheme();
    }
    
    /**
     * @inheritDoc
     */
    public void setStyle(Style s) {
        // make sure the labels have the style of the calendar and not the style of a label
        super.setStyle(s);
        month.setStyle(s);
        year.setStyle(s);
    }
    /**
     * Sets the style of the month view component within the calendar
     */
    public void setMonthViewStyle(Style s) {
        mv.setStyle(s);
    }
    /**
     * Sets the style of the month view component within the calendar
     */
    public Style getMonthViewStyle() {
        return mv.getStyle();
    }
    
    /**
     * Fires when a change is made to the month view of this component
     */
    public void addActionListener(ActionListener l){
        mv.addActionListener(l);
    }
    
    /**
     * Fires when a change is made to the month view of this component
     */
    public void removeActionListener(ActionListener l){
        mv.removeActionListener(l);
    }
}