/**
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.sparkimpl.plugin.privacy.ui;

import javax.swing.tree.DefaultMutableTreeNode;
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
import org.jivesoftware.sparkimpl.plugin.privacy.PrivacyManager;
import org.jivesoftware.sparkimpl.plugin.privacy.list.SparkPrivacyList;

/**
 * Class to handle privacy tree nodes
 * @author Bergunde Holger
 */
public class PrivacyTreeNode extends DefaultMutableTreeNode {
    private boolean _isContactGroup = false;
    private PrivacyItem _item = null;
    private SparkPrivacyList _list = null;
    private boolean _isPlaceHolder = false;
    private boolean _isGroupGroup = false;
    private boolean _isDefault = false;
    private boolean _isActive = false;
    private final PrivacyManager _pmanager = PrivacyManager.getInstance();

    /**
     * Creates a Node with a reference to the SparkPrivacyList and changes the
     * Name to list's name
     * 
     * @param list
     *            the list the Node will refer to
     */
    public PrivacyTreeNode(SparkPrivacyList list) {
	_list = list;
	_isActive = _list.isActive();
	_isDefault = _list.isDefault();
    }

    /**
     * Creates a Node with a String. Used for creating top node or nodes for a
     * better structured view, like Contact nodes or Group nodes
     */
    public PrivacyTreeNode(String nodeName) {
	super(nodeName);
	_isPlaceHolder = true;
    }

    /**
     * Creates a Node with a reference to the PrivacyItem and changes the Name
     * to item's name
     * @param item
     *            the privacyItem which should be displayed as a node
     */
    public PrivacyTreeNode(PrivacyItem item) {
	_item = item;
    }

    /**
     * Returns if this node has a reference to the currently active list
     */
    public boolean isActiveList() {
	return _isActive;
    }

    /**
     * Returns if this node has a reference to the default list
     */
    public boolean isDefaultList() {
	return _isDefault;
    }

    /**
     * Set the list as active list.
     */
    public void setListAsActive() {
	_isActive = true;
	_pmanager.setListAsActive(_list.getListName());
    }

    /**
     * Set the list as default list
     */
    public void setListAsDefault() {
	_isDefault = true;
	_pmanager.setListAsDefault(_list.getListName());
    }

    /**
     * Check if the current node is the parent node for all contacts
     */
    public boolean isContactGroup() {
	return _isContactGroup;
    }

    /**
     * Set this node as the contact node or not.
     */
    public void setisContactGroup(boolean bool) {
	this._isContactGroup = bool;
	this._isPlaceHolder = bool;
    }

    /**
     * Check if the current node is the parent node for all groups
     */
    public boolean isGroupNode() {
	return _isGroupGroup;
    }

    /**
     * Set this node as the group node or not.
     */
    public void setisGroupNode(boolean bool) {
	this._isGroupGroup = bool;
	this._isPlaceHolder = bool;
    }

    /**
     * Get the pricayList this node refers to
     */
    public SparkPrivacyList getPrivacyList() {
	return _list;
    }

    /**
     * Get the privacyItem this node refers to
     */
    public PrivacyItem getPrivacyItem() {
	return _item;
    }

    /**
     * Check if this node represents a PrivacyList
     */
    public boolean isPrivacyList() {
	return _list != null;
    }

    /**
     * Check if this node represents a PrivacyItem
     */
    public boolean isPrivacyItem() {
	return _item != null;
    }

    /**
     * Check if this node is for structured reasons without any reference to
     * privacyItem e.g. (like GroupNode, ContactNode)
     */
    public boolean isStructureNode() {
	return _isPlaceHolder;
    }
}
