1 16 package org.apache.myfaces.custom.date; 17 18 import java.io.Serializable ; 19 import java.text.ParseException ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Calendar ; 22 import java.util.Date ; 23 import java.util.Locale ; 24 25 import javax.faces.component.UIInput; 26 import javax.faces.context.FacesContext; 27 import javax.faces.el.ValueBinding; 28 29 import org.apache.myfaces.component.UserRoleAware; 30 import org.apache.myfaces.component.UserRoleUtils; 31 32 36 public class HtmlInputDate extends UIInput implements UserRoleAware { 37 public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlInputDate"; 38 public static final String COMPONENT_FAMILY = "javax.faces.Input"; 39 private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Date"; 40 private static final boolean DEFAULT_DISABLED = false; 41 42 private String _enabledOnUserRole = null; 43 private String _visibleOnUserRole = null; 44 45 50 private String _type = null; 51 private Boolean _popupCalendar = null; 52 53 54 private Boolean _disabled = null; 55 56 private UserData _userData = null; 58 59 public HtmlInputDate() { 60 setRendererType(DEFAULT_RENDERER_TYPE); 61 } 62 63 public void setUserData(UserData userData){ 64 this._userData = userData; 65 } 66 67 public UserData getUserData(Locale currentLocale){ 68 if( _userData == null ) 69 _userData = new UserData((Date ) getValue(), currentLocale); 70 return _userData; 71 } 72 73 public String getType() { 74 if (_type != null) return _type; 75 ValueBinding vb = getValueBinding("type"); 76 return vb != null ? (String )vb.getValue(getFacesContext()) : "date"; 77 } 78 public void setType(String string) { 79 _type = string; 80 } 81 82 public boolean isPopupCalendar(){ 83 if (_popupCalendar != null) 84 return _popupCalendar.booleanValue(); 85 ValueBinding vb = getValueBinding("popupCalendar"); 86 return vb != null ? ((Boolean )vb.getValue(getFacesContext())).booleanValue() : false; 87 } 88 public void setPopupCalendar(boolean popupCalendar){ 89 this._popupCalendar = Boolean.valueOf(popupCalendar); 90 } 91 92 public void setEnabledOnUserRole(String enabledOnUserRole){ 93 _enabledOnUserRole = enabledOnUserRole; 94 } 95 public String getEnabledOnUserRole(){ 96 if (_enabledOnUserRole != null) return _enabledOnUserRole; 97 ValueBinding vb = getValueBinding("enabledOnUserRole"); 98 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 99 } 100 101 public void setVisibleOnUserRole(String visibleOnUserRole){ 102 _visibleOnUserRole = visibleOnUserRole; 103 } 104 public String getVisibleOnUserRole(){ 105 if (_visibleOnUserRole != null) return _visibleOnUserRole; 106 ValueBinding vb = getValueBinding("visibleOnUserRole"); 107 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 108 } 109 110 public boolean isRendered(){ 111 if (!UserRoleUtils.isVisibleOnUserRole(this)) return false; 112 return super.isRendered(); 113 } 114 115 public boolean isDisabled(){ 116 if (_disabled != null) return _disabled.booleanValue(); 117 ValueBinding vb = getValueBinding("disabled"); 118 Boolean v = vb != null ? (Boolean )vb.getValue(getFacesContext()) : null; 119 return v != null ? v.booleanValue() : DEFAULT_DISABLED; 120 } 121 public void setDisabled(boolean disabled) { 122 _disabled = Boolean.valueOf(disabled); 123 } 124 125 public Object saveState(FacesContext context) { 126 Object values[] = new Object [7]; 127 values[0] = super.saveState(context); 128 values[1] = _type; 129 values[2] = _popupCalendar; 130 values[3] = _userData; 131 values[4] = _disabled; 132 values[5] = _enabledOnUserRole; 133 values[6] = _visibleOnUserRole; 134 return values; 135 } 136 137 public void restoreState(FacesContext context, Object state) { 138 Object values[] = (Object [])state; 139 super.restoreState(context, values[0]); 140 _type = (String )values[1]; 141 _popupCalendar = (Boolean )values[2]; 142 _userData = (UserData)values[3]; 143 _disabled = (Boolean )values[4]; 144 _enabledOnUserRole = (String )values[5]; 145 _visibleOnUserRole = (String )values[6]; 146 } 147 148 public static class UserData implements Serializable { 149 private String day; 150 private String month; 151 private String year; 152 private String hours; 153 private String minutes; 154 private String seconds; 155 156 public UserData(Date date, Locale currentLocale){ 157 if( date == null ) 158 date = new Date (); 159 160 Calendar calendar = Calendar.getInstance(currentLocale); 161 calendar.setTime( date ); 162 day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)); 163 month = Integer.toString(calendar.get(Calendar.MONTH)+1); 164 year = Integer.toString(calendar.get(Calendar.YEAR)); 165 hours = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY)); 166 minutes = Integer.toString(calendar.get(Calendar.MINUTE)); 167 seconds = Integer.toString(calendar.get(Calendar.SECOND)); 168 } 169 170 public Date parse() throws ParseException { 171 SimpleDateFormat fullFormat = new SimpleDateFormat ( "dd MM yyyy hh mm ss" ); 172 return fullFormat.parse(day+" "+month+" "+year+" "+hours+" "+minutes+" "+seconds); 173 } 174 175 private String formatedInt(String toFormat){ 176 if( toFormat == null ) 177 return null; 178 179 int i = -1; 180 try{ 181 i = Integer.parseInt( toFormat ); 182 }catch(NumberFormatException nfe){ 183 return toFormat; 184 } 185 if( i >= 0 && i < 10 ) 186 return "0"+i; 187 return Integer.toString(i); 188 } 189 190 191 public String getDay() { 192 return formatedInt( day ); 193 } 194 public void setDay(String day) { 195 this.day = day; 196 } 197 198 public String getMonth() { 199 return month; 200 } 201 public void setMonth(String month) { 202 this.month = month; 203 } 204 205 public String getYear() { 206 return year; 207 } 208 public void setYear(String year) { 209 this.year = year; 210 } 211 212 public String getHours() { 213 return formatedInt( hours ); 214 } 215 public void setHours(String hours) { 216 this.hours = hours; 217 } 218 public String getMinutes() { 219 return formatedInt( minutes ); 220 } 221 public void setMinutes(String minutes) { 222 this.minutes = minutes; 223 } 224 225 public String getSeconds() { 226 return formatedInt( seconds ); 227 } 228 public void setSeconds(String seconds) { 229 this.seconds = seconds; 230 } 231 } 232 } | Popular Tags |