LazyTreeNode.java

biz/hammurapi/swing/LazyTreeNode.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 081 Avoid static collections, they can grow in size over time. 2 30:9
Java Inspector 089 Undocumented field 2 24:9
Java Inspector 089 Undocumented field 2 26:9
Java Inspector 089 Undocumented field 2 28:9
Java Inspector 089 Undocumented field 2 30:9
Java Inspector 089 Undocumented method 2 32:9
Java Inspector 089 Undocumented constructor 2 36:9
Java Inspector 089 Undocumented constructor 2 41:9
Java Inspector 089 Undocumented constructor 2 46:9
Java Inspector 089 Undocumented parameter visitor 2 53:9
Java Inspector 089 Method return value is not documented 2 53:9
Java Inspector 089 Undocumented method 2 63:9
Java Inspector 089 Method return value is not properly documented 2 77:9
Java Inspector 089 Undocumented method 2 79:9
Java Inspector 089 Undocumented method 2 83:9
Java Inspector 089 Undocumented method 2 87:9
Java Inspector 089 Undocumented method 2 91:9
Java Inspector 089 Undocumented method 2 95:9
Java Inspector 089 Undocumented method 2 99:9
Java Inspector 089 Undocumented method 2 103:9
Java Inspector 089 Undocumented method 2 107:9
Java Inspector 089 Method is not properly documented 2 114:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 36:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 41:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 46:9

Source code

1package biz.hammurapi.swing;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Enumeration;
6import java.util.List;
7
8import javax.swing.tree.TreeNode;
9
10import biz.hammurapi.util.CollectionVisitable;
11import biz.hammurapi.util.Visitable;
12import biz.hammurapi.util.Visitor;
13
14/**
15 * Base class for tree nodes which load their children on demand.
16 * Subclasses shall implement loadChildren() method.
17 * @author Pavel
18 *
19 */
20public abstract class LazyTreeNode implements TreeNode, Visitable {
21
22 private List children;
23
24 protected String name;
25
26 protected TreeNode parent;
27
28 protected String description;
29
30 protected static final List EMPTY_LIST = Collections.unmodifiableList(new ArrayList());
31
32 public String toString() {
33 return name;
34 }
35
36 public LazyTreeNode(TreeNode parent, String name) {
37 this(parent);
38 this.name = name;
39 }
40
41 protected LazyTreeNode(TreeNode parent, String name, List children) {
42 this(parent, name);
43 this.children = children;
44 }
45
46 public LazyTreeNode(TreeNode parent) {
47 this.parent = parent;
48 }
49
50 /**
51 * Visits only children if they are loaded.
52 */
53 public boolean accept(Visitor visitor) {
54 if (visitor.visit(this)) {
55 if (children!=null) {
56 new CollectionVisitable(children, false).accept(visitor);
57 }
58 return true;
59 }
60 return false;
61 }
62
63 protected List getChildren() {
64 if (children==null) {
65 children = loadChildren();
66 if (children == null) {
67 children = EMPTY_LIST;
68 }
69 }
70 return children;
71 }
72
73 /**
74 * Loads node children.
75 * @return
76 */
77 protected abstract List loadChildren();
78
79 public Enumeration children() {
80 return Collections.enumeration(getChildren());
81 }
82
83 public boolean getAllowsChildren() {
84 return true;
85 }
86
87 public TreeNode getChildAt(int childIndex) {
88 return (TreeNode) getChildren().get(childIndex);
89 }
90
91 public int getChildCount() {
92 return getChildren().size();
93 }
94
95 public int getIndex(TreeNode node) {
96 return getChildren().indexOf(node);
97 }
98
99 public TreeNode getParent() {
100 return parent;
101 }
102
103 public boolean isLeaf() {
104 return getChildren().isEmpty();
105 }
106
107 public boolean isSelectable() {
108 return true;
109 }
110
111 /**
112 * @return Text for rendering tooltip.
113 */
114 public String getDescription() {
115 return description;
116 }
117}
118