blob: c40a184d3e555d30a1ae9770a9d65c2e614b59f2 [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 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.jface.dialogs.MessageDialog;
import org.eclipse.papyrus.designer.transformation.base.utils.ProjectManagement;
import org.eclipse.papyrus.designer.transformation.core.transformations.ExecuteTransformationChain;
import org.eclipse.papyrus.designer.transformation.core.transformations.TransformationContext;
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.swt.widgets.Display;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Package;
@SuppressWarnings("nls")
public class GenerateCodeHandler extends CmdHandler {
ExecuteTrafoChain chain;
@Override
public Object execute(ExecutionEvent arg0) throws ExecutionException {
updateSelectedEObject();
if (!(selectedEObject instanceof Classifier) || isRunning()) {
return null;
}
Package pkg = PackageUtil.getRootPackage((Classifier) 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 ...
try {
new ExecuteTransformationChain(pkg, project).executeTransformation(monitor, 0);
} finally {
prepareCmd.undo();
}
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
}
return null;
}
/**
* @return true, if code generation is already running
*/
public static boolean isRunning() {
if (TransformationContext.current != null) {
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"Code generation in progress", "A previous code generation instance is running, please wait until it finishes before starting another one");
return true;
}
return false;
}
}