0

Recursive numbering

asked 2008-10-27 15:15:01 +0800

Klem gravatar image Klem
30 2

Hi all...first af all I'm new to Zkoss, but not in java.. So hi everybody

I'm facing an head scratching problem

I have a tree. The user may drag and drop the cells. I want to parse the DOM of this tree to be able to generate for each cell an "id" formatted like a chapter number (1, 1.1, 1.1.2 and so on), so I can save the tree structure and store the location of each cell in a file.

The overal thing is almost working. However, I cannot make it understand that what comes after 1.1 is 1.2 if there was any subsections between 1.1 and 1.2 ( such as 1.1.1, 1.1.2, 1.1.2.1 etc etc)


To make it clear. While parsing the dom tree, I want it to output
default-----position: 0
aymen-----position: 0.0
off_papillon-----position: 0.0.0
test-----position: 1
off_bonjour-----position: 1.0
a plus-----position: 1.0.0
off gallery-----position: 1.1
yep-----position: 2
hoy-----position: 2.0
off_plop-----position: 2.1
third-----position: 3
test-----position: 4
test-----position: 4.0
test-----position: 4.1

I use the following code

 public void save() {
        lvl=-1;
        saveposition(rootitem,"");
    }
    
     public void saveposition(Treeitem c,String position){

        Treechildren l=c.getTreechildren();
        String parent=c.getLabel();
     
        for(int i=0;i<l.getChildren().size();i++){
            Treeitem t=(Treeitem)l.getChildren().get(i);
            String n=(String)t.getAttribute("pname");
          
            if(t.getLevel() == 1){
                pos=new StringBuffer();
                lvl++;
                pos.append(lvl);
                
            }
            else {
                pos.append(position);
                pos.append(i);
            }
            System.out.println("the treeitem is "+n+" position is:"+pos.toString());
            
            saveposition(t,".");
            
        }
    }

And the output is
the treeitem is default position is:0
the treeitem is aymen position is:0.0
the treeitem is off_papillon position is:0.0.0
the treeitem is test position is:1
the treeitem is off_bonjour position is:1.0
the treeitem is a plus position is:1.0.0
the treeitem is off gallery position is:1.0.0.1
the treeitem is yep position is:2
the treeitem is hoy position is:2.0
the treeitem is off_plop position is:2.0.1
the treeitem is third position is:3
the treeitem is test position is:4
the treeitem is test position is:4.0
the treeitem is test position is:4.0.1

I know this has something to do with the String buffer, but I cannot find the proper if statement to play with what needs to be appned. I've tried playing with depth children count etc etc....

Any input in appreciated.

PS : I've already looked at the AbstractTree small talks

delete flag offensive retag edit

3 Replies

Sort by » oldest newest

answered 2008-10-28 16:35:19 +0800

iantsai gravatar image iantsai
2755 1

I'm sorry, it's really hard for me to understand what you want to do.
Can you provide some executable code?

You can type some thing like this:

1. input data(mabe a text file).
2. program(a zul should be better)
3. the result(please indicate which part is incorrect)

link publish delete flag offensive edit

answered 2008-10-29 00:44:32 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2008-10-29 00:45:36 +0800

I think Klem want to print a tree hierarchy with 'dot' but he doesn't has a correct code logic to do it.

link publish delete flag offensive edit

answered 2008-10-30 09:42:37 +0800

Klem gravatar image Klem
30 2

updated 2008-10-30 10:55:48 +0800

I'll try to make myself clearer and give you as much as possible... I basically wants to do the opposite of this
http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D5091%3BcategoryId%3D14%3B

Context:
I have a tree that is generated from XML data. Each element has a "position" that I use to place it accordingly the tree. the position is formatted this way:
1, 1.1, 1.1.1, 2, 2.1, 2.1.1 etc etc.
The user may then drag and drop to change thr tree elements hierarchy.
When he clicks "save", I want to parse the DOM tree and regenerate a pôsition for each element so it can be updated in the XML.

To generate the tree
XML

<page id="1" name="default" position="0" status="online">
<atom>default</atom>
<atom>default_fr</atom>
<molecule>default</molecule>
<style>default</style>
<form>detail_form</form>
</page>
−
<page id="2" name="off gallery" position="1.1" status="offline">
<atom>gallery</atom>
<molecule>gallery</molecule>
<style>gallery</style>
<form>gallery_form</form>
</page>
−
<page id="3" name="off_papillon" position="0.0.0" status="offline">
<atom>papillon</atom>
<molecule>papillon</molecule>
<style>papillon</style>
<form>detailImage_form</form>
</page>
−
<page id="4" name="aymen" position="0.0" status="online">
<atom>aymen</atom>
<molecule>aymen</molecule>
<style>aymen</style>
<form>detailImage_form</form>
</page>
−
<page id="5" name="test" position="1" status="online">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="6" name="off_bonjour" position="1.0" status="offline">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="7" name="a plus" position="1.0.0" status="online">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="8" name="yep" position="2" status="online">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="9" name="hoy" position="2.0" status="online">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="10" name="off_plop" position="2.1" status="offline">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="11" name="third" position="3" status="online">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="12" name="test" position="4" status="online">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="13" name="test" position="4.0" status="online">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>
</page>
−
<page id="14" name="test" position="4.1" status="online">
<atom>test</atom>
<molecule>test</molecule>
<style>test</style>
<form>detailImage_form</form>

The tree is to bhe displayed in here

<tree use="eu.xx.xx.siteplan.SitePlan" id="treesiteplan" >
    <treecols >
        <treecol>
            <toolbar>
    <toolbarbutton label="${c:l('tree.openall')}">
        <attribute name="onClick">{
            treesiteplan.openAll();
        }</attribute>
    </toolbarbutton>
    <toolbarbutton label="${c:l('tree.closeall')}">
        <attribute name="onClick">{
            treesiteplan.closeAll();
        }</attribute>
    </toolbarbutton>
</toolbar> 
        </treecol>
    </treecols>
</tree>    

With this particular method

/**
     * implement method from the interface Tree
     */
    public void afterCompose() { 
        init();
        root=new Treechildren();
        
        
        this.appendChild(root);
        rootitem=new Treeitem();
        rootitem.setLabel(space);
        root.appendChild(rootitem);
        Treechildren child=new Treechildren();
        addNodes(rootitem,child,"0");
    }
    /**
     * this is a recursive method , it implements the siteplan's tree 
     * @param node
     * @param child
     * @param s
     */
    private void addNodes(Treeitem node,Treechildren child, String s) {
        
        String path=prop.getProperty("atom-template");
        node.appendChild(child);
        String id = ((String) this.map_PositionToId.get(s));

        if (id != null) {

            Page page = (Page) this.map_IdToPage.get(id);
            Treechildren nodeImpl = null;
            Position pos = new Position(s);



                nodeImpl = new Treechildren();
                String name = page.getName();
                System.out.println("the page name is :"+name+"-----position: "+page.getPosition());
                Treeitem item = new Treeitem(); 
               
                Treerow row = new DroppableTreeRow();
                Treecell tc = new Treecell(name);
          
                if(page.getPosition().equals("offline")) {
                    
                    tc.setStyle("div.cell-inner{font-family: cursive;color:red;}");
                }
                row.appendChild(new Treecell(name));
                item.appendChild(row); 
                child.appendChild(item);
                item.appendChild(nodeImpl);
               
                row.setDraggable("true");
                row.setDroppable("true");
                
                Menupopup pop = this.getMenu();
                item.setContext(pop);
                
                Vbox vb =(Vbox) this.getParent();
                pop.setParent(vb);
               
                addNodes(item,nodeImpl, pos.giveChildren());
            

            addNodes(node,child, pos.giveNext());

        }
    }

When th user clicks save, I want to regenerate well formed position so I can write them back.

 public void save() {
        spaceXML = new XMLspace(space);
        spaceXML.init();
        lvl=-1;
        saveposition(rootitem,"");
    }
    
     public void saveposition(Treeitem c,String position){

        Treechildren l=c.getTreechildren();
       
        String parent=c.getLabel();
        
        for(int i=0;i<l.getChildren().size();i++){
            Treeitem t=(Treeitem)l.getChildren().get(i);
            
            String n=(String)t.getAttribute("pname");
            String id=(String)t.getAttribute("pid");

            if(t.getLevel() == 1){
                pos=new StringBuffer();
                lvl++;
                pos.append(lvl);   
            }
            else {
                pos.append(position);
                pos.append(i);
            }
            System.out.println("the treeitem is "+n+" position is:"+pos.toString()+" id is "+id);
            //spaceXML.updatePosition(id, pos.toString());
          
            saveposition(t,".");
            
        }
    }

My problem is that instead of having output like this
0
0.0
0.0.0
1
1.0
1.0.0
1.1
2
2.0
2.1
3
4
4.0
4.1

I have an output like this
0
0.0
0.0.0
1
1.0
1.0.0
1.0.0.1 (should be 1.1)
2
2.0
2.0.1 (should be 2.1)
3
4
4.0
4.0.1(should be 4.1)

In fact in the save position() method I can't figure out what is my "exit" / "one way up" condition

Hope I made myself understood

Any input is gretly appreciated

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2008-10-27 15:15:01 +0800

Seen: 173 times

Last updated: Oct 30 '08

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More