Search in sources :

Example 1 with PickerField

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

the class PickerFieldDsTest method testUnsubscribeWithComponentListener.

@Test
public void testUnsubscribeWithComponentListener() {
    PickerField pickerField = (PickerField) factory.createComponent(PickerField.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    Group group = metadata.create(Group.class);
    group.setName("Test group");
    userDs.getItem().setGroup(group);
    pickerField.setDatasource(userDs, "group");
    // unbind
    pickerField.setDatasource(null, null);
    Component.ValueChangeListener listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    pickerField.addValueChangeListener(listener);
    userDs.getItem().setGroup(metadata.create(Group.class));
}
Also used : Group(com.haulmont.cuba.security.entity.Group) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) PickerField(com.haulmont.cuba.gui.components.PickerField) Test(org.junit.Test) Component(com.haulmont.cuba.gui.components.Component) User(com.haulmont.cuba.security.entity.User) Assert.assertEquals(org.junit.Assert.assertEquals) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) PickerField(com.haulmont.cuba.gui.components.PickerField) Component(com.haulmont.cuba.gui.components.Component) Test(org.junit.Test)

Example 2 with PickerField

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

the class PickerFieldDsTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    PickerField pickerField = (PickerField) factory.createComponent(PickerField.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    Group group = metadata.create(Group.class);
    group.setName("Test group");
    user.setGroup(group);
    // datasource before listener
    boolean[] valueWasChanged = { false };
    Component.ValueChangeListener listener = e -> valueWasChanged[0] = true;
    pickerField.addValueChangeListener(listener);
    pickerField.setDatasource(userDs, "group");
    assertEquals(true, valueWasChanged[0]);
    // reset state
    pickerField.setDatasource(null, null);
    pickerField.removeValueChangeListener(listener);
    valueWasChanged[0] = false;
    pickerField.setValue(null);
    // listener before datasource
    pickerField.addValueChangeListener(listener);
    pickerField.setDatasource(userDs, "group");
    assertEquals(true, valueWasChanged[0]);
}
Also used : Group(com.haulmont.cuba.security.entity.Group) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) PickerField(com.haulmont.cuba.gui.components.PickerField) Test(org.junit.Test) Component(com.haulmont.cuba.gui.components.Component) User(com.haulmont.cuba.security.entity.User) Assert.assertEquals(org.junit.Assert.assertEquals) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) PickerField(com.haulmont.cuba.gui.components.PickerField) Component(com.haulmont.cuba.gui.components.Component) Test(org.junit.Test)

Example 3 with PickerField

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

the class DynamicAttributesGuiTools method initEntityPickerField.

/**
 * Initializes the pickerField for selecting the dynamic attribute value. If the CategoryAttribute has "where" or
 * "join" clauses then the data in lookup screens will be filtered with these clauses
 *
 * @param pickerField       PickerField component whose lookup action must be initialized
 * @param categoryAttribute CategoryAttribute that is represented by the pickerField
 */
public void initEntityPickerField(PickerField pickerField, CategoryAttribute categoryAttribute) {
    Class javaClass = categoryAttribute.getJavaClassForEntity();
    if (javaClass == null) {
        throw new IllegalArgumentException("Entity type is not specified in category attribute");
    }
    MetaClass metaClass = metadata.getClassNN(javaClass);
    PickerField.LookupAction lookupAction = (PickerField.LookupAction) pickerField.getAction(PickerField.LookupAction.NAME);
    if (!Strings.isNullOrEmpty(categoryAttribute.getJoinClause()) || !Strings.isNullOrEmpty(categoryAttribute.getWhereClause())) {
        lookupAction = createLookupAction(pickerField, categoryAttribute.getJoinClause(), categoryAttribute.getWhereClause());
        pickerField.addAction(lookupAction);
    }
    if (lookupAction == null) {
        lookupAction = pickerField.addLookupAction();
    }
    String screen = categoryAttribute.getScreen();
    if (StringUtils.isNotBlank(screen)) {
        lookupAction.setLookupScreen(screen);
    } else {
        screen = windowConfig.getBrowseScreenId(metaClass);
        if (windowConfig.findWindowInfo(screen) != null) {
            lookupAction.setLookupScreen(screen);
            lookupAction.setLookupScreenOpenType(OpenType.THIS_TAB);
        } else {
            lookupAction.setLookupScreen(CommonLookupController.SCREEN_ID);
            lookupAction.setLookupScreenParams(ParamsMap.of(CommonLookupController.CLASS_PARAMETER, metaClass));
            lookupAction.setLookupScreenOpenType(OpenType.DIALOG);
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) PickerField(com.haulmont.cuba.gui.components.PickerField) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 4 with PickerField

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

the class PickerFieldDsTest method testUnsubscribeSubscribeDsListener.

@Test
public void testUnsubscribeSubscribeDsListener() {
    PickerField pickerField = (PickerField) factory.createComponent(PickerField.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    Group group = metadata.create(Group.class);
    group.setName("Test group");
    userDs.getItem().setGroup(group);
    pickerField.setDatasource(userDs, "group");
    // unbind
    pickerField.setDatasource(null, null);
    // setup
    boolean[] valueWasChanged = { false };
    Datasource.ItemPropertyChangeListener<User> listener = e -> valueWasChanged[0] = true;
    userDs.addItemPropertyChangeListener(listener);
    pickerField.setDatasource(userDs, "group");
    pickerField.setValue(null);
    assertEquals(true, valueWasChanged[0]);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Group(com.haulmont.cuba.security.entity.Group) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) PickerField(com.haulmont.cuba.gui.components.PickerField) Test(org.junit.Test) Component(com.haulmont.cuba.gui.components.Component) User(com.haulmont.cuba.security.entity.User) Assert.assertEquals(org.junit.Assert.assertEquals) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) PickerField(com.haulmont.cuba.gui.components.PickerField) Test(org.junit.Test)

Example 5 with PickerField

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

the class PickerFieldDsTest method testDatasourceRepeatableAssign.

@Test
public void testDatasourceRepeatableAssign() {
    PickerField pickerField = (PickerField) factory.createComponent(PickerField.NAME);
    pickerField.setDatasource(null, null);
    pickerField.setDatasource(null, null);
    Datasource<User> userDs1 = getTestUserDatasource();
    boolean exceptionWasThrown = false;
    try {
        pickerField.setDatasource(userDs1, null);
    } catch (Exception e) {
        exceptionWasThrown = true;
    }
    assertTrue(exceptionWasThrown);
    exceptionWasThrown = false;
    try {
        pickerField.setDatasource(null, "group");
    } catch (Exception e) {
        exceptionWasThrown = true;
    }
    assertTrue(exceptionWasThrown);
    pickerField.setDatasource(userDs1, "group");
    pickerField.setDatasource(userDs1, "group");
    userDs1.getItem().setGroup(metadata.create(Group.class));
    pickerField.setDatasource(userDs1, "group");
    Datasource<User> userDs2 = getTestUserDatasource();
    pickerField.setDatasource(userDs2, "group");
    pickerField.setValue(null);
    assertNotNull(userDs1.getItem().getGroup());
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) PickerField(com.haulmont.cuba.gui.components.PickerField) Test(org.junit.Test)

Aggregations

PickerField (com.haulmont.cuba.gui.components.PickerField)7 Group (com.haulmont.cuba.security.entity.Group)6 User (com.haulmont.cuba.security.entity.User)6 Test (org.junit.Test)6 Component (com.haulmont.cuba.gui.components.Component)5 Datasource (com.haulmont.cuba.gui.data.Datasource)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertNotNull (org.junit.Assert.assertNotNull)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 Ignore (org.junit.Ignore)5 MetaClass (com.haulmont.chile.core.model.MetaClass)1