Search in sources :

Example 1 with SearchField

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

the class SearchFieldLoader method loadComponent.

@Override
public void loadComponent() {
    super.loadComponent();
    SearchField searchField = (SearchField) resultComponent;
    String minSearchStringLength = element.attributeValue("minSearchStringLength");
    if (StringUtils.isNotEmpty(minSearchStringLength)) {
        searchField.setMinSearchStringLength(Integer.parseInt(minSearchStringLength));
    }
    String modeString = element.attributeValue("mode");
    if (StringUtils.isNotEmpty(modeString)) {
        SearchField.Mode mode;
        try {
            mode = SearchField.Mode.valueOf(StringUtils.upperCase(modeString));
        } catch (IllegalArgumentException e) {
            throw new GuiDevelopmentException("Unable to parse mode for search", context.getFullFrameId(), "mode", modeString);
        }
        searchField.setMode(mode);
    }
    String escapeValueForLike = element.attributeValue("escapeValueForLike");
    if (StringUtils.isNotEmpty(escapeValueForLike)) {
        searchField.setEscapeValueForLike(Boolean.parseBoolean(escapeValueForLike));
    }
}
Also used : SearchField(com.haulmont.cuba.gui.components.SearchField) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 2 with SearchField

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

the class SearchFieldDsTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    SearchField searchField = factory.createComponent(SearchField.class);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    searchField.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setGroup(groups.get(0));
    boolean[] valueWasChanged = { false };
    Component.ValueChangeListener listener = e -> valueWasChanged[0] = true;
    searchField.addValueChangeListener(listener);
    searchField.setDatasource(userDs, "group");
    assertEquals(true, valueWasChanged[0]);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Assert.assertTrue(org.junit.Assert.assertTrue) SearchField(com.haulmont.cuba.gui.components.SearchField) 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) 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) SearchField(com.haulmont.cuba.gui.components.SearchField) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Component(com.haulmont.cuba.gui.components.Component) Test(org.junit.Test)

Example 3 with SearchField

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

the class SearchFieldDsTest method testUnsubscribeDsListener.

@Test
public void testUnsubscribeDsListener() {
    SearchField searchField = factory.createComponent(SearchField.class);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    searchField.setOptionsDatasource(groupsDs);
    Datasource<User> userDs = getTestUserDatasource();
    Group group = groupsDs.getItems().iterator().next();
    userDs.getItem().setGroup(group);
    searchField.setDatasource(userDs, "group");
    // unbind
    searchField.setDatasource(null, null);
    Datasource.ItemPropertyChangeListener<User> listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    userDs.addItemPropertyChangeListener(listener);
    searchField.setValue(metadata.create(Group.class));
}
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) SearchField(com.haulmont.cuba.gui.components.SearchField) 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) 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) SearchField(com.haulmont.cuba.gui.components.SearchField) User(com.haulmont.cuba.security.entity.User) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with SearchField

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

the class SearchFieldDsTest method testDatasourceRepeatableAssign.

@Test
public void testDatasourceRepeatableAssign() {
    SearchField searchField = (SearchField) factory.createComponent(SearchField.NAME);
    searchField.setDatasource(null, null);
    searchField.setDatasource(null, null);
    Datasource<User> userDs1 = getTestUserDatasource();
    boolean exceptionWasThrown = false;
    try {
        searchField.setDatasource(userDs1, null);
    } catch (Exception e) {
        exceptionWasThrown = true;
    }
    assertTrue(exceptionWasThrown);
    exceptionWasThrown = false;
    try {
        searchField.setDatasource(null, "name");
    } catch (Exception e) {
        exceptionWasThrown = true;
    }
    assertTrue(exceptionWasThrown);
    searchField.setDatasource(userDs1, "name");
    searchField.setDatasource(userDs1, "name");
    userDs1.getItem().setName("Test name");
    searchField.setDatasource(userDs1, "name");
    Datasource<User> userDs2 = getTestUserDatasource();
    searchField.setDatasource(userDs2, "name");
    searchField.setValue(null);
    assertEquals("Test name", userDs1.getItem().getName());
}
Also used : SearchField(com.haulmont.cuba.gui.components.SearchField) User(com.haulmont.cuba.security.entity.User) Test(org.junit.Test)

Example 5 with SearchField

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

the class SearchFieldDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    SearchField searchField = factory.createComponent(SearchField.class);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    searchField.setOptionsDatasource(groupsDs);
    Datasource<User> userDs = getTestUserDatasource();
    Group group = groupsDs.getItems().iterator().next();
    User user = userDs.getItem();
    user.setGroup(group);
    searchField.setDatasource(userDs, "group");
    // unbind
    searchField.setDatasource(null, null);
    // setup
    boolean[] valueWasChanged = { false };
    Component.ValueChangeListener listener = e -> valueWasChanged[0] = true;
    // datasource before listener
    searchField.setDatasource(userDs, "group");
    searchField.addValueChangeListener(listener);
    user.setGroup(metadata.create(Group.class));
    assertEquals(true, valueWasChanged[0]);
    // reset state
    valueWasChanged[0] = false;
    searchField.setDatasource(null, null);
    searchField.removeValueChangeListener(listener);
    searchField.setValue(null);
    // listener before datasource
    searchField.addValueChangeListener(listener);
    searchField.setDatasource(userDs, "group");
    assertEquals(true, valueWasChanged[0]);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Assert.assertTrue(org.junit.Assert.assertTrue) SearchField(com.haulmont.cuba.gui.components.SearchField) 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) 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) SearchField(com.haulmont.cuba.gui.components.SearchField) User(com.haulmont.cuba.security.entity.User) UUID(java.util.UUID) Component(com.haulmont.cuba.gui.components.Component) Test(org.junit.Test)

Aggregations

SearchField (com.haulmont.cuba.gui.components.SearchField)7 User (com.haulmont.cuba.security.entity.User)6 Test (org.junit.Test)6 Component (com.haulmont.cuba.gui.components.Component)5 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)5 Datasource (com.haulmont.cuba.gui.data.Datasource)5 Group (com.haulmont.cuba.security.entity.Group)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 UUID (java.util.UUID)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 Ignore (org.junit.Ignore)5 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)1