use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.
the class DataGridLoader method loadColumn.
protected Column loadColumn(DataGrid component, Element element, Datasource ds) {
String id = element.attributeValue("id");
String property = element.attributeValue("property");
if (id == null) {
if (property != null) {
id = property;
} else {
throw new GuiDevelopmentException("A column must have whether id or property specified", context.getCurrentFrameId(), "DataGrid ID", component.getId());
}
}
Column column;
if (property != null) {
MetaPropertyPath metaPropertyPath = AppBeans.get(MetadataTools.NAME, MetadataTools.class).resolveMetaPropertyPath(ds.getMetaClass(), property);
column = component.addColumn(id, metaPropertyPath);
} else {
column = component.addColumn(id, null);
}
String expandRatio = element.attributeValue("expandRatio");
if (StringUtils.isNotEmpty(expandRatio)) {
column.setExpandRatio(Integer.parseInt(expandRatio));
}
String collapsed = element.attributeValue("collapsed");
if (StringUtils.isNotEmpty(collapsed)) {
column.setCollapsed(Boolean.parseBoolean(collapsed));
}
String collapsible = element.attributeValue("collapsible");
if (StringUtils.isNotEmpty(collapsible)) {
column.setCollapsible(Boolean.parseBoolean(collapsible));
}
String collapsingToggleCaption = element.attributeValue("collapsingToggleCaption");
if (StringUtils.isNotEmpty(collapsingToggleCaption)) {
collapsingToggleCaption = loadResourceString(collapsingToggleCaption);
column.setCollapsingToggleCaption(collapsingToggleCaption);
}
String sortable = element.attributeValue("sortable");
if (StringUtils.isNotEmpty(sortable)) {
column.setSortable(Boolean.parseBoolean(sortable));
}
String resizable = element.attributeValue("resizable");
if (StringUtils.isNotEmpty(resizable)) {
column.setResizable(Boolean.parseBoolean(resizable));
}
String editable = element.attributeValue("editable");
if (StringUtils.isNotEmpty(editable)) {
column.setEditable(Boolean.parseBoolean(editable));
}
// Default caption set to columns when it is added to a DataGrid,
// so we need to set caption as null to get caption from
// metaProperty if 'caption' attribute is empty
column.setCaption(null);
String caption = loadCaption(element);
if (caption == null) {
String columnCaption;
if (column.getPropertyPath() != null) {
MetaProperty metaProperty = column.getPropertyPath().getMetaProperty();
String propertyName = metaProperty.getName();
if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
columnCaption = LocaleHelper.isLocalizedValueDefined(categoryAttribute.getLocaleNames()) ? categoryAttribute.getLocaleName() : StringUtils.capitalize(categoryAttribute.getName());
} else {
MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(column.getPropertyPath());
columnCaption = messageTools.getPropertyCaption(propertyMetaClass, propertyName);
}
} else {
Class<?> declaringClass = ds.getMetaClass().getJavaClass();
String className = declaringClass.getName();
int i = className.lastIndexOf('.');
if (i > -1) {
className = className.substring(i + 1);
}
columnCaption = messages.getMessage(declaringClass, className + "." + id);
}
column.setCaption(columnCaption);
} else {
column.setCaption(caption);
}
column.setXmlDescriptor(element);
Integer width = loadWidth(element, "width");
if (width != null) {
column.setWidth(width);
}
Integer minimumWidth = loadWidth(element, "minimumWidth");
if (minimumWidth != null) {
column.setMinimumWidth(minimumWidth);
}
Integer maximumWidth = loadWidth(element, "maximumWidth");
if (maximumWidth != null) {
column.setMaximumWidth(maximumWidth);
}
column.setFormatter(loadFormatter(element));
return column;
}
use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.
the class FieldGroupLoader method loadField.
protected FieldGroup.FieldConfig loadField(Element element, Datasource ds, String columnWidth) {
String id = element.attributeValue("id");
String property = element.attributeValue("property");
if (Strings.isNullOrEmpty(id) && Strings.isNullOrEmpty(property)) {
throw new GuiDevelopmentException(String.format("id/property is not defined for field of FieldGroup '%s'. " + "Set id or property attribute.", resultComponent.getId()), context.getFullFrameId());
}
if (Strings.isNullOrEmpty(property)) {
property = id;
} else if (Strings.isNullOrEmpty(id)) {
id = property;
}
Datasource targetDs = ds;
Datasource datasource = loadDatasource(element);
if (datasource != null) {
targetDs = datasource;
}
CollectionDatasource optionsDs = null;
String optDsName = element.attributeValue("optionsDatasource");
if (StringUtils.isNotBlank(optDsName)) {
DsContext dsContext = getContext().getFrame().getDsContext();
optionsDs = findDatasourceRecursively(dsContext, optDsName);
if (optionsDs == null) {
throw new GuiDevelopmentException(String.format("Options datasource %s not found for field %s", optDsName, id), context.getFullFrameId());
}
}
boolean customField = false;
String custom = element.attributeValue("custom");
if (StringUtils.isNotEmpty(custom)) {
customField = Boolean.parseBoolean(custom);
}
if (StringUtils.isNotEmpty(element.attributeValue("generator"))) {
customField = true;
}
List<Element> elements = Dom4j.elements(element);
List<Element> customElements = elements.stream().filter(e -> !("formatter".equals(e.getName()) || "validator".equals(e.getName()))).collect(Collectors.toList());
if (!customElements.isEmpty()) {
if (customElements.size() > 1) {
throw new GuiDevelopmentException(String.format("FieldGroup field %s element cannot contains two or more custom field definitions", id), context.getCurrentFrameId());
}
if (customField) {
throw new GuiDevelopmentException(String.format("FieldGroup field %s cannot use both custom/generator attribute and inline component definition", id), context.getCurrentFrameId());
}
customField = true;
}
if (!customField && targetDs == null) {
throw new GuiDevelopmentException(String.format("Datasource is not defined for FieldGroup field '%s'. " + "Only custom fields can have no datasource.", property), context.getFullFrameId());
}
FieldGroup.FieldConfig field = resultComponent.createField(id);
if (property != null) {
field.setProperty(property);
}
if (datasource != null) {
field.setDatasource(datasource);
}
if (optionsDs != null) {
field.setOptionsDatasource(optionsDs);
}
String stylename = element.attributeValue("stylename");
if (StringUtils.isNotEmpty(stylename)) {
field.setStyleName(stylename);
}
MetaPropertyPath metaPropertyPath = null;
if (targetDs != null && property != null) {
MetaClass metaClass = targetDs.getMetaClass();
metaPropertyPath = metadataTools.resolveMetaPropertyPath(targetDs.getMetaClass(), property);
if (metaPropertyPath == null) {
if (!customField) {
throw new GuiDevelopmentException(String.format("Property '%s' is not found in entity '%s'", property, metaClass.getName()), context.getFullFrameId());
}
}
}
String propertyName = metaPropertyPath != null ? metaPropertyPath.getMetaProperty().getName() : null;
if (metaPropertyPath != null && DynamicAttributesUtils.isDynamicAttribute(metaPropertyPath.getMetaProperty())) {
CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
field.setCaption(categoryAttribute != null ? categoryAttribute.getLocaleName() : propertyName);
} else {
loadCaption(field, element);
if (field.getCaption() == null) {
field.setCaption(getDefaultCaption(field, targetDs));
}
}
loadDescription(field, element);
loadContextHelp(field, element);
field.setXmlDescriptor(element);
com.haulmont.cuba.gui.components.Formatter formatter = loadFormatter(element);
if (formatter != null) {
field.setFormatter(formatter);
}
String defaultWidth = element.attributeValue("width");
if (StringUtils.isEmpty(defaultWidth)) {
defaultWidth = columnWidth;
}
loadWidth(field, defaultWidth);
if (customField) {
field.setCustom(true);
}
String required = element.attributeValue("required");
if (StringUtils.isNotEmpty(required)) {
field.setRequired(Boolean.parseBoolean(required));
}
String requiredMsg = element.attributeValue("requiredMessage");
if (requiredMsg != null) {
requiredMsg = loadResourceString(requiredMsg);
field.setRequiredMessage(requiredMsg);
}
String tabIndex = element.attributeValue("tabIndex");
if (StringUtils.isNotEmpty(tabIndex)) {
field.setTabIndex(Integer.parseInt(tabIndex));
}
loadInputPrompt(field, element);
if (customElements.size() == 1) {
// load nested component defined as inline
Element customFieldElement = customElements.get(0);
LayoutLoader loader = new LayoutLoader(context, factory, layoutLoaderConfig);
loader.setLocale(getLocale());
loader.setMessagesPack(getMessagesPack());
ComponentLoader childComponentLoader = loader.createComponent(customFieldElement);
childComponentLoader.loadComponent();
Component customComponent = childComponentLoader.getResultComponent();
String inlineAttachMode = element.attributeValue("inlineAttachMode");
if (StringUtils.isNotEmpty(inlineAttachMode)) {
field.setComponent(customComponent, FieldGroup.FieldAttachMode.valueOf(inlineAttachMode));
} else {
field.setComponent(customComponent);
}
}
return field;
}
use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.
the class FieldGroupLoader method loadDynamicAttributeFields.
protected List<FieldGroup.FieldConfig> loadDynamicAttributeFields(Datasource ds) {
if (ds != null && metadataTools.isPersistent(ds.getMetaClass())) {
String windowId = ComponentsHelper.getWindow(resultComponent).getId();
Set<CategoryAttribute> attributesToShow = dynamicAttributesGuiTools.getAttributesToShowOnTheScreen(ds.getMetaClass(), windowId, resultComponent.getId());
if (!attributesToShow.isEmpty()) {
List<FieldGroup.FieldConfig> fields = new ArrayList<>();
ds.setLoadDynamicAttributes(true);
for (CategoryAttribute attribute : attributesToShow) {
FieldGroup.FieldConfig field = resultComponent.createField(DynamicAttributesUtils.encodeAttributeCode(attribute.getCode()));
field.setProperty(DynamicAttributesUtils.encodeAttributeCode(attribute.getCode()));
field.setCaption(attribute.getLocaleName());
field.setDatasource(ds);
field.setRequired(attribute.getRequired());
field.setRequiredMessage(messages.formatMainMessage("validation.required.defaultMsg", attribute.getLocaleName()));
loadWidth(field, attribute.getWidth());
// Currently, ListEditor does not support datasource binding so we create custom field
if (BooleanUtils.isTrue(attribute.getIsCollection())) {
CustomFieldGenerator fieldGenerator = new DynamicAttributeCustomFieldGenerator();
Component fieldComponent = fieldGenerator.generateField(ds, DynamicAttributesUtils.encodeAttributeCode(attribute.getCode()));
field.setCustom(true);
field.setComponent(fieldComponent);
applyPermissions(fieldComponent);
}
fields.add(field);
}
dynamicAttributesGuiTools.listenDynamicAttributesChanges(ds);
return fields;
}
}
return Collections.emptyList();
}
use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.
the class AttributeEditor method initFieldGroup.
protected void initFieldGroup() {
attributeFieldGroup.addCustomField("defaultBoolean", new FieldGroup.CustomFieldGenerator() {
@Override
public Component generateField(Datasource datasource, String propertyId) {
LookupField lookupField = factory.createComponent(LookupField.class);
Map<String, Object> options = new TreeMap<>();
options.put(datatypeFormatter.formatBoolean(true), true);
options.put(datatypeFormatter.formatBoolean(false), false);
lookupField.setOptionsMap(options);
lookupField.setDatasource(attributeDs, "defaultBoolean");
return lookupField;
}
});
attributeFieldGroup.addCustomField("dataType", new FieldGroup.CustomFieldGenerator() {
@Override
public Component generateField(Datasource datasource, String propertyId) {
dataTypeField = factory.createComponent(LookupField.class);
Map<String, Object> options = new TreeMap<>();
PropertyType[] types = PropertyType.values();
for (PropertyType propertyType : types) {
options.put(getMessage(propertyType.toString()), propertyType);
}
dataTypeField.setWidth(fieldWidth);
dataTypeField.setNewOptionAllowed(false);
dataTypeField.setRequired(true);
dataTypeField.setRequiredMessage(getMessage("dataTypeRequired"));
dataTypeField.setOptionsMap(options);
dataTypeField.setCaption(getMessage("dataType"));
dataTypeField.setFrame(frame);
dataTypeField.setDatasource(datasource, propertyId);
return dataTypeField;
}
});
attributeFieldGroup.addCustomField("screen", new FieldGroup.CustomFieldGenerator() {
@Override
public Component generateField(Datasource datasource, String propertyId) {
screenField = factory.createComponent(LookupField.class);
screenField.setId("screenField");
screenField.setCaption(getMessage("screen"));
screenField.setWidth(fieldWidth);
screenField.setRequired(true);
screenField.setRequiredMessage(getMessage("entityScreenRequired"));
screenField.setFrame(frame);
screenField.setDatasource(datasource, propertyId);
return screenField;
}
});
attributeFieldGroup.addCustomField("entityClass", new FieldGroup.CustomFieldGenerator() {
@Override
public Component generateField(Datasource datasource, String propertyId) {
entityTypeField = factory.createComponent(LookupField.class);
entityTypeField.setId("entityClass");
entityTypeField.setCaption(getMessage("entityType"));
entityTypeField.setRequired(true);
entityTypeField.setRequiredMessage(getMessage("entityTypeRequired"));
entityTypeField.setWidth(fieldWidth);
entityTypeField.setFrame(frame);
Map<String, Object> options = new TreeMap<>();
MetaClass entityType = null;
for (MetaClass metaClass : metadataTools.getAllPersistentMetaClasses()) {
if (!metadataTools.isSystemLevel(metaClass)) {
if (metadata.getTools().hasCompositePrimaryKey(metaClass) && !HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) {
continue;
}
options.put(messageTools.getDetailedEntityCaption(metaClass), metaClass.getJavaClass().getName());
if (attribute != null && metaClass.getJavaClass().getName().equals(attribute.getEntityClass())) {
entityType = metaClass;
}
}
}
entityTypeField.setOptionsMap(options);
entityTypeField.setValue(entityType);
entityTypeField.setDatasource(datasource, propertyId);
return entityTypeField;
}
});
attributeFieldGroup.addCustomField("defaultEntityId", (datasource, propertyId) -> {
defaultEntityField = factory.createComponent(PickerField.class);
defaultEntityField.setCaption(messages.getMessage(CategoryAttribute.class, "CategoryAttribute.defaultEntityId"));
defaultEntityField.addValueChangeListener(e -> {
Entity entity = (Entity) e.getValue();
if (entity != null) {
attribute.setObjectDefaultEntityId(referenceToEntitySupport.getReferenceId(entity));
} else {
attribute.setObjectDefaultEntityId(null);
}
((AbstractDatasource) attributeDs).modified(attribute);
});
entityLookupAction = defaultEntityField.addLookupAction();
defaultEntityField.addClearAction();
return defaultEntityField;
});
attributeFieldGroup.addCustomField("enumeration", (datasource, propertyId) -> {
enumerationListEditor = factory.createComponent(ListEditor.class);
enumerationListEditor.setWidth("100%");
enumerationListEditor.setItemType(ListEditor.ItemType.STRING);
enumerationListEditor.setRequired(true);
enumerationListEditor.setRequiredMessage(getMessage("enumRequired"));
enumerationListEditor.addValueChangeListener(e -> {
List<String> value = (List<String>) e.getValue();
attribute.setEnumeration(Joiner.on(",").join(value));
});
if (localizedFrame != null) {
enumerationListEditor.setEditorWindowId("localizedEnumerationWindow");
enumerationListEditor.setEditorParamsSupplier(() -> ParamsMap.of("enumerationLocales", attribute.getEnumerationLocales()));
enumerationListEditor.addEditorCloseListener(closeEvent -> {
if (closeEvent.getActionId().equals(COMMIT_ACTION_ID)) {
LocalizedEnumerationWindow enumerationWindow = (LocalizedEnumerationWindow) closeEvent.getWindow();
attribute.setEnumerationLocales(enumerationWindow.getLocalizedValues());
}
});
}
return enumerationListEditor;
});
attributeFieldGroup.addCustomField("whereClause", (datasource, propertyId) -> {
whereField = factory.createComponent(SourceCodeEditor.class);
whereField.setDatasource(attributeDs, "whereClause");
whereField.setWidth("100%");
whereField.setHeight(themeConstants.get("cuba.gui.customConditionFrame.whereField.height"));
whereField.setSuggester((source, text, cursorPosition) -> requestHint(whereField, text, cursorPosition));
whereField.setHighlightActiveLine(false);
whereField.setShowGutter(false);
return whereField;
});
attributeFieldGroup.addCustomField("joinClause", (datasource, propertyId) -> {
joinField = factory.createComponent(SourceCodeEditor.class);
joinField.setDatasource(attributeDs, "joinClause");
joinField.setWidth("100%");
joinField.setHeight(themeConstants.get("cuba.gui.customConditionFrame.joinField.height"));
joinField.setSuggester((source, text, cursorPosition) -> requestHint(joinField, text, cursorPosition));
joinField.setHighlightActiveLine(false);
joinField.setShowGutter(false);
return joinField;
});
attributeFieldGroup.addCustomField("constraintWizard", (datasource, propertyId) -> {
HBoxLayout hbox = factory.createComponent(HBoxLayout.class);
hbox.setWidth("100%");
LinkButton linkButton = factory.createComponent(LinkButton.class);
linkButton.setAction(new BaseAction("constraintWizard").withHandler(event -> openConstraintWizard()));
linkButton.setCaption(getMessage("constraintWizard"));
linkButton.setAlignment(Alignment.MIDDLE_LEFT);
hbox.add(linkButton);
return hbox;
});
attributeDs.addItemPropertyChangeListener(e -> {
String property = e.getProperty();
CategoryAttribute attribute = getItem();
if ("dataType".equalsIgnoreCase(property) || "lookup".equalsIgnoreCase(property) || "defaultDateIsCurrent".equalsIgnoreCase(property) || "entityClass".equalsIgnoreCase(property)) {
setupVisibility();
}
if ("name".equalsIgnoreCase(property)) {
fillAttributeCode();
}
if ("screen".equalsIgnoreCase(property) || "joinClause".equals(property) || "whereClause".equals(property)) {
dynamicAttributesGuiTools.initEntityPickerField(defaultEntityField, attribute);
}
});
}
use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.
the class DynamicAttributesCache method getAttributeForMetaClass.
@Nullable
public CategoryAttribute getAttributeForMetaClass(MetaClass metaClass, String code) {
code = DynamicAttributesUtils.decodeAttributeCode(code);
MetaClass targetMetaClass = resolveTargetMetaClass(metaClass);
Map<String, CategoryAttribute> attributes = attributesCache.get(targetMetaClass.getName());
if (attributes != null) {
return attributes.get(code);
}
return null;
}
Aggregations