2012-03-23 14 views
1

私は現在、生成中のテーブルのボタンのインデックス/位置を見つけることができません。テーブル内のボタンの位置を見つける

private class Node 
    { 
    String id; 
    String[] data; 
    List<Node> children = new ArrayList<Node>(); 
} 

private Node root = buildTree(); 

private Node buildTree() 
    { 
     Node root = new Node(); 
     root.id = "Main Component"; 
     root.data = new String[]{ "example", "example", "example", "example", "example", "example",}; 

    Node child1 = new Node(); 
    child1.id = "Part 1"; 
    child1.data = new String[]{ "example", "example", "example", "example", "example", "example", "example"}; 
    root.children.add(child1); 

    Node child11 = new Node(); 
    child11.id = "Part 1-1"; 
    child11.data = new String[]{ "example", "example", "example", "example", "example", "example", "example"}; 
    child1.children.add(child11); 

    Node child2 = new Node(); 
    child2.id = "Part 2"; 
    child2.data = new String[]{ "example", "example", "example", "example", "example", "example", "example"}; 
    root.children.add(child2); 

    Node child21 = new Node(); 
    child21.id = "Part 2-1"; 
    child21.data = new String[]{ "example", "example", "example", "example", "example", "example", "example"}; 
    child2.children.add(child21); 

    Node child22 = new Node(); 
    child22.id = "Part 2-2"; 
    child22.data = new String[]{ "example", "example", "example", "example", "example", "example", "example"}; 
    child2.children.add(child22); 

    Node child3 = new Node(); 
    child3.id = "Part 3"; 
    child3.data = new String[]{ "example", "example", "example", "example", "example", "example", "example"}; 
    root.children.add(child3); 

    Node child31 = new Node(); 
    child31.id = "Part 3-1"; 
    child31.data = new String[]{ "example", "example", "example", "example", "example", "example", "example"}; 
    child3.children.add(child31); 

    Node child32 = new Node(); 
    child32.id = "Part 3-2"; 
    child32.data = new String[]{ "example", "example", "example", "example", "example", "example", "example"}; 
    child3.children.add(child32); 

    return root; 
} 

ここは自分のデータモデルです。ここでのアイデアは、いくつかの関数を使ってテーブルに行を追加することですが、私は早い段階で立ち往生しています。

private void addRow(Node nde) 
{ 
    HorizontalPanel buttonPanel = new HorizontalPanel(); 

    ListBox comboBox = new ListBox(); 
    comboBox.addItem("option1"); 
    comboBox.addItem("option2"); 
    comboBox.addItem("option3"); 
    comboBox.addItem("option4"); 
    comboBox.addItem("option5"); 

    Button addRowButton = new Button("+/-"); 

    addRowButton.addClickHandler(new ClickHandler() { 
     public void onClick(ClickEvent event) { 
      //Get the button from the click event 
      //Get the row from the button - could I possibly set a class to each button that equals the id of the element being added so that I could just pull the id from the button 
      //Use the row to get the first column - returns a panel 
      //Get the components on the panel, find the label, and get the text 
      String id = ""; 
      clickFunction(id); 
     } 
    }); 

    buttonPanel.add(addRowButton); 
    buttonPanel.add(new Label(nde.id)); 

    int row = flexTable.getRowCount(); 
    flexTable.setWidget(row,0,buttonPanel); 
    flexTable.setWidget(row,1,comboBox); 
    flexTable.setText(row,2,nde.data[0]); 
    flexTable.setText(row,3,nde.data[1]); 
    flexTable.setText(row,4,nde.data[2]); 
    flexTable.setText(row,5,nde.data[3]); 
    flexTable.setText(row,6,nde.data[4]); 
    flexTable.setText(row,7,nde.data[5]); 
} 

これは私が行を追加していますどのように、水平パネルは、すべての行の最初の列であり、最初の要素は、ボタンであり、第二は、どのノードのIDが表示されて一致するタイトルでありますその行にそのIDを取得し、それを文字列と同じに設定し、それをclickFunctionに渡す方法が必要です。

private void clickFunction(String curRowId) 
{ 
    Node rowNode = findNode(root, curRowId); 

      //need to determine whether or not the tree needs to be expanded or minimized. 

      for(Node n : rowNode.children) { 
     addRow(n); 
    } 

} 

clickFunctionは私のfindNode機能ノードは、その子が拡張必要なコードを伝えるために再帰的に呼び出され

private Node findNode(Node nde, String id) 
{ 
    if(nde.id.equals(id)) 
    { 
     return nde; 
    } 

    for(Node n : nde.children) 
    { 
     Node rtrn = findNode(n, id); 
     if(rtrn != null) 
     { 
      return rtrn; 
     } 
    } 

    return null; 
} 

に渡されたIDを使用しています。私はこれが長すぎないことを願っています。すべての関連情報が含まれていることを確認して、私が実際に抱えている問題を解決するために実際に努力していることを証明したかったのです。私がずっと働いていても、一見単純な問題を理解することはできません。

すべての情報をお待ちしております。事前に感謝します!

答えて

0

ボタンのIDを設定するには、あなたがこのような何かを行うことができます...

Button b = new Button(); 
DOM.setElementAttribute(b.getElement(), "id", "my-button-id"); 

あなたは、あなたのfindNode方法でそれを一致させることができるはずです。

DOMの操作の詳細については、http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/DOM.htmlを参照してください。

+0

ボタンを作成してidを設定した場合、そのIDをクリックハンドラの作業に渡しますか? idを文字列と同じに設定し、それをonClickに渡す必要がありますか? – Anthony

+0

@Anthony - 私はそれについて完全にはわかりません...私の例で述べたものと同じようにidを設定してから、idをclickFunctionに渡して結果を戻すことができますか? –

+0

'final Button addRowButton =新しいボタン(" +/- "); \t \t DOM.setElementProperty(addRowButton.getElement()、 "id"、nde.id); \t \t addRowButton.addClickHandler(新はclickHandler(){ \t \t \t公共ボイドのonClick(ClickEventイベント){ \t \t \t \t文字列ID = DOM.getElementProperty(addRowButton.getElement()、 "ID"); \t \t \t \t clickFunction(ID); \t \t \t} \t \t}); ' F作業終わっそれともわたし。 – Anthony

関連する問題