Skip to content

Commit

Permalink
[test] Add selection dialog support to Papaya
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Bégaudeau <[email protected]>
  • Loading branch information
sbegaudeau committed Sep 17, 2024
1 parent 96708a8 commit 1d4158d
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.InterfaceNodeDescriptionProvider;
import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.RecordNodeDescriptionProvider;
import org.eclipse.sirius.web.papaya.representations.classdiagram.tools.ClassDiagramDropToolProvider;
import org.eclipse.sirius.web.papaya.representations.classdiagram.tools.ImportExistingTypesToolProvider;

/**
* Used to provide the view model used to create class diagrams.
Expand Down Expand Up @@ -79,8 +80,11 @@ public RepresentationDescription create(IColorProvider colorProvider) {
}

private DiagramPalette diagramPalette(IViewDiagramElementFinder cache) {
var importExistingTypesTool = new ImportExistingTypesToolProvider().getNodeTool(cache);
var dropTool = new ClassDiagramDropToolProvider().getDropTool(cache);

return new DiagramBuilders().newDiagramPalette()
.nodeTools(importExistingTypesTool)
.dropTool(dropTool)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*******************************************************************************
* 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.web.papaya.representations.classdiagram.tools;

import org.eclipse.sirius.components.view.builder.IViewDiagramElementFinder;
import org.eclipse.sirius.components.view.builder.generated.diagram.DiagramBuilders;
import org.eclipse.sirius.components.view.builder.generated.view.ViewBuilders;
import org.eclipse.sirius.components.view.diagram.NodeContainmentKind;
import org.eclipse.sirius.components.view.diagram.NodeTool;
import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.ClassNodeDescriptionProvider;
import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.EnumNodeDescriptionProvider;
import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.InterfaceNodeDescriptionProvider;
import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.RecordNodeDescriptionProvider;

/**
* Used to provide the tool used to import existing types.
*
* @author sbegaudeau
*/
@SuppressWarnings("checkstyle:MultipleStringLiterals")
public class ImportExistingTypesToolProvider {

public NodeTool getNodeTool(IViewDiagramElementFinder cache) {
var classNodeDescription = cache.getNodeDescription(ClassNodeDescriptionProvider.NAME).orElse(null);
var interfaceNodeDescription = cache.getNodeDescription(InterfaceNodeDescriptionProvider.NAME).orElse(null);
var enumNodeDescription = cache.getNodeDescription(EnumNodeDescriptionProvider.NAME).orElse(null);
var recordNodeDescription = cache.getNodeDescription(RecordNodeDescriptionProvider.NAME).orElse(null);

var treeDescription = new DiagramBuilders().newSelectionDialogTreeDescription()
.elementsExpression("aql:self.eResource().getResourceSet().getResources()")
.childrenExpression("aql:self.getChildren()")
.isSelectableExpression("aql:self.oclIsKindOf(papaya::Type)")
.build();

var dialogDescription = new DiagramBuilders().newSelectionDialogDescription()
.selectionMessage("Select the types to import")
.selectionDialogTreeDescription(treeDescription)
.build();

var ifClass = new ViewBuilders().newIf()
.conditionExpression("aql:selectedObject.eClass() = papaya::Class")
.children(
new DiagramBuilders().newCreateView()
.elementDescription(classNodeDescription)
.semanticElementExpression("aql:selectedObject")
.parentViewExpression("aql:selectedNode")
.containmentKind(NodeContainmentKind.CHILD_NODE)
.build()
)
.build();

var ifInterface = new ViewBuilders().newIf()
.conditionExpression("aql:selectedObject.eClass() = papaya::Interface")
.children(
new DiagramBuilders().newCreateView()
.elementDescription(interfaceNodeDescription)
.semanticElementExpression("aql:selectedObject")
.parentViewExpression("aql:selectedNode")
.containmentKind(NodeContainmentKind.CHILD_NODE)
.build()
)
.build();

var ifRecord = new ViewBuilders().newIf()
.conditionExpression("aql:selectedObject.eClass() = papaya::Record")
.children(
new DiagramBuilders().newCreateView()
.elementDescription(recordNodeDescription)
.semanticElementExpression("aql:selectedObject")
.parentViewExpression("aql:selectedNode")
.containmentKind(NodeContainmentKind.CHILD_NODE)
.build()
)
.build();

var ifEnum = new ViewBuilders().newIf()
.conditionExpression("aql:selectedObject.eClass() = papaya::Enum")
.children(
new DiagramBuilders().newCreateView()
.elementDescription(enumNodeDescription)
.semanticElementExpression("aql:selectedObject")
.parentViewExpression("aql:selectedNode")
.containmentKind(NodeContainmentKind.CHILD_NODE)
.build()
)
.build();

return new DiagramBuilders().newNodeTool()
.name("Import existing types")
.iconURLsExpression("/icons/full/obj16/Class.svg")
.dialogDescription(dialogDescription)
.body(
new ViewBuilders().newChangeContext()
.expression("aql:self")
.children(
ifClass,
ifInterface,
ifRecord,
ifEnum
)
.build()
)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*******************************************************************************/
package org.eclipse.sirius.web.papaya.representations.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IObjectSearchService;
Expand Down Expand Up @@ -51,4 +54,24 @@ public List<EObject> getSynchronizedObjects(IEditingContext editingContext, List
.map(EObject.class::cast)
.toList();
}

public ResourceSet getResourceSet(Resource resource) {
return resource.getResourceSet();
}

public List<Resource> getResources(ResourceSet resourceSet) {
return resourceSet.getResources();
}

public List<Object> getChildren(Object object) {
List<Object> children = new ArrayList<>();

if (object instanceof Resource resource) {
children.addAll(resource.getContents());
} else if (object instanceof EObject eObject) {
children.addAll(eObject.eContents());
}

return children;
}
}

0 comments on commit 1d4158d

Please sign in to comment.