Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » Create a diagram programmatically (I want to create a diagram programmatically with CreateDiagramCommand, but it throws a ClassCastException)
Create a diagram programmatically [message #1833593] Mon, 19 October 2020 11:30 Go to next message
Yasin Gül is currently offline Yasin GülFriend
Messages: 10
Registered: September 2020
Junior Member
Hi guys,

I want to create a simple activity diagram in a package from the Model Explorer.

I get the UML-Object "ActivityDiagram_Online Shopping" from the Model Explorer (see ModelExplorer.JPG) with the function:
IStructuredSelection selection = (IStructuredSelection)
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
		EObject selectedElement = getSelectedEObject(selection);
		CreateGraphicalElements graphic = new CreateGraphicalElements();
		graphic.createSimpleDiagram(selectedElement);



The method getSelectedEObject delivers me the EObject:
public EObject getSelectedEObject(IStructuredSelection selection)
	{
		EObject selectedEObject = null;

		if (selection != null)
		{
			selectedEObject = EMFHelper.getEObject(selection.getFirstElement());
		}
		return selectedEObject;
	}



And with createSimpleDiagram() I want to create a new diagram with the CreateDiagramCommand:

public void createSimpleDiagram(EObject owner)
	{
		CreateDiagramCommand createDiagramCommand = new CreateDiagramCommand();
		CreationCommandDescriptor creationCommandDescriptor = new CreationCommandDescriptor();
		String name = "TestDiagram";
		ModelSet modelSet = new ModelSet();[code][/code]

		try
		{
			modelSet = ServiceUtilsForResource.getInstance().getModelSet(owner.eResource());
						createDiagramCommand.createDiagram(modelSet, owner, name);
		}
		catch (ServiceException e)
		{
			System.out.println(e.getLocalizedMessage());
		}
	}



By debugging I find out that in the Class AbstractPapyrusGmfCreateDiagramCommandHandler the method getCreateDiagramCommand() throws a ClassCastException. The variable ViewPrototype prototype is cast to (DiagramPrototype) prototype (see ViewPrototype_DiagramPrototype.JPG) and the following Error is displayed:
org.eclipse.core.commands.ExecutionException: Error executing 'de.seppmed.papyrus.mbtassist.plugin.commands.ConnectMBTassist': java.lang.ClassCastException: org.eclipse.papyrus.infra.viewpoints.policy.UnavailableViewPrototype cannot be cast to org.eclipse.papyrus.infra.gmfdiag.common.helper.DiagramPrototype
	at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:170)
	at org.eclipse.core.commands.Command.executeWithChecks(Command.java:488)
	at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:487)
	at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:213)
	at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:438)
	at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.handleWidgetSelection(AbstractContributionItem.java:449)
	at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.lambda$2(AbstractContributionItem.java:475)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4213)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1037)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4030)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3630)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1158)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1047)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
	at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:658)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:557)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:154)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:150)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:137)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:107)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:657)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:594)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1447)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1420)
Caused by: org.eclipse.e4.core.di.InjectionException: java.lang.ClassCastException: org.eclipse.papyrus.infra.viewpoints.policy.UnavailableViewPrototype cannot be cast to org.eclipse.papyrus.infra.gmfdiag.common.helper.DiagramPrototype
	at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:68)
	at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:319)
	at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:253)
	at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:173)
	at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:156)
	... 33 more
Caused by: java.lang.ClassCastException: org.eclipse.papyrus.infra.viewpoints.policy.UnavailableViewPrototype cannot be cast to org.eclipse.papyrus.infra.gmfdiag.common.helper.DiagramPrototype
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler$1.doExecuteWithResult(AbstractPapyrusGmfCreateDiagramCommandHandler.java:438)
	at org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand.doExecute(AbstractTransactionalCommand.java:248)
	at org.eclipse.emf.workspace.AbstractEMFOperation.execute(AbstractEMFOperation.java:150)
	at org.eclipse.gmf.runtime.common.core.command.CompositeCommand.doExecuteWithResult(CompositeCommand.java:404)
	at org.eclipse.gmf.runtime.common.core.command.AbstractCommand.execute(AbstractCommand.java:135)
	at org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper.execute(GMFtoEMFCommandWrapper.java:123)
	at org.eclipse.emf.workspace.EMFCommandOperation.doExecute(EMFCommandOperation.java:119)
	at org.eclipse.emf.workspace.AbstractEMFOperation.execute(AbstractEMFOperation.java:150)
	at org.eclipse.core.commands.operations.DefaultOperationHistory.execute(DefaultOperationHistory.java:496)
	at org.eclipse.papyrus.infra.emf.gmf.command.CheckedOperationHistory.doExecute(CheckedOperationHistory.java:208)
	at org.eclipse.papyrus.infra.emf.gmf.command.CheckedOperationHistory.execute(CheckedOperationHistory.java:197)
	at org.eclipse.papyrus.infra.emf.gmf.command.NotifyingWorkspaceCommandStack.doExecute(NotifyingWorkspaceCommandStack.java:266)
	at org.eclipse.emf.transaction.impl.AbstractTransactionalCommandStack.execute(AbstractTransactionalCommandStack.java:165)
	at org.eclipse.emf.transaction.impl.AbstractTransactionalCommandStack.execute(AbstractTransactionalCommandStack.java:219)
	at org.eclipse.papyrus.infra.emf.gmf.command.NestingNotifyingWorkspaceCommandStack.execute(NestingNotifyingWorkspaceCommandStack.java:132)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.createDiagram(AbstractPapyrusGmfCreateDiagramCommandHandler.java:358)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.createDiagram(AbstractPapyrusGmfCreateDiagramCommandHandler.java:388)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.createDiagram(AbstractPapyrusGmfCreateDiagramCommandHandler.java:342)
	at de.seppmed.papyrus.mbtassist.plugin.handlers.CreateGraphicalElements.createSimpleDiagram(CreateGraphicalElements.java:86)
	at de.seppmed.papyrus.mbtassist.plugin.connector.ConnectMBTassist.execute(ConnectMBTassist.java:56)
	at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:283)
	at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:97)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:58)
	... 37 more 


What am I doing wrong?

best regards,
Yasin
Re: Create a diagram programmatically [message #1836607 is a reply to message #1833593] Fri, 08 January 2021 21:46 Go to previous message
pere joseph rodríguez is currently offline pere joseph rodríguezFriend
Messages: 29
Registered: October 2014
Junior Member
Have a look at this papyrus incubation project/
https://git.eclipse.org/c/papyrus/org.eclipse.papyrus.incubation.git/tree/diagramtemplate/plugins/org.eclipse.papyrus.diagramtemplate/src/org/eclipse/papyrus/diagramtemplate
Previous Topic:Target input for callbehavioraction
Next Topic:From UML to SQL
Goto Forum:
  


Current Time: Wed Jan 27 17:43:54 GMT 2021

Powered by FUDForum. Page generated in 0.01399 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top