I've updated DateSelector to clean up some usage issues and better docs:
*) Added a default prefix name of "org.apache.turbine.util.dateselector" and
added a default construtor which uses this prefix.
*) Made the getMonthSelector, getDaySelector, and getYearSelector static for
ease of use.
*) Examples are in the javadoc for the class of both static and non-static
usage.
*) Created an ecsOutput method that returns the select objects in an ecs
element container. Overloaded toString() to return the html output of the
object. toString() and output() both just call ecsOutput().toString() now.
*) Various documentation fixes.
Patch is attached. Let me know if there are any issues/comments.
jb
-----
Index: DateSelector.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/util/DateSelector.java,v
retrieving revision 1.5
diff -u -r1.5 DateSelector.java
--- DateSelector.java 2000/05/05 21:15:02 1.5
+++ DateSelector.java 2000/05/07 16:35:49
@@ -65,13 +65,22 @@
/**
*
* DateSelector is a utility class to handle the
- * creation of a set of date popup menus.
+ * 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>
*
- * This code was ported from leon's php3 code.
- *
- * Modifications added to use ecs and allow for each element to have
- * its own name and you can get each select list.
- *
* @author Jeffrey D. Brekke <a
href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
* @author Jon S. Stevens <a
href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
* @author Leon Atkinson <a
href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
@@ -80,6 +89,9 @@
*/
public class DateSelector
{
+ /** prefix for date names */
+ public static final String DEFAULT_PREFIX =
"org.apache.turbine.util.dateselector";
+
/** suffix for day parameter */
public static final String DAY_SUFFIX = "_day";
@@ -98,6 +110,19 @@
private int setDay = 0;
/**
+ 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).
*/
@@ -118,16 +143,30 @@
}
/**
Adds the onChange to all of <SELECT> tags
- This is limited to one function for all three popups.
+ 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 to use for onChange attribute. If null, then nothing
will be set.
+
*/
public DateSelector setOnChange ( String onChange )
{
- this.onChange = onChange;
- this.onChangeSet = true;
+ if (onChange != null)
+ {
+ this.onChange = onChange;
+ this.onChangeSet = true;
+ }
+ else
+ {
+ this.onChange = null;
+ this.onChangeSet = false;
+ }
return this;
}
+
/**
- Show the days in the date selector
+ 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
*/
public DateSelector setDay( int day )
{
@@ -136,7 +175,8 @@
return this;
}
/**
- Whether or not to show the days as a popup menu
+ 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.
*/
public DateSelector setShowDay ( boolean show )
{
@@ -145,10 +185,27 @@
}
/**
+ Individual getMonth, getDay, getYear static methods will not use this
setting.
+ @param selname the select name prefix.
+ */
+ public void setSelName( String selName )
+ {
+ this.selName = selName;
+ }
+
+ /**
+ @return the setting for the select name prefix.
+ */
+ public String getSelName()
+ {
+ return selName;
+ }
+
+ /**
@param name the name to use for the selected month
@return A select object with all the months
*/
- public Select getMonthSelector(String name)
+ static public Select getMonthSelector(String name)
{
return(getMonthSelector(name, Calendar.getInstance()));
}
@@ -160,11 +217,9 @@
@param Calendar to start with.
@return A select object with all the months
*/
- public Select getMonthSelector(String name, Calendar now)
+ static public Select getMonthSelector(String name, Calendar now)
{
Select monthSelect = new Select().setName(name);
- if (onChangeSet)
- monthSelect.setOnChange(onChange);
for (int curMonth = 0;curMonth <= 11; curMonth++)
{
@@ -184,7 +239,7 @@
@param name the name to use for the selected day
@return A select object with all the days in a month
*/
- public Select getDaySelector(String name)
+ static public Select getDaySelector(String name)
{
return(getDaySelector(name, Calendar.getInstance()));
}
@@ -194,11 +249,9 @@
@param now Calendar to start with.
@return A select object with all the days in a month
*/
- public Select getDaySelector(String name, Calendar now)
+ static public Select getDaySelector(String name, Calendar now)
{
Select daySelect = new Select().setName(name);
- if (onChangeSet)
- daySelect.setOnChange(onChange);
for(int currentDay=1; currentDay <= 31; currentDay++)
{
@@ -219,7 +272,7 @@
@return A select object with all the years starting five years from
now and five years before this year.
*/
- public Select getYearSelector(String name)
+ static public Select getYearSelector(String name)
{
return(getYearSelector(name, Calendar.getInstance()));
}
@@ -230,11 +283,9 @@
@return A select object with all the years starting five years from
now and five years before this year.
*/
- public Select getYearSelector(String name, Calendar now)
+ static public Select getYearSelector(String name, Calendar now)
{
Select yearSelect = new Select().setName(name);
- if (onChangeSet)
- yearSelect.setOnChange(onChange);
int startYear = now.get(Calendar.YEAR);
for(int currentYear = startYear-5; currentYear <= startYear+5;
currentYear++)
@@ -259,11 +310,36 @@
<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 the correct html string 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 the correct html string for the date selector
+ */
+ public String toString()
+ {
+ return(ecsOutput().toString());
+ }
+
+ /*
+ * @return an ECS container with the month, day, and year select objects
inside.
+ */
+ public ElementContainer ecsOutput()
+ {
if ( this.useDate == null )
{
this.useDate.setTime ( new Date() );
@@ -278,11 +354,23 @@
setDay);
}
else
- {
- daySelect = getDaySelector(selName + DAY_SUFFIX, useDate);
+ { Select tmp = getDaySelector(selName + DAY_SUFFIX, useDate);
+ if (onChangeSet)
+ tmp.setOnChange(onChange);
+ daySelect = tmp;
}
Select yearSelect = getYearSelector(selName + YEAR_SUFFIX, useDate);
-
- return (monthSelect.toString() + daySelect.toString() +
yearSelect.toString());
+ 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);
}
}
Jeffrey D. Brekke
mailto:[EMAIL PROTECTED]
http://sites.netscape.net/ekkerbj/homepage
____________________________________________________________________
Get your own FREE, personal Netscape WebMail account today at
http://webmail.netscape.com.
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]