Skip to content

Commit 3ab13a6

Browse files
committed
[1245] Uniformize read-only computation
Bug: #1245 Signed-off-by: Gwendal Daniel <gwendal.daniel@obeosoft.com>
1 parent 00445e7 commit 3ab13a6

10 files changed

Lines changed: 60 additions & 278 deletions

File tree

backend/application/syson-application-configuration/src/main/java/org/eclipse/syson/application/services/SysONReadOnlyObjectPredicateDelegate.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
*******************************************************************************/
1313
package org.eclipse.syson.application.services;
1414

15-
import java.util.Objects;
16-
1715
import org.eclipse.emf.ecore.EAnnotation;
18-
import org.eclipse.emf.ecore.EObject;
1916
import org.eclipse.emf.ecore.resource.Resource;
2017
import org.eclipse.sirius.components.core.api.IReadOnlyObjectPredicate;
2118
import org.eclipse.sirius.components.core.api.IReadOnlyObjectPredicateDelegate;
22-
import org.eclipse.syson.services.UtilService;
23-
import org.eclipse.syson.services.api.ISysONResourceService;
19+
import org.eclipse.sirius.web.application.library.services.LibraryMetadataAdapter;
2420
import org.eclipse.syson.sysml.Element;
2521
import org.eclipse.syson.sysml.util.ElementUtil;
2622
import org.springframework.stereotype.Service;
@@ -42,12 +38,6 @@
4238
@Service
4339
public class SysONReadOnlyObjectPredicateDelegate implements IReadOnlyObjectPredicateDelegate {
4440

45-
private final ISysONResourceService sysONResourceService;
46-
47-
public SysONReadOnlyObjectPredicateDelegate(final ISysONResourceService sysONResourceService) {
48-
this.sysONResourceService = Objects.requireNonNull(sysONResourceService);
49-
}
50-
5141
@Override
5242
public boolean canHandle(Object candidate) {
5343
return candidate instanceof Element || candidate instanceof Resource || candidate instanceof EAnnotation;
@@ -59,13 +49,26 @@ public boolean test(Object candidate) {
5949
boolean isReadOnly = false;
6050

6151
if (candidate instanceof Resource resource) {
52+
// TODO the library version is here for testing purpose before we get the actual update from Sirius Web
53+
// isReadOnly = ElementUtil.isStandardLibraryResource(resource)
54+
// || resource.eAdapters().stream()
55+
// .filter(ResourceMetadataAdapter.class::isInstance)
56+
// .map(ResourceMetadataAdapter.class::cast)
57+
// .map(ResourceMetadataAdapter::isReadOnly)
58+
// .findFirst()
59+
// .orElse(false);
6260
isReadOnly = ElementUtil.isStandardLibraryResource(resource)
63-
|| this.sysONResourceService.isImported(resource) && !new UtilService().getLibraries(resource, false).isEmpty();
64-
} else if (candidate instanceof Element || candidate instanceof EAnnotation) {
65-
// Derive editability from the editability of the containing resource.
66-
final EObject eObject = (EObject) candidate;
67-
final Resource resource = eObject.eResource();
68-
isReadOnly = this.test(resource);
61+
|| resource.eAdapters().stream()
62+
.filter(LibraryMetadataAdapter.class::isInstance)
63+
.findFirst()
64+
.isPresent();
65+
66+
} else if (candidate instanceof Element element) {
67+
// An element can be read-only if it is contained in a standard LibraryPackage, regardless of whether its
68+
// containing resource is read-only or not.
69+
isReadOnly = this.test(element.eResource()) || ElementUtil.isFromStandardLibrary(element);
70+
} else if (candidate instanceof EAnnotation eAnnotation) {
71+
isReadOnly = this.test(eAnnotation.eResource());
6972
}
7073

7174
return isReadOnly;

backend/application/syson-application-configuration/src/test/java/org/eclipse/syson/application/services/DetailsViewServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class DetailsViewServiceTest {
4646
public void setUp() {
4747
// Use a dummy CompsedAdapterFactory, we don't test methods that require the one used by SysON for the moment.
4848
this.detailsViewService = new DetailsViewService(new ComposedAdapterFactory(), new IFeedbackMessageService.NoOp(),
49-
new ComposedReadOnlyObjectPredicate(List.of(new SysONReadOnlyObjectPredicateDelegate(new SysONResourceService())), new DefaultReadOnlyObjectPredicate()));
49+
new ComposedReadOnlyObjectPredicate(List.of(new SysONReadOnlyObjectPredicateDelegate()), new DefaultReadOnlyObjectPredicate()));
5050
}
5151

5252
@Test

backend/application/syson-application-configuration/src/test/java/org/eclipse/syson/application/services/SysONReadOnlyObjectPredicateDelegateTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
*/
5858
public class SysONReadOnlyObjectPredicateDelegateTests {
5959

60-
private final IReadOnlyObjectPredicateDelegate readOnlyObjectPredicateDelegate = new SysONReadOnlyObjectPredicateDelegate(new SysONResourceService());
60+
private final IReadOnlyObjectPredicateDelegate readOnlyObjectPredicateDelegate = new SysONReadOnlyObjectPredicateDelegate();
6161

6262
@SafeVarargs
6363
private static Resource createResource(final URI uri, final Consumer<Resource>... postTreatments) {
@@ -310,12 +310,14 @@ public void testResourceWithSysmlPackageImported() {
310310
@Test
311311
@DisplayName("Imported resource containing LibraryPackage is read-only (both the resource and all of its contents)")
312312
public void testResourceWithSysmlLibraryPackageImported() {
313+
// TODO this should not be the case anymore, we will rely on whether the resource is read-only
313314
SysONReadOnlyObjectPredicateDelegateTests.this.assertResourceAndAllContentsIsReadOnly(this.importedResourceWithSysmlLibraryPackage, true);
314315
}
315316

316317
@Test
317318
@DisplayName("Imported resource containing Package and LibraryPackage is read-only (both the resource and all of its contents)")
318319
public void testResourceWithSysmlMixedPackagesImported() {
320+
// TODO this should not be the case anymore, we will rely on whether the resource is read-only
319321
SysONReadOnlyObjectPredicateDelegateTests.this.assertResourceAndAllContentsIsReadOnly(this.importedResourceWithSysmlMixedPackages, true);
320322
}
321323
}
@@ -357,6 +359,7 @@ private static LibraryMetadataAdapter createLibraryMetadataAdapter() {
357359
@Test
358360
@DisplayName("Resource from referenced library containing Package is read-only (both the resource and all of its contents)")
359361
public void testResourceWithSysmlPackageImported() {
362+
// TODO this is fixed by the new implementation
360363
// Note that this might be a bug, see https://github.com/eclipse-syson/syson/issues/1342.
361364
SysONReadOnlyObjectPredicateDelegateTests.this.assertResourceAndAllContentsIsReadOnly(this.referencedResourceWithSysmlPackage, false);
362365
}

backend/application/syson-application/src/test/java/org/eclipse/syson/services/SysMLReadOnlyServiceTest.java

Lines changed: 0 additions & 103 deletions
This file was deleted.

backend/services/syson-services/src/main/java/org/eclipse/syson/services/SysMLMoveElementService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import java.util.Objects;
1616

1717
import org.eclipse.emf.ecore.EObject;
18+
import org.eclipse.sirius.components.core.api.IReadOnlyObjectPredicate;
1819
import org.eclipse.syson.services.api.ISysMLMoveElementService;
19-
import org.eclipse.syson.services.api.ISysMLReadOnlyService;
2020
import org.eclipse.syson.services.api.MoveStatus;
2121
import org.eclipse.syson.sysml.Element;
2222
import org.eclipse.syson.sysml.FeatureMembership;
@@ -38,14 +38,14 @@
3838
@Service
3939
public class SysMLMoveElementService implements ISysMLMoveElementService {
4040

41-
private final ISysMLReadOnlyService readOnlyService;
41+
private final IReadOnlyObjectPredicate readOnlyObjectPredicate;
4242

4343
private final DeleteService deleteService;
4444

4545
private final UtilService utilService;
4646

47-
public SysMLMoveElementService(ISysMLReadOnlyService readOnlyService) {
48-
this.readOnlyService = Objects.requireNonNull(readOnlyService);
47+
public SysMLMoveElementService(IReadOnlyObjectPredicate readOnlyObjectPredicate) {
48+
this.readOnlyObjectPredicate = Objects.requireNonNull(readOnlyObjectPredicate);
4949
this.deleteService = new DeleteService();
5050
this.utilService = new UtilService();
5151
}
@@ -70,9 +70,9 @@ public MoveStatus moveSemanticElement(Element element, Element newParent) {
7070
moveStatus = MoveStatus.buildFailure("Unable to move an Element to one of its descendant");
7171
} else if (newParent instanceof Membership) {
7272
moveStatus = MoveStatus.buildFailure("Membership can't be used as a target of a move");
73-
} else if (this.readOnlyService.isReadOnly(element)) {
73+
} else if (this.readOnlyObjectPredicate.test(element)) {
7474
moveStatus = MoveStatus.buildFailure("Unable to move a read only Element");
75-
} else if (this.readOnlyService.isReadOnly(newParent)) {
75+
} else if (this.readOnlyObjectPredicate.test(element)) {
7676
moveStatus = MoveStatus.buildFailure("Unable to move a Element to a read only Element");
7777
} else {
7878
moveStatus = this.doMoveElement(element, newParent);

backend/services/syson-services/src/main/java/org/eclipse/syson/services/SysMLReadOnlyService.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

backend/services/syson-services/src/main/java/org/eclipse/syson/services/api/ISysMLReadOnlyService.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

backend/views/syson-diagram-common-view/src/main/java/org/eclipse/syson/diagram/common/view/services/ViewCreateService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
import org.eclipse.sirius.components.core.api.IEditingContext;
2626
import org.eclipse.sirius.components.core.api.IFeedbackMessageService;
2727
import org.eclipse.sirius.components.core.api.IObjectSearchService;
28+
import org.eclipse.sirius.components.core.api.IReadOnlyObjectPredicate;
2829
import org.eclipse.sirius.components.diagrams.Diagram;
2930
import org.eclipse.sirius.components.diagrams.Node;
3031
import org.eclipse.sirius.components.representations.Message;
3132
import org.eclipse.sirius.components.representations.MessageLevel;
3233
import org.eclipse.syson.services.DeleteService;
3334
import org.eclipse.syson.services.ElementInitializerSwitch;
3435
import org.eclipse.syson.services.UtilService;
35-
import org.eclipse.syson.services.api.ISysMLReadOnlyService;
3636
import org.eclipse.syson.sysml.AcceptActionUsage;
3737
import org.eclipse.syson.sysml.ActionDefinition;
3838
import org.eclipse.syson.sysml.ActionUsage;
@@ -93,7 +93,7 @@ public class ViewCreateService {
9393

9494
private final IObjectSearchService objectSearchService;
9595

96-
private final ISysMLReadOnlyService readOnlyService;
96+
private final IReadOnlyObjectPredicate readOnlyObjectPredicate;
9797

9898
private final ShowDiagramsInheritedMembersService showDiagramsInheritedMembersService;
9999

@@ -105,11 +105,11 @@ public class ViewCreateService {
105105

106106
private final IFeedbackMessageService feedbackMessageService;
107107

108-
public ViewCreateService(IObjectSearchService objectSearchService, ISysMLReadOnlyService readOnlyService,
108+
public ViewCreateService(IObjectSearchService objectSearchService, IReadOnlyObjectPredicate readOnlyObjectPredicate,
109109
ShowDiagramsInheritedMembersService showDiagramsInheritedMembersService, IFeedbackMessageService feedbackMessageService) {
110110
this.feedbackMessageService = Objects.requireNonNull(feedbackMessageService);
111111
this.objectSearchService = Objects.requireNonNull(objectSearchService);
112-
this.readOnlyService = Objects.requireNonNull(readOnlyService);
112+
this.readOnlyObjectPredicate = Objects.requireNonNull(readOnlyObjectPredicate);
113113
this.showDiagramsInheritedMembersService = Objects.requireNonNull(showDiagramsInheritedMembersService);
114114
this.elementInitializerSwitch = new ElementInitializerSwitch();
115115
this.deleteService = new DeleteService();
@@ -1197,11 +1197,11 @@ public Element createPartUsageAndInterface(PartUsage self) {
11971197
private Element getEdgeSemanticContainer(Node source, Node target, Diagram diagram, IEditingContext editingContext) {
11981198
final Element semanticContainer;
11991199
Element semanticSourceGraphicalParent = this.getGraphicalContainerSemanticElement(source, diagram, editingContext);
1200-
if (semanticSourceGraphicalParent != null && !this.readOnlyService.isReadOnly(semanticSourceGraphicalParent)) {
1200+
if (semanticSourceGraphicalParent != null && !this.readOnlyObjectPredicate.test(semanticSourceGraphicalParent)) {
12011201
semanticContainer = semanticSourceGraphicalParent;
12021202
} else {
12031203
Element semanticTargetGraphicalParent = this.getGraphicalContainerSemanticElement(target, diagram, editingContext);
1204-
if (semanticTargetGraphicalParent != null && !this.readOnlyService.isReadOnly(semanticTargetGraphicalParent)) {
1204+
if (semanticTargetGraphicalParent != null && !this.readOnlyObjectPredicate.test(semanticTargetGraphicalParent)) {
12051205
semanticContainer = semanticTargetGraphicalParent;
12061206
} else {
12071207
semanticContainer = null;

0 commit comments

Comments
 (0)