Below is a modified version of the DateSelector class that adds two more
constructors to allow you to specify the range of years given in the
selector. I hope its ok as I'm not the hottest java coder :-)
Sorry its not a diff - I'm still figuring this stuff out.
Regards,
Jon North
package org.apache.turbine.util;
/*
* Copyright (c) 1997-2000 The Java Apache Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions 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.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine",
* "Apache Turbine", "Turbine Project", "Apache Turbine Project" and
* "Java Apache Project" must not be used to endorse or promote products
* derived from this software without prior written permission.
*
* 5. Products derived from this software may not be called "Apache JServ"
* nor may "Apache" nor "Apache JServ" appear in their names without
* prior written permission of the Java Apache Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Java Apache Group. For more information
* on the Java Apache Project and the Apache JServ Servlet Engine project,
* please see <http://java.apache.org/>.
*
*/
// Java Core Classes
import java.util.*;
import java.text.*;
// ECS stuff.
import org.apache.ecs.html.*;
import org.apache.ecs.*;
/**
* DateSelector is a utility class to handle the creation of a set of
* date popup menus. The code is broken into a set of static methods
* for quick and easy access to the individual select objects:
*
* <pre>
* ElementContainer ec dateSelect = new ElementContainer();
* String myName = "mydate";
* ec.addElement(DateSelector.getMonthSelector(myName));
* ec.addElement(DateSelector.getDaySelector(myName));
* ec.addElement(DateSelector.getYearSelector(myName));
* </pre>
*
* There are also methods which will use attributes to build a
* complete month,day,year selector:
*
* <pre>
* DateSelector ds = new DateSelector(myName);
* dateSelect = ds.ecsOutput();
* </pre>
*
* The above element container would use the onChange setting and may
* hide the selected day if set via showDays().<br>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jeffrey D. Brekke</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Leon Atkinson</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jon North</a>
* @version $Id: DateSelector.java,v 1.3 2000/09/01 22:11:27 gonzalo Exp $
*/
public class DateSelector
{
/** Prefix for date names. */
public static final String DEFAULT_PREFIX = "DateSelector";
/** Suffix for day parameter. */
public static final String DAY_SUFFIX = "_day";
/** Suffix for month parameter. */
public static final String MONTH_SUFFIX = "_month";
/** Suffix for year parameter. */
public static final String YEAR_SUFFIX = "_year";
private Calendar useDate = null;
private String selName = null;
private static final String[] monthName =
new DateFormatSymbols().getMonths();
private String onChange = null;
private boolean onChangeSet = false;
private boolean showDays = true;
private int setDay = 0;
private static int yearsAfter = 5;
private static int yearsBefore = 5;
/**
* Constructor defaults to current date and uses the default
* prefix: <pre>DateSelector.DEFAULT</pre>
*/
public DateSelector( )
{
this.selName = DEFAULT_PREFIX;
this.useDate = Calendar.getInstance();
this.useDate.setTime ( new Date() );
}
/**
* Constructor, uses the date set in a calendar that has been
* already passed in (with the date set correctly).
*
* @param selName A String with the selector name.
* @param useDate A Calendar with a date.
*/
public DateSelector( String selName, Calendar useDate )
{
this.useDate = useDate;
this.selName = selName;
}
/**
* Constructor, uses the date set in a calendar that has been
* already passed in (with the date set correctly).
*
* @param selName A String with the selector name.
* @param useDate A Calendar with a date.
* @param yearsBefore Number of years prior to useDate to show in
selector
* @param yearsAfter Number of years after useDate to show in selector
*/
public DateSelector( String selName, Calendar useDate, int yearsBefore,
int yearsAfter )
{
this.useDate = useDate;
this.selName = selName;
if (yearsBefore < 0) yearsBefore = 0;
this.yearsBefore = yearsBefore;
if (yearsAfter < 0) yearsAfter = 0;
this.yearsAfter = yearsAfter ;
}
/**
* Constructor defaults to current date.
*
* @param selName A String with the selector name.
* @param yearsBefore Number of years prior to useDate to show in
selector
* @param yearsAfter Number of years after useDate to show in selector
*/
public DateSelector( String selName, int yearsBefore, int yearsAfter )
{
this.selName = selName;
this.useDate = Calendar.getInstance();
this.useDate.setTime ( new Date() );
if (yearsBefore < 0) yearsBefore = 0;
this.yearsBefore = yearsBefore;
if (yearsAfter < 0) yearsAfter = 0;
this.yearsAfter = yearsAfter;
}
/**
* Constructor defaults to current date.
*
* @param selName A String with the selector name.
*/
public DateSelector( String selName )
{
this.selName = selName;
this.useDate = Calendar.getInstance();
this.useDate.setTime ( new Date() );
}
/**
* Adds the onChange to all of <SELECT> tags. This is limited to
* one function for all three popups and is only used when the
* output() methods are used. Individual getMonth, getDay,
* getYear static methods will not use this setting.
*
* @param string A String to use for onChange attribute. If null,
* then nothing will be set.
* @return A DateSelector (self).
*/
public DateSelector setOnChange ( String onChange )
{
if (onChange != null)
{
this.onChange = onChange;
this.onChangeSet = true;
}
else
{
this.onChange = null;
this.onChangeSet = false;
}
return this;
}
/**
* Select the day to be selected if the showDays(false) behavior
* is used. Individual getMonth, getDay, getYear static methods
* will not use this setting.
*
* @param day The day.
* @return A DateSelector (self).
*/
public DateSelector setDay( int day )
{
this.setDay = day;
this.showDays = false;
return this;
}
/**
* Whether or not to show the days as a popup menu. The days will
* be a hidden parameter and the value set with setDay is used.
* Individual getMonth, getDay, getYear static methods will not
* use this setting.
*
* @param show True if the day should be shown.
* @return A DateSelector (self).
*/
public DateSelector setShowDay ( boolean show )
{
this.showDays = false;
return this;
}
/**
* Set the selector name prefix. Individual getMonth, getDay,
* getYear static methods will not use this setting.
*
* @param selname A String with the select name prefix.
*/
public void setSelName( String selName )
{
this.selName = selName;
}
/**
* Get the selector name prefix.
*
* @return A String with the select name prefix.
*/
public String getSelName()
{
return selName;
}
/**
* Return a month selector.
*
* @param name The name to use for the selected month.
* @return A select object with all the months.
*/
public static Select getMonthSelector(String name)
{
return(getMonthSelector(name, Calendar.getInstance()));
}
/**
* Return a month selector.
*
* Note: The values of the month placed into the select list are
* the month integers starting at 0 (ie: if the user selects
* February, the selected value will be 1).
*
* @param name The name to use for the selected month.
* @param now Calendar to start with.
* @return A select object with all the months.
*/
public static Select getMonthSelector(String name,
Calendar now)
{
Select monthSelect = new Select().setName(name);
for (int curMonth = 0;curMonth <= 11; curMonth++)
{
Option o = new Option();
o.addElement(monthName[curMonth]);
o.setValue(curMonth);
if ((now.get(Calendar.MONTH)) == curMonth)
{
o.setSelected(true);
}
monthSelect.addElement(o);
}
return(monthSelect);
}
/**
* Return a day selector.
*
* @param name The name to use for the selected day.
* @return A select object with all the days in a month.
*/
public static Select getDaySelector(String name)
{
return(getDaySelector(name, Calendar.getInstance()));
}
/**
* Return a day selector.
*
* @param name The name to use for the selected day.
* @param now Calendar to start with.
* @return A select object with all the days in a month.
*/
public static Select getDaySelector(String name,
Calendar now)
{
Select daySelect = new Select().setName(name);
for(int currentDay=1; currentDay <= 31; currentDay++)
{
Option o = new Option();
o.addElement(Integer.toString(currentDay));
o.setValue(currentDay);
if (now.get(Calendar.DAY_OF_MONTH) == currentDay)
{
o.setSelected(true);
}
daySelect.addElement(o);
}
return(daySelect);
}
/**
* Return a year selector.
*
* @param name The name to use for the selected year.
* @return A select object with all the years starting five years
* from now and five years before this year.
*/
public static Select getYearSelector(String name)
{
return(getYearSelector(name, Calendar.getInstance()));
}
/**
* Return a year selector.
*
* @param name The name to use for the selected year.
* @param now Calendar to start with.
* @return A select object with all the years starting yearsAfter years
* from now and yearsBefore years before this year.
*/
public static Select getYearSelector(String name,
Calendar now)
{
Select yearSelect = new Select().setName(name);
int startYear = now.get(Calendar.YEAR);
for(int currentYear = startYear - yearsBefore;
currentYear <= startYear + yearsAfter;
currentYear++)
{
Option o = new Option();
o.addElement(Integer.toString(currentYear));
o.setValue(currentYear);
if (startYear == currentYear)
{
o.setSelected(true);
}
yearSelect.addElement(o);
}
return(yearSelect);
}
/**
* Used to build the popupmenu in HTML. The properties set in the
* object are used to generate the correct HTML. The selName
* attribute is used to seed the names of the select lists. The
* names will be generated as follows:
*
* <ul>
* <li>selName + "_month"</li>
* <li>selName + "_day"</li>
* <li>selName + "_year"</li>
* </ul>
*
* If onChange was set it is also used in the generation of the
* output. The output HTML will list the select lists in the
* following order: month day year.
*
* @return A String with the correct HTML for the date selector.
*/
public String output()
{
return(ecsOutput().toString());
}
/**
* Used to build the popupmenu in HTML. The properties set in the
* object are used to generate the correct HTML. The selName
* attribute is used to seed the names of the select lists. The
* names will be generated as follows:
*
* <ul>
* <li>selName + "_month"</li>
* <li>selName + "_day"</li>
* <li>selName + "_year"</li>
* </ul>
*
* The output HTML will list the select lists in the following
* order: month day year.
*
* @return A String with the correct HTML for the date selector.
*/
public String toString()
{
return(ecsOutput().toString());
}
/*
* Return an ECS container with the month, day, and year select
* objects inside.
*
* @return An ECS container.
*/
public ElementContainer ecsOutput()
{
if ( this.useDate == null )
{
this.useDate.setTime ( new Date() );
}
Select monthSelect = getMonthSelector(selName + MONTH_SUFFIX,
useDate);
ConcreteElement daySelect = null;
if (!showDays)
{
daySelect = new Input(Input.hidden,
selName + DAY_SUFFIX,
setDay);
}
else
{
Select tmp = getDaySelector(selName + DAY_SUFFIX, useDate);
if (onChangeSet)
tmp.setOnChange(onChange);
daySelect = tmp;
}
Select yearSelect = getYearSelector(selName + YEAR_SUFFIX, useDate);
if (onChangeSet)
{
monthSelect.setOnChange(onChange);
yearSelect.setOnChange(onChange);
}
ElementContainer ec = new ElementContainer();
ec.addElement(new Comment("== BEGIN
org.apache.turbine.util.DateSelector.ecsOutput() =="));
ec.addElement(monthSelect);
ec.addElement(daySelect);
ec.addElement(yearSelect);
ec.addElement(new Comment("== END
org.apache.turbine.util.DateSelector.ecsOutput() =="));
return (ec);
}
}
--------------------------------------------------
DISCLAIMER: This message contains proprietary
information some or all of which may be
confidential and/or legally privileged. It is for
the intended recipient only who may use and apply
the information only for the intended purpose.
Internet communications are not secure and
therefore the British Biotech group does not
accept legal responsibility for the contents of
this message. Any views or opinions presented are
only those of the author and not those of the
British Biotech group. If you are not the intended
recipient please delete this e-mail and notify the
author immediately by calling ++44 (0)1865 748747;
do not use, disclose, distribute, copy, print or
rely on this e-mail.
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?: [EMAIL PROTECTED]