Tapas Bose
Tapas Bose

Reputation: 29816

How to make AjaxEditableLabel to show TextField?

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;
    }
};

Upvotes: 2

Views: 1071

Answers (1)

mark-cs
mark-cs

Reputation: 4707

If you look in AjaxEditableLabel it has two methods used for initializing the Editor/Label: newEditor and newLabel. In the newEditor method 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.

Upvotes: 4

Related Questions