-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1996] Add an Expand All menu item contribution to the Explorer View
Bug: #1996 Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
- Loading branch information
1 parent
c9d0a3b
commit 390180f
Showing
23 changed files
with
721 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...main/java/org/eclipse/sirius/web/services/explorer/ExplorerExpandAllTreePathProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.services.explorer; | ||
|
||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.eclipse.sirius.components.collaborative.trees.api.IExpandAllTreePathProvider; | ||
import org.eclipse.sirius.components.collaborative.trees.dto.ExpandAllTreePathInput; | ||
import org.eclipse.sirius.components.collaborative.trees.dto.ExpandAllTreePathSuccessPayload; | ||
import org.eclipse.sirius.components.collaborative.trees.dto.TreePath; | ||
import org.eclipse.sirius.components.core.api.IEditingContext; | ||
import org.eclipse.sirius.components.core.api.IObjectService; | ||
import org.eclipse.sirius.components.core.api.IPayload; | ||
import org.eclipse.sirius.components.emf.services.EditingContext; | ||
import org.eclipse.sirius.components.trees.Tree; | ||
import org.eclipse.sirius.web.services.explorer.api.IExplorerNavigationService; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Implementation of {@link IExpandAllTreePathProvider} for the Sirius Web Explorer. | ||
* | ||
* @author arichard | ||
*/ | ||
@Service | ||
public class ExplorerExpandAllTreePathProvider implements IExpandAllTreePathProvider { | ||
|
||
private final IObjectService objectService; | ||
|
||
private final IExplorerNavigationService explorerNavigationService; | ||
|
||
public ExplorerExpandAllTreePathProvider(IObjectService objectService, IExplorerNavigationService explorerNavigationService) { | ||
this.objectService = Objects.requireNonNull(objectService); | ||
this.explorerNavigationService = Objects.requireNonNull(explorerNavigationService); | ||
} | ||
|
||
@Override | ||
public boolean canHandle(Tree tree) { | ||
return tree != null && Objects.equals(ExplorerDescriptionProvider.DESCRIPTION_ID, tree.getDescriptionId()); | ||
} | ||
|
||
@Override | ||
public IPayload handle(IEditingContext editingContext, Tree tree, ExpandAllTreePathInput input) { | ||
int maxDepth = 0; | ||
String treeItemId = input.treeItemId(); | ||
Set<String> treeItemIdsToExpand = new HashSet<>(); | ||
var object = this.objectService.getObject(editingContext, treeItemId); | ||
if (object.isPresent()) { | ||
// We need to get the current depth of the tree item | ||
var itemAncestors = this.explorerNavigationService.getAncestors(editingContext, treeItemId); | ||
maxDepth = itemAncestors.size(); | ||
maxDepth = this.addAllContents(editingContext, treeItemId, maxDepth, treeItemIdsToExpand); | ||
} else { | ||
// The object may be a document | ||
var optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance) | ||
.map(EditingContext.class::cast) | ||
.map(EditingContext::getDomain); | ||
|
||
if (optionalEditingDomain.isPresent()) { | ||
var optionalResource = optionalEditingDomain.get().getResourceSet().getResources().stream() | ||
.filter(resource -> treeItemId.equals(resource.getURI().path().substring(1))) | ||
.findFirst(); | ||
if (optionalResource.isPresent()) { | ||
var contents = optionalResource.get().getContents(); | ||
if (!contents.isEmpty()) { | ||
treeItemIdsToExpand.add(treeItemId); | ||
for (var rootObject: contents) { | ||
var rootObjectId = this.objectService.getId(rootObject); | ||
var rootObjectTreePathMaxDepth = 1; | ||
maxDepth = this.addAllContents(editingContext, rootObjectId, rootObjectTreePathMaxDepth, treeItemIdsToExpand); | ||
maxDepth = Math.max(maxDepth, rootObjectTreePathMaxDepth); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return new ExpandAllTreePathSuccessPayload(input.id(), new TreePath(treeItemIdsToExpand.stream().toList(), maxDepth)); | ||
} | ||
|
||
private int addAllContents(IEditingContext editingContext, String treeItemId, int depth, Set<String> treeItemIdsToExpand) { | ||
var depthConsidered = depth; | ||
var contents = this.objectService.getContents(editingContext, treeItemId); | ||
if (!contents.isEmpty()) { | ||
treeItemIdsToExpand.add(treeItemId); | ||
|
||
for (var child: contents) { | ||
String childId = this.objectService.getId(child); | ||
treeItemIdsToExpand.add(childId); | ||
var childTreePathMaxDepth = depth + 1; | ||
childTreePathMaxDepth = this.addAllContents(editingContext, childId, childTreePathMaxDepth, treeItemIdsToExpand); | ||
depthConsidered = Math.max(depthConsidered, childTreePathMaxDepth); | ||
} | ||
} | ||
|
||
return depthConsidered; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...ces/src/main/java/org/eclipse/sirius/web/services/explorer/ExplorerNavigationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.services.explorer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.resource.Resource; | ||
import org.eclipse.sirius.components.core.api.IEditingContext; | ||
import org.eclipse.sirius.components.core.api.IObjectService; | ||
import org.eclipse.sirius.web.services.api.representations.IRepresentationService; | ||
import org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor; | ||
import org.eclipse.sirius.web.services.explorer.api.IExplorerNavigationService; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Services for the navigation through the Sirius Web Explorer. | ||
* | ||
* @author arichard | ||
*/ | ||
@Service | ||
public class ExplorerNavigationService implements IExplorerNavigationService { | ||
|
||
private final IObjectService objectService; | ||
|
||
private final IRepresentationService representationService; | ||
|
||
public ExplorerNavigationService(IObjectService objectService, IRepresentationService representationService) { | ||
this.objectService = Objects.requireNonNull(objectService); | ||
this.representationService = Objects.requireNonNull(representationService); | ||
} | ||
|
||
@Override | ||
public List<String> getAncestors(IEditingContext editingContext, String selectionEntryId) { | ||
List<String> ancestorsIds = new ArrayList<>(); | ||
|
||
var optionalRepresentation = this.representationService.getRepresentation(UUID.fromString(selectionEntryId)); | ||
var optionalSemanticObject = this.objectService.getObject(editingContext, selectionEntryId); | ||
|
||
Optional<Object> optionalObject = Optional.empty(); | ||
if (optionalRepresentation.isPresent()) { | ||
// The first parent of a representation item is the item for its targetObject. | ||
optionalObject = optionalRepresentation.map(RepresentationDescriptor::getTargetObjectId) | ||
.flatMap(objectId -> this.objectService.getObject(editingContext, objectId)); | ||
} else if (optionalSemanticObject.isPresent()) { | ||
// The first parent of a semantic object item is the item for its actual container | ||
optionalObject = optionalSemanticObject.filter(EObject.class::isInstance) | ||
.map(EObject.class::cast) | ||
.map(eObject -> Optional.<Object> ofNullable(eObject.eContainer()).orElse(eObject.eResource())); | ||
} | ||
|
||
while (optionalObject.isPresent()) { | ||
ancestorsIds.add(this.getItemId(optionalObject.get())); | ||
optionalObject = optionalObject | ||
.filter(EObject.class::isInstance) | ||
.map(EObject.class::cast) | ||
.map(eObject -> Optional.<Object>ofNullable(eObject.eContainer()).orElse(eObject.eResource())); | ||
} | ||
return ancestorsIds; | ||
} | ||
|
||
private String getItemId(Object object) { | ||
String result = null; | ||
if (object instanceof Resource) { | ||
Resource resource = (Resource) object; | ||
result = resource.getURI().path().substring(1); | ||
} else if (object instanceof EObject) { | ||
result = this.objectService.getId(object); | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.