forked from eclipse-sirius/sirius-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3826] Make EMF default form support attributes with a date
It supports EAttribute with EDataType with class java.time.Instant and java.time.LocalDate Bug: eclipse-sirius#3826 Signed-off-by: Laurent Fasani <[email protected]>
- Loading branch information
1 parent
5701515
commit debe92a
Showing
5 changed files
with
357 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...s/src/main/java/org/eclipse/sirius/components/emf/forms/InstantIfDescriptionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/******************************************************************************* | ||
* 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.time.Instant; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
import org.eclipse.emf.ecore.EAttribute; | ||
import org.eclipse.emf.ecore.EClassifier; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.edit.provider.ComposedAdapterFactory; | ||
import org.eclipse.sirius.components.emf.forms.api.IPropertiesValidationProvider; | ||
import org.eclipse.sirius.components.forms.DateTimeType; | ||
import org.eclipse.sirius.components.forms.WidgetIdProvider; | ||
import org.eclipse.sirius.components.forms.description.DateTimeDescription; | ||
import org.eclipse.sirius.components.forms.description.IfDescription; | ||
import org.eclipse.sirius.components.representations.Failure; | ||
import org.eclipse.sirius.components.representations.IStatus; | ||
import org.eclipse.sirius.components.representations.Success; | ||
import org.eclipse.sirius.components.representations.VariableManager; | ||
|
||
/** | ||
* Provides the default description of the widget to use to support DataType feature of type java.time.Instant. | ||
* | ||
* @author lfasani | ||
*/ | ||
public class InstantIfDescriptionProvider { | ||
private static final String IF_DESCRIPTION_ID = "java.time.Instant"; | ||
|
||
private static final String DATE_TIME_DESCRIPTION_ID = "DateTime"; | ||
|
||
private final ComposedAdapterFactory composedAdapterFactory; | ||
|
||
private final IPropertiesValidationProvider propertiesValidationProvider; | ||
|
||
private final Function<VariableManager, String> semanticTargetIdProvider; | ||
|
||
public InstantIfDescriptionProvider(ComposedAdapterFactory composedAdapterFactory, IPropertiesValidationProvider propertiesValidationProvider, Function<VariableManager, String> semanticTargetIdProvider) { | ||
this.composedAdapterFactory = Objects.requireNonNull(composedAdapterFactory); | ||
this.propertiesValidationProvider = Objects.requireNonNull(propertiesValidationProvider); | ||
this.semanticTargetIdProvider = Objects.requireNonNull(semanticTargetIdProvider); | ||
} | ||
|
||
public IfDescription getIfDescription() { | ||
return IfDescription.newIfDescription(IF_DESCRIPTION_ID) | ||
.targetObjectIdProvider(this.semanticTargetIdProvider) | ||
.predicate(this.getPredicate()) | ||
.controlDescriptions(List.of(this.getDateTimeDescription())) | ||
.build(); | ||
} | ||
|
||
private Function<VariableManager, Boolean> getPredicate() { | ||
return variableManager -> { | ||
var optionalEAttribute = variableManager.get(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class); | ||
return optionalEAttribute.filter(eAttribute -> { | ||
EClassifier eType = eAttribute.getEType(); | ||
return !eAttribute.isMany() && Objects.equals(eType.getInstanceClassName(), Instant.class.getName()); | ||
}).isPresent(); | ||
}; | ||
} | ||
|
||
private DateTimeDescription getDateTimeDescription() { | ||
return DateTimeDescription.newDateTimeDescription(DATE_TIME_DESCRIPTION_ID) | ||
.idProvider(new WidgetIdProvider()) | ||
.targetObjectIdProvider(this.semanticTargetIdProvider) | ||
.labelProvider(this.getLabelProvider()) | ||
.stringValueProvider(this.getValueProvider()) | ||
.newValueHandler(this.getNewValueHandler()) | ||
.diagnosticsProvider(this.propertiesValidationProvider.getDiagnosticsProvider()) | ||
.kindProvider(this.propertiesValidationProvider.getKindProvider()) | ||
.messageProvider(this.propertiesValidationProvider.getMessageProvider()) | ||
.type(DateTimeType.DATE_TIME) | ||
.build(); | ||
} | ||
|
||
private Function<VariableManager, String> getLabelProvider() { | ||
return new EStructuralFeatureLabelProvider(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, this.composedAdapterFactory); | ||
} | ||
|
||
private Function<VariableManager, String> getValueProvider() { | ||
return variableManager -> { | ||
var optionalEObject = variableManager.get(VariableManager.SELF, EObject.class); | ||
var optionalEAttribute = variableManager.get(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class); | ||
|
||
if (optionalEObject.isPresent() && optionalEAttribute.isPresent()) { | ||
EObject eObject = optionalEObject.get(); | ||
EAttribute eAttribute = optionalEAttribute.get(); | ||
|
||
Object value = eObject.eGet(eAttribute); | ||
if (value instanceof Instant instant) { | ||
return DateTimeFormatter.ISO_INSTANT.format(instant); | ||
} | ||
} | ||
|
||
return ""; | ||
}; | ||
} | ||
|
||
private BiFunction<VariableManager, String, IStatus> getNewValueHandler() { | ||
return (variableManager, newValue) -> { | ||
IStatus status = new Failure(""); | ||
var optionalEObject = variableManager.get(VariableManager.SELF, EObject.class); | ||
var optionalEAttribute = variableManager.get(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class); | ||
if (optionalEObject.isPresent() && optionalEAttribute.isPresent()) { | ||
EObject eObject = optionalEObject.get(); | ||
EAttribute eAttribute = optionalEAttribute.get(); | ||
|
||
if (newValue == null || newValue.isBlank()) { | ||
eObject.eSet(eAttribute, null); | ||
status = new Success(); | ||
} else { | ||
Instant instant = Instant.parse(newValue); | ||
eObject.eSet(eAttribute, instant); | ||
status = new Success(); | ||
} | ||
} | ||
return status; | ||
}; | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...src/main/java/org/eclipse/sirius/components/emf/forms/LocalDateIfDescriptionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/******************************************************************************* | ||
* 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.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
import org.eclipse.emf.ecore.EAttribute; | ||
import org.eclipse.emf.ecore.EClassifier; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.edit.provider.ComposedAdapterFactory; | ||
import org.eclipse.sirius.components.emf.forms.api.IPropertiesValidationProvider; | ||
import org.eclipse.sirius.components.forms.DateTimeType; | ||
import org.eclipse.sirius.components.forms.WidgetIdProvider; | ||
import org.eclipse.sirius.components.forms.description.DateTimeDescription; | ||
import org.eclipse.sirius.components.forms.description.IfDescription; | ||
import org.eclipse.sirius.components.representations.Failure; | ||
import org.eclipse.sirius.components.representations.IStatus; | ||
import org.eclipse.sirius.components.representations.Success; | ||
import org.eclipse.sirius.components.representations.VariableManager; | ||
|
||
/** | ||
* Provides the default description of the widget to use to support DataType feature of type java.time.LocalDate. | ||
* | ||
* @author lfasani | ||
*/ | ||
public class LocalDateIfDescriptionProvider { | ||
private static final String IF_DESCRIPTION_ID = "java.time.LocalDate"; | ||
|
||
private static final String DATE_DESCRIPTION_ID = "Date"; | ||
|
||
private final ComposedAdapterFactory composedAdapterFactory; | ||
|
||
private final IPropertiesValidationProvider propertiesValidationProvider; | ||
|
||
private final Function<VariableManager, String> semanticTargetIdProvider; | ||
|
||
public LocalDateIfDescriptionProvider(ComposedAdapterFactory composedAdapterFactory, IPropertiesValidationProvider propertiesValidationProvider, Function<VariableManager, String> semanticTargetIdProvider) { | ||
this.composedAdapterFactory = Objects.requireNonNull(composedAdapterFactory); | ||
this.propertiesValidationProvider = Objects.requireNonNull(propertiesValidationProvider); | ||
this.semanticTargetIdProvider = Objects.requireNonNull(semanticTargetIdProvider); | ||
} | ||
|
||
public IfDescription getIfDescription() { | ||
return IfDescription.newIfDescription(IF_DESCRIPTION_ID) | ||
.targetObjectIdProvider(this.semanticTargetIdProvider) | ||
.predicate(this.getPredicate()) | ||
.controlDescriptions(List.of(this.getDateTimeDescription())) | ||
.build(); | ||
} | ||
|
||
private Function<VariableManager, Boolean> getPredicate() { | ||
return variableManager -> { | ||
var optionalEAttribute = variableManager.get(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class); | ||
return optionalEAttribute.filter(eAttribute -> { | ||
EClassifier eType = eAttribute.getEType(); | ||
return !eAttribute.isMany() && Objects.equals(eType.getInstanceClassName(), LocalDate.class.getName()); | ||
}).isPresent(); | ||
}; | ||
} | ||
|
||
private DateTimeDescription getDateTimeDescription() { | ||
return DateTimeDescription.newDateTimeDescription(DATE_DESCRIPTION_ID) | ||
.idProvider(new WidgetIdProvider()) | ||
.targetObjectIdProvider(this.semanticTargetIdProvider) | ||
.labelProvider(this.getLabelProvider()) | ||
.stringValueProvider(this.getValueProvider()) | ||
.newValueHandler(this.getNewValueHandler()) | ||
.diagnosticsProvider(this.propertiesValidationProvider.getDiagnosticsProvider()) | ||
.kindProvider(this.propertiesValidationProvider.getKindProvider()) | ||
.messageProvider(this.propertiesValidationProvider.getMessageProvider()) | ||
.type(DateTimeType.DATE) | ||
.build(); | ||
} | ||
|
||
private Function<VariableManager, String> getLabelProvider() { | ||
return new EStructuralFeatureLabelProvider(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, this.composedAdapterFactory); | ||
} | ||
|
||
private Function<VariableManager, String> getValueProvider() { | ||
return variableManager -> { | ||
var optionalEObject = variableManager.get(VariableManager.SELF, EObject.class); | ||
var optionalEAttribute = variableManager.get(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class); | ||
|
||
if (optionalEObject.isPresent() && optionalEAttribute.isPresent()) { | ||
EObject eObject = optionalEObject.get(); | ||
EAttribute eAttribute = optionalEAttribute.get(); | ||
|
||
Object value = eObject.eGet(eAttribute); | ||
if (value instanceof LocalDate localDate) { | ||
Instant instant = localDate.atStartOfDay(ZoneId.of("UTC")).toInstant(); | ||
return DateTimeFormatter.ISO_INSTANT.format(instant); | ||
} | ||
} | ||
return ""; | ||
}; | ||
} | ||
|
||
private BiFunction<VariableManager, String, IStatus> getNewValueHandler() { | ||
return (variableManager, newValue) -> { | ||
IStatus status = new Failure(""); | ||
var optionalEObject = variableManager.get(VariableManager.SELF, EObject.class); | ||
var optionalEAttribute = variableManager.get(EMFFormDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class); | ||
if (optionalEObject.isPresent() && optionalEAttribute.isPresent()) { | ||
EObject eObject = optionalEObject.get(); | ||
EAttribute eAttribute = optionalEAttribute.get(); | ||
|
||
if (newValue == null || newValue.isBlank()) { | ||
eObject.eSet(eAttribute, null); | ||
status = new Success(); | ||
} else { | ||
Instant instant = Instant.parse(newValue); | ||
LocalDate localDate = LocalDateTime.ofInstant(instant, ZoneOffset.UTC).toLocalDate(); | ||
eObject.eSet(eAttribute, localDate); | ||
status = new Success(); | ||
} | ||
} | ||
return status; | ||
}; | ||
} | ||
} |
Oops, something went wrong.