How to correctly delete Part [message #1802900] |
Mon, 18 February 2019 17:41  |
João Pedro Messages: 52 Registered: December 2014 |
Member |
|
|
I'm iterating over a list of models and want to delete the corresponding ModelPart in some special cases.
2 questions about this:
1-How can I access the ModelPart within the Model? When I have the Part I can call part.getContent() to get the Model. Is there any similar method for getting the part from the model?
2-I found a way of getting the corresponding part by iterating over the Parent (root) part, which is a workaround in my opinion. However, even with the correct ModelPart I can't properly delete it. What am I doing wrong?
IRootPart<? extends Node> root = host.getRoot();
DeletionPolicy delPolicy = root.getAdapter(DeletionPolicy.class);
init(delPolicy); //I'm inside a handler
for (IVisualPart<? extends Node> a : new ArrayList<>(parent.getChildrenUnmodifiable())) {
if (a instanceof ModelPart) {
delPolicy.delete((IContentPart<? extends Node>) a);
}
}
commit(delPolicy);
[Updated on: Mon, 18 February 2019 18:53] Report message to a moderator
|
|
|
Re: How to correctly delete Part [message #1802907 is a reply to message #1802900] |
Mon, 18 February 2019 19:15   |
|
Hi Joao,
1) Each Viewer maintains a contentToPartMap where you can query the part that controls certain content.
2) Deletion logic depends on whether your ModelPart is parented at the root part or at another part:
https://github.com/eclipse/gef/blob/171559044bb8dcb7f88fd427f852efd39787978c/org.eclipse.gef.mvc.fx/src/org/eclipse/gef/mvc/fx/policies/DeletionPolicy.java#L134
if (contentPartToDelete.getParent() instanceof IRootPart) {
// remove content from viewer contents
ChangeContentsOperation changeContentsOperation = new ChangeContentsOperation(
viewer);
List<Object> newContents = new ArrayList<>(viewer.getContents());
newContents.remove(contentPartToDelete.getContent());
changeContentsOperation.setNewContents(newContents);
getRemoveContentChildrenOperation().add(changeContentsOperation);
} else {
// remove from content parent
ContentPolicy parentContentPolicy = contentPartToDelete.getParent()
.getAdapter(ContentPolicy.class);
if (parentContentPolicy != null) {
parentContentPolicy.init();
parentContentPolicy
.removeContentChild(contentPartToDelete.getContent());
ITransactionalOperation removeFromParentOperation = parentContentPolicy
.commit();
if (removeFromParentOperation != null
&& !removeFromParentOperation.isNoOp()) {
getRemoveContentChildrenOperation()
.add(removeFromParentOperation);
}
}
}
If it is parented at the root part, then a ChangeContentsOperation should be created.
Otherwise, the parent part needs to have ContentPolicy bound as an adapter, and it needs to implement doRemoveContentChild() in order to collaborate with DeletionPolicy.
Any way, you should be able to find out what is going wrong by debugging DeletionPolicy.
Best of luck,
Matthias
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01592 seconds