Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » EMF transaction for inserting a new Element in a diagram(EMF transaction and papyrus)
EMF transaction for inserting a new Element in a diagram [message #1048264] Wed, 24 April 2013 04:31 Go to next message
HERISSE Alexandre is currently offline HERISSE Alexandre
Messages: 3
Registered: April 2013
Junior Member
My question concerns the adding of a Comment or a NamedElement such as a CallBehaviour for example in an active activity diagram through an emf transaction.

I would use the following skeleton for example:

// we suppose we have access to the serviceRegistry instance
TransactionalEditingDomain domain = ServiceUtils.getInstance().getTransactionalEditingDomain(serviceRegistry);
domain.getCommandStack().execute(new RecordingCommand(domain) {
public void doExecute() {
// do the changes here
}
}

I have downloaded your Papyrus source code. However I have no idea where to search an example for adding an Element in the active editor or some code that might help me?
I have grep "RecordingCommand" but I found a lot of occurences with some "@generated code" comments. Thus I decided to ask for some help.

How to deal with my need ?


Thanks

Re: EMF transaction for inserting a new Element in a diagram [message #1048457 is a reply to message #1048264] Wed, 24 April 2013 09:47 Go to previous messageGo to next message
Christian W. Damus is currently offline Christian W. Damus
Messages: 768
Registered: July 2009
Senior Member
Hi, Alexandre,

Many (most?) such editing actions in Papyrus don't execute
RecordingCommands on the CommandStack but AbstractEMFOperations on the
IOperationHistory. They can be added to context menus using the
org.eclipse.ui.menus extension point, together with command handlers.
For an example, you might look at how the Add Child menu is implemented
in the Model Explorer.

HTH,

Christian


On 2013-04-24 12:48:39 +0000, HERISSE Alexandre said:

> My question concerns the adding of a Comment or a NamedElement such as
> a CallBehaviour for example in an active activity diagram through an
> emf transaction.
>
> I would use the following skeleton for example:
>
> // we suppose we have access to the serviceRegistry instance
> TransactionalEditingDomain domain =
> ServiceUtils.getInstance().getTransactionalEditingDomain(serviceRegistry);
>
> domain.getCommandStack().execute(new RecordingCommand(domain) {
> public void doExecute() {
> // do the changes here
> }
> }
>
> I have downloaded your Papyrus source code. However I have no idea
> where to search an example for adding an Element in the active editor
> or some code that might help me?
> I have grep "RecordingCommand" but I found a lot of occurences with
> some "@generated code" comments. Thus I decided to ask for some help.
>
> How to deal with my need ?
>
>
> Thanks
Re: EMF transaction for inserting a new Element in a diagram [message #1059902 is a reply to message #1048457] Wed, 22 May 2013 03:06 Go to previous messageGo to next message
HERISSE Alexandre is currently offline HERISSE Alexandre
Messages: 3
Registered: April 2013
Junior Member
Hi, I managed to create an EMF object (an initial node) in an activity diagram with this code :

"...
/**
*
*/
private IElementType getElementTypeToCreate() {
return UMLElementTypes.INITIAL_NODE;
}

/**
*
*/
private Command buildCommand()
{
IElementEditService provider = ElementEditServiceUtils.getCommandProvider(container);
if(provider == null) {
return UnexecutableCommand.INSTANCE;
}

// Retrieve create command from the Element Edit service
CreateElementRequest createRequest = null;
if(reference == null) {
createRequest = new CreateElementRequest(container, getElementTypeToCreate());
} else {
createRequest = new CreateElementRequest(container, getElementTypeToCreate(), reference);
}

ICommand createGMFCommand = provider.getEditCommand(createRequest);

Command emfCommand = new GMFtoEMFCommandWrapper(createGMFCommand);
return emfCommand;
}

/**
*
*/
public Object executeCommand()
{
Command creationcommand = null;

try
{
ServiceUtilsForActionHandlers util = new ServiceUtilsForActionHandlers();
creationcommand = buildCommand();

util.getTransactionalEditingDomain().getCommandStack().execute(creationcommand);

return creationcommand.getResult();

} catch (ServiceException e)
{
e.printStackTrace();
}

return null;
}
..."

However it does not appear on the diagram. I have heard that I need to create the GMF part of the object and link GMF part with EMF part.

May you indicate me how I could get some examples or some help to create the graphical part of this item ?

Thanks for your help.


Re: EMF transaction for inserting a new Element in a diagram [message #1059909 is a reply to message #1048264] Wed, 22 May 2013 03:18 Go to previous messageGo to next message
HERISSE Alexandre is currently offline HERISSE Alexandre
Messages: 3
Registered: April 2013
Junior Member
Hi, I managed to create an EMF object (an initial node) in an activity diagram with this code :

"...
/**
*
*/
private IElementType getElementTypeToCreate() {
return UMLElementTypes.INITIAL_NODE;
}

/**
*
*/
private Command buildCommand()
{
IElementEditService provider = ElementEditServiceUtils.getCommandProvider(container);
if(provider == null) {
return UnexecutableCommand.INSTANCE;
}

// Retrieve create command from the Element Edit service
CreateElementRequest createRequest = null;
if(reference == null) {
createRequest = new CreateElementRequest(container, getElementTypeToCreate());
} else {
createRequest = new CreateElementRequest(container, getElementTypeToCreate(), reference);
}

ICommand createGMFCommand = provider.getEditCommand(createRequest);

Command emfCommand = new GMFtoEMFCommandWrapper(createGMFCommand);
return emfCommand;
}

/**
*
*/
public Object executeCommand()
{
Command creationcommand = null;

try
{
ServiceUtilsForActionHandlers util = new ServiceUtilsForActionHandlers();
creationcommand = buildCommand();

util.getTransactionalEditingDomain().getCommandStack().execute(creationcommand);

return creationcommand.getResult();

} catch (ServiceException e)
{
e.printStackTrace();
}

return null;
}
..."

However it does not appear on the diagram. I have heard that I need to create the GMF part of the object and link GMF part with EMF part.

May you indicate me how I could get some examples or some help to create the graphical part of this item ?

Thanks for your help.
Re: EMF transaction for inserting a new Element in a diagram [message #1075367 is a reply to message #1059909] Mon, 29 July 2013 06:05 Go to previous messageGo to next message
Leila A is currently offline Leila A
Messages: 17
Registered: February 2013
Junior Member
Dear HERISSE Alexandre,

Thanks for your explanation!
I am trying to create an element programmatically.The code above works well with older version of Papyrus.
But the method GMFtoEMFCommandWrapper is deprecated and can't be found in the newest version.
Do you have any idea about what method I should use instead of GMFtoEMFCommandWrapper using the last version of Papyrus?

Thanks in advance! Smile
Regards,
Leila
Re: EMF transaction for inserting a new Element in a diagram [message #1075441 is a reply to message #1075367] Mon, 29 July 2013 08:43 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille Letavernier
Messages: 439
Registered: February 2011
Senior Member
Hi,

The GMFtoEMFCommandWrapper was previously duplicated in a few plug-ins. You should use the one located in org.eclipse.papyrus.infra.gmfdiag.commands (org.eclipse.papyrus.commands.GMFtoEMFCommandWrapper)

Regards,
Camille
Re: EMF transaction for inserting a new Element in a diagram [message #1076418 is a reply to message #1048264] Wed, 31 July 2013 07:13 Go to previous messageGo to next message
Leila A is currently offline Leila A
Messages: 17
Registered: February 2013
Junior Member
Dear Camille,

Thank you very much, my problem is solved now.
But I have one more problem with last version I wonder if you could help me?

I need to get the editPart. Therefore, I use the two methods below. It workes well with older version but in the newest version editor.getAdapter() in method getGraphicalViewer() returns null. Can you please let me know how I should fix it?

private static EditPartViewer getGraphicalViewer()
{
try{
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editor instanceof IMultiDiagramEditor)
{
Object viewer = editor.getAdapter(IDiagramGraphicalViewer.class);
if (viewer instanceof EditPartViewer)
return (EditPartViewer) viewer;
}
}
catch(Exception ex){}
return null;
}


public static Object getEditPart(){
try{
EditPartViewer currentViewer = getGraphicalViewer();
Map<?,?> currentRegistry = currentViewer.getEditPartRegistry();
List< ? > views = DiagramEditPartsUtil.getEObjectViews(selectedelement);
for (Object view : views)
{
if (currentRegistry.containsKey(view))
{
Object part = currentRegistry.get(view);
if (part instanceof EditPart)
return part;
}
}
}
catch(Exception ex){}
return null;
}


Thanks and regards,
Leila
Re: EMF transaction for inserting a new Element in a diagram [message #1077881 is a reply to message #1076418] Fri, 02 August 2013 05:03 Go to previous message
Leila A is currently offline Leila A
Messages: 17
Registered: February 2013
Junior Member
Hi again,

I solved the problem. If anyone has the same problem this is the solution:

IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
EditPart editpart = (EditPart) editor.getAdapter(EditPart.class);

Regards,
Leila
Previous Topic:how to create programatically an instance of given DataType in UML
Next Topic:Profile mode does not allows classes
Goto Forum:
  


Current Time: Fri Aug 22 05:52:44 EDT 2014

Powered by FUDForum. Page generated in 0.02393 seconds