blob: 880eda3b3de75616b9771198588013cb46f5ce6d [file] [log] [blame]
/*****************************************************************************
* Copyright (c) 2020 CEA LIST.
*
*
* All rights reserved. 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:
* Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr
*
*****************************************************************************/
package org.eclipse.papyrus.robotics.ros2.codegen.handlers;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.papyrus.designer.transformation.base.utils.ProjectManagement;
import org.eclipse.papyrus.designer.transformation.core.transformations.ExecuteTransformationChain;
import org.eclipse.papyrus.designer.transformation.profile.Transformation.ExecuteTrafoChain;
import org.eclipse.papyrus.robotics.ros2.codegen.commands.PrepareCodegenCmd;
import org.eclipse.papyrus.uml.diagram.common.handlers.CmdHandler;
import org.eclipse.papyrus.uml.tools.utils.PackageUtil;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Package;
@SuppressWarnings("nls")
public class RewriteCDTHandler extends CmdHandler {
protected ExecuteTrafoChain chain;
protected static boolean cmdActive;
// assure that project only gets rewritten once
protected static Map<String, Boolean> rewriteMap;
@Override
public Object execute(ExecutionEvent arg0) throws ExecutionException {
updateSelectedEObject();
if (!(selectedEObject instanceof Classifier) || GenerateCodeHandler.isRunning()) {
return null;
}
Package pkg = PackageUtil.getRootPackage((Class) selectedEObject);
PrepareCodegenCmd prepareCmd = new PrepareCodegenCmd(pkg);
if (prepareCmd.prepare()) {
IProject project = ProjectManagement.getCurrentProject();
Job job = new Job("Generate ROS code") {
@Override
public IStatus run(IProgressMonitor monitor) {
// execute the task ...
rewriteMap = new HashMap<String, Boolean>();
cmdActive = true;
try {
new ExecuteTransformationChain(pkg, project).executeTransformation(monitor, 0);
}
finally {
prepareCmd.undo();
cmdActive = false;
}
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
}
return null;
}
public static boolean isCmdActive() {
return cmdActive;
}
public static boolean rewriteProject(String projectName) {
if (cmdActive) {
if (!rewriteMap.containsKey(projectName)) {
rewriteMap.put(projectName, true);
return true;
}
}
return false;
}
}