Search in sources :

Example 11 with LookupField

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

the class LookupFieldDsTest method testUnsubscribeSubscribeOptions.

@Test
public void testUnsubscribeSubscribeOptions() {
    LookupField lookupField = factory.createComponent(LookupField.class);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    lookupField.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());
    Datasource<User> userDs = getTestUserDatasource();
    userDs.getItem().setGroup(groups.get(0));
    lookupField.setDatasource(userDs, "group");
    // unbind
    lookupField.setOptionsDatasource(null);
    Datasource.ItemChangeListener<Group> listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    groupsDs.addItemChangeListener(listener);
    lookupField.setValue(groups.get(1));
    // setup
    groupsDs.removeItemChangeListener(listener);
    boolean[] valueWasChanged = { false };
    listener = e -> valueWasChanged[0] = true;
    groupsDs.addItemChangeListener(listener);
    lookupField.setOptionsDatasource(groupsDs);
    lookupField.setValue(groups.get(2));
    assertEquals(true, valueWasChanged[0]);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Datasource(com.haulmont.cuba.gui.data.Datasource) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) UUID(java.util.UUID) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) Group(com.haulmont.cuba.security.entity.Group) List(java.util.List) Ignore(org.junit.Ignore) LookupField(com.haulmont.cuba.gui.components.LookupField) Component(com.haulmont.cuba.gui.components.Component) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Assert.assertEquals(org.junit.Assert.assertEquals) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) LookupField(com.haulmont.cuba.gui.components.LookupField) UUID(java.util.UUID) Test(org.junit.Test)

Example 12 with LookupField

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

the class LookupFieldDsTest method testUnsubscribeComponentListener.

@Test
public void testUnsubscribeComponentListener() {
    LookupField lookupField = factory.createComponent(LookupField.class);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    lookupField.setOptionsDatasource(groupsDs);
    Datasource<User> userDs = getTestUserDatasource();
    Group group = groupsDs.getItems().iterator().next();
    User user = userDs.getItem();
    user.setGroup(group);
    lookupField.setDatasource(userDs, "group");
    // unbind
    lookupField.setDatasource(null, null);
    Component.ValueChangeListener listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    lookupField.addValueChangeListener(listener);
    user.setGroup(metadata.create(Group.class));
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) UUID(java.util.UUID) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) Group(com.haulmont.cuba.security.entity.Group) List(java.util.List) Ignore(org.junit.Ignore) LookupField(com.haulmont.cuba.gui.components.LookupField) Component(com.haulmont.cuba.gui.components.Component) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Assert.assertEquals(org.junit.Assert.assertEquals) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) LookupField(com.haulmont.cuba.gui.components.LookupField) UUID(java.util.UUID) Component(com.haulmont.cuba.gui.components.Component) Test(org.junit.Test)

Example 13 with LookupField

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

the class LookupFieldDsTest method testUnsubscribeSubscribeDsListener.

@Test
public void testUnsubscribeSubscribeDsListener() {
    LookupField lookupField = factory.createComponent(LookupField.class);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    lookupField.setOptionsDatasource(groupsDs);
    Datasource<User> userDs = getTestUserDatasource();
    Group group = groupsDs.getItems().iterator().next();
    userDs.getItem().setGroup(group);
    lookupField.setDatasource(userDs, "group");
    // unbind
    lookupField.setDatasource(null, null);
    // setup
    boolean[] valueWasChanged = { false };
    Datasource.ItemPropertyChangeListener<User> listener = e -> valueWasChanged[0] = true;
    userDs.addItemPropertyChangeListener(listener);
    lookupField.setDatasource(userDs, "group");
    lookupField.setValue(null);
    assertEquals(true, valueWasChanged[0]);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Datasource(com.haulmont.cuba.gui.data.Datasource) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) UUID(java.util.UUID) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) Group(com.haulmont.cuba.security.entity.Group) List(java.util.List) Ignore(org.junit.Ignore) LookupField(com.haulmont.cuba.gui.components.LookupField) Component(com.haulmont.cuba.gui.components.Component) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Assert.assertEquals(org.junit.Assert.assertEquals) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) LookupField(com.haulmont.cuba.gui.components.LookupField) UUID(java.util.UUID) Test(org.junit.Test)

Example 14 with LookupField

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

the class LookupFieldLoader method loadDatasource.

@Override
protected void loadDatasource(DatasourceComponent component, Element element) {
    super.loadDatasource(component, element);
    String datasource = element.attributeValue("optionsDatasource");
    if (!StringUtils.isEmpty(datasource)) {
        Datasource ds = context.getDsContext().get(datasource);
        ((LookupField) component).setOptionsDatasource((CollectionDatasource) ds);
    }
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) LookupField(com.haulmont.cuba.gui.components.LookupField)

Aggregations

LookupField (com.haulmont.cuba.gui.components.LookupField)14 User (com.haulmont.cuba.security.entity.User)10 Component (com.haulmont.cuba.gui.components.Component)9 Datasource (com.haulmont.cuba.gui.data.Datasource)9 Test (org.junit.Test)9 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)8 Group (com.haulmont.cuba.security.entity.Group)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 UUID (java.util.UUID)7 Assert.assertEquals (org.junit.Assert.assertEquals)7 Assert.assertTrue (org.junit.Assert.assertTrue)7 Ignore (org.junit.Ignore)7 Map (java.util.Map)3 TreeMap (java.util.TreeMap)2 ParamsMap (com.haulmont.bali.util.ParamsMap)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 GlobalConfig (com.haulmont.cuba.core.global.GlobalConfig)1 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)1 AbstractWindow (com.haulmont.cuba.gui.components.AbstractWindow)1