private static final Logger LOG = LoggerFactory.getLogger(DatePickerRenderer.class);
@Override
public void onComponentCreated(FacesContext facesContext, UIComponent component, UIComponent parent) {
final UIDatePicker picker = (UIDatePicker) component;
if (picker.getFor() == null) {
picker.setFor("@auto");
}
picker.setImmediate(true);
final String linkId = picker.getId();
// create popup
final String popupId = linkId != null ? linkId + "popup" : facesContext.getViewRoot().createUniqueId();
final UIPopup popup = (UIPopup) CreateComponentUtils.createComponent(
facesContext, UIPopup.COMPONENT_TYPE, RendererTypes.POPUP, popupId);
final UIGridLayout layoutOfPopup = (UIGridLayout) CreateComponentUtils.createComponent(
facesContext, UIGridLayout.COMPONENT_TYPE, RendererTypes.GRID_LAYOUT, "layoutPopup");
layoutOfPopup.setColumns("auto");
layoutOfPopup.setRows("auto");
popup.getFacets().put(Facets.LAYOUT, layoutOfPopup);
popup.getAttributes().put(Attributes.Z_INDEX, 10);
picker.getFacets().put(Facets.PICKER_POPUP, popup);
popup.setRendered(false);
popup.onComponentPopulated(facesContext, parent);
FacesUtils.setBindingOrExpression(
popup, Attributes.LEFT, FacesUtils.createExpressionOrBinding("#{tobago.actionPosition.right.pixel + 5}"));
FacesUtils.setBindingOrExpression(
popup, Attributes.TOP, FacesUtils.createExpressionOrBinding("#{tobago.actionPosition.top.pixel}"));
final UIBox box = (UIBox) CreateComponentUtils.createComponent(
facesContext, UIBox.COMPONENT_TYPE, RendererTypes.BOX, "box");
popup.getChildren().add(box);
// TODO: set string resources in renderer
box.setLabel(ResourceManagerUtils.getPropertyNotNull(facesContext, "tobago", "datePickerTitle"));
final UIGridLayout layoutOfBox = (UIGridLayout) CreateComponentUtils.createComponent(
facesContext, UIGridLayout.COMPONENT_TYPE, RendererTypes.GRID_LAYOUT, "layout");
box.getFacets().put(Facets.LAYOUT, layoutOfBox);
layoutOfBox.setRows("*;auto;auto");
final UICalendar calendar = (UICalendar) CreateComponentUtils.createComponent(
facesContext, UICalendar.COMPONENT_TYPE, RendererTypes.CALENDAR, "calendar");
box.getChildren().add(calendar);
// fixme: should work automatically from the layout manager
final Measure width = getResourceManager().getThemeMeasure(facesContext, calendar, "minimumWidth");
layoutOfBox.setColumns(width.toString());
// add time input
final UIPanel timePanel = (UIPanel) CreateComponentUtils.createComponent(
facesContext, UIPanel.COMPONENT_TYPE, RendererTypes.PANEL, "timePanel");
box.getChildren().add(timePanel);
final UIGridLayout layoutOfTime = (UIGridLayout) CreateComponentUtils.createComponent(
facesContext, UIGridLayout.COMPONENT_TYPE, RendererTypes.GRID_LAYOUT, "timePanelLayout");
timePanel.getFacets().put(Facets.LAYOUT, layoutOfTime);
layoutOfTime.setColumns("1*;auto;1*");
final UIPanel cell1 = (UIPanel) CreateComponentUtils.createComponent(
facesContext, UIPanel.COMPONENT_TYPE, RendererTypes.PANEL, "cell1");
cell1.onComponentPopulated(facesContext, parent);
timePanel.getChildren().add(cell1);
final UITime time = (UITime) CreateComponentUtils.createComponent(
facesContext, UITime.COMPONENT_TYPE, RendererTypes.TIME, "time");
timePanel.getChildren().add(time);
final UIPanel cell2 = (UIPanel) CreateComponentUtils.createComponent(
facesContext, UIPanel.COMPONENT_TYPE, RendererTypes.PANEL, "cell2");
cell2.onComponentPopulated(facesContext, parent);
timePanel.getChildren().add(cell2);
timePanel.onComponentPopulated(facesContext, parent);
final UIPanel buttonPanel = (UIPanel) CreateComponentUtils.createComponent(
facesContext, UIPanel.COMPONENT_TYPE, RendererTypes.PANEL, "buttonPanel");
final UIGridLayout layoutOfButtons = (UIGridLayout) CreateComponentUtils.createComponent(
facesContext, UIGridLayout.COMPONENT_TYPE, RendererTypes.GRID_LAYOUT, "buttonPanelLayout");
buttonPanel.setLayoutManager(layoutOfButtons);
layoutOfButtons.setColumns("*;*");
layoutOfButtons.setRows("auto");
box.getChildren().add(buttonPanel);
box.onComponentPopulated(facesContext, parent);
final UIButton okButton = (UIButton) CreateComponentUtils.createComponent(
facesContext, UIButton.COMPONENT_TYPE, RendererTypes.BUTTON, "ok");
buttonPanel.getChildren().add(okButton);
okButton.setLabel(ResourceManagerUtils.getPropertyNotNull(facesContext, "tobago", "datePickerOk"));
okButton.setOnclick("writeIntoField2(this);");
okButton.getAttributes().put(Attributes.POPUP_CLOSE, "afterSubmit");
final UIButton cancelButton = (UIButton) CreateComponentUtils.createComponent(
facesContext, UIButton.COMPONENT_TYPE, RendererTypes.BUTTON, "cancel");
buttonPanel.getChildren().add(cancelButton);
cancelButton.setLabel(ResourceManagerUtils.getPropertyNotNull(facesContext, "tobago", "datePickerCancel"));
cancelButton.setOnclick("writeIntoField2(this);");
cancelButton.getAttributes().put(Attributes.POPUP_CLOSE, "immediate");
buttonPanel.onComponentPopulated(facesContext, parent);
// create image
// check the id: its might be better not calling createUniqueId
final String imageId = linkId != null ? linkId + "image" : facesContext.getViewRoot().createUniqueId();
final UIImage image = (UIImage) CreateComponentUtils.createComponent(
facesContext, UIImage.COMPONENT_TYPE, RendererTypes.IMAGE, imageId);
image.setRendered(true);
image.setValue("image/date.gif");
image.setAlt(""); //TODO: i18n (write a text)
StyleClasses.ensureStyleClasses(image).addFullQualifiedClass("tobago-datePicker-icon");
picker.getChildren().add(image);
}