KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > date > HtmlInputDate


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.date;
17
18 import java.io.Serializable JavaDoc;
19 import java.text.ParseException JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Calendar JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
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 /**
33  * @author Sylvain Vieujot (latest modification by $Author: svieujot $)
34  * @version $Revision: 1.9 $ $Date: 2005/02/03 15:01:27 $
35  */

36 public class HtmlInputDate extends UIInput implements UserRoleAware {
37     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.HtmlInputDate";
38     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Input";
39     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Date";
40     private static final boolean DEFAULT_DISABLED = false;
41     
42     private String JavaDoc _enabledOnUserRole = null;
43     private String JavaDoc _visibleOnUserRole = null;
44     
45     /**
46      * Same as for f:convertDateTime
47      * Specifies what contents the string value will be formatted to include, or parsed expecting.
48      * Valid values are "date", "time", and "both". Default value is "date".
49      */

50     private String JavaDoc _type = null;
51     private Boolean JavaDoc _popupCalendar = null;
52     
53     
54     private Boolean JavaDoc _disabled = null;
55     
56     // Values to hold the data entered by the user
57
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 JavaDoc currentLocale){
68         if( _userData == null )
69             _userData = new UserData((Date JavaDoc) getValue(), currentLocale);
70         return _userData;
71     }
72     
73     public String JavaDoc getType() {
74         if (_type != null) return _type;
75         ValueBinding vb = getValueBinding("type");
76         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : "date";
77     }
78     public void setType(String JavaDoc 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 JavaDoc)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 JavaDoc enabledOnUserRole){
93         _enabledOnUserRole = enabledOnUserRole;
94     }
95     public String JavaDoc getEnabledOnUserRole(){
96         if (_enabledOnUserRole != null) return _enabledOnUserRole;
97         ValueBinding vb = getValueBinding("enabledOnUserRole");
98         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
99     }
100
101     public void setVisibleOnUserRole(String JavaDoc visibleOnUserRole){
102         _visibleOnUserRole = visibleOnUserRole;
103     }
104     public String JavaDoc getVisibleOnUserRole(){
105         if (_visibleOnUserRole != null) return _visibleOnUserRole;
106         ValueBinding vb = getValueBinding("visibleOnUserRole");
107         return vb != null ? (String JavaDoc)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 JavaDoc v = vb != null ? (Boolean JavaDoc)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 JavaDoc saveState(FacesContext context) {
126         Object JavaDoc values[] = new Object JavaDoc[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 JavaDoc state) {
138         Object JavaDoc values[] = (Object JavaDoc[])state;
139         super.restoreState(context, values[0]);
140         _type = (String JavaDoc)values[1];
141         _popupCalendar = (Boolean JavaDoc)values[2];
142         _userData = (UserData)values[3];
143         _disabled = (Boolean JavaDoc)values[4];
144         _enabledOnUserRole = (String JavaDoc)values[5];
145         _visibleOnUserRole = (String JavaDoc)values[6];
146     }
147     
148     public static class UserData implements Serializable JavaDoc {
149         private String JavaDoc day;
150         private String JavaDoc month;
151         private String JavaDoc year;
152         private String JavaDoc hours;
153         private String JavaDoc minutes;
154         private String JavaDoc seconds;
155         
156         public UserData(Date JavaDoc date, Locale JavaDoc currentLocale){
157             if( date == null )
158                 date = new Date JavaDoc();
159             
160             Calendar JavaDoc 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 JavaDoc parse() throws ParseException JavaDoc{
171             SimpleDateFormat JavaDoc fullFormat = new SimpleDateFormat JavaDoc( "dd MM yyyy hh mm ss" );
172             return fullFormat.parse(day+" "+month+" "+year+" "+hours+" "+minutes+" "+seconds);
173         }
174         
175         private String JavaDoc formatedInt(String JavaDoc toFormat){
176             if( toFormat == null )
177                 return null;
178             
179             int i = -1;
180             try{
181                 i = Integer.parseInt( toFormat );
182             }catch(NumberFormatException JavaDoc 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 JavaDoc getDay() {
192             return formatedInt( day );
193         }
194         public void setDay(String JavaDoc day) {
195             this.day = day;
196         }
197
198         public String JavaDoc getMonth() {
199             return month;
200         }
201         public void setMonth(String JavaDoc month) {
202             this.month = month;
203         }
204         
205         public String JavaDoc getYear() {
206             return year;
207         }
208         public void setYear(String JavaDoc year) {
209             this.year = year;
210         }
211         
212         public String JavaDoc getHours() {
213             return formatedInt( hours );
214         }
215         public void setHours(String JavaDoc hours) {
216             this.hours = hours;
217         }
218         public String JavaDoc getMinutes() {
219             return formatedInt( minutes );
220         }
221         public void setMinutes(String JavaDoc minutes) {
222             this.minutes = minutes;
223         }
224
225         public String JavaDoc getSeconds() {
226             return formatedInt( seconds );
227         }
228         public void setSeconds(String JavaDoc seconds) {
229             this.seconds = seconds;
230         }
231     }
232 }
Popular Tags