Skip to content

Commit

Permalink
[3464] Improve i18n support for domain and view forms
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#3464
Signed-off-by: Denis Nikiforov <[email protected]>
  • Loading branch information
AresEkb committed May 13, 2024
1 parent c5d7091 commit 26652e0
Show file tree
Hide file tree
Showing 32 changed files with 1,143 additions and 88 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ They still support returning an `java.time.Instant` object directly.
- https://github.com/eclipse-sirius/sirius-web/issues/3456[#3456] [diagram] Fix an issue that were causing a large delay when opening a diagram, creating an edge or reconnecting an edge
- https://github.com/eclipse-sirius/sirius-web/issues/3391[#3391] [diagram] Accept gradient for node background
- https://github.com/eclipse-sirius/sirius-web/issues/3435[#3435] [diagram] Extract diagram style from useDropNode
- https://github.com/eclipse-sirius/sirius-web/issues/3464[#3464] [view] Improve i18n support for domain and view forms

== v2024.3.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,7 @@ private Function<VariableManager, String> getOptionIdProvider() {
}

private Function<VariableManager, String> getOptionLabelProvider() {
return variableManager -> {
Object litteral = variableManager.getVariables().get(SelectComponent.CANDIDATE_VARIABLE);
if (litteral instanceof Enumerator) {
return ((Enumerator) litteral).getLiteral();
}
return "";
};
return new EEnumLiteralLabelProvider(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, this.composedAdapterFactory);
}

private Function<VariableManager, Boolean> getOptionSelectedProvider() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.emf.forms;

import java.util.Objects;
import java.util.function.Function;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.sirius.components.forms.components.SelectComponent;
import org.eclipse.sirius.components.representations.VariableManager;

/**
* Utility class used to provide a label for an enumeration literal in the variable manager.
*
* @author aresekb
*/
public class EEnumLiteralLabelProvider implements Function<VariableManager, String> {

private final String featureVariableName;

private final AdapterFactory adapterFactory;

public EEnumLiteralLabelProvider(String featureVariableName, AdapterFactory adapterFactory) {
this.featureVariableName = Objects.requireNonNull(featureVariableName);
this.adapterFactory = Objects.requireNonNull(adapterFactory);
}

@Override
public String apply(VariableManager variableManager) {
Object object = variableManager.getVariables().get(VariableManager.SELF);
Object feature = variableManager.getVariables().get(this.featureVariableName);
Object literal = variableManager.getVariables().get(SelectComponent.CANDIDATE_VARIABLE);

String result = "";
if (object instanceof EObject eObject && feature instanceof EStructuralFeature eStructuralFeature) {
Adapter adapter = this.adapterFactory.adapt(eObject, IItemPropertySource.class);
if (adapter instanceof IItemPropertySource itemPropertySource) {
IItemPropertyDescriptor descriptor = itemPropertySource.getPropertyDescriptor(eObject, eStructuralFeature);
if (descriptor != null) {
result = descriptor.getLabelProvider(eObject).getText(literal);
}
}
}
if (result.isEmpty() && literal instanceof Enumerator enumerator) {
result = enumerator.getLiteral();
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private GroupDescription getGroupDescription() {

return GroupDescription.newGroupDescription("groupId")
.idProvider(variableManager -> "Core Properties")
.labelProvider(variableManager -> "Core Properties")
.labelProvider(variableManager -> this.emfMessageService.coreProperties())
.semanticElementsProvider(variableManager -> Collections.singletonList(variableManager.getVariables().get(VariableManager.SELF)))
.controlDescriptions(controlDescriptions)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2023 Obeo.
* Copyright (c) 2019, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -52,4 +52,24 @@ public String upperBoundaryReached(String newInstanceClass, String feature) {
return this.messageSourceAccessor.getMessage("UPPER_BOUNDARY_REACHED", new Object[] { newInstanceClass, feature });
}

@Override
public String properties() {
return this.messageSourceAccessor.getMessage("PROPERTIES");
}

@Override
public String coreProperties() {
return this.messageSourceAccessor.getMessage("CORE_PROPERTIES");
}

@Override
public String general() {
return this.messageSourceAccessor.getMessage("GENERAL");
}

@Override
public String shapePreview() {
return this.messageSourceAccessor.getMessage("SHAPE_PREVIEW");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2023 Obeo.
* Copyright (c) 2019, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -27,6 +27,14 @@ public interface IEMFMessageService {

String upperBoundaryReached(String newInstanceClass, String feature);

String properties();

String coreProperties();

String general();

String shapePreview();

/**
* Implementation which does nothing, used for mocks in unit tests.
*
Expand All @@ -53,5 +61,25 @@ public String invalidNumber(String newValue) {
public String upperBoundaryReached(String newInstanceClass, String feature) {
return "";
}

@Override
public String properties() {
return "";
}

@Override
public String coreProperties() {
return "";
}

@Override
public String general() {
return "";
}

@Override
public String shapePreview() {
return "";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
################################################################################
# Copyright (c) 2019, 2023 Obeo.
# Copyright (c) 2019, 2024 Obeo.
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
Expand All @@ -14,3 +14,7 @@ INVALID_INPUT = Invalid input type, "{0}" has been received while "{1}" was expe
UNEXPECTED_ERROR = An unexpected error has occurred, please contact the server administrator
INVALID_NUMBER = The new value "{0}" is not a valid number
UPPER_BOUNDARY_REACHED = Unable to create a new instance of "{0}" in feature "{1}" because it has reached its upper-bound cardinality.
PROPERTIES = Properties
CORE_PROPERTIES = Core Properties
GENERAL = General
SHAPE_PREVIEW = Shape Preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
################################################################################
# Copyright (c) 2024 Obeo.
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Obeo - initial API and implementation
#################################################################################
INVALID_INPUT = \u041D\u0435\u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u0439 \u0442\u0438\u043F, \u0431\u044B\u043B\u043E \u043F\u043E\u043B\u0443\u0447\u0435\u043D\u043E "{0}", \u0430 \u043E\u0436\u0438\u0434\u0430\u043B\u043E\u0441\u044C "{1}"
UNEXPECTED_ERROR = \u041F\u0440\u043E\u0438\u0437\u043E\u0448\u043B\u0430 \u043D\u0435\u043E\u0436\u0438\u0434\u0430\u043D\u043D\u0430\u044F \u043E\u0448\u0438\u0431\u043A\u0430, \u0441\u0432\u044F\u0436\u0438\u0442\u0435\u0441\u044C, \u043F\u043E\u0436\u0430\u043B\u0443\u0439\u0441\u0442\u0430, \u0441 \u0430\u0434\u043C\u0438\u043D\u0438\u0441\u0442\u0440\u0430\u0442\u043E\u0440\u043E\u043C
INVALID_NUMBER = \u041D\u043E\u0432\u043E\u0435 \u0437\u043D\u0430\u0447\u0435\u043D\u0438\u0435 "{0}" \u043D\u0435 \u044F\u0432\u043B\u044F\u0435\u0442\u0441\u044F \u0447\u0438\u0441\u043B\u043E\u043C
UPPER_BOUNDARY_REACHED = \u041D\u0435 \u0443\u0434\u0430\u043B\u043E\u0441\u044C \u0441\u043E\u0437\u0434\u0430\u0442\u044C \u043E\u0431\u044A\u0435\u043A\u0442 "{0}" \u0432 \u043A\u043E\u043B\u043B\u0435\u043A\u0446\u0438\u0438 "{1}", \u043F\u043E\u0442\u043E\u043C\u0443 \u0447\u0442\u043E \u0434\u043E\u0441\u0442\u0438\u0433\u043D\u0443\u0442 \u043F\u0440\u0435\u0434\u0435\u043B \u043D\u0430 \u043C\u0430\u043A\u0441\u0438\u043C\u0430\u043B\u044C\u043D\u043E\u0435 \u043A\u043E\u043B\u0438\u0447\u0435\u0441\u0442\u0432\u043E \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432.
PROPERTIES = \u0421\u0432\u043E\u0439\u0441\u0442\u0432\u0430
CORE_PROPERTIES = \u041E\u0441\u043D\u043E\u0432\u043D\u044B\u0435 \u0441\u0432\u043E\u0439\u0441\u0442\u0432\u0430
GENERAL = \u041E\u0431\u0449\u0438\u0435
SHAPE_PREVIEW = \u041F\u0440\u0435\u0434\u043F\u0440\u043E\u0441\u043C\u043E\u0442\u0440 \u0444\u0438\u0433\u0443\u0440\u044B
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.sirius.components.view.form.WidgetDescription;
import org.eclipse.sirius.components.view.widget.reference.ReferenceFactory;
import org.eclipse.sirius.components.view.widget.reference.ReferenceWidgetDescription;
import org.eclipse.sirius.web.sample.messages.DomainPropertiesMessageService;
import org.eclipse.sirius.web.sample.services.DomainAttributeServices;
import org.eclipse.sirius.web.services.api.representations.IInMemoryViewRegistry;
import org.springframework.context.annotation.Configuration;
Expand All @@ -65,10 +66,13 @@ public class DomainPropertiesConfigurer implements IPropertiesDescriptionRegistr

private final IInMemoryViewRegistry viewRegistry;

public DomainPropertiesConfigurer(ViewFormDescriptionConverter converter, IFeedbackMessageService feedbackMessageService, IInMemoryViewRegistry viewRegistry) {
private final DomainPropertiesMessageService messageService;

public DomainPropertiesConfigurer(ViewFormDescriptionConverter converter, IFeedbackMessageService feedbackMessageService, IInMemoryViewRegistry viewRegistry, DomainPropertiesMessageService messageService) {
this.viewRegistry = Objects.requireNonNull(viewRegistry);
this.converter = Objects.requireNonNull(converter);
this.feedbackMessageService = Objects.requireNonNull(feedbackMessageService);
this.messageService = Objects.requireNonNull(messageService);
}

@Override
Expand Down Expand Up @@ -101,7 +105,7 @@ private FormDescription getAttributeDetails() {
FormDescription form = FormFactory.eINSTANCE.createFormDescription();
form.setName("Attribute Details");
form.setDomainType("domain::Attribute");
form.setTitleExpression("Attribute Details");
form.setTitleExpression(this.messageService.getMessage("ATTRIBUTE_DETAILS"));

PageDescription page = FormFactory.eINSTANCE.createPageDescription();
page.setDomainType("domain::Attribute");
Expand All @@ -123,34 +127,34 @@ private GroupDescription createEntityGroup() {
GroupDescription group = FormFactory.eINSTANCE.createGroupDescription();
group.setDisplayMode(GroupDisplayMode.LIST);
group.setName(CORE_PROPERTIES);
group.setLabelExpression(CORE_PROPERTIES);
group.setLabelExpression(this.messageService.getMessage("CORE_PROPERTIES"));
group.setSemanticCandidatesExpression("aql:self");
group.getChildren().add(this.createStringAttributeEditWidget("Name", DomainPackage.Literals.NAMED_ELEMENT__NAME.getName()));
group.getChildren().add(this.createReferenceWidget("Super Type", DomainPackage.Literals.ENTITY__SUPER_TYPES.getName()));
group.getChildren().add(this.createReferenceWidget("Attributes", DomainPackage.Literals.ENTITY__ATTRIBUTES.getName()));
group.getChildren().add(this.createReferenceWidget("Relations", DomainPackage.Literals.ENTITY__RELATIONS.getName()));
group.getChildren().add(this.createBooleanAttributeEditWidget("Abstract", DomainPackage.Literals.ENTITY__ABSTRACT.getName()));
group.getChildren().add(this.createStringAttributeEditWidget("Name", "NAME", DomainPackage.Literals.NAMED_ELEMENT__NAME.getName()));
group.getChildren().add(this.createReferenceWidget("Super Types", "SUPER_TYPES", DomainPackage.Literals.ENTITY__SUPER_TYPES.getName()));
group.getChildren().add(this.createReferenceWidget("Attributes", "ATTRIBUTES", DomainPackage.Literals.ENTITY__ATTRIBUTES.getName()));
group.getChildren().add(this.createReferenceWidget("Relations", "RELATIONS", DomainPackage.Literals.ENTITY__RELATIONS.getName()));
group.getChildren().add(this.createBooleanAttributeEditWidget("Abstract", "ABSTRACT", DomainPackage.Literals.ENTITY__ABSTRACT.getName()));
return group;
}

private GroupDescription createGroup() {
GroupDescription group = FormFactory.eINSTANCE.createGroupDescription();
group.setDisplayMode(GroupDisplayMode.LIST);
group.setName(CORE_PROPERTIES);
group.setLabelExpression(CORE_PROPERTIES);
group.setLabelExpression(this.messageService.getMessage("CORE_PROPERTIES"));
group.setSemanticCandidatesExpression("aql:self");
group.getChildren().add(this.createStringAttributeEditWidget("Name", DomainPackage.Literals.NAMED_ELEMENT__NAME.getName()));
group.getChildren().add(this.createStringAttributeEditWidget("Name", "NAME", DomainPackage.Literals.NAMED_ELEMENT__NAME.getName()));
group.getChildren().add(this.createTypeSelectorWidget());
group.getChildren().add(this.createBooleanAttributeEditWidget("Optional", DomainPackage.Literals.FEATURE__OPTIONAL.getName()));
group.getChildren().add(this.createBooleanAttributeEditWidget("Many", DomainPackage.Literals.FEATURE__MANY.getName()));
group.getChildren().add(this.createBooleanAttributeEditWidget("Optional", "OPTIONAL", DomainPackage.Literals.FEATURE__OPTIONAL.getName()));
group.getChildren().add(this.createBooleanAttributeEditWidget("Many", "MANY", DomainPackage.Literals.FEATURE__MANY.getName()));
group.getChildren().add(this.createCardinalityLabel());
return group;
}

private WidgetDescription createStringAttributeEditWidget(String title, String attributeName) {
private WidgetDescription createStringAttributeEditWidget(String name, String label, String attributeName) {
TextfieldDescription textfield = FormFactory.eINSTANCE.createTextfieldDescription();
textfield.setName(title);
textfield.setLabelExpression(title);
textfield.setName(name);
textfield.setLabelExpression(this.messageService.getMessage(label));
textfield.setValueExpression("aql:self.%s".formatted(attributeName));
SetValue setValueOperation = ViewFactory.eINSTANCE.createSetValue();
setValueOperation.setFeatureName(attributeName);
Expand All @@ -159,10 +163,10 @@ private WidgetDescription createStringAttributeEditWidget(String title, String a
return textfield;
}

private WidgetDescription createBooleanAttributeEditWidget(String title, String attributeName) {
private WidgetDescription createBooleanAttributeEditWidget(String name, String label, String attributeName) {
CheckboxDescription checkbox = FormFactory.eINSTANCE.createCheckboxDescription();
checkbox.setName(title);
checkbox.setLabelExpression(title);
checkbox.setName(name);
checkbox.setLabelExpression(this.messageService.getMessage(label));
checkbox.setValueExpression("aql:self.%s".formatted(attributeName));
SetValue setValueOperation = ViewFactory.eINSTANCE.createSetValue();
setValueOperation.setFeatureName(attributeName);
Expand All @@ -174,7 +178,7 @@ private WidgetDescription createBooleanAttributeEditWidget(String title, String
private WidgetDescription createTypeSelectorWidget() {
SelectDescription selectWidget = FormFactory.eINSTANCE.createSelectDescription();
selectWidget.setName("Type");
selectWidget.setLabelExpression("Type");
selectWidget.setLabelExpression(this.messageService.getMessage("TYPE"));
selectWidget.setCandidatesExpression("aql:self.getAvailableDataTypes()");
selectWidget.setValueExpression("aql:self.getDataType()");
selectWidget.setCandidateLabelExpression("aql:candidate.capitalize()");
Expand All @@ -184,18 +188,18 @@ private WidgetDescription createTypeSelectorWidget() {
return selectWidget;
}

private WidgetDescription createReferenceWidget(String name, String referenceName) {
private WidgetDescription createReferenceWidget(String name, String label, String referenceName) {
ReferenceWidgetDescription refWidget = ReferenceFactory.eINSTANCE.createReferenceWidgetDescription();
refWidget.setName(name);
refWidget.setLabelExpression(name);
refWidget.setLabelExpression(this.messageService.getMessage(label));
refWidget.setReferenceNameExpression(referenceName);
return refWidget;
}

private WidgetDescription createCardinalityLabel() {
LabelDescription cardinalityLabel = FormFactory.eINSTANCE.createLabelDescription();
cardinalityLabel.setName("Cardinality");
cardinalityLabel.setLabelExpression("Cardinality");
cardinalityLabel.setLabelExpression(this.messageService.getMessage("CARDINALITY"));
cardinalityLabel.setValueExpression("aql:(if self.optional then '0' else '1' endif) + '..' + (if self.many then '*' else '1' endif)");
return cardinalityLabel;
}
Expand Down
Loading

0 comments on commit 26652e0

Please sign in to comment.