-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1831] Integrate a new API for layout based on DiagramLayoutData with…
… a dummy implementation Bug: #1831 Signed-off-by: Pierre-Charles David <[email protected]>
- Loading branch information
Showing
4 changed files
with
142 additions
and
4 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
57 changes: 57 additions & 0 deletions
57
...src/main/java/org/eclipse/sirius/components/diagrams/layout/api/IDiagramLayoutEngine.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,57 @@ | ||
/******************************************************************************* | ||
* 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.components.diagrams.layout.api; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.sirius.components.diagrams.Diagram; | ||
import org.eclipse.sirius.components.diagrams.events.IDiagramEvent; | ||
import org.eclipse.sirius.components.diagrams.layoutdata.DiagramLayoutData; | ||
|
||
/** | ||
* Used to layout diagrams. | ||
* | ||
* @author pcdavid | ||
*/ | ||
public interface IDiagramLayoutEngine { | ||
/** | ||
* Computes the new layout to be used for a diagram and its content. | ||
* | ||
* @param diagram | ||
* the diagram's structure (nodes, edges, etc.). | ||
* @param previousLayoutData | ||
* the layout data from the previous version of the diagram. This may be empty for the initial layout or | ||
* for a "full" layout. | ||
* @param optionalEvent | ||
* if the (incremental) layout was triggered by a user operation (e.g. the invocation of a tool at a | ||
* specific position) the corresponding event with the information relevant for the layout will be | ||
* available here. | ||
* @return the layout information to be used for the new version of the diagram. | ||
*/ | ||
DiagramLayoutData layout(Diagram diagram, DiagramLayoutData previousLayoutData, Optional<IDiagramEvent> optionalDiagramEvent); | ||
|
||
/** | ||
* Implementation which does nothing, used for mocks in unit tests. | ||
* | ||
* @author pcdavid | ||
*/ | ||
class NoOp implements IDiagramLayoutEngine { | ||
|
||
@Override | ||
public DiagramLayoutData layout(Diagram diagram, DiagramLayoutData previousLayoutData, Optional<IDiagramEvent> optionalDiagramEvent) { | ||
return Optional.ofNullable(previousLayoutData).orElse(new DiagramLayoutData(Map.of(), Map.of(), Map.of())); | ||
} | ||
|
||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
.../java/org/eclipse/sirius/components/diagrams/layout/experimental/DiagramLayoutEngine.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,61 @@ | ||
/******************************************************************************* | ||
* 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.components.diagrams.layout.experimental; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.sirius.components.diagrams.Diagram; | ||
import org.eclipse.sirius.components.diagrams.Node; | ||
import org.eclipse.sirius.components.diagrams.events.IDiagramEvent; | ||
import org.eclipse.sirius.components.diagrams.events.MoveEvent; | ||
import org.eclipse.sirius.components.diagrams.layout.api.IDiagramLayoutEngine; | ||
import org.eclipse.sirius.components.diagrams.layoutdata.DiagramLayoutData; | ||
import org.eclipse.sirius.components.diagrams.layoutdata.NodeLayoutData; | ||
import org.eclipse.sirius.components.diagrams.layoutdata.Position; | ||
import org.eclipse.sirius.components.diagrams.layoutdata.Size; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* The new layout engine. | ||
* | ||
* @author pcdavid | ||
*/ | ||
@Service | ||
public class DiagramLayoutEngine implements IDiagramLayoutEngine { | ||
@Override | ||
public DiagramLayoutData layout(Diagram diagram, DiagramLayoutData previousLayoutData, Optional<IDiagramEvent> optionalDiagramEvent) { | ||
Map<String, NodeLayoutData> nodeLayoutData = new HashMap<>(); | ||
|
||
int i = 0; | ||
for (Node node : diagram.getNodes()) { | ||
String nodeId = node.getId(); | ||
NodeLayoutData defaultNodeLayout = new NodeLayoutData(nodeId, new Position(50.0 * i, 0.0), new Size(40, 40)); | ||
NodeLayoutData nodeLayout = previousLayoutData.nodeLayoutData().getOrDefault(nodeId, defaultNodeLayout); | ||
nodeLayoutData.put(nodeId, nodeLayout); | ||
i++; | ||
} | ||
|
||
optionalDiagramEvent.filter(MoveEvent.class::isInstance).map(MoveEvent.class::cast).ifPresent(moveEvent -> { | ||
if (nodeLayoutData.containsKey(moveEvent.nodeId())) { | ||
org.eclipse.sirius.components.diagrams.Position newPosition = moveEvent.newPosition(); | ||
NodeLayoutData data = nodeLayoutData.get(moveEvent.nodeId()); | ||
nodeLayoutData.put(data.id(), new NodeLayoutData(data.id(), new Position(newPosition.getX(), newPosition.getY()), data.size())); | ||
} | ||
}); | ||
|
||
return new DiagramLayoutData(nodeLayoutData, previousLayoutData.edgeLayoutData(), previousLayoutData.labelLayoutData()); | ||
} | ||
|
||
} |
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