Skip to content

Commit

Permalink
#2763 Add migration contributor for DNode with auto-size enabled
Browse files Browse the repository at this point in the history
A new migration contributor has been added to update width and height of
DNodes with Square style when auto-size has been enabled in their
mapping.

Signed-off-by: Glenn Plouhinec <[email protected]>
  • Loading branch information
GlennPlou authored and pdulth committed Jan 25, 2024
1 parent 487d63a commit 9e91227
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<!--
Copyright (c) 2006, 2022 THALES GLOBAL SERVICES and others.
Copyright (c) 2006, 2024 THALES GLOBAL SERVICES and others.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -198,6 +198,9 @@
<migrationContributor
class="org.polarsys.capella.core.data.migration.aird.ImageDependenciesMigrationContributor">
</migrationContributor>
<migrationContributor
class="org.polarsys.capella.core.data.migration.aird.AutoSizeSquareStyleMigrationContributor">
</migrationContributor>

</extension>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2022 THALES GLOBAL SERVICES.
* Copyright (c) 2006, 2024 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -138,8 +138,9 @@ public class MigrationConstants {
public static final String MIGRATION_KIND__FCDDIAGRAM = "MIGRATION_KIND__FCDDIAGRAM";
public static final String MIGRATION_KIND__VIEWPOINT = "MIGRATION_KIND__VIEWPOINT";
public static final String MIGRATION_KIND__IMAGE_DEPENDENCIES = "MIGRATION_KIND__IMAGE_DEPENDENCIES";
public static final String MIGRATION_KIND__AUTOSIZE_SQUARE_STYLE = "MIGRATION_KIND__AUTOSIZE_SQUARE_STYLE";

public static final String[] DEFAULT_KIND_ORDER = { MIGRATION_KIND__CHECK_MISSING_VP, MIGRATION_KIND__PATTERN,
MIGRATION_KIND__SEMANTIC, MIGRATION_KIND__DIAGRAM, MIGRATION_KIND__FCDDIAGRAM, MIGRATION_KIND__AFM,
MIGRATION_KIND__VIEWPOINT, MIGRATION_KIND__SCF, MIGRATION_KIND__IMAGE_DEPENDENCIES };
MIGRATION_KIND__VIEWPOINT, MIGRATION_KIND__SCF, MIGRATION_KIND__IMAGE_DEPENDENCIES, MIGRATION_KIND__AUTOSIZE_SQUARE_STYLE };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*******************************************************************************
* Copyright (c) 2022 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.data.migration.aird;

import java.text.MessageFormat;
import java.util.List;
import java.util.Optional;
import java.util.Spliterators;
import java.util.stream.StreamSupport;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
import org.eclipse.sirius.common.tools.api.interpreter.EvaluationException;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
import org.eclipse.sirius.diagram.DNode;
import org.eclipse.sirius.diagram.LabelPosition;
import org.eclipse.sirius.diagram.Square;
import org.eclipse.sirius.diagram.description.style.SquareDescription;
import org.eclipse.sirius.viewpoint.DAnalysis;
import org.eclipse.sirius.viewpoint.DRepresentation;
import org.eclipse.sirius.viewpoint.DRepresentationDescriptor;
import org.polarsys.capella.core.data.migration.Activator;
import org.polarsys.capella.core.data.migration.MigrationConstants;
import org.polarsys.capella.core.data.migration.MigrationRunnable;
import org.polarsys.capella.core.data.migration.context.MigrationContext;
import org.polarsys.capella.core.data.migration.contribution.Messages;

/**
* Migration contributor that will open the session and update width and height value of DNodes with Square style when
* auto-size has been enabled in their mapping; auto-size is enabled when SquareDescription width == -1 and height ==
* -1. If auto-size is enabled for these nodes, then width and height of the Square style should be updated with -1
* value, and the width/height of the DNode should be updated with the value of the SizeComputationExpression.
*
* @author Glenn Plouhinec
*/
public class AutoSizeSquareStyleMigrationContributor extends AirdMigrationContributor {

@Override
public MigrationRunnable getRunnable(IFile file) {
return new AirdMigrationRunnable(file) {
@Override
public String getName() {
return org.polarsys.capella.core.data.migration.aird.Messages.MigrationAction_AutoSizeSquareStyleMigration;
}

@Override
public IStatus run(MigrationContext context, boolean checkVersion) {
IFile airdFile = getFile();
URI airdURI = URI.createPlatformResourceURI(airdFile.getFullPath().toString(), true);
Session session = SessionManager.INSTANCE.getSession(airdURI, new NullProgressMonitor());
if (session != null) {
session.open(new NullProgressMonitor());

session.getTransactionalEditingDomain().getCommandStack().execute(new RecordingCommand(session.getTransactionalEditingDomain()) {
@Override
protected void doExecute() {
updateSquareStyle(session);
}
});
session.save(new NullProgressMonitor());
session.close(new NullProgressMonitor());
}
return Status.OK_STATUS;
}
};
}

private void updateSquareStyle(Session session) {
final Optional<DAnalysis> sharedMainDAnalysis = session.getSharedMainDAnalysis();
if (sharedMainDAnalysis.isPresent()) {
DAnalysis dAnalysis = sharedMainDAnalysis.get();
List<DNode> allDNodeWithAutoSizedSquareStyle = getAllDNodeWithAutoSizeSquareStyle(dAnalysis);
for (DNode dNode : allDNodeWithAutoSizedSquareStyle) {
if (dNode.getOwnedStyle() instanceof Square squareStyle && dNode.getActualMapping().getStyle() instanceof SquareDescription squareDesc) {
if (squareStyle.getWidth() != -1 || squareStyle.getHeight() != -1) {
squareStyle.setWidth(-1);
squareStyle.setHeight(-1);
}
Integer computedSCE = getSizeComputationExpressionValue(dNode, session.getInterpreter(), squareDesc.getSizeComputationExpression());
if (computedSCE != null) {
dNode.setHeight(computedSCE);
dNode.setWidth(computedSCE);
}
}
}
}
}

private List<DNode> getAllDNodeWithAutoSizeSquareStyle(DAnalysis dAnalysis) {
List<DRepresentationDescriptor> repdesc = dAnalysis.getOwnedViews().stream() //
.flatMap(dView -> dView.getOwnedRepresentationDescriptors().stream()) //
.toList();
List<DRepresentation> allRepresentations = repdesc.stream() //
.map(DRepresentationDescriptor::getRepresentation) //
.toList();
List<DNode> allDNodes = allRepresentations.stream().flatMap(rep -> StreamSupport.stream( //
Spliterators.spliteratorUnknownSize(rep.eAllContents(), 0), false)) //
.filter(DNode.class::isInstance) //
.map(DNode.class::cast) //
.toList();
List<DNode> allDNodeWithAutoSizedSquareStyle = allDNodes.stream() //
.filter(dnode -> dnode.getOwnedStyle() instanceof Square ownedStyle //
&& dnode.getActualMapping().getStyle() instanceof SquareDescription squareDesc //
&& squareDesc.getWidth() == -1 && squareDesc.getHeight() == -1 //
&& ownedStyle.getLabelPosition().getValue() == LabelPosition.NODE) //
.toList();
return allDNodeWithAutoSizedSquareStyle;
}

private Integer getSizeComputationExpressionValue(DNode dNode, IInterpreter interpreter, String sizeComputationExpression) {
Integer result = null;
if (sizeComputationExpression != null && !sizeComputationExpression.isBlank() && interpreter != null)
try {
result = interpreter.evaluateInteger(dNode, sizeComputationExpression);
} catch (EvaluationException e) {
String errorMsg = MessageFormat.format(Messages.MigrationAction_AutoSizeSquareNodes_sizeComputationExpressionEvaluationError, sizeComputationExpression, dNode.getName(),
dNode.getParentDiagram().getName(), dNode.getActualMapping().getName());
Activator.getDefault().getLog().error(errorMsg, e);
}
return result;
}

public String getKind() {
return MigrationConstants.MIGRATION_KIND__AUTOSIZE_SQUARE_STYLE;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2020 THALES GLOBAL SERVICES.
* Copyright (c) 2006, 2024 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -21,6 +21,7 @@
public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.polarsys.capella.core.data.migration.aird.messages"; //$NON-NLS-1$

public static String MigrationAction_AutoSizeSquareStyleMigration;
public static String MigrationAction_DiagramMigration;
public static String MigrationAction_ImageProjectDependencies;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#===============================================================================
# Copyright (c) 2006, 2020 THALES GLOBAL SERVICES.
# Copyright (c) 2006, 2024 THALES GLOBAL SERVICES.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -10,5 +10,6 @@
# Contributors:
# Thales - initial API and implementation
#===============================================================================
MigrationAction_AutoSizeSquareStyleMigration=Auto-size Square style migration
MigrationAction_DiagramMigration=Diagrams migration
MigrationAction_ImageProjectDependencies=Image project dependencies migration
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2023 THALES GLOBAL SERVICES.
* Copyright (c) 2006, 2024 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -20,6 +20,7 @@
*/
public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.polarsys.capella.core.data.migration.contribution.messages"; //$NON-NLS-1$
public static String MigrationAction_AutoSizeSquareNodes_sizeComputationExpressionEvaluationError;
public static String MigrationAction_ConfirmationDialog_Title;
public static String MigrationAction_ConfirmationDialog_Message;
public static String MigrationAction_ConfirmationDialog_ToggleMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#===============================================================================
# Copyright (c) 2006, 2023 THALES GLOBAL SERVICES.
# Copyright (c) 2006, 2024 THALES GLOBAL SERVICES.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -10,6 +10,7 @@
# Contributors:
# Thales - initial API and implementation
#===============================================================================
MigrationAction_AutoSizeSquareNodes_sizeComputationExpressionEvaluationError=Failed to evaluate sizeComputationExpression ''{0}'' for node ''{1}'' in diagram ''{2}'' with mapping ''{3}''.
MigrationAction_ConfirmationDialog_Title=Migration of selected models
MigrationAction_ConfirmationDialog_Message=It is recommended to backup the models before a migration.\nClick Ok to start the migration.
MigrationAction_ConfirmationDialog_ToggleMessage=Backup models before the migration
Expand Down

0 comments on commit 9e91227

Please sign in to comment.