You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2005/11/10 18:55:44 UTC

svn commit: r332339 - in /myfaces: forrest/trunk/content/xdocs/tomahawk/ tomahawk/trunk/src/java/org/apache/myfaces/custom/date/ tomahawk/trunk/tld/

Author: mmarinschek
Date: Thu Nov 10 09:55:30 2005
New Revision: 332339

URL: http://svn.apache.org/viewcvs?rev=332339&view=rev
Log:
fix for MYFACES-238. Thanks to Dave Brondsema.

Modified:
    myfaces/forrest/trunk/content/xdocs/tomahawk/inputDate.xml
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDate.java
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java
    myfaces/tomahawk/trunk/tld/tomahawk.tld

Modified: myfaces/forrest/trunk/content/xdocs/tomahawk/inputDate.xml
URL: http://svn.apache.org/viewcvs/myfaces/forrest/trunk/content/xdocs/tomahawk/inputDate.xml?rev=332339&r1=332338&r2=332339&view=diff
==============================================================================
--- myfaces/forrest/trunk/content/xdocs/tomahawk/inputDate.xml (original)
+++ myfaces/forrest/trunk/content/xdocs/tomahawk/inputDate.xml Thu Nov 10 09:55:30 2005
@@ -19,6 +19,8 @@
         <section>
             <title>Screen Shot</title>
             <figure src="images/inputdate.png" alt="inputdate"/>
+            <p></p>
+            <figure src="images/inputdate2.png" alt="inputdate2"/>
         </section>
         <!-- API -->
         <section>
@@ -50,8 +52,9 @@
         <section>
             <title>Usage</title>
             <source>
-&lt;t:inputDate type="{date|time|both}"
+&lt;t:inputDate type="{date|time|both|short_time|full}"
                 popupCalendar="{true|false}"/&gt;
+                ampm="{true|false}"/&gt;
             </source>
         </section>
         <!-- Syntax -->
@@ -59,10 +62,14 @@
             <title>Syntax</title>
             <note label="&lt;t:inputDate&gt;">
                 <code>
-                    type - defines the content type. One of these constants (date | time | both).
+                    type - defines the content type. One of these constants (date | time | both | full | short_time).
                 </code>
                 <br/>
                 <code>popupCalendar - if true the value can be set using a popup inputCalendar component.</code><br/>
+                <code>
+                    ampm - If true, use 12hr times with AM/PM selector; if false, use 24hr time.  Default false.
+                </code>
+                <br/>
             </note>
         </section>
         <!-- Instructions -->

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java?rev=332339&r1=332338&r2=332339&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java Thu Nov 10 09:55:30 2005
@@ -55,6 +55,7 @@
     private static final String ID_HOURS_POSTFIX = ".hours";
     private static final String ID_MINUTES_POSTFIX = ".minutes";
     private static final String ID_SECONDS_POSTFIX = ".seconds";
+    private static final String ID_AMPM_POSTFIX = ".ampm";
 
     protected boolean isDisabled(FacesContext facesContext, HtmlInputDate inputDate) {
         if( !UserRoleUtils.isEnabledOnUserRole(inputDate) )
@@ -72,6 +73,7 @@
         if( userData == null )
             userData = inputDate.getUserData(currentLocale);
         String type = inputDate.getType();
+        boolean ampm = inputDate.isAmpm();
         String clientId = uiComponent.getClientId(facesContext);
 
         boolean disabled = isDisabled(facesContext, inputDate);
@@ -84,7 +86,7 @@
         writer.startElement(HTML.SPAN_ELEM, uiComponent);
         writer.writeAttribute(HTML.ID_ATTR, clientId, null);
 
-        if( ! type.equals("time")){
+        if( ! (type.equals("time") || type.equals("short_time"))){
 	        encodeInputDay(inputDate, writer, clientId, userData, disabled, readonly);
 	        encodeInputMonth(inputDate, writer, clientId, userData, currentLocale, disabled, readonly);
 	        encodeInputYear(inputDate, writer, clientId, userData, disabled, readonly);
@@ -103,6 +105,9 @@
 						writer.write(":");
 	        	encodeInputSeconds(uiComponent, writer, clientId, userData, disabled, readonly);
 					}
+            if (ampm) {
+                encodeInputAmpm(uiComponent, writer, clientId, userData, disabled, readonly, currentLocale);
+            }
         }
         
         writer.endElement(HTML.SPAN_ELEM);
@@ -199,6 +204,46 @@
         encodeInputField(uiComponent, writer, clientId + ID_SECONDS_POSTFIX, userData.getSeconds(), 2, disabled, readonly);
     }
     
+    protected void encodeAmpmChoice(DateFormatSymbols symbols, UIComponent uiComponent, ResponseWriter writer, int calendar_ampm, int selected) throws IOException {
+    	String[] ampm_choices = symbols.getAmPmStrings();
+        writer.write("\t\t");
+        writer.startElement(HTML.OPTION_ELEM, uiComponent);
+        writer.writeAttribute(HTML.VALUE_ATTR, calendar_ampm, null);
+        if (calendar_ampm == selected)
+            writer.writeAttribute(HTML.SELECTED_ATTR, HTML.SELECTED_ATTR, null);
+        writer.writeText(ampm_choices[calendar_ampm], null);
+        writer.endElement(HTML.OPTION_ELEM);
+    }
+    
+    protected void encodeInputAmpm(UIComponent uiComponent, ResponseWriter writer, String clientId,
+			UserData userData, boolean disabled, boolean readonly, Locale currentLocale) throws IOException {
+        writer.startElement(HTML.SELECT_ELEM, uiComponent);
+        writer.writeAttribute(HTML.ID_ATTR, clientId + ID_AMPM_POSTFIX, null);
+        writer.writeAttribute(HTML.NAME_ATTR, clientId + ID_AMPM_POSTFIX, null);
+        writer.writeAttribute(HTML.SIZE_ATTR, "1", null);
+        HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES);
+        HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.EVENT_HANDLER_ATTRIBUTES);
+        HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.COMMON_FIELD_EVENT_ATTRIBUTES);
+
+        if (disabled) {
+            writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
+        }
+        if (readonly) {
+            writer.writeAttribute(HTML.READONLY_ATTR, Boolean.TRUE, null);
+        }
+
+        DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);
+        
+        int selectedAmpm = userData.getAmpm() == null ? -1 : Integer.parseInt(userData.getAmpm());
+        encodeAmpmChoice(symbols, uiComponent, writer, Calendar.AM, selectedAmpm);
+        encodeAmpmChoice(symbols, uiComponent, writer, Calendar.PM, selectedAmpm);
+
+
+        // bug #970747: force separate end tag
+        writer.writeText("", null);
+        writer.endElement(HTML.SELECT_ELEM);
+    }
+    
     protected void encodePopupCalendarButton(FacesContext facesContext, UIComponent uiComponent, ResponseWriter writer, String clientId, Locale currentLocale) throws IOException{
 
         DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);
@@ -253,7 +298,7 @@
         String type = inputDate.getType();
         Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
 
-        if( ! type.equals( "time" ) ){
+        if( ! (type.equals( "time" ) || type.equals( "short_time" )) ){
             userData.setDay( (String) requestMap.get(clientId + ID_DAY_POSTFIX) );
             userData.setMonth( (String) requestMap.get(clientId + ID_MONTH_POSTFIX) );
             userData.setYear( (String) requestMap.get(clientId + ID_YEAR_POSTFIX) );
@@ -263,7 +308,12 @@
             userData.setHours( (String) requestMap.get(clientId + ID_HOURS_POSTFIX) );
             userData.setMinutes( (String) requestMap.get(clientId + ID_MINUTES_POSTFIX) );
             if (type.equals("full") || type.equals("time"))
-							userData.setSeconds( (String) requestMap.get(clientId + ID_SECONDS_POSTFIX) );
+				userData.setSeconds( (String) requestMap.get(clientId + ID_SECONDS_POSTFIX) );
+            else
+            	userData.setSeconds("0");
+            if (inputDate.isAmpm()) {
+            	userData.setAmpm( (String) requestMap.get(clientId + ID_AMPM_POSTFIX) );
+            }
         }
 
         inputDate.setSubmittedValue( userData );

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDate.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDate.java?rev=332339&r1=332338&r2=332339&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDate.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDate.java Thu Nov 10 09:55:30 2005
@@ -51,6 +51,7 @@
     private String _type = null;
     private Boolean _popupCalendar = null;
     private String _timeZone = null;
+    private Boolean _ampm = null;
 
 
     private Boolean _disabled = null;
@@ -60,7 +61,7 @@
     }
 
     public UserData getUserData(Locale currentLocale){
-        return new UserData((Date) getValue(), currentLocale, getTimeZone());
+        return new UserData((Date) getValue(), currentLocale, getTimeZone(), isAmpm());
     }
 
 	public String getType() {
@@ -81,6 +82,16 @@
     public void setPopupCalendar(boolean popupCalendar){
         this._popupCalendar = Boolean.valueOf(popupCalendar);
     }
+    
+    public boolean isAmpm(){
+   		if (_ampm != null)
+   		    return _ampm.booleanValue();
+   		ValueBinding vb = getValueBinding("ampm");
+   		return vb != null ? ((Boolean)vb.getValue(getFacesContext())).booleanValue() : false;
+    }
+    public void setAmpm(boolean ampm){
+        this._ampm = Boolean.valueOf(ampm);
+    }
 
     public String getTimeZone(){
         if(_timeZone != null) return _timeZone;
@@ -136,7 +147,7 @@
     }
 
     public Object saveState(FacesContext context) {
-        Object values[] = new Object[8];
+        Object values[] = new Object[9];
         values[0] = super.saveState(context);
         values[1] = _type;
         values[2] = _popupCalendar;
@@ -145,6 +156,7 @@
         values[5] = _enabledOnUserRole;
         values[6] = _visibleOnUserRole;
         values[7] = _timeZone;
+        values[8] = _ampm;
         return values;
     }
 
@@ -158,6 +170,7 @@
         _enabledOnUserRole = (String)values[5];
         _visibleOnUserRole = (String)values[6];
         _timeZone = (String)values[7];
+        _ampm = (Boolean)values[8];
     }
 
     public static class UserData implements Serializable {
@@ -169,8 +182,11 @@
         private String minutes;
         private String seconds;
         private TimeZone timeZone = null;
+        private String ampm;
+        private boolean uses_ampm;
 
-        public UserData(Date date, Locale currentLocale, String _timeZone){
+        public UserData(Date date, Locale currentLocale, String _timeZone, boolean uses_ampm){
+        	this.uses_ampm = uses_ampm;
             if( date == null )
                 date = new Date();
 
@@ -183,7 +199,17 @@
             day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
             month = Integer.toString(calendar.get(Calendar.MONTH)+1);
             year = Integer.toString(calendar.get(Calendar.YEAR));
-            hours = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY));
+            if (uses_ampm) {
+            	int int_hours = calendar.get(Calendar.HOUR);
+            	// ampm hours must be in range 0-11 to be handled right; we have to handle "12" specially
+            	if (int_hours == 0) {
+            		int_hours = 12;
+            	}
+            	hours = Integer.toString(int_hours);
+                ampm = Integer.toString(calendar.get(Calendar.AM_PM));
+            } else {
+            	hours = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY));
+            }
             minutes = Integer.toString(calendar.get(Calendar.MINUTE));
             seconds = Integer.toString(calendar.get(Calendar.SECOND));
         }
@@ -196,9 +222,20 @@
             tempCalendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
             tempCalendar.set(Calendar.MONTH,Integer.parseInt(month)-1);
             tempCalendar.set(Calendar.YEAR,Integer.parseInt(year));
-            tempCalendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hours));
+            if (uses_ampm) {
+            	int int_hours = Integer.parseInt(hours);
+            	// ampm hours must be in range 0-11 to be handled right; we have to handle "12" specially
+            	if (int_hours == 12) {
+        			int_hours = 0;
+            	}
+            	tempCalendar.set(Calendar.HOUR,int_hours);
+                tempCalendar.set(Calendar.AM_PM,Integer.parseInt(ampm));
+            } else {
+            	tempCalendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hours));
+            }
             tempCalendar.set(Calendar.MINUTE,Integer.parseInt(minutes));
             tempCalendar.set(Calendar.SECOND,Integer.parseInt(seconds));
+            tempCalendar.set(Calendar.MILLISECOND, 0);
             
             return tempCalendar.getTime();
         }
@@ -259,5 +296,12 @@
         public void setSeconds(String seconds) {
             this.seconds = seconds;
         }
+        
+        public String getAmpm() {
+            return ampm;
+        }
+        public void setAmpm(String ampm) {
+            this.ampm = ampm;
+        }
     }
-}
\ No newline at end of file
+}

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java?rev=332339&r1=332338&r2=332339&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlInputDateTag.java Thu Nov 10 09:55:30 2005
@@ -35,6 +35,7 @@
 
     // HtmlInputDate attributes
     private String type;
+    private String ampm;
     private String popupCalendar;
     private String timeZone;
 
@@ -58,6 +59,7 @@
         enabledOnUserRole=null;
         visibleOnUserRole=null;
         type=null;
+        ampm=null;
         popupCalendar=null;
         timeZone = null;
     }
@@ -67,6 +69,7 @@
 
         setStringProperty(component, "type", type);
         setBooleanProperty(component, "popupCalendar", popupCalendar);
+        setBooleanProperty(component, "ampm", ampm);
         setStringProperty(component, "timeZone", timeZone);
 
         setStringProperty(component, UserRoleAware.ENABLED_ON_USER_ROLE_ATTR, enabledOnUserRole);
@@ -81,6 +84,10 @@
         this.popupCalendar = popupCalendar;
     }
 
+    public void setAmpm(String ampm){
+        this.ampm = ampm;
+    }
+
     public void setEnabledOnUserRole(String enabledOnUserRole){
         this.enabledOnUserRole = enabledOnUserRole;
     }
@@ -93,4 +100,4 @@
     {
         this.timeZone = timeZone;
     }
-}
\ No newline at end of file
+}

Modified: myfaces/tomahawk/trunk/tld/tomahawk.tld
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/tld/tomahawk.tld?rev=332339&r1=332338&r2=332339&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/tld/tomahawk.tld (original)
+++ myfaces/tomahawk/trunk/tld/tomahawk.tld Thu Nov 10 09:55:30 2005
@@ -1114,11 +1114,11 @@
         &html_event_handler_attributes;
         &html_input_attributes;
 		&user_role_attributes;
-		<!-- type == date | time | both -->
         <attribute>
 	        <name>type</name>
             <required>false</required>
             <rtexprvalue>false</rtexprvalue>
+            <description>date | time | short_time | both | full</description>
         </attribute>
         <attribute>
 	        <name>popupCalendar</name>
@@ -1129,6 +1129,12 @@
             <name>timeZone</name>
             <required>false</required>
             <rtexprvalue>false</rtexprvalue>
+        </attribute>
+        <attribute>
+	        <name>ampm</name>
+            <required>false</required>
+            <rtexprvalue>false</rtexprvalue>
+            <description>If true, use 12hr times with AM/PM selector; if false, use 24hr time.  Default false.</description>
         </attribute>
     </tag>