[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.modeling.gmf] Re: how can I get the reference to the element that I have just created by executing a command?
|
Hi Begoña
May be what I can suggest, is to test your code in the run method of a
context menu implementing IObjectActionDelegate.
In that case, the domain is obtained via the selectionChanged method :
if (structuredSelection.size() == 1
&& structuredSelection.getFirstElement() instanceof DomainEditPart) {
selectedElement = (DomainEditPart) structuredSelection
.getFirstElement();
in a second step you will have to readapt your code in order to match you
requirements, but you will ensure the viewer variable is correctly set.
Here is the piece of code I've used, it's very closed to the tutorial.
cc = new CompoundCommand("Create Child");
CreateViewRequest request = CreateViewRequestFactory.getCreateShapeRequest(
XyzElementTypes.Xyz_1001, selectedElement.getDiagramPreferencesHint());
Command cmd = domainEditPart.getCommand(request);
cc.add(cmd);
selectedElement.getDiagramEditDomain().getDiagramCommandStack().execute(cc);
adapter = (IAdaptable) ((List) request.getNewObject()).get(0);
View view = (View) adapter.getAdapter(View.class);
if (view != null) {
Xyz newXyz = (Xyz)view.getElement();
reqSet = new SetRequest(selectedElement.getEditingDomain(),
newXyz, XyzPackage.eINSTANCE.getXyz_Name(), s.getName());
operation = new SetValueCommand(reqSet);
selectedElement.getDiagramEditDomain().getDiagramCommandStack().execute(new
ICommandProxy(operation));
...
Sylvain
Begoña Moros wrote:
> Hi Sylvain,
>
> thank you for your answer but I have not been able to apply the MindMap
> example to my case.
>
> Reading the MindMap tutorial, I guessed that after creating the request
> and getting the command, by this code:
>
> EditPartViewer viewer = selectedElement.getViewer();
> EditPart elementPart = (EditPart)
> viewer.getEditPartRegistry().get(topicViewAdapter.getAdapter(View.class));
>
> the elementPart variable would be referencing to the EditPart of the new
> element (the one created by executing the command).
>
> I have tryed to adapted this example to my case, but the elementPart is
> null. My code has been included in the handleNotificationEvent-method of
> the ProductRequirementEditPart-class, since a new ProductRequirement is
> created programmatically when a specific event happens. It is as follows:
>
> ...
>
> IElementType type = ParamMMElementTypes.ProductRequirement_1001;
>
>
> ViewAndElementDescriptor viewDescriptor = new
> ViewAndElementDescriptor(new CreateElementRequestAdapter(new
> CreateElementRequest(type)),
>
> Node.class,
>
> ((IHintedType) type).getSemanticHint(),
>
> this.getDiagramPreferencesHint());
>
>
> CreateViewAndElementRequest request = new
> CreateViewAndElementRequest(viewDescriptor);
>
>
> Command command = catalog.getCommand(request);
>
>
> IAdaptable viewAdapter = (IAdaptable) ((List)
> request.getNewObject()).get(0);
>
>
> this.getDiagramEditDomain().getDiagramCommandStack().execute(command);
>
>
> EditPartViewer viewer = this.getViewer();
>
> EditPart elementPart = (EditPart)
> viewer.getEditPartRegistry().get(viewAdapter.getAdapter(View.class));
>
> Do you know what is wrong in this code? Probably there is something that I
> did not understand from the tutorial.
>
> Best regards,
> Begoña
>
>
> "sylvain ladoux" <sylvain.ladoux@xxxxxxxx> escribió en el mensaje
> news:fd0qv1$5ko$1@xxxxxxxxxxxxxxxxxxxx
>> Begoa Moros wrote:
>>
>>> Hi all,
>>>
>>> after creating a new element as it is explained by tip 1 in
>>> http://wiki.eclipse.org/GMF_Tips, is it possible to get the reference to
>>> the
>>> element that has been created? I need to achieve this in order to
>>> properly initialize the new element since the default initialization it
>>> is
>>> not enough in this case.
>>>
>>> Thanks in advance for any hint!!
>>
>> Hi Begoa
>>
>> Look at Custom Actions in the MindMap tutorial.
>> http://wiki.eclipse.org/GMF_Tutorial_Part_3
>>
>> Best regards,
>> Sylvain
>>