Search in sources :

Example 1 with CheckBox

use of com.haulmont.cuba.gui.components.CheckBox in project cuba by cuba-platform.

the class CheckBoxDsTest method testDatasourceRepeatableAssign.

@Test
public void testDatasourceRepeatableAssign() {
    CheckBox checkBox = uiComponents.create(CheckBox.NAME);
    checkBox.setDatasource(null, null);
    checkBox.setDatasource(null, null);
    Datasource<User> userDs1 = getTestUserDatasource();
    checkBox.setDatasource(userDs1, "active");
    checkBox.setDatasource(userDs1, "active");
    boolean exceptionWasThrown = false;
    try {
        checkBox.setDatasource(userDs1, null);
    } catch (Exception e) {
        exceptionWasThrown = true;
    }
    assertTrue(exceptionWasThrown);
    exceptionWasThrown = false;
    try {
        checkBox.setDatasource(null, "active");
    } catch (Exception e) {
        exceptionWasThrown = true;
    }
    assertTrue(exceptionWasThrown);
    userDs1.getItem().setActive(true);
    checkBox.setDatasource(userDs1, "active");
    Datasource<User> userDs2 = getTestUserDatasource();
    checkBox.setDatasource(userDs2, "active");
    checkBox.setValue(false);
    assertEquals(true, userDs1.getItem().getActive());
}
Also used : User(com.haulmont.cuba.security.entity.User) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Test(org.junit.jupiter.api.Test)

Example 2 with CheckBox

use of com.haulmont.cuba.gui.components.CheckBox in project cuba by cuba-platform.

the class CheckBoxDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    CheckBox checkBox = uiComponents.create(CheckBox.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setActive(true);
    checkBox.setDatasource(userDs, "active");
    // unbind
    checkBox.setDatasource(null, null);
    // datasource before listener
    checkBox.setDatasource(userDs, "active");
    assertEquals(true, checkBox.getValue());
    boolean[] valueWasChanged = { false };
    Consumer listener = e -> valueWasChanged[0] = true;
    checkBox.addValueChangeListener(listener);
    user.setActive(false);
    assertEquals(true, valueWasChanged[0]);
    assertEquals(false, checkBox.getValue());
    // reset state
    checkBox.removeValueChangeListener(listener);
    checkBox.setDatasource(null, null);
    valueWasChanged[0] = false;
    checkBox.setValue(true);
    // listener before datasource
    checkBox.addValueChangeListener(listener);
    checkBox.setDatasource(userDs, "active");
    assertEquals(true, valueWasChanged[0]);
    assertEquals(false, checkBox.getValue());
}
Also used : Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Datasource(com.haulmont.cuba.gui.data.Datasource) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.security.entity.User) User(com.haulmont.cuba.security.entity.User) Consumer(java.util.function.Consumer) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Test(org.junit.jupiter.api.Test)

Example 3 with CheckBox

use of com.haulmont.cuba.gui.components.CheckBox in project cuba by cuba-platform.

the class CheckBoxDsTest method testUnsubscribeDsListener.

@Test
public void testUnsubscribeDsListener() {
    CheckBox checkBox = uiComponents.create(CheckBox.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setActive(true);
    checkBox.setDatasource(userDs, "active");
    // unbind
    checkBox.setDatasource(null, null);
    Datasource.ItemPropertyChangeListener<User> propertyChangeListener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    userDs.addItemPropertyChangeListener(propertyChangeListener);
    checkBox.setValue(false);
    assertEquals(true, user.getActive());
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Datasource(com.haulmont.cuba.gui.data.Datasource) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.security.entity.User) User(com.haulmont.cuba.security.entity.User) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Test(org.junit.jupiter.api.Test)

Example 4 with CheckBox

use of com.haulmont.cuba.gui.components.CheckBox in project cuba by cuba-platform.

the class CheckBoxDsTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    CheckBox checkBox = uiComponents.create(CheckBox.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setActive(true);
    // listener before datasource
    Boolean[] valueWasChanged = { false };
    Consumer listener = e -> valueWasChanged[0] = true;
    checkBox.addValueChangeListener(listener);
    checkBox.setDatasource(userDs, "active");
    assertEquals(true, valueWasChanged[0]);
    // reset state
    checkBox.setDatasource(null, null);
    checkBox.removeValueChangeListener(listener);
    valueWasChanged[0] = false;
    // datasource before listener
    checkBox.setDatasource(userDs, "active");
    checkBox.addValueChangeListener(listener);
    user.setActive(false);
    assertEquals(true, valueWasChanged[0]);
}
Also used : Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Datasource(com.haulmont.cuba.gui.data.Datasource) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.security.entity.User) User(com.haulmont.cuba.security.entity.User) Consumer(java.util.function.Consumer) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Test(org.junit.jupiter.api.Test)

Example 5 with CheckBox

use of com.haulmont.cuba.gui.components.CheckBox in project cuba by cuba-platform.

the class WebTableFieldFactory method createField.

@SuppressWarnings("unchecked")
@Override
public com.vaadin.v7.ui.Field<?> createField(com.vaadin.v7.data.Container container, Object itemId, Object propertyId, Component uiContext) {
    String fieldPropertyId = String.valueOf(propertyId);
    Table.Column columnConf = webTable.getColumnsInternal().get(propertyId);
    TableDataContainer tableDataContainer = (TableDataContainer) container;
    Entity entity = (Entity) tableDataContainer.getInternalItem(itemId);
    InstanceContainer instanceContainer = webTable.getInstanceContainer((E) entity);
    com.haulmont.cuba.gui.components.Component columnComponent = createField(new ContainerValueSource(instanceContainer, fieldPropertyId), fieldPropertyId, columnConf.getXmlDescriptor());
    if (columnComponent instanceof Field) {
        Field cubaField = (Field) columnComponent;
        Map<Table.Column, String> requiredColumns = webTable.getRequiredColumnsInternal();
        if (requiredColumns != null && requiredColumns.containsKey(columnConf)) {
            cubaField.setRequired(true);
            cubaField.setRequiredMessage(requiredColumns.get(columnConf));
        }
    }
    if (!(columnComponent instanceof CheckBox)) {
        // todo get rid of concrete CheckBox class !
        columnComponent.setWidthFull();
    }
    if (columnComponent instanceof BelongToFrame) {
        BelongToFrame belongToFrame = (BelongToFrame) columnComponent;
        if (belongToFrame.getFrame() == null) {
            belongToFrame.setFrame(webTable.getFrame());
        }
    }
    applyPermissions(columnComponent);
    columnComponent.setParent(webTable);
    Component componentImpl = getComponentImplementation(columnComponent);
    if (componentImpl instanceof com.vaadin.v7.ui.Field) {
        return (com.vaadin.v7.ui.Field<?>) componentImpl;
    }
    return new EditableColumnFieldWrapper(componentImpl, columnComponent);
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Table(com.haulmont.cuba.gui.components.Table) WebAbstractTable(com.haulmont.cuba.web.gui.components.WebAbstractTable) BelongToFrame(com.haulmont.cuba.gui.components.Component.BelongToFrame) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) Field(com.haulmont.cuba.gui.components.Field) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Component(com.vaadin.ui.Component)

Aggregations

CheckBox (com.haulmont.cuba.gui.components.CheckBox)7 User (com.haulmont.cuba.security.entity.User)6 Test (org.junit.jupiter.api.Test)6 Datasource (com.haulmont.cuba.gui.data.Datasource)5 Consumer (java.util.function.Consumer)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 Disabled (org.junit.jupiter.api.Disabled)5 Entity (com.haulmont.cuba.core.entity.Entity)1 BelongToFrame (com.haulmont.cuba.gui.components.Component.BelongToFrame)1 Field (com.haulmont.cuba.gui.components.Field)1 Table (com.haulmont.cuba.gui.components.Table)1 ContainerValueSource (com.haulmont.cuba.gui.components.data.value.ContainerValueSource)1 InstanceContainer (com.haulmont.cuba.gui.model.InstanceContainer)1 WebAbstractTable (com.haulmont.cuba.web.gui.components.WebAbstractTable)1 Component (com.vaadin.ui.Component)1