/*
 * 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 javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;

public class NodeUserObject extends SelectionTreeNodeUserObject {

    private SelectionTreeExampleBean treeBean;

    // message bundle references
    private static String nodeToolTip;

    // actual node label
    private String label;

    // value of the component
    private String value;

    /**
     * Creates a new <code>NodeUserObject</code> object. Default image states
     * are set as well as the expansion of all branch leafs.
     *
     * @param wrapper         parent tree node which wraps this object
     * @param treeBeanPointer callback to parent TreeBean class.
     */
    public NodeUserObject(SelectionTreeModel tree, SelectionTreeNode node,
                          SelectionTreeExampleBean treeBeanPointer) {
        this(tree, node, treeBeanPointer, false);
    }

    public NodeUserObject(SelectionTreeModel tree, SelectionTreeNode node,
                          SelectionTreeExampleBean treeBeanPointer, boolean disabled) {
        super(tree, node, disabled);

        treeBean = treeBeanPointer;
        label = generateLabel();
        value = generateValues();

        setLeafIcon("/xmlhttp/css/rime/css-images/tree_document.gif");
        setBranchContractedIcon("/xmlhttp/css/rime/css-images/tree_folder_close.gif");
        setBranchExpandedIcon("/xmlhttp/css/rime/css-images/tree_folder_open.gif");
        setText(label);
        setTooltip(nodeToolTip);
        setExpanded(false);
    }


    /**
     * Generates a label for the node based on an incrementing int.
     *
     * @return the generated label (eg. 'Node 5')
     */
    private String generateLabel() {
        return "Node " + treeBean.getIncreasedLabelCount();
    }

    /**
     * Returns true if this node is the root of the tree. The root is the only
     * node in the tree with a null parent; every tree has exactly one root.
     *
     * @return true if this node is the root of its tree.
     */
    public boolean isRootNode() {
        return getWrapper().isRoot();
    }

    /**
     * Deletes this not from the parent tree.
     *
     * @param event that fired this method
     */
    public void deleteNode(ActionEvent event) {
        ((DefaultMutableTreeNode) getWrapper().getParent())
                .remove(getWrapper());
    }

    /**
     * Returns the label associated with the node.
     *
     * @return the label of the node
     */
    public String getLabel() {
        return label;
    }

    /**
     * Registers a user click with this object and updates the selected node in
     * the TreeBean.
     *
     * @param event that fired this method
     */
    public void nodeClicked(ActionEvent event) {
        treeBean.setSelectedNode(this.label);
        treeBean.setSelectedNodeObject(this);
        this.setExpanded(!this.isExpanded());

    }

    /**
     * gets the information that is stored in each node.
     *
     * @return the string value
     */
    public String getValue() {
        return value;
    }

    /**
     * sets the value which is the information stored in the node.
     *
     * @param value to be stored
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * generates default names for the value attribute depending on the
     * component type
     *
     * @return the string value
     */
    public String generateValues() {
        return "label";
    }

}
