Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » How to correctly delete Part
How to correctly delete Part [message #1802900] Mon, 18 February 2019 17:41 Go to next message
João Pedro is currently offline João PedroFriend
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 Go to previous messageGo to next message
Matthias Wienand is currently offline Matthias WienandFriend
Messages: 230
Registered: March 2015
Senior Member
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
Re: How to correctly delete Part [message #1802941 is a reply to message #1802907] Tue, 19 February 2019 11:18 Go to previous messageGo to next message
João Pedro is currently offline João PedroFriend
Messages: 52
Registered: December 2014
Member
Thanks for the answer Matthias!

Where exactly is that contentToPartMap function? I can't see any reference to it.

Regarding point 2, managed to solve it with your advice! Thanks ;)

[Updated on: Tue, 19 February 2019 17:30]

Report message to a moderator

Re: How to correctly delete Part [message #1802963 is a reply to message #1802941] Tue, 19 February 2019 18:09 Go to previous messageGo to next message
João Pedro is currently offline João PedroFriend
Messages: 52
Registered: December 2014
Member
Regarding point 1, for future reference I could do it like this:

Map<Object, IContentPart<? extends Node>> content = getHost().getViewer().getContentPartMap();
						            
	 ModelPart modelPart = (ModelPart) content.get(model);


Thank you very much Matthias!
Re: How to correctly delete Part [message #1803079 is a reply to message #1802963] Thu, 21 February 2019 21:34 Go to previous message
Matthias Wienand is currently offline Matthias WienandFriend
Messages: 230
Registered: March 2015
Senior Member
You are welcome and faster than me :-)

[Updated on: Thu, 21 February 2019 21:35]

Report message to a moderator

Previous Topic:Question for Documentation
Next Topic:Create general handler for creating/deleting parts
Goto Forum:
  


Current Time: Thu Oct 21 16:17:53 GMT 2021

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

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

Back to the top