de.fatalix.lighty.web.component.field.InlineEditField.java Source code

Java tutorial

Introduction

Here is the source code for de.fatalix.lighty.web.component.field.InlineEditField.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package de.fatalix.lighty.web.component.field;

import com.vaadin.event.FieldEvents;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;

/**
 *
 * @author felix.husse
 */
public class InlineEditField extends CustomField<String> {

    private final Label captionLabel;
    private final Label valueLabel;
    private final TextField valueTextField;
    private final boolean editable;

    public InlineEditField(String caption, String value, boolean editable) {
        captionLabel = new Label(caption);
        //captionLabel.setSizeUndefined();
        captionLabel.setWidth(100, Unit.PIXELS);

        valueTextField = new TextField();
        valueTextField.setValue(value);
        valueTextField.addBlurListener(new FieldEvents.BlurListener() {

            public void blur(FieldEvents.BlurEvent event) {
                setValue(valueTextField.getValue());
            }
        });

        valueLabel = new Label(value);
        valueLabel.setSizeUndefined();
        setInternalValue(value);
        this.editable = editable;
    }

    @Override
    protected Component initContent() {
        HorizontalLayout fieldLayout = new HorizontalLayout();
        fieldLayout.setSpacing(true);
        fieldLayout.addComponent(captionLabel);
        if (editable) {
            fieldLayout.addComponent(valueTextField);
        } else {
            fieldLayout.addComponent(valueLabel);
        }

        return fieldLayout;
    }

    public void setTextFieldColumns(final int columns) {
        valueTextField.setColumns(columns);
    }

    public void setFieldValue(final String value) {
        if (editable) {
            valueTextField.setValue(value);
        } else {
            valueLabel.setValue(value);
        }
        setInternalValue(value);
    }

    @Override
    public void setCaption(final String caption) {
        captionLabel.setValue(caption);
    }

    @Override
    public Class<? extends String> getType() {
        return String.class;
    }

}