我嘗試創建以下結構的頁面:如何在檢票口使用模型?
MainPage {
DateSelector {}
TabGroup {
Tab0 {...}
Tab1 {...}
Tab2 {...}
}
}
DateSelector是一個組件(延伸面板),其使得用戶能夠選擇一個時間範圍(的startDate,結束日期)。 TabGroup擴展「AjaxTabbedPanel」。 TabGroup中顯示的選項卡應該使用選定的時間範圍來顯示內容。
因此,如果用戶選擇了在選擇器在所選擇的選項卡內容應該更新一個時間跨度。
爲此,我實現DateSelector的用戶可以重寫,以更新其內容的方法。
我有我的計算策略是對框架的感覺。我能否以某種方式利用模型來增強實施?這是什麼「錯誤」?選項卡能否與DateSelector共享時間範圍,以便在時間範圍更改時更新?沒有回調?
的DateSelector被實現這樣的:
abstract public class TimespanSelectionPanel extends Panel {
public TimespanSelectionPanel(String id, Timespan timespan) {
super(id, new CompoundPropertyModel<Timespan>(timespan));
final DateTextField fromTextField = DateTextField.forDatePattern("from", "dd.MM.yyyy");
fromTextField.setOutputMarkupId(true);
fromTextField.add(new DatePicker());
final DateTextField toTextField = DateTextField.forDatePattern("to", "dd.MM.yyyy");
toTextField.setOutputMarkupId(true);
toTextField.add(new DatePicker());
fromTextField.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
}
});
toTextField.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
}
});
add(new AjaxLink<String>("today") {
@Override
public void onClick(AjaxRequestTarget target) {
updateComponent(fromTextField, toTextField, target, Timespan.today());
}
});
add(new AjaxLink<String>("yesterday") {
@Override
public void onClick(AjaxRequestTarget target) {
updateComponent(fromTextField, toTextField, target, Timespan.yesterday());
}
});
add(new AjaxLink<String>("lastMonth") {
@Override
public void onClick(AjaxRequestTarget target) {
updateComponent(fromTextField, toTextField, target, Timespan.lastMonth());
}
});
add(new AjaxLink<String>("actualMonth") {
@Override
public void onClick(AjaxRequestTarget target) {
Timespan timespan = adjustToTextField(fromTextField, toTextField, target, Timespan.actualMonth());
updateComponent(fromTextField, toTextField, target, timespan);
}
});
add(new AjaxLink<String>("fromTo") {
@Override
public void onClick(AjaxRequestTarget target) {
Timespan newTimespan = adjustToTextField(fromTextField, toTextField, target, fromTextField
.getModelObject(), toTextField.getModelObject());
update(target, newTimespan);
}
});
add(fromTextField);
add(toTextField);
}
private Timespan adjustToTextField(DateTextField fromTextField, DateTextField toTextField,
AjaxRequestTarget target, Timespan timespan) {
return adjustToTextField(fromTextField, toTextField, target, timespan.getFrom(), timespan.getTo());
}
private Timespan adjustToTextField(final DateTextField fromTextField, final DateTextField toTextField,
AjaxRequestTarget target, Date from, Date to) {
Date today = DateUtil.today();
if (to.after(today)) {
to = today;
toTextField.setModelObject(today);
target.addComponent(toTextField);
}
if (from.after(to)) {
from = to;
fromTextField.setModelObject(from);
target.addComponent(fromTextField);
}
return Timespan.fromTo(from, to);
}
private void updateComponent(final DateTextField fromTextField, final DateTextField toTextField,
AjaxRequestTarget target, Timespan newTimespan) {
fromTextField.setModelObject(newTimespan.getFrom());
toTextField.setModelObject(newTimespan.getTo());
target.addComponent(fromTextField);
target.addComponent(toTextField);
update(target, newTimespan);
}
abstract protected void update(AjaxRequestTarget target, Timespan timespan);
}