/* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 
 - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 
 - Redistribution in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.
 
 Neither the name of Sun Microsystems, Inc. or the names of
 contributors may be used to endorse or promote products derived
 from this software without specific prior written permission.
 
 This software is provided "AS IS," without a warranty of any
 kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 
 You acknowledge that Software is not designed, licensed or intended
 for use in the design, construction, operation or maintenance of
 any nuclear facility.
 $Id: DateSelectorTag.java,v 1.1 2003/07/11 22:55:58 gmurray Exp $ */

package com.sun.j2ee.blueprints.waf.view.taglibs.smart;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import java.io.*;
import java.util.*;

/**
 * HTML 'input' tag. Use with NameTag and ValueTag.
 */
public class DateSelectorTag extends SimpleTagSupport {

    private String locale = "en_US";
    private String prefix = "date";
    private Calendar calendar;

    public void setLocale(String locale) {this.locale=locale;}
    public void setPrefix(String prefix) {this.prefix=prefix;}    
    public void setDate(Date d) {
      
        if (locale == null){
            calendar = Calendar.getInstance();
        } else {
            calendar = Calendar.getInstance(new Locale(locale));
        }
    }
    
    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }
    
    public void doTag() throws JspTagException, IOException { 
        JspWriter out = getJspContext().getOut();
        // default to current date if not specified
        if (calendar == null) {
            Date date = new Date();
            setDate(date);
        }
        //int dayCount = calendar.getMaximum(Calendar.DAY_OF_MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH) ;
        int month = calendar.get(Calendar.MONTH) + 1;
        int year = calendar.get(Calendar.YEAR) ;
        // create the select tags
        if ((locale != null) && locale.toLowerCase().equals("en_us") ||
            locale.toLowerCase().equals("en-us")) {
            createSelect(prefix + "_month", 1 , 12 , month, out);
            createSelect(prefix + "_day", 1, 31, day, out);
            createSelect(prefix + "_year", year, year + 4, year, out);
        } else if ((locale != null) && locale.toLowerCase().equals("ja_jp") ||
            locale.toLowerCase().equals("ja-jp")) {
            createSelect(prefix + "_year", 2003, 4, year, out);
            createSelect(prefix + "_month", 1, 12 , month, out);
            createSelect(prefix + "_day", 1, 31, day, out);
        }
       reset();
    }
    
    private void createSelect(String name,
                                                      int start,
                                                      int count,
                                                      int selectedIndex,                                                    
                                                     JspWriter out) throws IOException {
        out.write("<select name=\"" + name + "\">");
        for (int loop=start; loop < count +1; loop++) {
            if (loop == selectedIndex) {
                out.write("<option selected>" + loop + "</option>");
            } else {
                out.write("<option>" + loop + "</option>");
            }
        }
        out.write("</select>");
    }
    
    private void reset() {
        locale = "en_US";
        prefix = "date";
        calendar = null;
    }
}