Skip to content

Commit

Permalink
[3961] Review changes
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#3961
Signed-off-by: Jerome Gout <[email protected]>
  • Loading branch information
jerome-obeo committed Sep 10, 2024
1 parent 3ce3cf3 commit 8915d91
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.views.explorer.handlers;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.eclipse.sirius.components.collaborative.api.ChangeDescription;
import org.eclipse.sirius.components.collaborative.api.IEditingContextEventHandler;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IInput;
import org.eclipse.sirius.components.core.api.IPayload;
import org.eclipse.sirius.components.core.api.IRepresentationDescriptionSearchService;
import org.eclipse.sirius.components.trees.description.TreeDescription;
import org.eclipse.sirius.web.application.views.explorer.dto.EditingContextExplorerDescriptionsInput;
import org.eclipse.sirius.web.application.views.explorer.dto.EditingContextExplorerDescriptionsPayload;
import org.eclipse.sirius.web.application.views.explorer.dto.ExplorerDescriptionMetadata;
import org.eclipse.sirius.web.application.views.explorer.services.api.IExplorerDescriptionService;
import org.eclipse.sirius.web.application.views.explorer.services.ExplorerDescriptionProvider;
import org.eclipse.sirius.web.application.views.explorer.services.api.IExplorerDescriptionMapper;
import org.eclipse.sirius.web.application.views.explorer.services.api.IExplorerTreeDescriptionProvider;
import org.springframework.stereotype.Service;

import reactor.core.publisher.Sinks;
Expand All @@ -36,10 +42,17 @@
@Service
public class EditingContextExplorerDescriptionEventHandler implements IEditingContextEventHandler {

private final IExplorerDescriptionService explorerDescriptionService;

public EditingContextExplorerDescriptionEventHandler(IExplorerDescriptionService explorerDescriptionService) {
this.explorerDescriptionService = Objects.requireNonNull(explorerDescriptionService);
private final IExplorerDescriptionMapper explorerDescriptionMapper;

private final IRepresentationDescriptionSearchService representationDescriptionSearchService;

private final List<IExplorerTreeDescriptionProvider> explorerTreeDescriptionProviders;

public EditingContextExplorerDescriptionEventHandler(IExplorerDescriptionMapper explorerDescriptionMapper, IRepresentationDescriptionSearchService representationDescriptionSearchService, List<IExplorerTreeDescriptionProvider> explorerTreeDescriptionProviders) {
this.explorerDescriptionMapper = Objects.requireNonNull(explorerDescriptionMapper);
this.representationDescriptionSearchService = representationDescriptionSearchService;
this.explorerTreeDescriptionProviders = explorerTreeDescriptionProviders;
}

@Override
Expand All @@ -51,8 +64,23 @@ public boolean canHandle(IEditingContext editingContext, IInput input) {
public void handle(Sinks.One<IPayload> payloadSink, Sinks.Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
List<ExplorerDescriptionMetadata> explorerDescriptions = List.of();
if (input instanceof EditingContextExplorerDescriptionsInput) {
explorerDescriptions = this.explorerDescriptionService.findAll(editingContext);
explorerDescriptions = this.findAllExplorerTreeDescriptions(editingContext);
}
payloadSink.tryEmitValue(new EditingContextExplorerDescriptionsPayload(input.id(), explorerDescriptions));
}

List<ExplorerDescriptionMetadata> findAllExplorerTreeDescriptions(IEditingContext editingContext) {
var optionalDefaultExplorerDescription = this.representationDescriptionSearchService.findById(editingContext, ExplorerDescriptionProvider.DESCRIPTION_ID)
.filter(TreeDescription.class::isInstance)
.map(TreeDescription.class::cast);

var explorers = this.explorerTreeDescriptionProviders.stream()
.flatMap(provider -> provider.getDescriptions(editingContext).stream())
.map(this.explorerDescriptionMapper::toDTO)
.sorted(Comparator.comparing(ExplorerDescriptionMetadata::label))
.collect(Collectors.toList());

optionalDefaultExplorerDescription.ifPresent(treeDescription -> explorers.add(this.explorerDescriptionMapper.toDTO(treeDescription)));
return explorers;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@
import java.util.Optional;

import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.sirius.components.collaborative.api.IRepresentationImageProvider;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.core.api.IRepresentationMetadataSearchService;
import org.eclipse.sirius.components.core.api.IURLParser;
import org.eclipse.sirius.components.core.api.IEditingContextRepresentationDescriptionProvider;
import org.eclipse.sirius.components.domain.Domain;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.eclipse.sirius.components.representations.GetOrCreateRandomIdProvider;
import org.eclipse.sirius.components.representations.IRepresentationDescription;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.components.trees.description.TreeDescription;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataSearchService;
import org.springframework.stereotype.Service;

/**
Expand All @@ -37,41 +33,30 @@
* @author Jerome Gout
*/
@Service
public class DomainExplorerRepresentationDescriptionProvider extends DomainTreeRepresentationDescriptionProvider {
public class DomainExplorerRepresentationDescriptionProvider implements IEditingContextRepresentationDescriptionProvider {

public static final String DESCRIPTION_ID = "domain_explorer_description";

public DomainExplorerRepresentationDescriptionProvider(IObjectService objectService, IURLParser urlParser, List<IRepresentationImageProvider> representationImageProviders,
IRepresentationDataSearchService representationDataSearchService, IRepresentationMetadataSearchService representationMetadataSearchService) {
super(objectService, urlParser, representationImageProviders, representationDataSearchService, representationMetadataSearchService);
private final DomainTreeRepresentationDescriptionProvider domainTreeRepresentationDescriptionProvider;

public DomainExplorerRepresentationDescriptionProvider(DomainTreeRepresentationDescriptionProvider domainTreeRepresentationDescriptionProvider) {
this.domainTreeRepresentationDescriptionProvider = domainTreeRepresentationDescriptionProvider;
}


@Override
public List<IRepresentationDescription> getRepresentationDescriptions(IEditingContext editingContext) {
var result = super.getRepresentationDescriptions(editingContext);
var result = this.domainTreeRepresentationDescriptionProvider.getRepresentationDescriptions(editingContext);
TreeDescription domainTreeDescription = (TreeDescription) result.get(0);

var explorerDescription = TreeDescription.newTreeDescription(DESCRIPTION_ID)
.label("Domain explorer")
.idProvider(new GetOrCreateRandomIdProvider())
.canCreatePredicate(variableManager -> false)
.treeItemIdProvider(domainTreeDescription.getTreeItemIdProvider())
.kindProvider(domainTreeDescription.getKindProvider())
.labelProvider(domainTreeDescription.getLabelProvider())
.targetObjectIdProvider(variableManager -> variableManager.get(IEditingContext.EDITING_CONTEXT, IEditingContext.class).map(IEditingContext::getId).orElse(null))
.iconURLProvider(domainTreeDescription.getIconURLProvider())
.editableProvider(domainTreeDescription.getEditableProvider())
.deletableProvider(domainTreeDescription.getDeletableProvider())
.selectableProvider(domainTreeDescription.getSelectableProvider())
.elementsProvider(this::getElements)
.hasChildrenProvider(domainTreeDescription.getHasChildrenProvider())
.childrenProvider(domainTreeDescription.getChildrenProvider())
.parentObjectProvider(domainTreeDescription.getParentObjectProvider())
.deleteHandler(domainTreeDescription.getDeleteHandler())
.renameHandler(domainTreeDescription.getRenameHandler())
.treeItemObjectProvider(domainTreeDescription.getTreeItemObjectProvider())
.build();
var explorerDescription = TreeDescription.newTreeDescription(domainTreeDescription)
.id(DESCRIPTION_ID)
.label("Domain explorer")
.idProvider(new GetOrCreateRandomIdProvider())
.canCreatePredicate(variableManager -> false)
.targetObjectIdProvider(variableManager -> variableManager.get(IEditingContext.EDITING_CONTEXT, IEditingContext.class).map(IEditingContext::getId).orElse(null))
.elementsProvider(this::getElements)
.build();

return List.of(explorerDescription);
}
Expand All @@ -83,7 +68,7 @@ private List<Domain> getElements(VariableManager variableManager) {
.map(IEMFEditingContext.class::cast)
.map(IEMFEditingContext::getDomain)
.map(EditingDomain::getResourceSet);
return optionalResourceSet.map(resourceSet -> resourceSet.getResources().stream()
return optionalResourceSet.map(resourceSet -> resourceSet.getResources().stream()
.flatMap(res -> res.getContents().stream())
.filter(Domain.class::isInstance)
.map(Domain.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public DomainExplorerTreeDescriptionProvider(IRepresentationDescriptionSearchSer

@Override
public List<TreeDescription> getDescriptions(IEditingContext editingContext) {
var optionalDomainExplorerDescription = this.getDomainExplorerTreeDescrption(editingContext);
var optionalDomainExplorerDescription = this.getDomainExplorerTreeDescription(editingContext);

if (optionalDomainExplorerDescription.isPresent()) {
return List.of(optionalDomainExplorerDescription.get());
}
return List.of();
}

private Optional<TreeDescription> getDomainExplorerTreeDescrption(IEditingContext editingContext) {
private Optional<TreeDescription> getDomainExplorerTreeDescription(IEditingContext editingContext) {
if (this.isContainingDomainElement(editingContext)) {
return this.representationDescriptionSearchService
.findById(editingContext, DomainExplorerRepresentationDescriptionProvider.DESCRIPTION_ID)
Expand All @@ -61,10 +61,8 @@ private Optional<TreeDescription> getDomainExplorerTreeDescrption(IEditingContex
private boolean isContainingDomainElement(IEditingContext editingContext) {
if (editingContext instanceof IEMFEditingContext emfEditingContext) {
return emfEditingContext.getDomain().getResourceSet().getResources().stream()
.filter(res -> res.getContents().size() > 0)
.map(res -> res.getContents().get(0))
.filter(Domain.class::isInstance)
.count() > 0;
.flatMap(res -> res.getContents().stream())
.anyMatch(Domain.class::isInstance);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import { WorkbenchViewComponentProps } from '@eclipse-sirius/sirius-components-core';
import {
FilterBar,
TreeDescription,
TreeFilter,
TreeToolBar,
TreeToolBarContext,
Expand All @@ -28,6 +27,7 @@ import { ExplorerViewState } from './ExplorerView.types';
import { useExplorerDescriptions } from './useExplorerDescriptions';
import { useExplorerSubscription } from './useExplorerSubscription';
import { DEFAULT_EXPLORER_DESCRIPTION_ID } from './useExplorerSubscription.types';
import { TreeDescriptionsMenu } from './TreeDescriptionsMenu';

const useStyles = makeStyles()((theme: Theme) => ({
treeView: {
Expand All @@ -52,8 +52,7 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
treeFilters: [],
expanded: [],
maxDepth: 1,
treeDescriptions: [],
activeTreeDescription: null,
activeTreeDescriptionId: DEFAULT_EXPLORER_DESCRIPTION_ID,
};
const [state, setState] = useState<ExplorerViewState>(initialState);
const treeToolBarContributionComponents = useContext<TreeToolBarContextValue>(TreeToolBarContext).map(
Expand All @@ -63,15 +62,15 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon

const { tree } = useExplorerSubscription(
editingContextId,
state.activeTreeDescription?.id,
state.activeTreeDescriptionId,
activeTreeFilterIds,
state.expanded,
state.maxDepth
);

const { loading, treeFilters } = useTreeFilters(editingContextId, 'explorer://');

const { loading: explorersLoading, explorerDescriptions } = useExplorerDescriptions(editingContextId);
const { explorerDescriptions } = useExplorerDescriptions(editingContextId);

useEffect(() => {
if (!loading) {
Expand All @@ -84,25 +83,6 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
}
}, [loading, treeFilters]);

const getDefaultExplorerDescription = (all: TreeDescription[]): TreeDescription => {
const defaultIndex = all.findIndex((td) => td.id === DEFAULT_EXPLORER_DESCRIPTION_ID);
return all[defaultIndex];
};

useEffect(() => {
if (!explorersLoading) {
const allExplorerDescriptions: TreeDescription[] = explorerDescriptions.map((gqlExplorerDescription) => ({
id: gqlExplorerDescription.id,
label: gqlExplorerDescription.label,
}));
setState((prevState) => ({
...prevState,
treeDescriptions: allExplorerDescriptions,
activeTreeDescription: getDefaultExplorerDescription(allExplorerDescriptions),
}));
}
}, [explorersLoading, explorerDescriptions]);

const treeElement = useRef<HTMLDivElement>(null);
useEffect(() => {
const downHandler = (event) => {
Expand Down Expand Up @@ -154,6 +134,16 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
setState((prevState) => ({ ...prevState, expanded, maxDepth }));
};

const treeDescriptionSelector: JSX.Element = explorerDescriptions.length > 1 && (
<TreeDescriptionsMenu
treeDescriptions={explorerDescriptions}
activeTreeDescriptionId={state.activeTreeDescriptionId}
onTreeDescriptionChange={(treeDescription) =>
setState((prevState) => ({ ...prevState, activeTreeDescriptionId: treeDescription.id }))
}
/>
);

return (
<div className={styles.treeView} ref={treeElement}>
<TreeToolBar
Expand All @@ -171,13 +161,9 @@ export const ExplorerView = ({ editingContextId, readOnly }: WorkbenchViewCompon
return { ...prevState, treeFilters };
})
}
treeToolBarContributionComponents={treeToolBarContributionComponents}
treeDescriptions={state.treeDescriptions}
activeTreeDescription={state.activeTreeDescription}
onTreeDescriptionChange={(treeDescription) =>
setState((prevState) => ({ ...prevState, activeTreeDescription: treeDescription }))
}
/>
treeToolBarContributionComponents={treeToolBarContributionComponents}>
{treeDescriptionSelector}
</TreeToolBar>
<div className={styles.treeContent}>
{filterBar}
<TreeView
Expand Down
Loading

0 comments on commit 8915d91

Please sign in to comment.