I have a AjaxEditableLabel
AjaxEditableLabel<String> task = new AjaxEditableLabel<String>("task", new PropertyModel<String>(getModel(), "task"));
Now what I am trying to do is on some condition this AjaxEditableLabel will render as TextField. By default it is rendered as Label. Say if the model String task is null or blank the AjaxEditableLabel will be TextField else it will be Label.
Is it possible?
Thanks.
Edit:
AjaxEditableLabel<String> task = new AjaxEditableLabel<String>("task", new PropertyModel<String>(getModel(), "task")) {
private static final long serialVersionUID = 40L;
private TextField<String> editor;
protected FormComponent<String> newEditor(MarkupContainer parent, String componentId, IModel<String> model) {
editor = (TextField<String>) super.newEditor(parent, componentId, model);
Object modelObject = getDefaultModelObject();
if (modelObject == null || "".equals(modelObject)) {
editor.setVisible(true);
} else {
editor.setVisible(false);
}
return editor;
}
protected WebComponent newLabel(MarkupContainer parent, String componentId, IModel<String> model) {
Label label = (Label) super.newLabel(parent, componentId, model);
if (editor.isVisible()) {
label.setVisible(false);
}
return label;
}
};
If you look in
AjaxEditableLabelit has two methods used for initializing the Editor/Label:newEditorandnewLabel. In thenewEditormethod by default it sets the editor to invisible. Overriding these two methods and adding custom logic to show the components based on the model value should do what you want.