Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[394] Add variables "diagram" and "view" during direct edit execution #395

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/org.eclipse.sirius.diagram/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ HideDDiagramElementLabel_hideLabels = Hide labels
ImagePathConstraint_absolutePathError= (Image) The absolute path {0} is not supported. Element: {1}
ImagePathConstraint_relativePathError=(Image) The image with the path {0} can not be found for the element {1}
ImagePathConstraint_workspaceImagePathError=(Image) The image with the path {0} can not be found for the diagram element {1} in the diagram {2}
InitGlobalDirectEditVariablesTask_setLabel=Init Acceleo interpreter with variables for direct edit
InitGlobalDirectEditVariablesTask_unsetLabel=Init Acceleo interpreter by unsetting variables for direct edit
NodeFilter_notNullErrorMsg = The node must not be null
NodeIdentifier_parametersErrorMsg = semantic & mappingName are mandatories
NodeMappingHelper_nodeCreationErrorMsg = Error creating nodes : domain class is not defined on a mapping
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.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.diagram.business.internal.helper.task;

import org.eclipse.sirius.business.api.helper.task.AbstractCommandTask;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreterSiriusVariables;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.tools.api.Messages;
import org.eclipse.sirius.viewpoint.DRepresentationElement;

/**
* A Task to initialize the global variables before executing a direct edit tools. The same variables that are set
* {@link org.eclipse.sirius.diagram.business.internal.metamodel.helper.DiagramElementMappingHelper#computeInputLabelOfDirectEditLabel(org.eclipse.sirius.diagram.DDiagramElement, DDiagram, org.eclipse.sirius.diagram.description.tool.DirectEditLabel, IInterpreter)}.</BR>
* It can also be used to unset these variables (with the dedicated constructor).
*
* @author Laurent Redor
*/
public class InitGlobalDirectEditVariablesTask extends AbstractCommandTask {

private IInterpreter interpreter;

private DDiagram dDiagram;

private DRepresentationElement dRepresentationElement;

private boolean unset;

/**
* Default constructor to set the variables.
*
* @param interpreter
* the interpreter
* @param dDiagram
* The representation containing the <code>dRepresentationElement</code>
* @param dRepresentationElement
* The current <code>dRepresentationElement</code> edited
*/
public InitGlobalDirectEditVariablesTask(final IInterpreter interpreter, final DDiagram dDiagram, final DRepresentationElement dRepresentationElement) {
this.interpreter = interpreter;
this.dDiagram = dDiagram;
this.dRepresentationElement = dRepresentationElement;
}

/**
* Default constructor to unset the variables.
*
* @param interpreter
* the interpreter
* @param dDiagram
* The representation containing the <code>dRepresentationElement</code>
* @param dRepresentationElement
* The current <code>dRepresentationElement</code> edited
* @param set
*
*/
public InitGlobalDirectEditVariablesTask(final IInterpreter interpreter) {
this.interpreter = interpreter;
this.unset = true;
}

/**
* {@inheritDoc}
*
* @see org.eclipse.sirius.business.api.helper.task.ICommandTask#execute()
*/
@Override
public void execute() {
if (unset) {
interpreter.unSetVariable(IInterpreterSiriusVariables.DIAGRAM);
interpreter.unSetVariable(IInterpreterSiriusVariables.VIEW);
} else {
interpreter.setVariable(IInterpreterSiriusVariables.DIAGRAM, dDiagram);
interpreter.setVariable(IInterpreterSiriusVariables.VIEW, dRepresentationElement);
}
}

/**
* {@inheritDoc}
*
* @see org.eclipse.sirius.business.api.helper.task.ICommandTask#getLabel()
*/
@Override
public String getLabel() {
// TODO
if (unset) {
return Messages.InitGlobalDirectEditVariablesTask_unsetLabel;
} else {
return Messages.InitGlobalDirectEditVariablesTask_setLabel;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ public final class Messages {
@TranslatableMessage
public static String ImagePathConstraint_workspaceImagePathError;

@TranslatableMessage
public static String InitGlobalDirectEditVariablesTask_setLabel;

@TranslatableMessage
public static String InitGlobalDirectEditVariablesTask_unsetLabel;

@TranslatableMessage
public static String NodeFilter_notNullErrorMsg;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2018 THALES GLOBAL SERVICES.
* Copyright (c) 2011, 2024 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -20,6 +20,7 @@
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.business.api.query.EObjectQuery;
import org.eclipse.sirius.diagram.business.internal.helper.task.InitGlobalDirectEditVariablesTask;
import org.eclipse.sirius.diagram.description.tool.DirectEditLabel;
import org.eclipse.sirius.diagram.tools.api.Messages;
import org.eclipse.sirius.ext.base.Option;
Expand Down Expand Up @@ -64,14 +65,17 @@ public Command buildCommand() {
messageFormat = directEditTool.getMask().getMask();
}
IInterpreter interpreter = InterpreterUtil.getInterpreter(repElement);
result.getTasks().add(new InitInterpreterFromParsedVariableTask(interpreter, messageFormat, newValue));

Option<DDiagram> parentDiagram = getDDiagram();
if (parentDiagram.some() && repElement.getTarget() != null && directEditTool.getInitialOperation() != null) {
result.getTasks().add(new InitInterpreterFromParsedVariableTask(interpreter, messageFormat, newValue));
result.getTasks().add(new InitGlobalDirectEditVariablesTask(interpreter, parentDiagram.get(), repElement));

final ICommandTask operations = taskHelper.buildTaskFromModelOperation(parentDiagram.get(), repElement.getTarget(), directEditTool.getInitialOperation().getFirstModelOperations());
result.getTasks().add(operations);
}
addPostOperationTasks(result, interpreter);
result.getTasks().add(new InitGlobalDirectEditVariablesTask(interpreter));
result.getTasks().add(new InitInterpreterFromParsedVariableTask(interpreter, messageFormat, newValue, true));
return result;
}
return UnexecutableCommand.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2023 THALES GLOBAL SERVICES and others.
* Copyright (c) 2007, 2024 THALES GLOBAL SERVICES and others.
* 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
Expand Down Expand Up @@ -306,6 +306,8 @@ private SiriusCommand buildCommandFromCell(final DCell currentCell, final TableT
addMaskInitTask(result, interpreterContext, mask, newValue);
// Creation of the tasks to execute the tool
addOperationTask(result, currentCell, interpreterContext, tool.getFirstModelOperation());
// Clear mask variables
addMaskResetTask(result, interpreterContext, mask, newValue);

return result;
}
Expand Down Expand Up @@ -338,6 +340,9 @@ private SiriusCommand buildCommandFromIntersection(final DLine currentLine, fina
// Creation of the tasks to execute the tool
addOperationTask(result, currentLine, interpreterContext, tool.getFirstModelOperation());

// Clear mask variables
addMaskResetTask(result, interpreterContext, tool.getMask(), newValue);

return result;
}

Expand Down Expand Up @@ -570,6 +575,13 @@ private void addMaskInitTask(SiriusCommand result, EObject context, EditMaskVari
}
}

private void addMaskResetTask(SiriusCommand result, EObject context, EditMaskVariables mask, final Object newValue) {
if (mask != null && mask.getMask() != null) {
// Reset all mask variables initialized before
result.getTasks().add(new InitInterpreterFromParsedVariableTask2(InterpreterUtil.getInterpreter(context), mask.getMask(), newValue, true));
}
}

private void addOperationTask(SiriusCommand result, DSemanticDecorator tableElement, EObject context, ModelOperation operation) {
DTable table = TableHelper.getTable(tableElement);
result.getTasks().add(commandTaskHelper.buildTaskFromModelOperation(table, context, operation));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2014 THALES GLOBAL SERVICES.
* Copyright (c) 2010, 2024 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -23,19 +23,17 @@
import junit.framework.TestCase;

/**
* Test the parsing of mask variables and the variable duplication for integer
* variables names.
* Test the parsing of mask variables and the variable duplication for integer variables names.
*
* This is done by two tasks : InitInterpreterFromParsedVariableTask and
* InitInterpreterFromParsedVariableTask2.
* This is done by two tasks : InitInterpreterFromParsedVariableTask and InitInterpreterFromParsedVariableTask2.
*
* This test checks the parsing and the int name variable duplication for
* different masks and values.
* This test checks the parsing and the int name variable duplication for different masks and values.
*
* The duplication consists in creating an argX named variable for each X
* variable: 0 and arg0 for example. It is required by some interpreters which
* cannot have int variables like Acceleo3. Acceleo2 was supporting it thanks to
* its prefix $.
* The duplication consists in creating an argX named variable for each X variable: 0 and arg0 for example. It is
* required by some interpreters which cannot have int variables like Acceleo3. Acceleo2 was supporting it thanks to its
* prefix $.
*
* This test also checks that the dedicated task correctly cleans the interpreter.
*
* @author mporhel
*
Expand Down Expand Up @@ -205,6 +203,10 @@ private void doTestParsedVariableTask(String mask, String message, Map<Integer,
new InitInterpreterFromParsedVariableTask(interpreter, mask, message).execute();

doTestVariables(expectedVariableValues);

// Call the task that should clean the interpreter
new InitInterpreterFromParsedVariableTask(interpreter, mask, message, true).execute();
doTestVariables(expectedVariableValues, true);
}

private void doTestParsedVariableTask2(String mask, String message, Map<Integer, String> expectedVariableValues) {
Expand All @@ -214,30 +216,41 @@ private void doTestParsedVariableTask2(String mask, String message, Map<Integer,
new InitInterpreterFromParsedVariableTask2(interpreter, mask, message).execute();

doTestVariables(expectedVariableValues);

// Call the task that should clean the interpreter
new InitInterpreterFromParsedVariableTask2(interpreter, mask, message, true).execute();
doTestVariables(expectedVariableValues, true);
}

private void doTestVariables(Map<Integer, String> expectedVariableValues) {
doTestVariables(expectedVariableValues, false);
}

private void doTestVariables(Map<Integer, String> expectedVariableValues, boolean clean) {

// Check the global variable number, we should have duplication for all
// int named variables.
assertEquals("The int named variables should be duplicated, example : 0 and arg0.", expectedVariableValues.size() * 2, interpreter.getVariables().size());

for (Integer varIntName : expectedVariableValues.keySet()) {
String intKey = varIntName.toString();
String argKey = "arg" + intKey;

assertTrue("The variable named " + intKey + " is not present in the variable list.", interpreter.getVariables().containsKey(intKey));
assertTrue("The variable named " + argKey + " is not present in the variable list.", interpreter.getVariables().containsKey(argKey));

// The test deals with String value.
String intKeyValue = (String) interpreter.getVariable(intKey);
String argKeyValue = (String) interpreter.getVariable(argKey);

String expectedValue = expectedVariableValues.get(varIntName);
assertEquals("The variable named " + intKey + " does not have the expected value.", intKeyValue, expectedValue);
assertEquals("The variable named " + argKeyValue + " does not have the expected value.", argKeyValue, expectedValue);
if (clean) {
assertEquals("No variable should be contains in the interpreter after direct edit.", 0, interpreter.getVariables().size());
} else {
assertEquals("The int named variables should be duplicated, example : 0 and arg0.", expectedVariableValues.size() * 2, interpreter.getVariables().size());

for (Integer varIntName : expectedVariableValues.keySet()) {
String intKey = varIntName.toString();
String argKey = "arg" + intKey;

assertTrue("The variable named " + intKey + " is not present in the variable list.", interpreter.getVariables().containsKey(intKey));
assertTrue("The variable named " + argKey + " is not present in the variable list.", interpreter.getVariables().containsKey(argKey));

// The test deals with String value.
String intKeyValue = (String) interpreter.getVariable(intKey);
String argKeyValue = (String) interpreter.getVariable(argKey);

String expectedValue = expectedVariableValues.get(varIntName);
assertEquals("The variable named " + intKey + " does not have the expected value.", intKeyValue, expectedValue);
assertEquals("The variable named " + argKeyValue + " does not have the expected value.", argKeyValue, expectedValue);
}
}

}

@Override
Expand Down
Loading