package org.pentaho.ui.xul.swing.tags;

import java.util.ArrayList;
import java.util.List;

import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.XulDomContainer;
import org.pentaho.ui.xul.containers.XulTree;
import org.pentaho.ui.xul.containers.XulTreeChildren;
import org.pentaho.ui.xul.containers.XulTreeItem;
import org.pentaho.ui.xul.containers.XulTreeRow;
import org.pentaho.ui.xul.dom.Element;
import org.pentaho.ui.xul.swing.SwingElement;

public class SwingTreeChildren extends SwingElement implements XulTreeChildren {

  XulTree tree;

  List<XulTreeItem> items;

  public SwingTreeChildren(Element self, XulComponent parent, XulDomContainer domContainer, String tagName) {
    super("treechildren");

    tree = (XulTree) parent;
    items = new ArrayList<XulTreeItem>();
    managedObject = "empty";

  }

  public void addItem(XulTreeItem item) {
    items.add(item);
    this.addChild(item);
  }

  public XulTreeRow addNewRow() {

    SwingTreeItem item = new SwingTreeItem(this);
    item.setRow(new SwingTreeRow(item));
    items.add(item);
    addChild(item);

    return item.getRow();
  }

  public XulTreeItem getItem(int rowIndex) {
    return (items.size() > rowIndex) ? items.get(rowIndex) : null;
  }

  public XulTree getTree() {
    return tree;
  }

  public boolean isAlternatingbackground() {
    // TODO Auto-generated method stub
    return false;
  }

  public boolean isHierarchical() {
    // TODO Auto-generated method stub
    return false;
  }

  public int getItemCount() {
    if (items == null) {
      return 0;
    }
    return items.size();
  }

  public void removeAll() {
    while (items.size() > 0) {
      removeItem(0);
    }
  }

  public void removeItem(XulTreeItem item) {
    items.remove(item);
    this.removeChild(item);
  }

  public void removeItem(int rowIndex) {
    this.removeChild(items.get(rowIndex));
    items.remove(rowIndex);
  }

  public void setAlternatingbackground(boolean alt) {
    // TODO Auto-generated method stub

  }

  @Override
  public void layout() {
    tree.setRootChildren(this);
    initialized = true;
  }

  @Override
  public void addComponent(XulComponent c) {
    super.addComponent(c);
    items.add((XulTreeItem) c);
    if (initialized) {
      resetContainer();
      layout();
    }

  }
}

@@ -1673,16 +1673,23 @@
 veItem(0
+, false
 );%0A    %7D
@@ -1689,16 +1689,35 @@
 ;%0A    %7D%0A
+    tree.update();%0A
   %7D%0A%0A  p
@@ -1802,32 +1802,51 @@
 oveChild(item);%0A
+    tree.update();%0A
   %7D%0A%0A  public vo
@@ -1863,32 +1863,49 @@
 tem(int rowIndex
+, boolean refresh
 ) %7B%0A    this.rem
@@ -1963,16 +1963,145 @@
 Index);%0A
+    if (refresh) %7B%0A    %09tree.update();%0A    %7D%0A    %0A  %7D%0A  %0A  public void removeItem(int rowIndex) %7B%0A  %09removeItem(rowIndex, true);%0A
   %7D%0A%0A  p

update UI when removing items from tree