2012-08-02 79 views
3

我寫了下面的代碼打印樹: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> 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

你有這樣的代碼:

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 
} 
+0

非常感謝。它確實有效。現在我的問題是,如果我想從addSelectionhandler方法的外部打印出樹,那我該怎麼做呢?我有一個靜態私有字符串,它獲取節點名稱,但是當我離開selectionHandler方法時,我無法打印它們。謝謝 – Maria 2012-08-02 15:59:57

+0

@Maria - 如果你從樹中的任意節點開始,例如。 'TreeItem A',你可以打印來自該節點的路徑。或者,您可以將打印邏輯移動到自己的方法中,例如'printTreeFromNode(TreeItem節點)'並將TreeItem傳遞給它。 – 2012-08-02 16:07:54

+0

我想向上打印樹,這意味着從根節點到選定節點。 – Maria 2012-08-02 16:48:40