automatically add parent node when child added [message #539124] |
Wed, 09 June 2010 13:23  |
Eclipse User |
|
|
|
Hello everybody,
Lets consider a GMF editor for an xml file like:
I would like to let the user add a "c" figure directly to an "a" parent (select a "c" creation tool from the palette and put it on an "a" figure ) and the "b" intermediate node (and, of course Edit part and figure) to be automatically generated so, at the end, the model to look like the xml above.
I think it should be done in the CreateCommand class for c (CCreateCommand) but an instance of this class is not created unless trying to create a "c" inside a "b" parent, which, of course, does not exist. I tried to debug in order to find the condition for the creation of the command but I did not find it.
Thanks for any clues,
Emil
|
|
|
Re: automatically add parent node when child added [message #539579 is a reply to message #539124] |
Fri, 11 June 2010 08:17   |
Eclipse User |
|
|
|
I have performed some tests on the subject, I still did not find a way to do what I need.
Just for Test:
Lets automatically create a "C" child when a "B" element is created.
This works fine, and it is done by modifying the BCreateCommand#doExecuteWithResult() method and creating the c emf element as child of b. Works fine, we can see a "B" with a "C" child on the diagram.
Now, what I actually need, is to create a "B" parent when a "C" is created.
What I have done 'till now:
- first of al, we need to allow the creation of "C" when the user mouse goes over an "A" element. In order to do so we need to modify the ATypeItemSemanticEditPolicy#getCreateCommand() method and return a Create Command for the case of a "C" element. So, I added a condition like this:
protected Command getCreateCommand(CreateElementRequest req) {
...............................
............................
if (MyModelElementTypes.CType_3006 == req.getElementType()) {
return getGEFWrapper(new CWithParentCreateCommand(req));
}
}
Now I need to write a class CWithParentCreateCommand.
This class is identical to the BCreateCommand (the one where a C child is added).
Well. Everything goes fine: We can select the "C" creation tool now, we can trigger the create command on an "A" object, and indeed a "B" parent is automatically created for the "C" element, like we needed:
Only one thing goes wrong: the "A" figure is not updated so we cannot see the "b" and the "c" on the diagram. If we resize the "a" figure, then "b" and "c" becomes visible.
Finally, we have 2 cases:
1. We automatically add a "c" child when a "b" element is added. Everything works fine.
2. We automatically add a "b" parent when a "c" element is added: works for the model, the view is not updated.
Note: in both cases the same XXXCreateCommand class is used. In both of the cases an "A" parent will receive a "B" child. The difference is that, in the first case the request points to a "B" object while in the second case the request points to a "C".
Any clues are welcomed.
Emil.
[Updated on: Fri, 11 June 2010 08:26] by Moderator
|
|
|
|
|
Re: automatically add parent node when child added [message #540800 is a reply to message #540551] |
Thu, 17 June 2010 08:17  |
Eclipse User |
|
|
|
Thanks Olivier for your answer.
I tried your solution with no success. But the problem was not there ..
The problem was that a visual ID could not be found for a C child added for an A parent (I recall that we wanted to automatically create the intermediate B element).
The solution is a little bit tricky:
Step1: implement a BwithCCreateCommand class for the Type B which creates a a "B" with a child "C". The "doExecuteWithResult" method looks like:
protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
B newElement = DevFactory.eINSTANCE.createB();
AAAType owner = (AAAType) getElementToEdit();
owner.setB(newElement);
// ---> C creation
CType c = DevFactory.eINSTANCE.createCType();
newElement.setC(c);
// <--- CTypecreation
doConfigure(newElement, monitor, info);
((CreateElementRequest) getRequest()).setNewElement(newElement);
return CommandResult.newOKCommandResult(newElement);
}
Step 2: modify the ATypeItemSemanticEditPolicy#getCreateCommand() as explained before.
Step3.
Modify the method XXXViewProvider#createNode method.
Add a condition to the case corresponding to C.VisualID :
case CTypeEditPart.VISUAL_ID:
if (containerView.getType().equals(new Integer(ATypeEditPart.VISUAL_ID).toString()))
{
return createB_NNNN(domainElement, containerView, index,
persisted, preferencesHint);
}else
return createC_NNNN(domainElement, containerView, index,
persisted, preferencesHint);
That's it, it works now.
[Updated on: Thu, 17 June 2010 08:32] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.32906 seconds