View Javadoc

1   /*
2    * Project Bimbo.
3    * Copyright 2008 Frank Cornelis.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package net.sf.bimbo.impl;
19  
20  import java.io.PrintWriter;
21  import java.util.Calendar;
22  import java.util.Date;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import net.sf.bimbo.Input;
27  import net.sf.bimbo.spi.Renderer;
28  
29  /**
30   * Date renderer implementation.
31   * 
32   * @author fcorneli
33   * 
34   */
35  public class DateRenderer implements Renderer<Date> {
36  
37  	public void renderInput(String fieldName, Date initialValue,
38  			Input inputAnnotation, PrintWriter writer) {
39  		writer.println("<select name=\"" + fieldName + ".day\">");
40  		for (int day = 1; day <= 31; day++) {
41  			writer.println("<option>" + day + "</option>");
42  		}
43  		writer.println("</select>");
44  		writer.println("-");
45  		writer.println("<select name=\"" + fieldName + ".month\">");
46  		for (int month = 1; month <= 12; month++) {
47  			writer.println("<option>" + month + "</option>");
48  		}
49  		writer.println("</select>");
50  		writer.println("-");
51  		writer.println("<select name=\"" + fieldName + ".year\">");
52  		for (int year = 1950; year <= 2010; year++) {
53  			writer.println("<option>" + year + "</option>");
54  		}
55  		writer.println("</select>");
56  	}
57  
58  	public void renderOutput(String fieldName, Date outputValue,
59  			PrintWriter writer) {
60  		// TODO Auto-generated method stub
61  
62  	}
63  
64  	public Date restore(String fieldName, HttpServletRequest request) {
65  		String day = request.getParameter(fieldName + ".day");
66  		String month = request.getParameter(fieldName + ".month");
67  		String year = request.getParameter(fieldName + ".year");
68  		Calendar calendar = Calendar.getInstance();
69  		calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
70  				Integer.parseInt(day), 0, 0, 0);
71  		Date value = calendar.getTime();
72  		return value;
73  	}
74  }