Skip to Content
avatar image
Former Member

Expand all child nodes of the recursive tree

Hi everybody,

I have a problem by implementing of the action to expand all child nodes of the recursive tree and need your help.

I used the TuTWD_Tree example project provided by SAP. What I want to reach is to expand all the child nodes of the recursive tree (TheTree) by clicking the ToolBarButton.

I did the following jobs:

- I added a ToolBarButton named "EXPAND" in the TreeView.

- Then I implemented the onActionExpandNodes(). The codes I wrote in the onActionExpandNodes are as following:

...

IPrivateTreeView.ITreeNodeNode rootNode = wdContext.nodeTreeNode();

for (int i = 0; i < rootNode.size(); i++){

IPrivateTreeView.ITreeNodeElement element = (IPrivateTreeView.ITreeNodeElement)rootNode.

getElementAt(i);

element.setIsExpanded(true);

IPrivateTreeView.ITreeNodeNode subNode = element.nodeChildNode();

if (!subNode.isEmpty()){

for(int n = 0; n < subNode.size(); n++){

IPrivateTreeView.ITreeNodeElement element1 = (IPrivateTreeView.ITreeNodeElement)subNode.getElementAt(n);

if (element1 != null){

element1.setIsExpanded(true);

}

}

}

}

...

After deploying and running the project I see that the twisty icon before the driver "C" and the drive "D" and the direct child folder "Games" are expanded when I clicked the "EXPAND" ToolBarButton. The other folders under C or D are not expanded. Certainly there are somethings uncorrect but I do not know what.

Are there anyones who already have experience on this topic?

Please give me your advices!

Thank you in advance!

Bye

Thao

PS.

##############################################################

##

  1. Filesystem resources for Web Dynpro Tutorial Tree

##

##############################################################

Drives = C;D

C = Documents;Program_Files;Temp;Windows;start.bat

Documents = Word;calc.xls;db.mdb

Word = first.doc;second.doc

Program_Files = Winzip;Accessories

Winzip = wz.com;wzinst.hlp;test.zip

Accessories = calculator.exe;notepad.exe;paint.exe

Windows = Java;Temporary_Internet_Files

Java = app.java;app.class

Temporary_Internet_Files = cookie1.txt;cookie2.txt;cookie3.txt;cookie4.txt

D = Games

Games = Soccer;Chess

Soccer = soccer.exe;field.lnd

Chess = chess.exe

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

4 Answers

  • avatar image
    Former Member
    Apr 13, 2005 at 06:35 PM

    In your action you are not adding child nodes. You can do it as:

    **********************************************************

    for (int i = 0; i < rootNode.size(); i++){

    IPrivateTreeView.ITreeNodeElement element = (IPrivateTreeView.ITreeNodeElement)rootNode.

    getElementAt(i);

    //Adding child nodes

    <b>addChildren(element);</b>

    element.setIsExpanded(true);

    IPrivateTreeView.ITreeNodeNode subNode = element.nodeChildNode();

    if (!subNode.isEmpty()){

    for(int n = 0; n < subNode.size(); n++){

    IPrivateTreeView.ITreeNodeElement element1 = (IPrivateTreeView.ITreeNodeElement)subNode.getElementAt(n);

    //Adding child nodes

    <b>addChildren(element1);</b>

    //if (element1 != null){

    element1.setIsExpanded(true);

    //}

    }

    }

    }

    **********************************************************

    Also even this will only expand the first level. In order to expand all you need some recurssive way to get child nodes, and expand them.

    Note: The reason folder Games showed as expanded is because in tutorial they expand it in the wdDoInit() method.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Qamar,

      thank you for your tip.

      > The reason folder Games showed as expanded is because in tutorial they expand it in the wdDoInit() method.

      - Yes, I know it. But I forgot to tell that I modify the code in the wdDoInit() method by setting

      ...

      } else {

      level1Node.setIsExpanded(false);

      }

      for the folder Games .

      - After implementing your tip

      <b>addChildren(element);</b>

      it works well. I can expand all child nodes in the tree by static programming.

      In order to expand the recursive tree dynamically I wrote the methode named getWholeTree(). The codes in this methode are as following:

      <b>public void getWholeTree</b>( com.sap.tut.wd.tree.wdp.IPrivateTreeView.ITreeNodeNode node, java.util.ArrayList result ) {

      for (int i = 0; i < node.size(); i++){

      IPrivateTreeView.ITreeNodeElement element = (IPrivateTreeView.ITreeNodeElement)node.getElementAt(i);

      // Adding child nodes

      addChildren(element);

      element.setIsExpanded(true);

      IPrivateTreeView.ITreeNodeNode subNode = element.nodeChildNode();

      if (subNode != null) {

      if (!subNode.isEmpty()){

      <b>getWholeTree(subNode, result);</b> } else {

      result.add(element);

      }

      }

      }

      }

      In the onActionExpandNodes I called the methode as following:

      IPrivateTreeView.ITreeNodeNode subNode1 = wdContext.nodeTreeNode();

      ArrayList result = new ArrayList();

      IWDMessageManager messageMgr = wdThis.wdGetAPI().getComponent().getMessageManager();

      try {

      getWholeTree(subNode1, result);

      } catch (Exception e){

      messageMgr.reportSuccess(e.getLocalizedMessage());

      }

      After deploying it could not work. I know that it has the problem with the recursive methode. But why?

      Can you help me? Or anyone has the experience on this?

      Thank you.

      Thao

  • avatar image
    Former Member
    Apr 14, 2005 at 09:50 PM

    Here you go:

    1. You need to create a new context attribute "size" of type integer for "treeNode" context.

    2. Update addChildren() as follows:

    ********************************************************

    private void addChildren(IPrivateTreeView.ITreeNodeElement parent) {

    <b>int parentSize = 0;</b>

    String resourceText =

    resourceHandlerForTree.getString(parent.getText());

    IPrivateTreeView.ITreeNodeElement child = null;

    // if there is no entry in Filesystem.properties for key parent.getText() then the key is returned

    if (resourceText.equals(parent.getText())) {

    child = parent.nodeChildNode().createTreeNodeElement();

    <b>parentSize = 1;</b>

    child.setText("Empty Folder");

    child.setHasChildren(false);

    child.setIgnoreAction(true);

    parent.nodeChildNode().addElement(child);

    } else {

    // populate all children

    StringTokenizer strTokenizer =

    new StringTokenizer(resourceText, ";");

    String token = null;

    while (strTokenizer.hasMoreTokens()) {

    token = strTokenizer.nextToken();

    child = parent.nodeChildNode().createTreeNodeElement();

    <b>parentSize = parentSize + 1;</b>

    if (token.indexOf(".") != -1) {

    // token is a file

    child.setHasChildren(false);

    child.setIgnoreAction(false);

    child.setIconSource("~sapicons/s_b_crea.gif");

    } else {

    // token is a folder

    child.setHasChildren(true);

    child.setIgnoreAction(true);

    child.setIconSource("~sapicons/s_clofol.gif");

    }

    child.setText(token);

    child.setIsExpanded(false);

    parent.nodeChildNode().addElement(child);

    }

    }

    <b>parent.setSize(parentSize);</b> }

    ******************************************************

    3. Update onActionExpandAll() as:

    *******************************************************

    public void onActionExpandAll(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

    {

    //@@begin onActionExpandAll(ServerEvent)

    IPrivateTreeView.ITreeNodeNode rootNode = wdContext.nodeTreeNode();

    ArrayList result = new ArrayList();

    IWDMessageManager messageMgr = wdThis.wdGetAPI().getComponent().getMessageManager();

    try {

    for (int i = 0; i<rootNode.size(); i++) {

    getWholeTree((IPrivateTreeView.ITreeNodeElement)rootNode.getElementAt(i));

    }

    } catch (Exception e){

    messageMgr.reportSuccess(e.getLocalizedMessage());

    }

    *******************************************************

    4. Finally the recursive one:

    public void getWholeTree( com.sap.tut.wd.tree.wdp.IPrivateTreeView.ITreeNodeElement element) {

    addChildren(element);

    element.setIsExpanded(true);

    if(element.getHasChildren()){

    for (int i=0; i< element.getSize(); i++) {

    getWholeTree(element.getChildNodeElementAt(i));

    }

    }

    }

    Hope this will help.

    cheers

    ~Qamar

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 19, 2006 at 09:20 AM

    hi,

    See this thread:

    ABAP WD: Fully expand a Tree

    Its for ABAP, but there is hardly any coding done, so the idea should work for Java

    Regards,

    Joris

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 19, 2006 at 10:11 AM

    Hi,

    If your node structure is,

    Treenode

    --treeText

    --ChildNode(Recursive)

    then if you want to expand all the Nodes and Subnodes, simply you can bind a boolean attribute to the expand property of the treeNodeType. and in the expand button action you can use,

    wdContext.currentContextElement().set<Attribute>(true);

    regards

    karthik

    Add comment
    10|10000 characters needed characters exceeded