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;
}
});
}
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());
}
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);
}
});
}
}
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());
}
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));
}
Aggregations