package com.acme.main.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeNode;

/**
 * Entry point for the showcase.
 * 
 */
public class Main implements EntryPoint {

    private final static Logger logger = Logger.getLogger(EntryPoint.class.getName());

    private int currId = 1;

    @Override
    public void onModuleLoad() {

        Tree tree = new Tree();
        tree.setModelType(TreeModelType.PARENT);
        
        TreeNode[] data = new TreeNode[4];
        TreeNode p1 = new TreeNode();
        p1.setAttribute("objCode", "1");
        p1.setAttribute("id", 1);
        TreeNode p2 = new TreeNode();
        p2.setAttribute("objCode", "1.1");
        p2.setAttribute("id", 2);
        p2.setAttribute("parentId", 1);
        TreeNode p3 = new TreeNode();
        p3.setAttribute("objCode", "1.2");
        p3.setAttribute("id", 3);
        p3.setAttribute("parentId", 1);
        TreeNode p4 = new TreeNode();
        p4.setAttribute("objCode", "1.1.1");
        p4.setAttribute("id", 4);
        p4.setAttribute("parentId", 2);
        
        data[0] = p1;
        data[1] = p2;
        data[2] = p3;
        data[3] = p4;
        
        tree.setData(data);
        
        Tree filteredTree = tree.getFilteredTree(new AdvancedCriteria("objCode", OperatorId.ISTARTS_WITH, "1.1"));
        
        if(filteredTree == null) {
            logger.log(Level.INFO, "Filtered tree is null");    
        }
        
        Record[] filteredNodes = tree.findAll(new AdvancedCriteria("objCode", OperatorId.ISTARTS_WITH, "1.1"));

        if(filteredNodes == null || filteredNodes.length == 0) {
            logger.log(Level.INFO, "Filtered nodes not found");
        } else {
            for(Record filteredNode : filteredNodes) {
                logger.log(Level.INFO, filteredNode.getAttribute("objCode"));
            }
        }
                
        TreeGrid treeGrid = new TreeGrid();
        treeGrid.setData(tree);
        
        final HLayout layout = new HLayout();
        layout.addMember(treeGrid);
        layout.setWidth100();
        layout.setHeight(300);
        layout.show();

    }
}
