2 * jSite2 - SortableMutableTreeNode.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.util.swing;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.Enumeration;
26 import java.util.List;
28 import javax.swing.tree.MutableTreeNode;
29 import javax.swing.tree.TreeNode;
32 * {@link MutableTreeNode} subclass that allows to sort its children.
34 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 public class SortableTreeNode implements MutableTreeNode {
38 /** The parent node. */
39 private MutableTreeNode parentNode;
41 /** The user-defined object. */
42 private Object userObject;
44 /** Whether this node allows children. */
45 private boolean allowsChildren;
47 /** The children of this node. */
48 private List<MutableTreeNode> children = new ArrayList<MutableTreeNode>();
51 * Creates a new sortable tree node.
53 * @param allowsChildren
54 * <code>true</code> if this node allows children,
55 * <code>false</code> otherwise
57 public SortableTreeNode(boolean allowsChildren) {
58 this(null, allowsChildren);
62 * Creates a new sortable tree node that contains the given user-defined
66 * The user-defined object
68 public SortableTreeNode(Object userObject) {
69 this(userObject, true);
73 * Creates a new sortable tree node that contains the given user-defined
77 * The user-defined object
78 * @param allowsChildren
79 * <code>true</code> if this node allows children,
80 * <code>false</code> otherwise
82 public SortableTreeNode(Object userObject, boolean allowsChildren) {
83 this.allowsChildren = allowsChildren;
84 this.userObject = userObject;
94 public boolean getAllowsChildren() {
95 return allowsChildren;
101 public TreeNode getChildAt(int childIndex) {
102 return children.get(childIndex);
108 public int getChildCount() {
109 return children.size();
115 public int getIndex(TreeNode node) {
116 return children.indexOf(node);
122 public TreeNode getParent() {
127 * Returns the user-defined object.
129 * @return The user-defined object
131 public Object getUserObject() {
138 public boolean isLeaf() {
139 return children.isEmpty();
145 public Enumeration<?> children() {
146 return Collections.enumeration(children);
154 * Adds the given node to this node as a child.
157 * The child node to add
159 public void add(MutableTreeNode child) {
161 child.setParent(this);
167 public void insert(MutableTreeNode child, int index) {
168 children.add(index, child);
169 child.setParent(this);
175 public void remove(int index) {
176 children.remove(index).setParent(null);
182 public void remove(MutableTreeNode node) {
183 children.remove(node);
184 node.setParent(null);
190 public void removeFromParent() {
191 if (parentNode != null) {
192 parentNode.remove(this);
198 * Removes all children of this node.
200 public void removeAll() {
201 for (MutableTreeNode childNode : children) {
202 childNode.setParent(null);
210 public void setParent(MutableTreeNode newParent) {
211 parentNode = newParent;
217 public void setUserObject(Object userObject) {
218 this.userObject = userObject;
222 * Sorts the children of this node.
225 Collections.sort(children, new Comparator<MutableTreeNode>() {
230 @SuppressWarnings( { "synthetic-access", "unchecked" })
231 public int compare(MutableTreeNode firstNode, MutableTreeNode secondNode) {
232 if (!(firstNode instanceof SortableTreeNode) || !(secondNode instanceof SortableTreeNode)) {
235 SortableTreeNode firstSortableNode = (SortableTreeNode) firstNode;
236 SortableTreeNode secondSortableNode = (SortableTreeNode) secondNode;
237 if ((firstSortableNode.userObject == null) && (secondSortableNode.userObject == null)) {
240 if ((firstSortableNode.userObject == null) && (secondSortableNode.userObject != null)) {
243 if ((firstSortableNode.userObject != null) && (secondSortableNode.userObject == null)) {
246 if (!(firstSortableNode.userObject instanceof Comparable) || !(secondSortableNode.userObject instanceof Comparable)) {
249 return ((Comparable<Object>) firstSortableNode.userObject).compareTo(secondSortableNode.userObject);
258 public String toString() {
259 return (userObject != null) ? userObject.toString() : null;