How to implement insertChildAfter or insertChildBefore

I like to add a treenode just below the current URCAP program node. At the same tree level. Not as a child below.
Syntax:
To execute the java API function insertChildAfter, the current treenode (to use as 'add just after this treenode) is required 'but also the parent of the current tree node (to use a the parent/root to add a child to).

The current treenode is available via TreeNode root = programModel.getRootTreeNode(this);
But how can I get the parent tree node ?

Any example available ?
Thanks, Wijnand

insertChildAfter does not need the parent tree node (which is the rootTreeNode).
u only need to pass the existing ChildNode which the new node is inserted after and the new ProgramNode (not a TreeNode):

Thanks for your answer.
Documentation is:
TreeNode. insertChildAfter(TreeNode existingChildNode, ProgramNode programNode)
Inserts a child under this TreeNode directly after the existing selected child node.

It seems that you need both the TreeNode (being the 'parent’Node of the exisitingChildNode) and the existingChildNode.
My URCAP program node is the ‘existingChildNode’ after which a new programnode should be insert. But that is not the TreeNode.
Executing myTreeNode.insertChildAfter(myTreeNode,MyProgramNode) is not correct I think.

More info to make it more clear hopefully. see example.

My URCAPs program nodes are nested.
My current one is ‘MWM Basis’2’. I do like to insert an extra ‘MWM Basis’ (own URCAP programnode) between 2 and 3. The (root) TreeNode to add to is ‘MWM Basis : r1’, the childNode where to add after is ‘MWM Basis : 2’. I think, I should execute the method on ‘MWM Basis : r1’ object but I don’t have the object available now. Only the ‘MWM Basis: 2’ object.
image

Hi @wthomassen

Did you ever manage to solve this problem?? I am having the same issue, trying to add a child node at the same level as the selected node , as per your diagram above. My code looks like this:

ProgramAPI API = apiProvider.getProgramAPI();
ProgramModel model = API.getProgramModel();
ProgramNodeFactory nf = model.getProgramNodeFactory();
TreeNode root = model.getRootTreeNode(this);
try {
    root.InsertChildAfter(root, nf.createURCapProgramNode(newNodeService.class);
}
catch(TreeStructureException e) {
    e.printStackTrace();
}

Checking the terminal I get TreeStructureException: No such child com.ur.urcap.domain.program.TreeNodeImpl@2c512157.

I suspect that as you mentioned the parent of the sub tree needs to be added as the first argument of insetChildAfter but I don’t know how to give java access to it, though I suspect it is in root.getRootTreeNode(someArgument);

Hi @sam.verity-hilton.

Sadly I have not found any working solution until now.

I think one of the issues here is that you are using TreeNode root = model.getRootTreeNode(this); as the TreeNode to insert a child after. This would give you the parent node. And the URCap is only allowed to insert children to itself (it cannot create siblings to itself).

So the argument of getRootTreeNode() should be an instance of a child node of the parent you are working within.

2 Likes

Is there any examples of how to do that in any of the sample caps? Or if not could you provide a short sample, I’ve been struggling with figuring out how to get this to work.

Thanks

So if I’m correct in thinking, insertChildBefore (or after) must be called from a parent node, and specify which child the new node is to be added before/after, using @wthomassen’s example above, in the contribution class of MWM Basis:
r1
have something like (excuse the pseudo code)

TreeNode MWMBasisR1
TreeNode MWMBasis2
MWMBasisR1.insertChildAfter(MWMBasis2, new MWMBasis())

Hi, i resolved this question recently with below code:

and in the same urcapprogramnodecontribution, to implement another method below:
image

and in the parentnodecontribution of this urcapchildnode, it will need to call the child’s CustomAPI to assign parent TreeNode into.