Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » How to programmaticly change an EMF attribute value outside an edit part
How to programmaticly change an EMF attribute value outside an edit part [message #517611] Mon, 01 March 2010 14:29 Go to next message
Marco is currently offline MarcoFriend
Messages: 27
Registered: July 2009
Junior Member
Hello GMF-Experts,

I would like to change the value of an EMF attribute YYY programmaticly.

I already tried it the following way (inside the edit part of the model
element, intended to modify):

getEditingDomain().getCommandStack().execute(
SetCommand.create(getEditingDomain(),
((ShapeImpl) getModel()).getElement(),
XXXPackage.eINSTANCE.getYYY(),
"new value of attribute"));

It works fine, but the problem is, I need to have a reference to my
EditPart.

Is there any way to change an EMF attribute value outside an edit part,
without having a reference on it? The attribute YYY should be changed in
the element, which is currently selected in the diagram editor.

It would be very kind, if you had any advice for me. Thank you very much!

Regards,
Marco
Re: How to programmaticly change an EMF attribute value outside an edit part [message #517616 is a reply to message #517611] Mon, 01 March 2010 14:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
Marco,

Comments below.

Marco wrote:
> Hello GMF-Experts,
>
> I would like to change the value of an EMF attribute YYY programmaticly.
>
> I already tried it the following way (inside the edit part of the
> model element, intended to modify):
>
> getEditingDomain().getCommandStack().execute(
> SetCommand.create(getEditingDomain(),
> ((ShapeImpl) getModel()).getElement(),
> XXXPackage.eINSTANCE.getYYY(),
> "new value of attribute"));
>
> It works fine, but the problem is, I need to have a reference to my
> EditPart.
Where's the reference to the edit part?
AdapterFactoryEditingDomain.getEditingDomainFor(...) probably helps if
you're referring to the getEditingDomain() call.
>
> Is there any way to change an EMF attribute value outside an edit
> part, without having a reference on it? The attribute YYY should be
> changed in the element, which is currently selected in the diagram
> editor.
>
> It would be very kind, if you had any advice for me. Thank you very much!
>
> Regards,
> Marco


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to programmaticly change an EMF attribute value outside an edit part [message #517639 is a reply to message #517616] Mon, 01 March 2010 15:09 Go to previous messageGo to next message
Marco is currently offline MarcoFriend
Messages: 27
Registered: July 2009
Junior Member
Hello Ed,

thank you for your advice. But how can I get a reference to the EObject,
which belongs to the currently selected element in the diagram editor?

Regards,
Marco


Ed Merks schrieb:
> Marco,
>
> Comments below.
>
> Marco wrote:
>> Hello GMF-Experts,
>>
>> I would like to change the value of an EMF attribute YYY programmaticly.
>>
>> I already tried it the following way (inside the edit part of the
>> model element, intended to modify):
>>
>> getEditingDomain().getCommandStack().execute(
>> SetCommand.create(getEditingDomain(),
>> ((ShapeImpl) getModel()).getElement(),
>> XXXPackage.eINSTANCE.getYYY(),
>> "new value of attribute"));
>>
>> It works fine, but the problem is, I need to have a reference to my
>> EditPart.
> Where's the reference to the edit part?
> AdapterFactoryEditingDomain.getEditingDomainFor(...) probably helps if
> you're referring to the getEditingDomain() call.
>>
>> Is there any way to change an EMF attribute value outside an edit
>> part, without having a reference on it? The attribute YYY should be
>> changed in the element, which is currently selected in the diagram
>> editor.
>>
>> It would be very kind, if you had any advice for me. Thank you very much!
>>
>> Regards,
>> Marco
Re: How to programmaticly change an EMF attribute value outside an edit part [message #517652 is a reply to message #517639] Mon, 01 March 2010 10:56 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
Marco,

I can't answer a question like this without context. Obviously one
needs to start from somewhere with something, but I don't know where you
are nor what you've got your hands on at that point. Typically Actions
will be passed the selection upon which they operate and hence directly
have access to the object that's needed.


Marco wrote:
> Hello Ed,
>
> thank you for your advice. But how can I get a reference to the
> EObject, which belongs to the currently selected element in the
> diagram editor?
>
> Regards,
> Marco
>
>
> Ed Merks schrieb:
>> Marco,
>>
>> Comments below.
>>
>> Marco wrote:
>>> Hello GMF-Experts,
>>>
>>> I would like to change the value of an EMF attribute YYY
>>> programmaticly.
>>>
>>> I already tried it the following way (inside the edit part of the
>>> model element, intended to modify):
>>>
>>> getEditingDomain().getCommandStack().execute(
>>> SetCommand.create(getEditingDomain(),
>>> ((ShapeImpl) getModel()).getElement(),
>>> XXXPackage.eINSTANCE.getYYY(),
>>> "new value of attribute"));
>>>
>>> It works fine, but the problem is, I need to have a reference to my
>>> EditPart.
>> Where's the reference to the edit part?
>> AdapterFactoryEditingDomain.getEditingDomainFor(...) probably helps
>> if you're referring to the getEditingDomain() call.
>>>
>>> Is there any way to change an EMF attribute value outside an edit
>>> part, without having a reference on it? The attribute YYY should be
>>> changed in the element, which is currently selected in the diagram
>>> editor.
>>>
>>> It would be very kind, if you had any advice for me. Thank you very
>>> much!
>>>
>>> Regards,
>>> Marco


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to programmaticly change an EMF attribute value outside an edit part [message #517664 is a reply to message #517652] Mon, 01 March 2010 11:16 Go to previous messageGo to next message
Marco is currently offline MarcoFriend
Messages: 27
Registered: July 2009
Junior Member
Hello Ed,

I will try to precise my use case:

1. When the user selects an element in the diagram editor, its
EMF-Attributes should be loaded and visualized in a "Custom View" (like
in the Properties view).

2. When the user performs a special action in that "Custom View", then
some of the EMF-Attributes of the currently selected element in the
diagram view should be changed (like I already mentioned in my first post).

The "Custom View", from which the change-operation is trigerred, has no
explicit reference to the EObject or the EditPart. I'm searching for a
way, to globaly access the desired element through the workbench.

Can you give me any advice, please?

Kind regards,
Marco


Ed Merks schrieb:
> Marco,
>
> I can't answer a question like this without context. Obviously one
> needs to start from somewhere with something, but I don't know where you
> are nor what you've got your hands on at that point. Typically Actions
> will be passed the selection upon which they operate and hence directly
> have access to the object that's needed.
>
>
> Marco wrote:
>> Hello Ed,
>>
>> thank you for your advice. But how can I get a reference to the
>> EObject, which belongs to the currently selected element in the
>> diagram editor?
>>
>> Regards,
>> Marco
>>
>>
>> Ed Merks schrieb:
>>> Marco,
>>>
>>> Comments below.
>>>
>>> Marco wrote:
>>>> Hello GMF-Experts,
>>>>
>>>> I would like to change the value of an EMF attribute YYY
>>>> programmaticly.
>>>>
>>>> I already tried it the following way (inside the edit part of the
>>>> model element, intended to modify):
>>>>
>>>> getEditingDomain().getCommandStack().execute(
>>>> SetCommand.create(getEditingDomain(),
>>>> ((ShapeImpl) getModel()).getElement(),
>>>> XXXPackage.eINSTANCE.getYYY(),
>>>> "new value of attribute"));
>>>>
>>>> It works fine, but the problem is, I need to have a reference to my
>>>> EditPart.
>>> Where's the reference to the edit part?
>>> AdapterFactoryEditingDomain.getEditingDomainFor(...) probably helps
>>> if you're referring to the getEditingDomain() call.
>>>>
>>>> Is there any way to change an EMF attribute value outside an edit
>>>> part, without having a reference on it? The attribute YYY should be
>>>> changed in the element, which is currently selected in the diagram
>>>> editor.
>>>>
>>>> It would be very kind, if you had any advice for me. Thank you very
>>>> much!
>>>>
>>>> Regards,
>>>> Marco
Re: How to programmaticly change an EMF attribute value outside an edit part [message #517687 is a reply to message #517664] Mon, 01 March 2010 17:25 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
Marco,

Comments below.

Marco wrote:
> Hello Ed,
>
> I will try to precise my use case:
>
> 1. When the user selects an element in the diagram editor, its
> EMF-Attributes should be loaded and visualized in a "Custom View"
> (like in the Properties view).
So the selection of the editor should be used as input to the view.
>
>
> 2. When the user performs a special action in that "Custom View", then
> some of the EMF-Attributes of the currently selected element in the
> diagram view should be changed (like I already mentioned in my first
> post).
So you know the input to the view and that these actions work on that.
>
> The "Custom View", from which the change-operation is trigerred, has
> no explicit reference to the EObject or the EditPart.
How then does it show attributes of something?
> I'm searching for a way, to globaly access the desired element through
> the workbench.
I don't think there is a general way to do that. I suspect your not on
the right track
>
> Can you give me any advice, please?
What else in the IDE is similar to what you're trying to do?
>
> Kind regards,
> Marco
>
>
> Ed Merks schrieb:
>> Marco,
>>
>> I can't answer a question like this without context. Obviously one
>> needs to start from somewhere with something, but I don't know where
>> you are nor what you've got your hands on at that point. Typically
>> Actions will be passed the selection upon which they operate and
>> hence directly have access to the object that's needed.
>>
>>
>> Marco wrote:
>>> Hello Ed,
>>>
>>> thank you for your advice. But how can I get a reference to the
>>> EObject, which belongs to the currently selected element in the
>>> diagram editor?
>>>
>>> Regards,
>>> Marco
>>>
>>>
>>> Ed Merks schrieb:
>>>> Marco,
>>>>
>>>> Comments below.
>>>>
>>>> Marco wrote:
>>>>> Hello GMF-Experts,
>>>>>
>>>>> I would like to change the value of an EMF attribute YYY
>>>>> programmaticly.
>>>>>
>>>>> I already tried it the following way (inside the edit part of the
>>>>> model element, intended to modify):
>>>>>
>>>>> getEditingDomain().getCommandStack().execute(
>>>>> SetCommand.create(getEditingDomain(),
>>>>> ((ShapeImpl) getModel()).getElement(),
>>>>> XXXPackage.eINSTANCE.getYYY(),
>>>>> "new value of attribute"));
>>>>>
>>>>> It works fine, but the problem is, I need to have a reference to
>>>>> my EditPart.
>>>> Where's the reference to the edit part?
>>>> AdapterFactoryEditingDomain.getEditingDomainFor(...) probably helps
>>>> if you're referring to the getEditingDomain() call.
>>>>>
>>>>> Is there any way to change an EMF attribute value outside an edit
>>>>> part, without having a reference on it? The attribute YYY should
>>>>> be changed in the element, which is currently selected in the
>>>>> diagram editor.
>>>>>
>>>>> It would be very kind, if you had any advice for me. Thank you
>>>>> very much!
>>>>>
>>>>> Regards,
>>>>> Marco


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to programmaticly change an EMF attribute value outside an edit part [message #517798 is a reply to message #517639] Tue, 02 March 2010 02:46 Go to previous messageGo to next message
Sven Krause is currently offline Sven KrauseFriend
Messages: 119
Registered: July 2009
Senior Member
Hi Marco,

xyzDiagramEditor.getDiagramGraphicalViewer().getSelection() or
xyzDiagramEditor.getDiagramGraphicalViewer().getSelectedEdit Parts() does
the trick.

Sven
[Solved] How to programmaticly change an EMF attribute value outside an edit part [message #517820 is a reply to message #517798] Tue, 02 March 2010 09:31 Go to previous messageGo to next message
Marco is currently offline MarcoFriend
Messages: 27
Registered: July 2009
Junior Member
Thank you for all your replys. That was exactly the solution I was
searching for.

Here is a little example scenario:


// Get active editor in workbench
IEditorPart activeEditor = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage().getActiveEditor( );

// Check, if active editor is a diagram editor
if (activeEditor instanceof DiagramEditor) {

// Get selected edit part in active editor
List<EditPart> selectedEditParts = ((DiagramEditor) activeEditor)
.getDiagramGraphicalViewer().getSelectedEditParts();

// Get the primary selected edit part in selection
EditPart editPart = selectedEditParts.get(0);

// Only handle Image2EditParts
if (editPart instanceof Image2EditPart) {
Image2EditPart image2EditPart = (Image2EditPart) editPart;

/*
* Change the value of the property 'file' of the selected
* Image2EditPart to 'bla'
*/
image2EditPart.getEditingDomain().getCommandStack().execute(
SetCommand.create(image2EditPart.getEditingDomain(),
((ShapeImpl) image2EditPart.getModel())
.getElement(),
GadModelPackage.eINSTANCE.getImage_File(),
"bla"));
}
}

Best regards,
Marco


Sven Krause schrieb:
> Hi Marco,
>
> xyzDiagramEditor.getDiagramGraphicalViewer().getSelection() or
> xyzDiagramEditor.getDiagramGraphicalViewer().getSelectedEdit Parts() does
> the trick.
>
> Sven
Re: [Solved] How to programmaticly change an EMF attribute value outside an edit part [message #517838 is a reply to message #517820] Tue, 02 March 2010 09:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
Marco,

I wonder why you cast to ShapeImpl rather than to Shape?


Marco wrote:
> Thank you for all your replys. That was exactly the solution I was
> searching for.
>
> Here is a little example scenario:
>
>
> // Get active editor in workbench
> IEditorPart activeEditor = PlatformUI.getWorkbench()
> .getActiveWorkbenchWindow().getActivePage().getActiveEditor( );
>
> // Check, if active editor is a diagram editor
> if (activeEditor instanceof DiagramEditor) {
>
> // Get selected edit part in active editor
> List<EditPart> selectedEditParts = ((DiagramEditor) activeEditor)
> .getDiagramGraphicalViewer().getSelectedEditParts();
>
> // Get the primary selected edit part in selection
> EditPart editPart = selectedEditParts.get(0);
>
> // Only handle Image2EditParts
> if (editPart instanceof Image2EditPart) {
> Image2EditPart image2EditPart = (Image2EditPart) editPart;
>
> /*
> * Change the value of the property 'file' of the selected
> * Image2EditPart to 'bla'
> */
> image2EditPart.getEditingDomain().getCommandStack().execute(
> SetCommand.create(image2EditPart.getEditingDomain(),
> ((ShapeImpl) image2EditPart.getModel())
> .getElement(),
> GadModelPackage.eINSTANCE.getImage_File(),
> "bla"));
> }
> }
>
> Best regards,
> Marco
>
>
> Sven Krause schrieb:
>> Hi Marco,
>>
>> xyzDiagramEditor.getDiagramGraphicalViewer().getSelection() or
>> xyzDiagramEditor.getDiagramGraphicalViewer().getSelectedEdit Parts()
>> does the trick.
>>
>> Sven


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [Solved] How to programmaticly change an EMF attribute value outside an edit part [message #517839 is a reply to message #517838] Tue, 02 March 2010 09:58 Go to previous messageGo to next message
Marco is currently offline MarcoFriend
Messages: 27
Registered: July 2009
Junior Member
Because Shape does not declare a getElement() method.

Ed Merks schrieb:
> Marco,
>
> I wonder why you cast to ShapeImpl rather than to Shape?
>
>
> Marco wrote:
>> Thank you for all your replys. That was exactly the solution I was
>> searching for.
>>
>> Here is a little example scenario:
>>
>>
>> // Get active editor in workbench
>> IEditorPart activeEditor = PlatformUI.getWorkbench()
>> .getActiveWorkbenchWindow().getActivePage().getActiveEditor( );
>>
>> // Check, if active editor is a diagram editor
>> if (activeEditor instanceof DiagramEditor) {
>>
>> // Get selected edit part in active editor
>> List<EditPart> selectedEditParts = ((DiagramEditor) activeEditor)
>> .getDiagramGraphicalViewer().getSelectedEditParts();
>>
>> // Get the primary selected edit part in selection
>> EditPart editPart = selectedEditParts.get(0);
>>
>> // Only handle Image2EditParts
>> if (editPart instanceof Image2EditPart) {
>> Image2EditPart image2EditPart = (Image2EditPart) editPart;
>>
>> /*
>> * Change the value of the property 'file' of the selected
>> * Image2EditPart to 'bla'
>> */
>> image2EditPart.getEditingDomain().getCommandStack().execute(
>> SetCommand.create(image2EditPart.getEditingDomain(),
>> ((ShapeImpl) image2EditPart.getModel())
>> .getElement(),
>> GadModelPackage.eINSTANCE.getImage_File(),
>> "bla"));
>> }
>> }
>>
>> Best regards,
>> Marco
>>
>>
>> Sven Krause schrieb:
>>> Hi Marco,
>>>
>>> xyzDiagramEditor.getDiagramGraphicalViewer().getSelection() or
>>> xyzDiagramEditor.getDiagramGraphicalViewer().getSelectedEdit Parts()
>>> does the trick.
>>>
>>> Sven
Re: How to programmaticly change an EMF attribute value outside an edit part [message #517847 is a reply to message #517611] Tue, 02 March 2010 10:36 Go to previous messageGo to next message
Aurélien Pupier is currently offline Aurélien PupierFriend
Messages: 637
Registered: July 2009
Location: Grenoble, FRANCE
Senior Member

Hi,

you cast to View which is the interface that declare the getElement() method.

Smile

Regards,


Aurélien Pupier - Red Hat
Senior Software Engineer in Fuse Tooling team
Re: How to programmaticly change an EMF attribute value outside an edit part [message #517857 is a reply to message #517847] Tue, 02 March 2010 10:55 Go to previous messageGo to next message
Marco is currently offline MarcoFriend
Messages: 27
Registered: July 2009
Junior Member
Okay, changed my cast from ShapeImpl to the basic interface View.

Regards,
Marco

Aurelien Pupier schrieb:
> Hi,
>
> you cast to View which is the interface that declare the getElement()
> method.
>
> :)
>
> Regards,
Re: [Solved] How to programmaticly change an EMF attribute value outside an edit part [message #517863 is a reply to message #517839] Tue, 02 March 2010 11:35 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
Marco,

Shape inherits from Node which inherits from View which declares the
getElement method. Neither Shape nor ShapeImpl directly declare/define
the getElement method; in both cases it's inherited from View or
ViewImpl. So this can't be the real reason...


Marco wrote:
> Because Shape does not declare a getElement() method.
>
> Ed Merks schrieb:
>> Marco,
>>
>> I wonder why you cast to ShapeImpl rather than to Shape?
>>
>>
>> Marco wrote:
>>> Thank you for all your replys. That was exactly the solution I was
>>> searching for.
>>>
>>> Here is a little example scenario:
>>>
>>>
>>> // Get active editor in workbench
>>> IEditorPart activeEditor = PlatformUI.getWorkbench()
>>> .getActiveWorkbenchWindow().getActivePage().getActiveEditor( );
>>>
>>> // Check, if active editor is a diagram editor
>>> if (activeEditor instanceof DiagramEditor) {
>>>
>>> // Get selected edit part in active editor
>>> List<EditPart> selectedEditParts = ((DiagramEditor) activeEditor)
>>> .getDiagramGraphicalViewer().getSelectedEditParts();
>>>
>>> // Get the primary selected edit part in selection
>>> EditPart editPart = selectedEditParts.get(0);
>>>
>>> // Only handle Image2EditParts
>>> if (editPart instanceof Image2EditPart) {
>>> Image2EditPart image2EditPart = (Image2EditPart) editPart;
>>>
>>> /*
>>> * Change the value of the property 'file' of the selected
>>> * Image2EditPart to 'bla'
>>> */
>>> image2EditPart.getEditingDomain().getCommandStack().execute(
>>> SetCommand.create(image2EditPart.getEditingDomain(),
>>> ((ShapeImpl) image2EditPart.getModel())
>>> .getElement(),
>>> GadModelPackage.eINSTANCE.getImage_File(),
>>> "bla"));
>>> }
>>> }
>>>
>>> Best regards,
>>> Marco
>>>
>>>
>>> Sven Krause schrieb:
>>>> Hi Marco,
>>>>
>>>> xyzDiagramEditor.getDiagramGraphicalViewer().getSelection() or
>>>> xyzDiagramEditor.getDiagramGraphicalViewer().getSelectedEdit Parts()
>>>> does the trick.
>>>>
>>>> Sven


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Show only the name in the "model elements selection" dialog box
Next Topic:How to enable Cut / Copy / Paste???
Goto Forum:
  


Current Time: Tue Mar 19 02:03:04 GMT 2024

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

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

Back to the top