Skip to content

Commit

Permalink
[2759] Reactivate the SelectionDialog
Browse files Browse the repository at this point in the history
In addtion, this commit introduce the generic concept of Dialog
to make it possible to define any kind of dialog for diagram node tools.

Bug:#2759
Signed-off-by: Florian Barbin <[email protected]>
  • Loading branch information
florianbarbin committed Jun 18, 2024
1 parent a66d309 commit 6eb29cc
Show file tree
Hide file tree
Showing 58 changed files with 1,797 additions and 1,108 deletions.
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private ITool convertTool(org.eclipse.sirius.components.diagrams.tools.ITool too
if (tool instanceof org.eclipse.sirius.components.diagrams.tools.SingleClickOnDiagramElementTool singleClickOnDiagramElementTool) {
convertedTool = new SingleClickOnDiagramElementTool(singleClickOnDiagramElementTool.getId(), singleClickOnDiagramElementTool.getLabel(),
singleClickOnDiagramElementTool.getIconURL(), singleClickOnDiagramElementTool.getTargetDescriptions(),
singleClickOnDiagramElementTool.getSelectionDescriptionId(), singleClickOnDiagramElementTool.isAppliesToDiagramRoot());
singleClickOnDiagramElementTool.getDialogDescriptionId(), singleClickOnDiagramElementTool.isAppliesToDiagramRoot());
}
if (tool instanceof org.eclipse.sirius.components.diagrams.tools.SingleClickOnTwoDiagramElementsTool singleClickOnTwoDiagramElementsTool) {
List<SingleClickOnTwoDiagramElementsCandidate> candidates = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ private SingleClickOnDiagramElementTool convertNodeCreationDescription(Map<Strin
List<String> imagePath = this.toolImageProvider.getIcon(nodeCreationTool);
List<IDiagramElementDescription> targetDescriptions = this.getParentNodeDescriptions(nodeCreationTool.getNodeMappings(), id2NodeDescriptions);
var selectModelElementVariableOpt = new SelectModelElementVariableProvider().getSelectModelElementVariable(nodeCreationTool.getVariable());
String selectionDescriptionId = null;
String dialogDescriptionId = null;
if (selectModelElementVariableOpt.isPresent()) {
selectionDescriptionId = this.identifierProvider.getIdentifier(selectModelElementVariableOpt.get());
dialogDescriptionId = this.identifierProvider.getIdentifier(selectModelElementVariableOpt.get());
}
// @formatter:off
return SingleClickOnDiagramElementTool.newSingleClickOnDiagramElementTool(id)
Expand All @@ -245,7 +245,7 @@ private SingleClickOnDiagramElementTool convertNodeCreationDescription(Map<Strin
.handler(this.createNodeCreationHandler(interpreter, nodeCreationTool))
.targetDescriptions(targetDescriptions)
.appliesToDiagramRoot(this.atLeastOneRootMapping(nodeCreationTool.getNodeMappings()))
.selectionDescriptionId(selectionDescriptionId)
.dialogDescriptionId(dialogDescriptionId)
.build();
// @formatter:on
}
Expand All @@ -257,9 +257,9 @@ private SingleClickOnDiagramElementTool convertContainerCreationDescription(Map<
List<String> imagePath = this.toolImageProvider.getIcon(containerCreationDescription);
List<IDiagramElementDescription> targetDescriptions = this.getParentNodeDescriptions(containerCreationDescription.getContainerMappings(), id2NodeDescriptions);
var selectModelElementVariableOpt = new SelectModelElementVariableProvider().getSelectModelElementVariable(containerCreationDescription.getVariable());
String selectionDescriptionId = null;
String dialogDescriptionId = null;
if (selectModelElementVariableOpt.isPresent()) {
selectionDescriptionId = this.identifierProvider.getIdentifier(selectModelElementVariableOpt.get());
dialogDescriptionId = this.identifierProvider.getIdentifier(selectModelElementVariableOpt.get());
}
// @formatter:off
return SingleClickOnDiagramElementTool.newSingleClickOnDiagramElementTool(id)
Expand All @@ -268,7 +268,7 @@ private SingleClickOnDiagramElementTool convertContainerCreationDescription(Map<
.handler(this.createContainerCreationHandler(interpreter, containerCreationDescription))
.targetDescriptions(targetDescriptions)
.appliesToDiagramRoot(this.atLeastOneRootMapping(containerCreationDescription.getContainerMappings()))
.selectionDescriptionId(selectionDescriptionId)
.dialogDescriptionId(dialogDescriptionId)
.build();
// @formatter:on
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @author mcharfadi
*/
public record SingleClickOnDiagramElementTool(String id, String label, List<String> iconURL, List<IDiagramElementDescription> targetDescriptions, String selectionDescriptionId,
public record SingleClickOnDiagramElementTool(String id, String label, List<String> iconURL, List<IDiagramElementDescription> targetDescriptions, String dialogDescriptionId,
boolean appliesToDiagramRoot) implements ITool {

public SingleClickOnDiagramElementTool {
Expand Down Expand Up @@ -53,7 +53,7 @@ public static final class Builder {

private List<IDiagramElementDescription> targetDescriptions;

private String selectionDescriptionId;
private String dialogDescriptionId;

private boolean appliesToDiagramRoot;

Expand Down Expand Up @@ -81,13 +81,13 @@ public Builder appliesToDiagramRoot(boolean appliesToDiagramRoot) {
return this;
}

public Builder selectionDescriptionId(String selectionDescriptionId) {
this.selectionDescriptionId = selectionDescriptionId;
public Builder dialogDescriptionId(String dialogDescriptionId) {
this.dialogDescriptionId = dialogDescriptionId;
return this;
}

public SingleClickOnDiagramElementTool build() {
return new SingleClickOnDiagramElementTool(this.id, this.label, this.iconURL, this.targetDescriptions, this.selectionDescriptionId, this.appliesToDiagramRoot);
return new SingleClickOnDiagramElementTool(this.id, this.label, this.iconURL, this.targetDescriptions, this.dialogDescriptionId, this.appliesToDiagramRoot);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ private IStatus executeTool(IEditingContext editingContext, IDiagramContext diag

if (self.isPresent()) {
VariableManager variableManager = this.populateVariableManager(editingContext, diagramContext, node, edge, self);
String selectionDescriptionId = tool.getSelectionDescriptionId();
if (selectionDescriptionId != null && selectedObjectId != null) {
var selectionDescriptionOpt = this.representationDescriptionSearchService.findById(editingContext, selectionDescriptionId);
String dialogDescriptionId = tool.getDialogDescriptionId();
if (dialogDescriptionId != null && selectedObjectId != null) {
var selectionDescriptionOpt = this.representationDescriptionSearchService.findById(editingContext, dialogDescriptionId);
var selectedObjectOpt = this.objectService.getObject(editingContext, selectedObjectId);
if (selectionDescriptionOpt.isPresent() && selectedObjectOpt.isPresent()) {
variableManager.put(SingleClickOnDiagramElementTool.SELECTED_OBJECT, selectedObjectOpt.get());
}
}
if (selectionDescriptionId == null || selectedObjectId != null) {
if (dialogDescriptionId == null || selectedObjectId != null) {
result = tool.getHandler().apply(variableManager);
Position newPosition = Position.at(startingPositionX, startingPositionY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ type SingleClickOnDiagramElementTool implements Tool {
label: String!
iconURL: [String!]!
appliesToDiagramRoot: Boolean!
selectionDescriptionId: String
dialogDescriptionId: String
targetDescriptions: [DiagramElementDescription!]!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private SingleClickOnDiagramElementTool createTool(String toolId, boolean applie
.label(TOOL_LABEL)
.iconURL(List.of(TOOL_IMAGE_URL))
.targetDescriptions(diagramElementsDescriptions)
.selectionDescriptionId(null)
.dialogDescriptionId(null)
.handler(variableManager -> new Success(ChangeKind.SEMANTIC_CHANGE, Map.of()))
.appliesToDiagramRoot(appliesToDiagramRoot)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ query getPalette($editingContextId: ID!, $representationId: ID!, $diagramElement
}
}
}
fragment ToolFields on Tool {
__typename
id
Expand All @@ -65,7 +65,7 @@ query getPalette($editingContextId: ID!, $representationId: ID!, $diagramElement
id
}
appliesToDiagramRoot
selectionDescriptionId
dialogDescriptionId
}
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class SingleClickOnDiagramElementTool implements ITool {

private boolean appliesToDiagramRoot;

private String selectionDescriptionId;
private String dialogDescriptionId;

private SingleClickOnDiagramElementTool() {
// Prevent instantiation
Expand Down Expand Up @@ -78,8 +78,8 @@ public String getLabel() {
return this.label;
}

public String getSelectionDescriptionId() {
return this.selectionDescriptionId;
public String getDialogDescriptionId() {
return this.dialogDescriptionId;
}

@Override
Expand Down Expand Up @@ -113,7 +113,7 @@ public static final class Builder {

private boolean appliesToDiagramRoot;

private String selectionDescriptionId;
private String dialogDescriptionId;

private Builder(String id) {
this.id = Objects.requireNonNull(id);
Expand Down Expand Up @@ -144,8 +144,8 @@ public Builder handler(Function<VariableManager, IStatus> handler) {
return this;
}

public Builder selectionDescriptionId(String selectionDescriptionId) {
this.selectionDescriptionId = selectionDescriptionId;
public Builder dialogDescriptionId(String dialogDescriptionId) {
this.dialogDescriptionId = dialogDescriptionId;
return this;
}

Expand All @@ -157,7 +157,7 @@ public SingleClickOnDiagramElementTool build() {
tool.handler = Objects.requireNonNull(this.handler);
tool.targetDescriptions = Objects.requireNonNull(this.targetDescriptions);
tool.appliesToDiagramRoot = this.appliesToDiagramRoot;
tool.selectionDescriptionId = this.selectionDescriptionId;
tool.dialogDescriptionId = this.dialogDescriptionId;
return tool;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo and others.
* 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
*******************************************************************************/

import { useData } from '@eclipse-sirius/sirius-components-core';
import React, { useState } from 'react';
import { DialogContextProviderState, DialogContextValue, ToolVariable } from './DialogContext.types';
import { diagramDialogContributionExtensionPoint } from './diagramDialogExtensionPoint';
import { DiagramDialogContribution, DialogComponentProps } from './diagramDialogExtensionPoint.types';

const defaultValue: DialogContextValue = {
showDialog: () => {},
};

export const DialogContext = React.createContext<DialogContextValue>(defaultValue);

export const DialogContextProvider = ({ children }) => {
const [state, setState] = useState<DialogContextProviderState>({
dialogDescriptionId: undefined,
dialogKindId: undefined,
editingContextId: undefined,
targetObjectId: undefined,
onConfirm: () => {},
open: false,
});

const { data: dialogContributions } = useData<DiagramDialogContribution[]>(diagramDialogContributionExtensionPoint);
const showDialog = (
dialogKindId: string,
editingContextId: string,
dialogDescriptionId,
targetObjectId,
onConfirm: (variables: ToolVariable[]) => void
) => {
setState({ open: true, dialogKindId, editingContextId, dialogDescriptionId, targetObjectId, onConfirm });
};

const onFinish = (toolVariables: ToolVariable[]) => {
state.onConfirm(toolVariables);
setState((prevState) => ({ ...prevState, open: false, dialogKindId: undefined }));
};

const onClose = () => {
setState((prevState) => ({ ...prevState, open: false, dialogKindId: undefined }));
};

let DialogComponent: React.ComponentType<DialogComponentProps> | undefined;
const dialogComponentProps: DialogComponentProps = {
dialogDescriptionId: state.dialogDescriptionId ?? '',
editingContextId: state.editingContextId ?? '',
targetObjectId: state.targetObjectId ?? '',
onFinish,
onClose,
};
if (state.open && state.dialogKindId) {
const dialogContribution: DiagramDialogContribution | undefined = dialogContributions.find((dialogContribution) =>
dialogContribution.canHandle(state.dialogKindId as string)
);
if (dialogContribution) {
DialogComponent = dialogContribution.component;
}
}
return (
<DialogContext.Provider value={{ showDialog }}>
{children}
{state.open && DialogComponent && <DialogComponent {...dialogComponentProps} />}
</DialogContext.Provider>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo and others.
* 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
*******************************************************************************/

export interface DialogContextValue {
showDialog: (
dialogKindId: string,
editingContextId: string,
dialogDescriptionId: string,
targetObjectId: string,
onConfirm: (variables: ToolVariable[]) => void
) => void;
}

export interface ToolVariable {
name: string;
value: string;
type: ToolVariableType;
}

export enum ToolVariableType {
String,
ObjectId,
ObjectIdArray,
}

export interface DialogContextProviderState {
open: boolean;
dialogKindId: string | undefined;
editingContextId: string | undefined;
dialogDescriptionId: string | undefined;
targetObjectId: string | undefined;
onConfirm: (variables: ToolVariable[]) => void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* 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
*******************************************************************************/
import { DataExtensionPoint } from '@eclipse-sirius/sirius-components-core';
import { DiagramDialogContribution } from './diagramDialogExtensionPoint.types';

export const diagramDialogContributionExtensionPoint: DataExtensionPoint<Array<DiagramDialogContribution>> = {
identifier: 'diagram#diagramDialogContribution',
fallback: [],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*******************************************************************************
* 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
*******************************************************************************/

import { ToolVariable } from './DialogContext.types';
export interface DiagramDialogContribution {
canHandle: (dialogKindId: string) => boolean;
component: React.ComponentType<DialogComponentProps>;
}

export interface DialogComponentProps {
editingContextId: string;
dialogDescriptionId: string;
targetObjectId: string;
onClose: () => void;
onFinish: (variables: ToolVariable[]) => void;
}
Loading

0 comments on commit 6eb29cc

Please sign in to comment.