Created
May 25, 2013 11:01
-
-
Save AgileSpirit/5648700 to your computer and use it in GitHub Desktop.
A Wicket DateSelector FormComponent based on 3 DropDownChoice components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns:wicket="http://wicket.apache.org"> | |
<body> | |
<wicket:panel> | |
<select class="fieldInput" wicket:id="day" ></select> | |
<select class="fieldInput" wicket:id="month" ></select> | |
<select class="fieldInput" wicket:id="year" ></select> | |
</wicket:panel> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.agile.spirit.wicket.selectors.dateselector; | |
import java.text.SimpleDateFormat; | |
import java.util.*; | |
import org.apache.wicket.ajax.AjaxRequestTarget; | |
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; | |
import org.apache.wicket.markup.html.form.*; | |
import org.apache.wicket.model.*; | |
/** | |
* Created by Jérémy Buget for Agile Spirit. | |
* Date: 20/05/13 | |
* Time: 00:32 | |
*/ | |
public class DateSelector extends FormComponentPanel<Date> { | |
private static final long serialVersionUID = 3786488200906993510L; | |
// Managed model object values | |
private Integer day; | |
private Integer month; | |
private Integer year; | |
// UI Components | |
protected DropDownChoice<Integer> daySelector; | |
protected DropDownChoice<Integer> monthSelector; | |
protected DropDownChoice<Integer> yearSelector; | |
public DateSelector(String id) { | |
this(id, new Model<Date>(null)); | |
} | |
public DateSelector(String id, IModel<Date> model) { | |
super(id, model); | |
setDefaultModel(model); | |
if (model == null) { | |
setDefaultModel(new Model<Date>(null)); | |
} | |
if (model.getObject() != null) { | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(model.getObject()); | |
day = cal.get(Calendar.DAY_OF_MONTH); | |
month = cal.get(Calendar.MONTH); | |
year = cal.get(Calendar.YEAR); | |
} | |
buildDaySelector(); | |
buildMonthSelector(); | |
buildYearSelector(); | |
} | |
private void buildDaySelector() { | |
IModel<Integer> model = new PropertyModel<Integer>(this, "day"); | |
daySelector = new DropDownChoice<Integer>("day", model, getDays()); | |
daySelector.setNullValid(true); | |
daySelector.add(newItemSelectedBehavior()); | |
daySelector.setOutputMarkupId(true); | |
daySelector.setOutputMarkupPlaceholderTag(true); | |
add(daySelector); | |
} | |
private void buildMonthSelector() { | |
IModel<Integer> model = new PropertyModel<Integer>(this, "month"); | |
MonthChoiceRenderer renderer = new MonthChoiceRenderer(); | |
monthSelector = new DropDownChoice<Integer>("month", model, getMonths(), renderer); | |
monthSelector.setNullValid(true); | |
monthSelector.add(newItemSelectedBehavior()); | |
add(monthSelector); | |
} | |
private void buildYearSelector() { | |
IModel<Integer> model = new PropertyModel<Integer>(this, "year"); | |
yearSelector = new DropDownChoice<Integer>("year", model, getYears()); | |
yearSelector.setNullValid(true); | |
yearSelector.add(newItemSelectedBehavior()); | |
add(yearSelector); | |
} | |
/** | |
* Get the days, depending on selected "year" and "month". | |
* | |
* @return the days | |
*/ | |
protected List<Integer> getDays() { | |
List<Integer> days = new ArrayList<Integer>(31); | |
int totalDays = 31; | |
if (year != null && month != null) { | |
Calendar cal = new GregorianCalendar(year, month, 1); | |
totalDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH); | |
} | |
for (int i = 1; i <= totalDays; i++) { | |
days.add(i); | |
} | |
return days; | |
} | |
/** | |
* Get the months. | |
* | |
* @return the months | |
*/ | |
private List<Integer> getMonths() { | |
List<Integer> months = new ArrayList<Integer>(12); | |
for (int i = 0; i < 12; i++) { | |
months.add(i); | |
} | |
return months; | |
} | |
/** | |
* Get the years. | |
* | |
* @return the years | |
*/ | |
protected List<Integer> getYears() { | |
List<Integer> years = new ArrayList<Integer>(); | |
Calendar cal = Calendar.getInstance(); | |
for (int i = cal.get(Calendar.YEAR); i > cal.get(Calendar.YEAR) - 5; i--) { | |
years.add(i); | |
} | |
return years; | |
} | |
/** | |
* Get an AjaxBehavior that updates the component Date model object if possible and updates the day choices. | |
* | |
* @return the behavior | |
*/ | |
private AjaxFormComponentUpdatingBehavior newItemSelectedBehavior() { | |
return new AjaxFormComponentUpdatingBehavior("onchange") { | |
private static final long serialVersionUID = -8869649812234932095L; | |
@Override | |
protected void onUpdate(AjaxRequestTarget target) { | |
if (day != null && month != null && year != null) { | |
Calendar cal = Calendar.getInstance(); | |
cal.set(year, month, day, 0, 0, 0); | |
Date selectedDate = cal.getTime(); | |
DateSelector.this.setModelObject(selectedDate); | |
} | |
daySelector.setChoices(getDays()); | |
target.add(daySelector); | |
} | |
}; | |
} | |
private class MonthChoiceRenderer implements IChoiceRenderer<Integer> { | |
private static final long serialVersionUID = 1717541897012722956L; | |
@Override | |
public String getDisplayValue(Integer object) { | |
if (object == null) { | |
return null; | |
} | |
SimpleDateFormat format = new SimpleDateFormat("MMMM"); | |
Calendar cal = Calendar.getInstance(); | |
cal.set(Calendar.MONTH, object.intValue()); | |
return format.format(cal.getTime()); | |
} | |
@Override | |
public String getIdValue(Integer integer, int index) { | |
return String.valueOf(index); | |
} | |
} | |
@Override | |
protected void convertInput() { | |
if (day == null || year == null || month == null) { | |
setConvertedInput(null); | |
} else { | |
Date date = getModelObject(); | |
setConvertedInput(date); | |
} | |
} | |
@Override | |
protected void onConfigure() { | |
super.onConfigure(); | |
daySelector.setRequired(this.isRequired()); | |
monthSelector.setRequired(this.isRequired()); | |
yearSelector.setRequired(this.isRequired()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
day.nullValid=jour | |
month.nullValid=mois | |
year.nullValid=annee |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns:wicket="http://wicket.apache.org"> | |
<head> | |
<title>Wicket Spirit - DateSelector</title> | |
</head> | |
<body> | |
<div wicket:id="feedback" ></div> | |
<form wicket:id="form"> | |
<div> | |
<div wicket:id="dateSelector"></div> | |
</div> | |
<button type="submit">Test</button> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.agile.spirit.wicket.selectors.dateselector; | |
import org.apache.wicket.markup.html.WebPage; | |
import org.apache.wicket.markup.html.form.Form; | |
import org.apache.wicket.markup.html.panel.FeedbackPanel; | |
import org.apache.wicket.model.Model; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
/** | |
* Created by Jérémy Buget for Agile Spirit. | |
* Date: 20/05/13 | |
* Time: 01:17 | |
*/ | |
public class DateSelectorPage extends WebPage { | |
private final DateSelector dateSelector; | |
public DateSelectorPage() { | |
super(); | |
FeedbackPanel feedback = new FeedbackPanel("feedback"); | |
feedback.setOutputMarkupId(true); | |
feedback.setOutputMarkupPlaceholderTag(true); | |
add(feedback); | |
Form<Void> form = new Form<Void>("form"){ | |
@Override | |
protected void onSubmit() { | |
super.onSubmit(); | |
System.out.println(getFormattedDate(dateSelector.getModelObject())); | |
} | |
}; | |
add(form); | |
dateSelector = new DateSelector("dateSelector", new Model<Date>(null)); | |
dateSelector.setRequired(true); | |
form.add(dateSelector); | |
} | |
private String getFormattedDate(Date date) { | |
if (date == null) { | |
return null; | |
} | |
return (new SimpleDateFormat("dd-MM-yyyy")).format(date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment