use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.
the class ComponentLoaderHelper method loadValidator.
@SuppressWarnings("unchecked")
public static Consumer<?> loadValidator(Element validatorElement, ComponentLoader.Context context, ClassManager classManager) {
String className = validatorElement.attributeValue("class");
String scriptPath = validatorElement.attributeValue("script");
String script = validatorElement.getText();
Consumer<?> validator = null;
if (StringUtils.isNotBlank(scriptPath) || StringUtils.isNotBlank(script)) {
validator = new ScriptValidator(validatorElement, context.getMessageGroup());
} else {
Class aClass = classManager.findClass(className);
if (aClass == null)
throw new GuiDevelopmentException(String.format("Class %s is not found", className), context);
if (!StringUtils.isBlank(context.getMessageGroup()))
try {
validator = (Consumer<?>) ReflectionHelper.newInstance(aClass, validatorElement, context.getMessageGroup());
} catch (NoSuchMethodException e) {
//
}
if (validator == null) {
try {
validator = (Consumer<?>) ReflectionHelper.newInstance(aClass, validatorElement);
} catch (NoSuchMethodException e) {
try {
validator = (Consumer<?>) ReflectionHelper.newInstance(aClass);
} catch (NoSuchMethodException e1) {
// todo log warn
}
}
}
if (validator == null) {
throw new GuiDevelopmentException(String.format("Validator class %s has no supported constructors", aClass), context);
}
}
return validator;
}
use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.
the class ComponentLoaderHelper method loadFormatter.
@Nullable
public static Formatter<?> loadFormatter(Element element, ClassManager classManager, ComponentLoader.Context context) {
Element formatterElement = element.element("formatter");
if (formatterElement != null) {
String className = formatterElement.attributeValue("class");
if (StringUtils.isEmpty(className)) {
throw new GuiDevelopmentException("Formatter's attribute 'class' is not specified", context);
}
Class<?> aClass = classManager.findClass(className);
if (aClass == null) {
throw new GuiDevelopmentException(String.format("Class %s is not found", className), context);
}
try {
Constructor<?> constructor = aClass.getConstructor(Element.class);
try {
return (Formatter<?>) constructor.newInstance(formatterElement);
} catch (Throwable e) {
throw new GuiDevelopmentException(String.format("Unable to instantiate class %s: %s", className, e), context);
}
} catch (NoSuchMethodException e) {
try {
return (Formatter<?>) aClass.getDeclaredConstructor().newInstance();
} catch (Exception e1) {
throw new GuiDevelopmentException(String.format("Unable to instantiate class %s: %s", className, e1), context);
}
}
} else {
return null;
}
}
use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.
the class DatasourceLoaderHelper method loadDatasource.
/**
* Loads datasource with property and returns empty optional if datasource is not defined in the component or
* it has already {@link ValueSource}
*
* @param componentId component id
* @param element component descriptor
* @param context loader context
* @param componentContext component loader context
* @return optional with {@link DatasourceValueSource}
*/
public static Optional<DatasourceValueSource> loadDatasource(String componentId, Element element, ComponentLoader.Context context, ComponentLoaderContext componentContext) {
String datasource = element.attributeValue("datasource");
if (!StringUtils.isEmpty(datasource)) {
if (componentContext.getDsContext() == null) {
throw new IllegalStateException("'datasource' attribute can be used only in screens with 'dsContext' element. " + "In a screen with 'data' element use 'dataContainer' attribute.");
}
Datasource ds = componentContext.getDsContext().get(datasource);
if (ds == null) {
throw new GuiDevelopmentException(String.format("Datasource '%s' is not defined", datasource), context, "Component ID", componentId);
}
String property = loadProperty(componentId, element, context);
return Optional.of(new DatasourceValueSource(ds, property));
}
return Optional.empty();
}
use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.
the class EmbeddedLoader method loadComponent.
@Override
public void loadComponent() {
assignFrame(resultComponent);
String relativeSrc = element.attributeValue("relativeSrc");
if (StringUtils.isNotEmpty(relativeSrc)) {
resultComponent.setRelativeSource(relativeSrc);
}
String srcAttr = element.attributeValue("src");
if (srcAttr != null) {
if (srcAttr.startsWith(URL_PREFIX)) {
String src = srcAttr.substring(URL_PREFIX.length());
URL targetUrl;
try {
targetUrl = new URL(src);
} catch (MalformedURLException e) {
throw new GuiDevelopmentException("Incorrect URL in Embedded src attribute", context, "src", srcAttr);
}
resultComponent.setType(Embedded.Type.BROWSER);
resultComponent.setSource(targetUrl);
} else if (srcAttr.startsWith(THEME_PREFIX)) {
resultComponent.setSource(srcAttr);
} else if (srcAttr.startsWith(FILE_PREFIX)) {
String src = srcAttr.substring(FILE_PREFIX.length());
resultComponent.setType(Embedded.Type.OBJECT);
resultComponent.setSource(src);
} else {
throw new GuiDevelopmentException("Illegal src attribute value. 'url://' or 'file://' or theme:// prefix expected", context, "src", srcAttr);
}
}
String typeAttribute = element.attributeValue("type");
if (StringUtils.isNotEmpty(typeAttribute)) {
Embedded.Type type = Embedded.Type.valueOf(typeAttribute);
resultComponent.setType(type);
}
loadVisible(resultComponent, element);
loadEnable(resultComponent, element);
loadStyleName(resultComponent, element);
loadHeight(resultComponent, element);
loadWidth(resultComponent, element);
loadAlign(resultComponent, element);
loadCaption(resultComponent, element);
loadDescription(resultComponent, element);
loadContextHelp(resultComponent, element);
loadIcon(resultComponent, element);
}
use of io.jmix.ui.GuiDevelopmentException in project jmix by jmix-framework.
the class FieldGroupLoader method loadComponent.
@Override
public void loadComponent() {
assignFrame(resultComponent);
String fieldFactoryBean = element.attributeValue("fieldFactoryBean");
if (StringUtils.isNotEmpty(fieldFactoryBean)) {
FieldGroupFieldFactory fieldFactory = applicationContext.getBean(fieldFactoryBean, FieldGroupFieldFactory.class);
resultComponent.setFieldFactory(fieldFactory);
}
assignXmlDescriptor(resultComponent, element);
loadVisible(resultComponent, element);
loadWidth(resultComponent, element);
loadEditable(resultComponent, element);
loadEnable(resultComponent, element);
loadStyleName(resultComponent, element);
loadCss(resultComponent, element);
loadHtmlSanitizerEnabled(resultComponent, element);
loadIcon(resultComponent, element);
loadCaption(resultComponent, element);
loadDescription(resultComponent, element);
loadContextHelp(resultComponent, element);
loadHeight(resultComponent, element);
loadAlign(resultComponent, element);
loadCaptionAlignment(resultComponent, element);
loadFieldCaptionWidth(resultComponent, element);
Datasource ds = loadDatasource(element);
resultComponent.setDatasource(ds);
if (element.elements("column").isEmpty()) {
Iterable<FieldGroup.FieldConfig> rootFields = loadFields(resultComponent, element, ds, null);
Iterable<FieldGroup.FieldConfig> dynamicAttributeFields = loadDynamicAttributeFields(ds);
for (FieldGroup.FieldConfig field : dynamicAttributeFields) {
if (resultComponent.getWidth() > 0 && field.getWidth() == null) {
field.setWidth("100%");
}
}
for (FieldGroup.FieldConfig field : Iterables.concat(rootFields, dynamicAttributeFields)) {
resultComponent.addField(field);
}
} else {
List<Element> columnElements = element.elements("column");
List<Element> fieldElements = element.elements("field");
if (fieldElements.size() > 0) {
Map<String, Object> params = new HashMap<>();
String fieldGroupId = resultComponent.getId();
if (StringUtils.isNotEmpty(fieldGroupId)) {
params.put("FieldGroup ID", fieldGroupId);
}
throw new GuiDevelopmentException("FieldGroup field elements should be placed within its column.", context, params);
}
resultComponent.setColumns(columnElements.size());
int colIndex = 0;
for (Element columnElement : columnElements) {
String flex = columnElement.attributeValue("flex");
if (StringUtils.isNotEmpty(flex)) {
resultComponent.setColumnExpandRatio(colIndex, Float.parseFloat(flex));
}
String columnWidth = loadThemeString(columnElement.attributeValue("width"));
Iterable<FieldGroup.FieldConfig> columnFields = loadFields(resultComponent, columnElement, ds, columnWidth);
if (colIndex == 0) {
columnFields = Iterables.concat(columnFields, loadDynamicAttributeFields(ds));
}
for (FieldGroup.FieldConfig field : columnFields) {
resultComponent.addField(field, colIndex);
}
String columnFieldCaptionWidth = columnElement.attributeValue("fieldCaptionWidth");
if (StringUtils.isNotEmpty(columnFieldCaptionWidth)) {
if (columnFieldCaptionWidth.startsWith(MessageTools.MARK)) {
columnFieldCaptionWidth = loadResourceString(columnFieldCaptionWidth);
}
if (columnFieldCaptionWidth.endsWith("px")) {
columnFieldCaptionWidth = columnFieldCaptionWidth.substring(0, columnFieldCaptionWidth.indexOf("px"));
}
resultComponent.setFieldCaptionWidth(colIndex, Integer.parseInt(columnFieldCaptionWidth));
}
colIndex++;
}
}
for (FieldGroup.FieldConfig field : resultComponent.getFields()) {
if (!field.isCustom()) {
if (!DynAttrUtils.isDynamicAttributeProperty(field.getProperty())) {
// the following does not make sense for dynamic attributes
loadValidators(resultComponent, field);
loadRequired(resultComponent, field);
loadEnable(resultComponent, field);
}
loadVisible(resultComponent, field);
loadEditable(resultComponent, field);
}
}
resultComponent.bind();
ComponentContext componentContext = getComponentContext();
for (FieldGroup.FieldConfig field : resultComponent.getFields()) {
if (field.getXmlDescriptor() != null) {
String generator = field.getXmlDescriptor().attributeValue("generator");
if (generator != null) {
componentContext.addInjectTask((boundContext, window) -> {
DeclarativeFieldGenerator fieldGenerator = new DeclarativeFieldGenerator(resultComponent, generator);
Component fieldComponent = fieldGenerator.generateField(field.getTargetDatasource(), field.getProperty());
field.setComponent(fieldComponent);
});
}
}
}
}
Aggregations