/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.example.selectiontree;

import com.icesoft.faces.facelets.component.selectiontree.SelectionTreeModel;
import com.icesoft.faces.facelets.component.selectiontree.SelectionTreeNode;
import com.icesoft.faces.facelets.component.selectiontree.SelectionTreeNodeUserObject;

import java.io.Serializable;

public class SelectionTreeExampleBean implements Serializable {

    private SelectionTreeNode rootTreeNode;

    // label count increases one for each new node
    private int labelCount = 0;

    // selected node name
    private String selectedNode = "";

    // object reference used to delete and copy the node
    private SelectionTreeNodeUserObject selectedNodeObject = null;

    private SelectionTreeModel treeModel;


    /**
     * Gets the tree node.
     *
     * @return the tree node
     */
    public SelectionTreeNodeUserObject getSelectedNodeObject() {
        return selectedNodeObject;
    }

    /**
     * Sets the tree node.
     *
     * @param selectedNodeObject the new tree node
     */
    public void setSelectedNodeObject(SelectionTreeNodeUserObject selectedNodeObject) {
        this.selectedNodeObject = selectedNodeObject;
    }


    /**
     * Construction the default tree structure by combining tree nodes.
     */
    public SelectionTreeExampleBean() {
        rootTreeNode = new SelectionTreeNode();
        SelectionTreeNodeUserObject rootObject = new NodeUserObject(treeModel, rootTreeNode, this);
        rootTreeNode.setUserObject(rootObject);
        rootObject.setExpanded(true);
        treeModel = new SelectionTreeModel(rootTreeNode);
        ((SelectionTreeNodeUserObject) rootTreeNode.getUserObject()).setTree(treeModel);


        for (int i = 0; i < 3; i++) {
            SelectionTreeNode branchNode = new SelectionTreeNode();
            SelectionTreeNodeUserObject branchObject = new NodeUserObject(treeModel, branchNode, this);
            branchNode.setUserObject(branchObject);
            branchObject.setLeaf(false);
            rootTreeNode.add(branchNode);
            if (i == 2) {
                branchObject.getCheckbox().setChecked();
                branchObject.getCheckbox().setDisabled(true);
            }

            for (int j = 0; j < 3; j++) {
                SelectionTreeNode branchNode1 = new SelectionTreeNode();
                SelectionTreeNodeUserObject branchObject1 = new NodeUserObject(treeModel, branchNode1, this);
                branchNode1.setUserObject(branchObject1);
                branchObject1.setLeaf(true);
                branchNode.add(branchNode1);
                if (i == 2) {
                    branchObject1.getCheckbox().setChecked();
                    branchObject1.getCheckbox().setDisabled(true);
                }
            }
        }
    }

    /**
     * Gets the tree's default model.
     *
     * @return tree model.
     */
    public SelectionTreeModel getTreeModel() {
        return treeModel;
    }

    /**
     * Sets the tree's default model.
     *
     * @param model new default tree model
     */
    public void setTreeModel(SelectionTreeModel treeModel) {
        this.treeModel = treeModel;
    }

    /**
     * Gets the selected node display text.
     *
     * @return selected node display text.
     */
    public String getSelectedNode() {
        return selectedNode;
    }

    /**
     * Sets the selected node. This changes a local instance variable used for
     * display, it does not directly change the tree node state.
     *
     * @param selectedNode selected node text.
     */
    public void setSelectedNode(String selectedNode) {
        this.selectedNode = selectedNode;
    }

    /**
     * Increment the label count and return it.
     *
     * @return the new label count
     */
    public int getIncreasedLabelCount() {
        return ++labelCount;
    }


}
