Search in sources :

Example 1 with LookupField

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

the class SessionAttributeEditor method init.

@Override
public void init(Map<String, Object> params) {
    datasource = getDsContext().get("attribute");
    FieldGroup fields = (FieldGroup) getComponent("fields");
    FieldGroup.FieldConfig field = fields.getField("datatype");
    fields.addCustomField(field, new FieldGroup.CustomFieldGenerator() {

        @Override
        public Component generateField(Datasource datasource, String propertyId) {
            LookupField lookup = AppConfig.getFactory().createComponent(LookupField.class);
            lookup.setDatasource(datasource, propertyId);
            lookup.setRequiredMessage(getMessage("datatypeMsg"));
            lookup.setRequired(true);
            lookup.setPageLength(15);
            Map<String, Object> options = new TreeMap<>();
            String mainMessagePack = AppConfig.getMessagesPack();
            for (String datatypeId : Datatypes.getIds()) {
                options.put(messages.getMessage(mainMessagePack, "Datatype." + datatypeId), datatypeId);
            }
            lookup.setOptionsMap(options);
            return lookup;
        }
    });
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) FieldGroup(com.haulmont.cuba.gui.components.FieldGroup) LookupField(com.haulmont.cuba.gui.components.LookupField) Component(com.haulmont.cuba.gui.components.Component) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 2 with LookupField

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

the class Users method init.

@Override
public void init(Map<String, Object> params) {
    Map<String, Locale> locales = configuration.getConfig(GlobalConfig.class).getAvailableLocales();
    Map<String, Object> options = new TreeMap<>();
    for (Map.Entry<String, Locale> entry : locales.entrySet()) {
        options.put(entry.getKey(), messages.getTools().localeToString(entry.getValue()));
    }
    DataGrid.Column column = usersGrid.addGeneratedColumn("language", new DataGrid.ColumnGenerator<User, Component>() {

        @Override
        public Component getValue(DataGrid.ColumnGeneratorEvent<User> event) {
            LookupField component = componentsFactory.createComponent(LookupField.class);
            component.setOptionsMap(options);
            component.setWidth("100%");
            User user = event.getItem();
            component.setValue(user.getLanguage());
            component.addValueChangeListener(e -> user.setLanguage((String) e.getValue()));
            return component;
        }

        @Override
        public Class<Component> getType() {
            return Component.class;
        }
    });
    column.setRenderer(new WebComponentRenderer());
}
Also used : Locale(java.util.Locale) GlobalConfig(com.haulmont.cuba.core.global.GlobalConfig) WebComponentRenderer(com.haulmont.cuba.web.gui.components.renderers.WebComponentRenderer) User(com.haulmont.cuba.security.entity.User) Inject(javax.inject.Inject) AbstractWindow(com.haulmont.cuba.gui.components.AbstractWindow) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) TreeMap(java.util.TreeMap) Configuration(com.haulmont.cuba.core.global.Configuration) Locale(java.util.Locale) Map(java.util.Map) LookupField(com.haulmont.cuba.gui.components.LookupField) DataGrid(com.haulmont.cuba.gui.components.DataGrid) Component(com.haulmont.cuba.gui.components.Component) User(com.haulmont.cuba.security.entity.User) GlobalConfig(com.haulmont.cuba.core.global.GlobalConfig) DataGrid(com.haulmont.cuba.gui.components.DataGrid) WebComponentRenderer(com.haulmont.cuba.web.gui.components.renderers.WebComponentRenderer) LookupField(com.haulmont.cuba.gui.components.LookupField) TreeMap(java.util.TreeMap) Component(com.haulmont.cuba.gui.components.Component) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 3 with LookupField

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

the class LookupFieldLoader method loadNewOptionHandler.

protected void loadNewOptionHandler(LookupField component, Element element) {
    String newOptionAllowed = element.attributeValue("newOptionAllowed");
    if (StringUtils.isNotEmpty(newOptionAllowed)) {
        component.setNewOptionAllowed(Boolean.parseBoolean(newOptionAllowed));
    }
    String newOptionHandlerMethod = element.attributeValue("newOptionHandler");
    if (StringUtils.isNotEmpty(newOptionHandlerMethod)) {
        FrameOwner controller = getComponentContext().getFrame().getFrameOwner();
        Class<? extends FrameOwner> windowClass = controller.getClass();
        Method newOptionHandler;
        try {
            newOptionHandler = windowClass.getMethod(newOptionHandlerMethod, LookupField.class, String.class);
        } catch (NoSuchMethodException e) {
            Map<String, Object> params = ParamsMap.of("LookupField Id", component.getId(), "Method name", newOptionHandlerMethod);
            throw new GuiDevelopmentException("Unable to find new option handler method for lookup field", context, params);
        }
        component.setNewOptionHandler(caption -> {
            try {
                newOptionHandler.invoke(controller, component, caption);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException("Unable to invoke new option handler", e);
            }
        });
    }
}
Also used : FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) LookupField(com.haulmont.cuba.gui.components.LookupField) Method(java.lang.reflect.Method) ParamsMap(com.haulmont.bali.util.ParamsMap) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with LookupField

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

the class LookupFieldDsTest method testSetValueWithoutOptions.

@Test
public void testSetValueWithoutOptions() {
    LookupField lookupField = uiComponents.create(LookupField.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setName("Test name");
    lookupField.setDatasource(userDs, "name");
    assertEquals("Test name", lookupField.getValue());
}
Also used : User(com.haulmont.cuba.security.entity.User) LookupField(com.haulmont.cuba.gui.components.LookupField) Test(org.junit.jupiter.api.Test)

Example 5 with LookupField

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

the class LookupFieldDsTest method testUnsubscribeDsListener.

@Test
public void testUnsubscribeDsListener() {
    LookupField lookupField = uiComponents.create(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);
    Datasource.ItemPropertyChangeListener<User> propertyChangeListener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    userDs.addItemPropertyChangeListener(propertyChangeListener);
    lookupField.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) 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.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) LookupField(com.haulmont.cuba.gui.components.LookupField) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) 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.jupiter.api.Test)

Aggregations

LookupField (com.haulmont.cuba.gui.components.LookupField)14 User (com.haulmont.cuba.security.entity.User)10 Datasource (com.haulmont.cuba.gui.data.Datasource)9 Test (org.junit.jupiter.api.Test)9 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)8 HasValue (com.haulmont.cuba.gui.components.HasValue)7 Group (com.haulmont.cuba.security.entity.Group)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 UUID (java.util.UUID)7 Consumer (java.util.function.Consumer)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)7 Disabled (org.junit.jupiter.api.Disabled)7 Map (java.util.Map)3 Component (com.haulmont.cuba.gui.components.Component)2 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