|
Re: Papyrus Activity Diagram Creation via API [message #1821456 is a reply to message #1821189] |
Thu, 13 February 2020 11:07 |
|
Here is a code snippet that creates an activity diagram within a given activity element and adds the activity itself to the diagram
TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(activity);
RecordingCommand cmd = new RecordingCommand(editingDomain, "Create Activity Diagram", "") {
@Override
protected void doExecute() {
TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(activity);
final Resource diagramResource = NotationUtils.getNotationResourceForDiagram(activity, ted);
Diagram activityDiagram = ViewService.createDiagram(activity,"PapyrusUMLActivityDiagram", UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
activityDiagram.setName("My Activity Diagram");
activityDiagram.setElement(activity);
diagramResource.getContents().add(activityDiagram);
PapyrusDiagramStyle style =(new StyleFactoryImpl()).createPapyrusDiagramStyle();
style.setOwner(activity);
style.setDiagramKindId("org.eclipse.papyrus.uml.diagram.activity");
activityDiagram.getStyles().add(style);
String visualId = UMLVisualIDRegistry.getType(org.eclipse.papyrus.uml.diagram.activity.edit.parts.ActivityEditPart.VISUAL_ID);
String type = org.eclipse.papyrus.uml.diagram.activity.part.UMLVisualIDRegistry.getType(visualId);
Node activityNode = ViewService.createNode(activityDiagram, activity, type, PreferencesHint.USE_DEFAULTS);
}
};
editingDomain.getCommandStack().execute(cmd);
Thomas Wiman
MetaModelAgent Product Manager
|
|
|
|
Re: Papyrus Activity Diagram Creation via API [message #1822530 is a reply to message #1822455] |
Mon, 09 March 2020 14:00 |
|
Hi,
My code snippet unfortunately contained a reference to an implementation class constructor StyleFactoryImpl()
A better solution would be to exchange that line to the following:
PapyrusDiagramStyle style = org.eclipse.papyrus.infra.gmfdiag.style.StyleFactory.createPapyrusDiagramStyle()
To create model resources (.di, .uml, .notation-files) given a project you can use the following snippet which return the root element of the model:
public Package createModelResource(final IResource container, final String modelFileName) {
ArchitectureDomainManager manager = ArchitectureDomainManager.getInstance();
String contextId = manager.getDefaultArchitectureContextId();
String[] viewpointsIds = new String[2];
viewpointsIds[0] = "org.eclipse.papyrus.uml.analysis";
viewpointsIds[1] = "org.eclipse.papyrus.uml.design";
final ModelSet modelSet = new ModelSet();
ModelsReader reader = new ModelsReader(); //Standard ModelsReader for Di + UML + Notation
reader.readModel(modelSet);
String path = container.getFullPath().toOSString()+File.separator+modelFileName+".di";
URI uri = URI.createPlatformResourceURI(path, true);
modelSet.createModels(uri);
ArchitectureDescriptionUtils arc = new ArchitectureDescriptionUtils(modelSet);
Command command = arc.createNewModel(contextId, viewpointsIds);
TransactionalEditingDomain ted = modelSet.getTransactionalEditingDomain();
ted.getCommandStack().execute(command);
try {
modelSet.save(new NullProgressMonitor());
} catch (IOException e) {
e.printStackTrace();
}
//Opens the new model in an editor
IFile file = null;
if (container instanceof IFolder) {
file = ((IFolder)container).getFile(modelFileName);
} else if (container instanceof IProject) {
file = ((IProject)container).getFile(modelFileName);
}
if (file == null) {
return null;
} else {
Package rootPackage = openEditor(file);
return rootPackage;
}
}
Thomas Wiman
MetaModelAgent Product Manager
|
|
|
Re: Papyrus Activity Diagram Creation via API [message #1822751 is a reply to message #1822530] |
Thu, 12 March 2020 19:28 |
Dying Devil Messages: 3 Registered: February 2020 |
Junior Member |
|
|
Hi there,
I have put your code into a plugin project and tried to get it to run. Sadly I get a nullpointer exception when trying to get the command from the architecturedescriptionutils... What am I doing wrong?
The code is:
package modelcreator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import java.io.File;
import java.io.IOException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
import org.eclipse.papyrus.infra.architecture.ArchitectureDescriptionUtils;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
public class Activator implements BundleActivator {
private static BundleContext context;
static BundleContext getContext() {
return context;
}
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
System.out.println("Starting generation:\n");
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project = root.getProject("sysml");
if (!project.exists()) project.create(null);
if (!project.isOpen()) project.open(null);
if (!project.hasNature(JavaCore.NATURE_ID)) {
IProjectDescription description = project.getDescription();
String[] prevNatures= description.getNatureIds();
String[] newNatures= new String[prevNatures.length + 1];
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
newNatures[prevNatures.length]= JavaCore.NATURE_ID;
description.setNatureIds(newNatures);
project.setDescription(description, null);
System.out.println("Added nature...\n");
}
IFolder folder = project.getFolder(project.getName()); // project has same name as folder
IFile file = folder.getFile(project.getName()+".di"); // creation of the .di file
//IFile notation = folder.getFile(project.getName()+".notation"); // creation of the .di file
//IFile uml = folder.getFile(project.getName()+".uml"); // creation of the .di file
if (!folder.exists()) folder.create(IResource.NONE, true, null);
if (!project.isOpen()) project.open(null);
System.out.println("Project folder: " + project.getFullPath()+"\n");
System.out.println("File: " + file.getFullPath().toString()+"\n");
Package pkg = createModelResource(file, "sysml");
}
public Package createModelResource(final IResource container, final String modelFileName) {
ArchitectureDomainManager manager = ArchitectureDomainManager.getInstance();
String contextId = manager.getDefaultArchitectureContextId();
String[] viewpointsIds = new String[2];
viewpointsIds[0] = "org.eclipse.papyrus.uml.analysis";
viewpointsIds[1] = "org.eclipse.papyrus.uml.design";
final ModelSet modelSet = new ModelSet();
ModelsReader reader = new ModelsReader(); //Standard ModelsReader for Di + UML + Notation
reader.readModel(modelSet);
String path = container.getFullPath().toOSString()+File.separator+modelFileName+".di";
URI uri = URI.createPlatformResourceURI(path, true);
modelSet.createModels(uri);
ArchitectureDescriptionUtils arc = new ArchitectureDescriptionUtils(modelSet);
org.eclipse.emf.common.command.Command command = arc.createNewModel(contextId, viewpointsIds);
final TransactionalEditingDomain ted = modelSet.getTransactionalEditingDomain();
ted.getCommandStack().execute(command);
try {
modelSet.save(new NullProgressMonitor());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03638 seconds