Skip to content

Commit

Permalink
[4088] Change tree item context menu entries internal management
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#4088
Signed-off-by: Jerome Gout <[email protected]>
Signed-off-by: Florian ROUËNÉ <[email protected]>
  • Loading branch information
jerome-obeo authored and frouene committed Oct 15, 2024
1 parent 9e07dee commit d10f0c4
Show file tree
Hide file tree
Showing 25 changed files with 538 additions and 489 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ This will allow specifier to create images that fir perfectly in the project tem
- https://github.com/eclipse-sirius/sirius-web/issues/3826[#3826] [form] Make EMF default form support attributes with a date
- https://github.com/eclipse-sirius/sirius-web/issues/2163[#2163] [form] Make EMF default form support non changeable features
- https://github.com/eclipse-sirius/sirius-web/issues/4086[#4086] [form] Wrap widget returned by property section in a div with a specific classname
- https://github.com/eclipse-sirius/sirius-web/issues/4088[#4088] Change tree item context menu entries internal management


== v2024.9.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ private TreeDescription getModelBrowserDescription(String descriptionId, Functio
.treeItemObjectProvider(this::getTreeItemObject)
.parentObjectProvider(this::getParentObject)
.treeItemLabelProvider(this::getLabel)
.contextMenuEntries(List.of())
.build();
}

Expand Down Expand Up @@ -280,8 +279,7 @@ private StyledString getLabel(VariableManager variableManager) {
StyledString styledString = this.objectService.getStyledLabel(self);
if (!styledString.toString().isBlank()) {
return styledString;
}
else {
} else {
var kind = this.objectService.getKind(self);
label = this.urlParser.getParameterValues(kind).get(SemanticKindConstants.ENTITY_ARGUMENT).get(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public List<IRepresentationDescription> getRepresentationDescriptions(IEditingCo
.renameHandler(this::getRenameHandler)
.treeItemObjectProvider(this::getTreeItemObject)
.treeItemLabelProvider(this::getLabel)
.contextMenuEntries(List.of())
.build();
return List.of(explorerTreeDescription);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public List<IRepresentationDescription> getRepresentationDescriptions(IEditingCo
.canCreatePredicate(variableManager -> false)
.targetObjectIdProvider(variableManager -> variableManager.get(IEditingContext.EDITING_CONTEXT, IEditingContext.class).map(IEditingContext::getId).orElse(null))
.elementsProvider(this::getElements)
.contextMenuEntries(List.of())
.build();

return List.of(explorerDescription);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public List<IRepresentationDescription> getRepresentationDescriptions(IEditingCo
.deleteHandler(variableManager -> new Success())
.renameHandler((variableManager, newName) -> new Success())
.treeItemObjectProvider(this::getTreeItemObject)
.contextMenuEntries(List.of())
.build();
return List.of(treeDescription);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* 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.collaborative.trees.api;

import org.eclipse.sirius.components.collaborative.trees.dto.FetchTreeItemContextMenuEntryData;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.trees.Tree;
import org.eclipse.sirius.components.trees.TreeItem;
import org.eclipse.sirius.components.trees.description.TreeDescription;

/**
* Interface allowing to provide the data of fetch context menu entries necessary to perform the fetch action itself.
*
* @author Jerome Gout
*/
public interface IFetchTreeItemContextMenuEntryDataProvider {

boolean canHandle(TreeDescription treeDescription);

FetchTreeItemContextMenuEntryData handle(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem, String treeItemMenuContextEntryId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* 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.collaborative.trees.api;

import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.representations.IStatus;
import org.eclipse.sirius.components.trees.Tree;
import org.eclipse.sirius.components.trees.TreeItem;
import org.eclipse.sirius.components.trees.description.TreeDescription;

/**
* Interface allowing to perform single click context menu entries.
*
* @author Jerome Gout
*/
public interface ISingleClickTreeItemContextMenuEntryExecutor {

boolean canExecute(TreeDescription treeDescription);

IStatus execute(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem, String treeItemMenuContextEntryId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* 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.collaborative.trees.api;

import java.util.List;

import org.eclipse.sirius.components.collaborative.trees.dto.ITreeItemContextMenuEntry;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.trees.Tree;
import org.eclipse.sirius.components.trees.TreeItem;
import org.eclipse.sirius.components.trees.description.TreeDescription;

/**
* Interface allowing to provide context menu entries for a tree item.
*
* @author Jerome Gout
*/
public interface ITreeItemContextMenuEntryProvider {

boolean canHandle(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem);

List<ITreeItemContextMenuEntry> getTreeItemContextMenuEntries(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.trees.handlers;

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

import org.eclipse.sirius.components.collaborative.api.ChangeDescription;
import org.eclipse.sirius.components.collaborative.api.ChangeKind;
import org.eclipse.sirius.components.collaborative.api.Monitoring;
import org.eclipse.sirius.components.collaborative.trees.api.IFetchTreeItemContextMenuEntryDataProvider;
import org.eclipse.sirius.components.collaborative.trees.api.ITreeEventHandler;
import org.eclipse.sirius.components.collaborative.trees.api.ITreeInput;
import org.eclipse.sirius.components.collaborative.trees.dto.FetchTreeItemContextMenuEntryData;
Expand All @@ -28,10 +30,6 @@
import org.eclipse.sirius.components.core.api.ErrorPayload;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IPayload;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.components.trees.FetchTreeItemContextMenuEntry;
import org.eclipse.sirius.components.trees.FetchTreeItemContextMenuEntryKind;
import org.eclipse.sirius.components.trees.ITreeItemContextMenuEntry;
import org.eclipse.sirius.components.trees.Tree;
import org.eclipse.sirius.components.trees.TreeItem;
import org.eclipse.sirius.components.trees.description.TreeDescription;
Expand All @@ -56,10 +54,13 @@ public class FetchTreeItemContextMenuEntryDataEventHandler implements ITreeEvent

private final Counter counter;

private final List<IFetchTreeItemContextMenuEntryDataProvider> fetchTreeItemContextMenuEntryDataProviders;

public FetchTreeItemContextMenuEntryDataEventHandler(ICollaborativeTreeMessageService messageService, ITreeQueryService treeQueryService,
MeterRegistry meterRegistry) {
MeterRegistry meterRegistry, List<IFetchTreeItemContextMenuEntryDataProvider> fetchTreeItemContextMenuEntryDataProviders) {
this.messageService = Objects.requireNonNull(messageService);
this.treeQueryService = Objects.requireNonNull(treeQueryService);
this.fetchTreeItemContextMenuEntryDataProviders = Objects.requireNonNull(fetchTreeItemContextMenuEntryDataProviders);

this.counter = Counter.builder(Monitoring.EVENT_HANDLER)
.tag(Monitoring.NAME, this.getClass().getSimpleName())
Expand All @@ -86,21 +87,11 @@ public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDesc
if (optionalTreeItem.isPresent()) {
TreeItem treeItem = optionalTreeItem.get();

var variableManager = new VariableManager();
variableManager.put(IEditingContext.EDITING_CONTEXT, editingContext);
variableManager.put(TreeDescription.TREE, tree);
variableManager.put(TreeItem.SELECTED_TREE_ITEM, treeItem);
variableManager.put(TreeDescription.ID, input.treeItemId());
var semanticTreeItemObject = treeDescription.getTreeItemObjectProvider().apply(variableManager);
variableManager.put(VariableManager.SELF, semanticTreeItemObject);

Optional<ITreeItemContextMenuEntry> action = treeDescription.getContextMenuEntries().stream()
.filter(a -> Objects.equals(a.getId(), input.menuEntryId()))
Optional<FetchTreeItemContextMenuEntryData> metadata = this.fetchTreeItemContextMenuEntryDataProviders.stream()
.filter(provider -> provider.canHandle(treeDescription))
.map(provider -> provider.handle(editingContext, treeDescription, tree, treeItem, input.menuEntryId()))
.filter(Objects::nonNull)
.findFirst();
Optional<FetchTreeItemContextMenuEntryData> metadata = action
.filter(FetchTreeItemContextMenuEntry.class::isInstance)
.map(FetchTreeItemContextMenuEntry.class::cast)
.map(fetchTreeItemContextMenuEntry -> this.getFetchMetadata(fetchTreeItemContextMenuEntry, variableManager));
if (metadata.isPresent()) {
payload = new FetchTreeItemContextMenuEntryDataSuccessPayload(treeInput.id(), metadata.get());
}
Expand All @@ -110,11 +101,4 @@ public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDesc
changeDescriptionSink.tryEmitNext(changeDescription);
payloadSink.tryEmitValue(payload);
}

private FetchTreeItemContextMenuEntryData getFetchMetadata(FetchTreeItemContextMenuEntry fetchAction, VariableManager variableManager) {
String urlToFetch = fetchAction.getUrlToFetch().apply(variableManager);
FetchTreeItemContextMenuEntryKind fetchKind = fetchAction.getFetchKind().apply(variableManager);
return new FetchTreeItemContextMenuEntryData(urlToFetch, fetchKind);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.trees.handlers;

import java.util.List;
import java.util.Objects;

import org.eclipse.sirius.components.collaborative.api.ChangeDescription;
import org.eclipse.sirius.components.collaborative.api.ChangeKind;
import org.eclipse.sirius.components.collaborative.api.Monitoring;
import org.eclipse.sirius.components.collaborative.trees.api.ISingleClickTreeItemContextMenuEntryExecutor;
import org.eclipse.sirius.components.collaborative.trees.api.ITreeEventHandler;
import org.eclipse.sirius.components.collaborative.trees.api.ITreeInput;
import org.eclipse.sirius.components.collaborative.trees.dto.InvokeSingleClickTreeItemContextMenuEntryInput;
Expand All @@ -28,10 +30,8 @@
import org.eclipse.sirius.components.core.api.SuccessPayload;
import org.eclipse.sirius.components.representations.Failure;
import org.eclipse.sirius.components.representations.Success;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.components.trees.Tree;
import org.eclipse.sirius.components.trees.TreeItem;
import org.eclipse.sirius.components.trees.SingleClickTreeItemContextMenuEntry;
import org.eclipse.sirius.components.trees.description.TreeDescription;
import org.springframework.stereotype.Service;

Expand All @@ -54,10 +54,13 @@ public class InvokeSingleClickTreeItemContextMenuEntryEventHandler implements IT

private final Counter counter;

private final List<ISingleClickTreeItemContextMenuEntryExecutor> singleClickTreeItemContextMenuEntryExecutors;

public InvokeSingleClickTreeItemContextMenuEntryEventHandler(ICollaborativeTreeMessageService messageService, ITreeQueryService treeQueryService,
MeterRegistry meterRegistry) {
MeterRegistry meterRegistry, List<ISingleClickTreeItemContextMenuEntryExecutor> singleClickTreeItemContextMenuEntryExecutors) {
this.messageService = Objects.requireNonNull(messageService);
this.treeQueryService = Objects.requireNonNull(treeQueryService);
this.singleClickTreeItemContextMenuEntryExecutors = Objects.requireNonNull(singleClickTreeItemContextMenuEntryExecutors);

this.counter = Counter.builder(Monitoring.EVENT_HANDLER)
.tag(Monitoring.NAME, this.getClass().getSimpleName())
Expand All @@ -78,33 +81,22 @@ public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDesc
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, treeInput.representationId(), treeInput);

if (treeInput instanceof InvokeSingleClickTreeItemContextMenuEntryInput input) {

var optionalTreeItem = this.treeQueryService.findTreeItem(tree, input.treeItemId());

if (optionalTreeItem.isPresent()) {
TreeItem treeItem = optionalTreeItem.get();

VariableManager variableManager = new VariableManager();
variableManager.put(IEditingContext.EDITING_CONTEXT, editingContext);
variableManager.put(TreeItem.SELECTED_TREE_ITEM, treeItem);
variableManager.put(TreeDescription.ID, input.treeItemId());
variableManager.put(TreeDescription.TREE, tree);
var semanticTreeItemObject = treeDescription.getTreeItemObjectProvider().apply(variableManager);
variableManager.put(VariableManager.SELF, semanticTreeItemObject);

var optionalAction = treeDescription.getContextMenuEntries().stream()
.filter(action -> Objects.equals(action.getId(), input.menuEntryId()))
.filter(SingleClickTreeItemContextMenuEntry.class::isInstance)
.map(SingleClickTreeItemContextMenuEntry.class::cast)
.findFirst();
if (optionalAction.isPresent()) {
var status = optionalAction.get().getHandler().apply(variableManager);
if (status instanceof Success success) {
changeDescription = new ChangeDescription(success.getChangeKind(), treeInput.representationId(), treeInput, success.getParameters());
payload = new SuccessPayload(treeInput.id());
} else if (status instanceof Failure failure) {
payload = new ErrorPayload(treeInput.id(), failure.getMessages());
}
var status = this.singleClickTreeItemContextMenuEntryExecutors.stream()
.filter(executor -> executor.canExecute(treeDescription))
.findFirst()
.map(executor -> executor.execute(editingContext, treeDescription, tree, treeItem, input.menuEntryId()))
.orElseGet(() -> new Failure(this.messageService.noSingleClickTreeItemExecutor()));

if (status instanceof Success success) {
changeDescription = new ChangeDescription(success.getChangeKind(), treeInput.representationId(), treeInput, success.getParameters());
payload = new SuccessPayload(treeInput.id());
} else if (status instanceof Failure failure) {
payload = new ErrorPayload(treeInput.id(), failure.getMessages());
}
}
}
Expand Down
Loading

0 comments on commit d10f0c4

Please sign in to comment.