cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to add a node in a TreeByNestingTableColumn

Former Member
0 Kudos

Hello All,

I'm still having problems doing this!

I followed the tutorial step by step on how to Integrate a Tree structure in Web Dynpro table.

I'm using NWDS 7.0.18 on EP 7.0

The tutorial can be found [here|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/de59f7c2-0401-0010-f08d-8659cef543ce]

I changed mine around a little bit to use Levels.java instead of Catalog.java.

I got it working fine. Now the next step I want to do is add a toolbar button that will add a node in the tree structure. So I created the relevant button with an action onActionInsertNode and its signature its the same as the signature for Loading children nodes


public void onActionInsertNode(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent,
 com.sjm.wdp.wdp.IPrivateTreeAppCompView.ILEVELElement element )
  {
    //@@begin onActionInsertNode(ServerEvent)
    addNewLevel(element.nodeCHILD_LEVEL(), element.getID()); // Null pointer exception here
    //@@end
  }

I created a new method called addNewLevel to add a single element which is the new node. It looks like this


public void addNewLevel( com.sjm.wdp.wdp.IPrivateTreeAppCompView.ILEVELNode node,
 java.lang.String parentId )
  {
    //@@begin addNewLevel()
	IPrivateTreeAppCompView.ILEVELElement newLevelElement;
	if(parentId.equals(Levels.getParentId(node.LEAD_SELECTION)))
	{
		newLevelElement = wdContext.nodeLEVEL().createLEVELElement();
		node.addElement(newLevelElement);
	}
    //@@end
  }

Now when I click the Insert Node button, I want to insert a new node underneath the selected node, ie I want to make it the child of the selected node. node.LEAD_SELECTION. If I look at my code it seems as if I'm following the same logic as the code provided by the tutorial, except the tutorial loops thru the multidimensional array in the Java file and I'm just trying to add a single child node.

I get a null pointer exception when the onActionInsertNode fires, Ive indicated above where.

Can someone kindly please explain what I am doing wrong?

Thanks in advance.

PS. I forgot to mention that when I created my action onActionInsertNode, I did NOT check the without validation checkbox. I don't actually know what this checkbox does, but in the tutorial is asks you to check this box when you create onActionLoadChildren.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

void onAddNodeUnderLeadSelectionAction(...)
{
  if (wdContext.nodeLEVEL().getLeadSelection() != IWDNode.NO_SELECTION)
  {
    ILEVELElement e = wdContext.nodeLEVEL().currentLEVELElement();
    ILEVELElement child = e.nodeLEVEL().createAndAddLEVELElement();
  }
}

Armin

Former Member
0 Kudos

Ok, so I realised that in my wdDoModifyView method I didn't have a source mapping associated with the button event. So I added the following code:


public static void wdDoModifyView(IPrivateTreeAppCompView wdThis,
 IPrivateTreeAppCompView.IContextNode wdContext, 
com.sap.tc.webdynpro.progmodel.api.IWDView view, 
boolean firstTime)
  {
    //@@begin wdDoModifyView
    if(firstTime)
    {
    	IWDToolBarButton btn = (IWDToolBarButton) view.getElement("ToolBarItems");
    	btn.mappingOfOnAction().addSourceMapping("path", "element");
    	
	IWDTreeByNestingTableColumn masterColumn = 
    	(IWDTreeByNestingTableColumn) view.getElement("MasterColumn");    		
    	masterColumn.mappingOfOnLoadChildren().addSourceMapping("path", "element");
    	
    }
    //@@end
  }

But now I get the following exception


The initial exception that caused the request to fail, was:



   com.sap.tc.webdynpro.services.exceptions.WDIllegalArgumentException: Parameter path not found 

    at com.sap.tc.webdynpro.services.event.Event.getParameter(Event.java:171)
    at com.sap.tc.webdynpro.services.event.ParameterMapping.applyMapping(ParameterMapping.java:144)
    at com.sap.tc.webdynpro.clientserver.uielements.adaptbase.AbstractAdapter._getActionEvent(AbstractAdapter.java:276)
    at com.sap.tc.webdynpro.clientserver.uielements.adaptbase.AbstractAdapter.fireAction(AbstractAdapter.java:441)
    at com.sap.tc.webdynpro.clientserver.uielib.standard.uradapter.ToolBarButtonAdapter.onBUTTONCLICK(ToolBarButtonAdapter.java:515)
    ... 31 more


See full exception chain for details. 

It doesn't recognise the path parameter in the buttons btn.mappingOfOnAction().addSourceMapping("path", "element") line.

Any ideas or help PLEASE?

M

Former Member
0 Kudos

The error message is correct because there exists no such event parameter for the ToolBarButton.onAction. If you want to add a new element under the lead selected element you don't need any parameter mapping.

Armin

Former Member
0 Kudos

Hi Armin,

Is that method, createAndAddElement just pseudocode? I havent been able to find it.

Another thing I'm running into is that the LEAD_SELECTION always reads 0, no matter where I click on the table. I'm using this code to read the lead selection on the tables onLeadSelection action.


public void onActionShowLSVal(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionShowLSVal(ServerEvent)
    int i = wdContext.nodeLEVEL().getLeadSelection();    
    wdContext.currentContextElement().setDebug("Lead Selection " + i);
    //@@end
  }

I've also tried this code but still always reads zero


public void onActionShowLSVal(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionShowLSVal(ServerEvent)
     int i = wdContext.currentLEVELElement().node().getLeadSelection();
     wdContext.currentContextElement().setDebug("Lead Selection " + i);
    //@@end
  }

Debug is just a value attribute bound to a textview ui element to print the Lead Selection value to the screen.

Former Member
0 Kudos

No, it's not pseudo-code. In newer releases there exists an API method createAndAddElement() and a generated method createAndAdd<NodeName>Element(). In older releases just use create<NodeName>Element + addElement().

What do you mean with "no matter where I click on the table"? If you click on the selection button for a row, then the lead-selection is set to the index of that row. If you click inside a row it depends on the table's selectionChangeBehaviour if this changes the lead-selection or not.

If you want to get the index of a row when clicking on any cell editor (independent from the current lead-selection), then you should use a parameter mapping like for example


IWDButton button = (IWDButton) view.getElement("button_used_as_cell_editor");
button.mappingOfOnAction().addSourceMapping("nodeElement", "row");

and define an action parameter named "row" of type IWDNodeElement or I<NodeName>Element. This will give you the node element corresponding to the row where the button was clicked.

Armin

Former Member
0 Kudos

Ok, now I found out why the Lead Selection was always 0. It was because I needed to get the Lead Selection of the CHILD_LEVEL node which is the recursive node of LEVEL node.

The new code looks like this:


public void onActionInsertNode(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent,
 com.sjm.wdp.wdp.IPrivateTreeAppCompView.ILEVELElement element )
  {
    //@@begin onActionInsertNode(ServerEvent)
    //addNewLevel(element.nodeCHILD_LEVEL(), element.getID());
    if(wdContext.nodeLEVEL().getLeadSelection() != IWDNode.NO_SELECTION)
    {
		IPrivateTreeAppCompView.ILEVELElement newChildElem = wdContext.createLEVELElement(); 
		wdContext.nodeLEVEL().addElement(wdContext.nodeLEVEL().nodeCHILD_LEVEL().getLeadSelection(), newChildElem);
    }
    //@@end
  }

However, there is still a problem.

The root node lead selection is -1. Thats fine.

The 1st child node under root node lead selection is 0.

The 2nd child node under root node lead selection is 1.

Now, all the children nodes under 1st child node lead selection reads 0 and so do all its grand children.

The same for the 2nd child node. All its children and grandchildren also have lead selection 1.

This makes sense as the hierarchy is in a tree like sturcture...but then how do you insert a new node at runtime in the correct position?

Any suggestions are most welcomed.

Thanks

Marshall.

Former Member
0 Kudos

Use getTreeSelection() in a hierarchy of non-singleton nodes.

Armin

Former Member
0 Kudos

The singleton property of the node LEVEL is set to true.

What does that mean? I can't change it either.

The node LEVEL has a repeatedNode CHILD_LEVEL.

Former Member
0 Kudos

That's because it is a top-level node with direct recursion. Top-level nodes are always non-singleton because the context root only have at most one element. But the recursion behaves like a non-singleton subnode which means you will have a recursive child node for every node element.

In your example, use wdContext.nodeLEVEL().getTreeSelection() to get the selection at any nested level.

Armin

Former Member
0 Kudos

Hi Armin,

So using what you have given me so far here is how the getTreeSelection().index() is returning


context                      getTreeSelection().index()
-Root                                       -1    
  - child1                                   0
    - gchild1_1                            0
      - leaf1_1                             0
      - leaf1_2                             1
    - gchild1_2                            1
    - gchild1_3                            2
  - child2                                   1
    - gchild2_1                            0
    - gchild2_2                            1 

Here is my code to add a node into the desired position in the tree.


public void onActionInsertNode(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent,
 com.sjm.wdp.wdp.IPrivateTreeAppCompView.ILEVELElement element )
  {
    //@@begin onActionInsertNode(ServerEvent)
    //addNewLevel(element.nodeCHILD_LEVEL(), element.getID());
    if(wdContext.nodeLEVEL().getLeadSelection() != IWDNode.NO_SELECTION)
    {
    	try
    	{
		IPrivateTreeAppCompView.ILEVELElement newChildElem = 
                                                 wdContext.createLEVELElement(); 
		wdContext.nodeLEVEL().addElement
                               (wdContext.nodeLEVEL().getTreeSelection().index(), newChildElem);
    	}
	catch(Exception e)
	{
	}
    }
    //@@end
  }   

However, despite specifying where to add the node, the node keeps getting created and added at the root level. Please help out.

Marshall.

Former Member
0 Kudos

Well I decided to add something in and made an important discovery.

I decided to retrieve the node TITLE on the onLeadSelection method of the table. The code is such:


public void onActionShowLSVal(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionShowLSVal(ServerEvent)
    try
    {
		int i = wdContext.nodeLEVEL().getTreeSelection().index();   
		wdContext.currentContextElement().setDebug("Lead Selection " + i);
		
		wdContext.currentContextElement().setNode
			(wdContext.nodeLEVEL().getElementAt
                                 (wdContext.nodeLEVEL().getTreeSelection().index())
								  .getAttributeAsText("TITLE"));
    }
    catch(Exception e)
    {
    }
    
   }

Now, everytime I click the lead selection button of the tree table of the relevant node, I expected to see the correct name of the node title retrieved. However the lead selection number changes but the title of the node is always the root node!!!!!! So whenever I do a addElement it always adds it to the root node. But I dont understand why its always the root node? This is peculiar behaviour, any suggestions.???

Marshall

Former Member
0 Kudos

To add a new child node under the tree seleciton, use this code


onInsertChildUnderTreeSelectionAction(...)
{
  ILEVELElement selection = wdContext.nodeLEVEL().getTreeSelection();
  ILEVELElement child = selection.nodeLEVEL().createLEVELElement();
  selection.nodeLEVEL().addElement(child);
}

Armin

Former Member
0 Kudos

Hi Armin,

You had posted this reply to another thread back in Dec 2008 called "Adding a node to a tree".

However, this code behaves really strange.

If I put the code into the onLeadSelection of the table UI, then it adds the node ONLY if the node is NOT expanded! It wont add the node if the node is expanded.

But if I put the code in a method, and call this method from a Button UI's onAction method, then the node does NOT get added.

Here is the code that is fired from a Button click which doesnt work but if I put this same code into the onLeadSelect event of the Table UI then it will work only if the node is NOT exapnded:


public void onActionInsertNode(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent, 
com.sjm.wdp.wdp.IPrivateTreeAppCompView.ILEVELElement element )
  {
    //@@begin onActionInsertNode(ServerEvent)
    //addNewLevel(element.nodeCHILD_LEVEL(), element.getID());
    if(wdContext.nodeLEVEL().getLeadSelection() != IWDNode.NO_SELECTION)
    {
    	try
    	{
    	
		IPrivateTreeAppCompView.ILEVELElement selectedElem = 
		(IPrivateTreeAppCompView.ILEVELElement) wdContext.nodeLEVEL().getTreeSelection();
		if(selectedElem != null && selectedElem.nodeCHILD_LEVEL().size() == 0)
		{
			IPrivateTreeAppCompView.ILEVELElement newElem = 
                            selectedElem.nodeCHILD_LEVEL().createLEVELElement();
				selectedElem.nodeCHILD_LEVEL().addElement(newElem);
		}
    	}
	catch(Exception e)
	{
	}
    }
    //@@end
  }

Armin, you have been really great with your responses, I really appreciate it.

Any suggestions for this?

Marshall.

Answers (0)