Skip to content

Commit

Permalink
[1139] Hard code a pseudo "semantic browser" in the default rules for…
Browse files Browse the repository at this point in the history
… testing

Bug: #1139
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
  • Loading branch information
pcdavid committed Apr 22, 2022
1 parent 7cbcbe3 commit 6469953
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.emf.compatibility.properties.api.IPropertiesValidationProvider;
import org.eclipse.sirius.components.emf.services.messages.IEMFMessageService;
import org.eclipse.sirius.components.forms.GroupDisplayMode;
import org.eclipse.sirius.components.forms.description.AbstractControlDescription;
import org.eclipse.sirius.components.forms.description.ForDescription;
import org.eclipse.sirius.components.forms.description.FormDescription;
Expand Down Expand Up @@ -71,6 +72,7 @@ public FormDescription getFormDescription() {
List<GroupDescription> groupDescriptions = new ArrayList<>();
GroupDescription groupDescription = this.getGroupDescription();

groupDescriptions.add(this.getSemanticBrowserGroupDescription());
groupDescriptions.add(groupDescription);

List<PageDescription> pageDescriptions = new ArrayList<>();
Expand Down Expand Up @@ -204,4 +206,20 @@ private GroupDescription getGroupDescription() {
// @formatter:on
}

private GroupDescription getSemanticBrowserGroupDescription() {
List<AbstractControlDescription> controlDescriptions = new ArrayList<>();
SemanticBrowserDescriptionProvider semanticBrowserDescriptionProvider = new SemanticBrowserDescriptionProvider(this.objectService, this.propertiesValidationProvider);
controlDescriptions.add(semanticBrowserDescriptionProvider.getCurrentTreeDescription());
controlDescriptions.add(semanticBrowserDescriptionProvider.getOutgoingTreeDescription());
controlDescriptions.add(semanticBrowserDescriptionProvider.getIncomingTreeDescription());
// @formatter:off
return GroupDescription.newGroupDescription("semanticBrowserGroupId") //$NON-NLS-1$
.idProvider(variableManager -> "Semantic Browser") //$NON-NLS-1$
.labelProvider(variableManager -> "Semantic Browser") //$NON-NLS-1$
.displayModeProvider(variableManager -> GroupDisplayMode.TOGGLEABLE_AREAS)
.semanticElementsProvider(variableManager -> Collections.singletonList(variableManager.getVariables().get(VariableManager.SELF)))
.controlDescriptions(controlDescriptions)
.build();
// @formatter:on
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*******************************************************************************
* Copyright (c) 2022 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.compatibility.properties;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
import org.eclipse.sirius.components.compatibility.forms.WidgetIdProvider;
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.emf.compatibility.properties.api.IPropertiesValidationProvider;
import org.eclipse.sirius.components.forms.TreeNode;
import org.eclipse.sirius.components.forms.description.TreeDescription;
import org.eclipse.sirius.components.representations.VariableManager;

/**
* Provides a tree widget description with incoming, current and outgoing trees.
*
* @author pcdavid
*/
public class SemanticBrowserDescriptionProvider {
public static final String CANDIDATE_VARIABLE = "candidate"; //$NON-NLS-1$

private static final String SEMANTIC_BROWSER = "semantic_browser"; //$NON-NLS-1$

private static final String CATEGORY_INCOMING = "Incoming"; //$NON-NLS-1$

private static final String CATEGORY_CURRENT = "Current"; //$NON-NLS-1$

private static final String CATEGORY_OUTGOING = "Outgoing"; //$NON-NLS-1$

private final IObjectService objectService;

private final IPropertiesValidationProvider propertiesValidationProvider;

public SemanticBrowserDescriptionProvider(IObjectService objectService, IPropertiesValidationProvider propertiesValidationProvider) {
this.objectService = objectService;
this.propertiesValidationProvider = propertiesValidationProvider;
}

private String getNodeId(VariableManager variableManager) {
String result = ""; //$NON-NLS-1$
Optional<Object> optionalCandidate = variableManager.get(CANDIDATE_VARIABLE, Object.class);
if (optionalCandidate.isPresent()) {
Object candidate = optionalCandidate.get();
result = this.objectService.getId(candidate);
}
return result;
}

private String getNodeLabel(VariableManager variableManager) {
String result = ""; //$NON-NLS-1$
Optional<Object> optionalCandidate = variableManager.get(CANDIDATE_VARIABLE, Object.class);
if (optionalCandidate.isPresent()) {
Object candidate = optionalCandidate.get();
result = this.objectService.getLabel(candidate);
}
return result;
}

private String getNodeImageURL(VariableManager variableManager) {
String result = ""; //$NON-NLS-1$
Optional<Object> optionalCandidate = variableManager.get(CANDIDATE_VARIABLE, Object.class);
if (optionalCandidate.isPresent()) {
Object candidate = optionalCandidate.get();
result = this.objectService.getImagePath(candidate);
}
return result;
}

private String getNodeKind(VariableManager variableManager) {
String result = ""; //$NON-NLS-1$
Optional<Object> optionalCandidate = variableManager.get(CANDIDATE_VARIABLE, Object.class);
if (optionalCandidate.isPresent()) {
Object candidate = optionalCandidate.get();
result = this.objectService.getKind(candidate);
}
return result;
}

private List<?> getContents(VariableManager variableManager) {
List<?> result = List.of();
Optional<EObject> optionalCandidate = variableManager.get(CANDIDATE_VARIABLE, EObject.class);
if (optionalCandidate.isPresent()) {
EObject candidate = optionalCandidate.get();
result = candidate.eContents();
}
return result;
}

private List<?> getOutgoing(VariableManager variableManager) {
List<?> result = List.of();
Optional<EObject> optionalSelf = variableManager.get(VariableManager.SELF, EObject.class);
Optional<EObject> optionalCandidate = variableManager.get(CANDIDATE_VARIABLE, EObject.class);
if (optionalCandidate.isPresent() && optionalCandidate.equals(optionalSelf)) {
EObject candidate = optionalCandidate.get();
result = candidate.eCrossReferences();
}
return result;
}

private List<?> getIncoming(VariableManager variableManager) {
List<?> result = List.of();
Optional<EObject> optionalSelf = variableManager.get(VariableManager.SELF, EObject.class);
Optional<EObject> optionalCandidate = variableManager.get(CANDIDATE_VARIABLE, EObject.class);
if (optionalCandidate.isPresent() && optionalCandidate.equals(optionalSelf)) {
EObject candidate = optionalCandidate.get();
ECrossReferenceAdapter xref = ECrossReferenceAdapter.getCrossReferenceAdapter(candidate);
if (xref != null) {
result = xref.getInverseReferences(candidate).stream().map(Setting::getEObject).collect(Collectors.toList());
}
}
return result;
}

public TreeDescription getCurrentTreeDescription() {
// @formatter:off
return TreeDescription.newTreeDescription(SEMANTIC_BROWSER + CATEGORY_CURRENT).idProvider(new WidgetIdProvider())
.diagnosticsProvider(this.propertiesValidationProvider.getDiagnosticsProvider())
.kindProvider(this.propertiesValidationProvider.getKindProvider())
.messageProvider(this.propertiesValidationProvider.getMessageProvider())
.labelProvider(variableManager -> CATEGORY_CURRENT)
.nodeIdProvider(this::getNodeId)
.nodeLabelProvider(this::getNodeLabel)
.nodeImageURLProvider(this::getNodeImageURL)
.nodeKindProvider(this::getNodeKind)
.nodeSelectableProvider(variableManager -> true)
.childrenProvider(this::getContents)
.expandedNodeIdsProvider(this::getAllNodeIds)
.build();
// @formatter:on
}

private List<String> getAllNodeIds(VariableManager variableManager) {
List<String> result = new ArrayList<>();
for (var element : variableManager.get("nodes", List.class).orElse(List.of())) { //$NON-NLS-1$
if (element instanceof TreeNode) {
TreeNode node = (TreeNode) element;
result.add(node.getId());
}
}
return result;
}

public TreeDescription getOutgoingTreeDescription() {
// @formatter:off
return TreeDescription.newTreeDescription(SEMANTIC_BROWSER + CATEGORY_OUTGOING).idProvider(new WidgetIdProvider())
.diagnosticsProvider(this.propertiesValidationProvider.getDiagnosticsProvider())
.kindProvider(this.propertiesValidationProvider.getKindProvider())
.messageProvider(this.propertiesValidationProvider.getMessageProvider())
.labelProvider(variableManager -> CATEGORY_OUTGOING)
.nodeIdProvider(this::getNodeId)
.nodeLabelProvider(this::getNodeLabel)
.nodeImageURLProvider(this::getNodeImageURL)
.nodeKindProvider(this::getNodeKind)
.nodeSelectableProvider(variableManager -> true)
.childrenProvider(this::getOutgoing)
.expandedNodeIdsProvider(this::getAllNodeIds)
.build();
// @formatter:on
}

public TreeDescription getIncomingTreeDescription() {
// @formatter:off
return TreeDescription.newTreeDescription(SEMANTIC_BROWSER + CATEGORY_INCOMING).idProvider(new WidgetIdProvider())
.diagnosticsProvider(this.propertiesValidationProvider.getDiagnosticsProvider())
.kindProvider(this.propertiesValidationProvider.getKindProvider())
.messageProvider(this.propertiesValidationProvider.getMessageProvider())
.labelProvider(variableManager -> CATEGORY_INCOMING)
.nodeIdProvider(this::getNodeId)
.nodeLabelProvider(this::getNodeLabel)
.nodeImageURLProvider(this::getNodeImageURL)
.nodeKindProvider(this::getNodeKind)
.nodeSelectableProvider(variableManager -> true)
.childrenProvider(this::getIncoming)
.expandedNodeIdsProvider(this::getAllNodeIds)
.build();
// @formatter:on
}
}

0 comments on commit 6469953

Please sign in to comment.