Search in sources :

Example 1 with LookupPickerField

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

the class LookupBuilderProcessor method handleSelectionWithField.

@SuppressWarnings("unchecked")
protected <E extends Entity> void handleSelectionWithField(@SuppressWarnings("unused") LookupBuilder<E> builder, HasValue<E> field, Collection<E> itemsFromLookup) {
    if (itemsFromLookup.isEmpty()) {
        return;
    }
    Collection<E> selectedItems = transform(itemsFromLookup, builder);
    Entity newValue = selectedItems.iterator().next();
    View viewForField = clientConfig.getReloadUnfetchedAttributesFromLookupScreens() && metadataTools.isPersistent(newValue.getClass()) ? getViewForField(field) : null;
    if (viewForField != null && !entityStates.isLoadedWithView(newValue, viewForField)) {
        newValue = dataManager.reload(newValue, viewForField);
    }
    if (field instanceof LookupPickerField) {
        LookupPickerField lookupPickerField = (LookupPickerField) field;
        Options options = lookupPickerField.getOptions();
        if (options instanceof EntityOptions) {
            EntityOptions entityOptions = (EntityOptions) options;
            if (entityOptions.containsItem(newValue)) {
                entityOptions.updateItem(newValue);
            }
            if (lookupPickerField.isRefreshOptionsOnLookupClose()) {
                entityOptions.refresh();
            }
        }
    }
    // In case of PickerField set the value as if the user had set it
    if (field instanceof SupportsUserAction) {
        ((SupportsUserAction<E>) field).setValueFromUser((E) newValue);
    } else {
        field.setValue((E) newValue);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) Options(com.haulmont.cuba.gui.components.data.Options) SupportsUserAction(com.haulmont.cuba.gui.components.SupportsUserAction) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions)

Example 2 with LookupPickerField

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

the class LookupPickerFieldLoader method loadComponent.

@Override
public void loadComponent() {
    super.loadComponent();
    LookupPickerField lookupPickerField = (LookupPickerField) resultComponent;
    String metaClass = element.attributeValue("metaClass");
    if (!StringUtils.isEmpty(metaClass)) {
        lookupPickerField.setMetaClass(getMetadata().getClass(metaClass));
    }
    loadActions(lookupPickerField, element);
    if (lookupPickerField.getActions().isEmpty()) {
        GuiActionSupport guiActionSupport = getGuiActionSupport();
        boolean actionsByMetaAnnotations = guiActionSupport.createActionsByMetaAnnotations(lookupPickerField);
        if (!actionsByMetaAnnotations) {
            if (isLegacyFrame()) {
                lookupPickerField.addLookupAction();
                lookupPickerField.addClearAction();
            } else {
                Actions actions = getActions();
                lookupPickerField.addAction(actions.create(LookupAction.ID));
                lookupPickerField.addAction(actions.create(ClearAction.ID));
            }
        }
    }
    String refreshOptionsOnLookupClose = element.attributeValue("refreshOptionsOnLookupClose");
    if (refreshOptionsOnLookupClose != null) {
        lookupPickerField.setRefreshOptionsOnLookupClose(Boolean.parseBoolean(refreshOptionsOnLookupClose));
    }
}
Also used : LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) Actions(com.haulmont.cuba.gui.components.Actions) GuiActionSupport(com.haulmont.cuba.gui.components.actions.GuiActionSupport)

Example 3 with LookupPickerField

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

the class LookupPickerFieldDsTest method testUnsubscribeSubscribeOptions.

@Test
public void testUnsubscribeSubscribeOptions() {
    LookupPickerField component = uiComponents.create(LookupPickerField.NAME);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    component.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setGroup(groups.get(0));
    component.setDatasource(userDs, "group");
    // unbind
    component.setOptionsDatasource(null);
    Datasource.ItemChangeListener<Group> listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    groupsDs.addItemChangeListener(listener);
    component.setValue(groups.get(1));
    // bind
    component.setOptionsDatasource(groupsDs);
    groupsDs.removeItemChangeListener(listener);
    boolean[] valueWasChanged = { false };
    listener = e -> valueWasChanged[0] = true;
    groupsDs.addItemChangeListener(listener);
    component.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) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) UUID(java.util.UUID) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Group(com.haulmont.cuba.security.entity.Group) List(java.util.List) HasValue(com.haulmont.cuba.gui.components.HasValue) Assertions(org.junit.jupiter.api.Assertions) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 4 with LookupPickerField

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

the class LookupPickerFieldDsTest method testOptionsDsUnsubscribe.

@Test
public void testOptionsDsUnsubscribe() {
    LookupPickerField component = uiComponents.create(LookupPickerField.NAME);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    component.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setGroup(groups.get(0));
    component.setDatasource(userDs, "group");
    // unbind
    component.setOptionsDatasource(null);
    Datasource.ItemChangeListener<Group> listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    groupsDs.addItemChangeListener(listener);
    component.setValue(groups.get(1));
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Datasource(com.haulmont.cuba.gui.data.Datasource) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) UUID(java.util.UUID) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Group(com.haulmont.cuba.security.entity.Group) List(java.util.List) HasValue(com.haulmont.cuba.gui.components.HasValue) Assertions(org.junit.jupiter.api.Assertions) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 5 with LookupPickerField

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

the class LookupPickerFieldDsTest method testDatasourceRepeatableAssign.

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

Aggregations

LookupPickerField (com.haulmont.cuba.gui.components.LookupPickerField)11 Group (com.haulmont.cuba.security.entity.Group)9 User (com.haulmont.cuba.security.entity.User)9 Test (org.junit.jupiter.api.Test)9 HasValue (com.haulmont.cuba.gui.components.HasValue)7 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)7 Datasource (com.haulmont.cuba.gui.data.Datasource)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 UUID (java.util.UUID)7 Consumer (java.util.function.Consumer)7 Assertions (org.junit.jupiter.api.Assertions)7 Disabled (org.junit.jupiter.api.Disabled)7 Entity (com.haulmont.cuba.core.entity.Entity)1 Actions (com.haulmont.cuba.gui.components.Actions)1 SupportsUserAction (com.haulmont.cuba.gui.components.SupportsUserAction)1 GuiActionSupport (com.haulmont.cuba.gui.components.actions.GuiActionSupport)1 Options (com.haulmont.cuba.gui.components.data.Options)1 EntityOptions (com.haulmont.cuba.gui.components.data.meta.EntityOptions)1