Source code: ledestin/swing/SetTreeNode.java
1 /*
2 * Copyright (C) 2000-2001 Dmitry Macsema. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted under the terms of the following
6 * Open Source license:
7 *
8 * The GNU General Public License, version 2, or any later version, as
9 * published by the Free Software Foundation
10 * (http://www.fsf.org/copyleft/gpl.html);
11 */
12
13 package ledestin.swing;
14
15 import java.util.*;
16 import javax.swing.*;
17 import javax.swing.tree.*;
18
19 /**
20 A tree node storing its children as a TreeSet.
21 */
22 public class SetTreeNode implements MutableTreeNode {
23 private TreeSet children = new TreeSet();
24 private MutableTreeNode parent;
25 private String title;
26
27 public SetTreeNode(String title) {
28 this(title, null);
29 }
30 public SetTreeNode(String title, MutableTreeNode parent) {
31 this.title = title;
32 this.parent = parent;
33 }
34 public Enumeration children() {
35 return Collections.enumeration(children);
36 }
37 public boolean getAllowsChildren() {
38 return true;
39 }
40 public TreeNode getChildAt(int childIndex) {
41 Object[] a = children.toArray();
42 return (TreeNode)a[childIndex];
43 }
44 public int getChildCount() {
45 return children.size();
46 }
47 public int getIndex(TreeNode node) {
48 Iterator it = children.iterator();
49 int i = 0;
50 while (it.hasNext()) {
51 TreeNode anode = (TreeNode)it.next();
52 i++;
53 if (node == anode) {
54 break;
55 }
56 }
57 return i;
58 }
59 public TreeNode getParent() {
60 return parent;
61 }
62 public boolean isLeaf() {
63 return false;
64 }
65 public void insert(MutableTreeNode child, int index) {
66 children.add(child);
67 }
68 public void remove(int index) {
69 }
70 public void remove(MutableTreeNode node) {
71 children.remove(node);
72 }
73 public void removeFromParent() {
74 parent.remove(this);
75 }
76 public void setParent(MutableTreeNode newParent) {
77 parent = newParent;
78 }
79 public void setUserObject(Object object) {
80 }
81 public String toString() {
82 return title;
83 }
84 }