All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jface.viewers.TreeNode Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2005, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.viewers;

import java.util.Objects;

/**
 * A simple data structure that is useful for implemented tree models. This can
 * be returned by
 * {@link org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)}.
 * It allows simple delegation of methods from
 * {@link org.eclipse.jface.viewers.ITreeContentProvider} such as
 * {@link org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object)},
 * {@link org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object)} and
 * {@link org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object)}
 *
 * @since 3.2
 */
public class TreeNode {

	/**
	 * The array of child tree nodes for this tree node. If there are no
	 * children, then this value may either by an empty array or
	 * null. There should be no null children in
	 * the array.
	 */
	private TreeNode[] children;

	/**
	 * The parent tree node for this tree node. This value may be
	 * null if there is no parent.
	 */
	private TreeNode parent;

	/**
	 * The value contained in this node. This value may be anything.
	 */
	protected Object value;

	/**
	 * Constructs a new instance of TreeNode.
	 *
	 * @param value
	 *            The value held by this node; may be anything.
	 */
	public TreeNode(final Object value) {
		this.value = value;
	}

	@Override
	public boolean equals(final Object object) {
		if (object instanceof TreeNode) {
			return Objects.equals(this.value, ((TreeNode) object).value);
		}

		return false;
	}

	/**
	 * Returns the child nodes. Empty arrays are converted to null
	 * before being returned.
	 *
	 * @return The child nodes; may be null, but never empty.
	 *         There should be no null children in the array.
	 */
	public TreeNode[] getChildren() {
		if (children != null && children.length == 0) {
			return null;
		}
		return children;
	}

	/**
	 * Returns the parent node.
	 *
	 * @return The parent node; may be null if there are no
	 *         parent nodes.
	 */
	public TreeNode getParent() {
		return parent;
	}

	/**
	 * Returns the value held by this node.
	 *
	 * @return The value; may be anything.
	 */
	public Object getValue() {
		return value;
	}

	/**
	 * Returns whether the tree has any children.
	 *
	 * @return true if its array of children is not
	 *         null and is non-empty; false
	 *         otherwise.
	 */
	public boolean hasChildren() {
		return children != null && children.length > 0;
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(value);
	}

	/**
	 * Sets the children for this node.
	 *
	 * @param children
	 *            The child nodes; may be null or empty. There
	 *            should be no null children in the array.
	 */
	public void setChildren(final TreeNode[] children) {
		this.children = children;
	}

	/**
	 * Sets the parent for this node.
	 *
	 * @param parent
	 *            The parent node; may be null.
	 */
	public void setParent(final TreeNode parent) {
		this.parent = parent;
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy