Add siblingNodes

hello all,

I have a question regarding inserting a sibling. I want to add siblingNodes to the treeNode via buttons in the node. But I have not been able to do this until now.

I have already tried the following:

  • Using the contribution of the parent to call in addition to the child’s own contribution to send the command through the parent. However, it doesn’t work to link 2 contributions to a view.

  • Sending the command via get and set, using the parent to get the data to execute the task. However, I don’t know where to run the task without having to open this node.

Is there anyone who is familiar with this and can give me a hint how to do this properly?

Kind regards,
Zoë

Your first attempt is the correct one. Here is a snippet of the method called when pressing a button on my child node:

image
reassign root as itself, then create a program node of the desired type (in this case a straight move). Then call parent.insertChildAfter(root, programNode) to insert the sibling node after the child that made the call. The service will construct its own view just like any other node creation.

You’ll just need to make sure you’re correctly assigning the TreeNode “parent” when the node was created initially.

Thankyou again for the quick reaction @eric.feldmann
I feel like I’m really missing the basics of java so sorry for the maybe easy questions… but I have no idea how I should do this…

I’ve tried different ways to attach different things to TreeNode “parent”: parentContribution, parentService just as a variable, but also as
this.parent = ( ParentContribution) apiProvider.getProgramAPI().getProgramModel().getRootTreeNode(this);

But it doesn’t work and it doesn’t feel like i am on the right track with that…

The important thing for assigning a parent node is that it must be done from the parent contribution originally. I’m not exactly sure why, but Polyscope is DESIGNED to prevent you from accessing the parent node from the child. The API has very little capability in managing the tree structure, as you’ll find the more you dig into it. It’s the biggest shortcoming in my opinion.

That said, all you have to do is create a button on the PARENT view that adds a child. Take another look at the “onStraightMoveAdded()” function I posted above, as this is what I call when I click the button on the parent as well. Except I add one additional line:

((URCapProgramNode) root.locateDescendantTreeNode(programNode).getProgramNode()).getAs(MeltonChild.class).setParent(root);

“MeltonChild” is just my custom API class name. Substitute that with whatever you’ve named your class. So essentially, from the PARENT contribution, I’ve added a child node, and I pass a reference to MYSELF into it. That’s how you establish the link.

Then in the child node, the “setParent(TreeNode parent)” method is just
public void setParent(TreeNode root) { this.parent = root; }

1 Like

Thank you for your quick reply.

I tried it your way. I still didn’t quite get it right away but it did give me an idea. I did it the second way with get/set in combination with a timer in the public contribution.

Thank you again for your help!

kind regards,

Zoë