GWT - 打印一棵树

时间:2012-08-02 15:46:04

标签: java gwt

我编写了以下代码来打印树:

package test.com.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestGwt implements EntryPoint 
{
    public static String bc="";

    public void onModuleLoad() 
    {
        VerticalPanel v = new VerticalPanel();
        TreeItem a = new TreeItem( "A" );
        TreeItem b = new TreeItem( "B" );
        TreeItem c = new TreeItem( "C" );

        TreeItem d = new TreeItem( "D" );

        TreeItem f1 = new TreeItem( "f1" );
        TreeItem f11 = new TreeItem( "f11" );
        TreeItem f12 = new TreeItem( "f12" );
        d.addItem( f1 );
        f1.addItem( f11 );
        f1.addItem( f12 );


        final Tree t = new Tree();
        t.addItem( a );
        t.addItem( b );
        t.addItem( c );
        t.addItem( d );

        t.addSelectionHandler(new SelectionHandler<TreeItem>() 
        {
            @Override
            public void onSelection(SelectionEvent<TreeItem> event) 
            {           
                TreeItem selectedItem= event.getSelectedItem();
                if(selectedItem != null)
                {
                    System.out.println("Selected Item: "+ selectedItem.getText());
                    TreeItem parentItem  = event.getSelectedItem().getParentItem();
                    t.setSelectedItem(parentItem);
                    parentItem=parentItem.getParentItem();

                }


            }
        });

        v.add(t);
        // Add it to the root panel.
        RootPanel.get().add(v);
    }
}

节点D具有子节点f1。 f1具有子节点f11和f12。如果用户选择f12,则应打印:

d取代; F1&GT; F12。

当所选项目变为D时,我的代码会崩溃,因为D没有父项。如何改进此代码,以便我能够根据用户选择的节点带来bradcrumb?

有没有办法检查所选节点是否是根节点?

非常感谢。 -Maria

更新了工作代码:

package test.com.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestGwt implements EntryPoint 
{
    public static String bc="";

    public void onModuleLoad() 
    {
        VerticalPanel v = new VerticalPanel();
        TreeItem a = new TreeItem( "A" );
        TreeItem b = new TreeItem( "B" );
        TreeItem c = new TreeItem( "C" );

        TreeItem d = new TreeItem( "D" );

        TreeItem f1 = new TreeItem( "f1" );
        TreeItem f11 = new TreeItem( "f11" );
        TreeItem f12 = new TreeItem( "f12" );
        d.addItem( f1 );
        f1.addItem( f11 );
        f1.addItem( f12 );


        final Tree t = new Tree();
        t.addItem( a );
        t.addItem( b );
        t.addItem( c );
        t.addItem( d );


        t.addSelectionHandler(new SelectionHandler<TreeItem>() 
        {
            @Override
            public void onSelection(SelectionEvent<TreeItem> event) 
            {           
                TreeItem pItem = event.getSelectedItem().getParentItem();
                if(pItem == null)
                {
                    bc+=" > "+event.getSelectedItem().getText();
                }
                else
                {
                    TreeItem selectedItem= event.getSelectedItem();
                    if(selectedItem != null)
                    {
                        bc+=" > "+ selectedItem.getText();
                        TreeItem parentItem  = event.getSelectedItem().getParentItem();
                        t.setSelectedItem(parentItem);
                        parentItem=parentItem.getParentItem();

                    }
                }               
            }
        });
        System.out.println(bc);
        v.add(t);
        // Add it to the root panel.
        RootPanel.get().add(v);
    }
}

1 个答案:

答案 0 :(得分:1)

您有以下代码:

System.out.println("Selected Item: "+ selectedItem.getText());
TreeItem parentItem  = event.getSelectedItem().getParentItem();
t.setSelectedItem(parentItem);
parentItem=parentItem.getParentItem();

您需要检查parentItem是否为空。如果它为null,则它是根。像这样:

TreeItem parentItem = event.getSelectedItem().getParentItem();
if (parentItem == null) {
    // then it is the root; special root-handling instructions would go here
} else {
    // then it is NOT the root
}