All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsimpledb.vaadin.NullableField Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.vaadin;

import com.vaadin.data.Property;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Field;

import org.dellroad.stuff.vaadin7.FieldLayout;

/**
 * Wraps a {@link Field} that is capable of displaying, but not necessarily choosing, a null value,
 * and adds a "Null" button that sets the value to null.
 */
@SuppressWarnings("serial")
public class NullableField extends FieldLayout {

    private final SmallButton nullButton = new SmallButton("Null", new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            NullableField.this.field.setValue(null);
        }
    });

    public NullableField(Field field) {
        super(field);
        this.setMargin(false);
        this.setSpacing(true);
        this.addComponent(this.field);
        this.addComponent(this.nullButton);
        this.setComponentAlignment(this.nullButton, Alignment.MIDDLE_LEFT);
        this.field.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                NullableField.this.updateDisplay();
            }
        });
        if (this.field instanceof Property.ReadOnlyStatusChangeNotifier) {
            final Property.ReadOnlyStatusChangeNotifier notifier = (Property.ReadOnlyStatusChangeNotifier)field;
            notifier.addReadOnlyStatusChangeListener(new Property.ReadOnlyStatusChangeListener() {
                @Override
                public void readOnlyStatusChange(Property.ReadOnlyStatusChangeEvent event) {
                    NullableField.this.updateDisplay();
                }
            });
        }
    }

    @Override
    public void attach() {
        super.attach();
        this.updateDisplay();
    }

// Internal methods

    private void updateDisplay() {
        this.nullButton.setEnabled(this.field.getValue() != null && !this.field.isReadOnly());
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy