1 package de.matthias_burbach.deputy.swing;
2
3 import javax.swing.tree.DefaultMutableTreeNode;
4
5 import de.matthias_burbach.deputy.core.project.ProjectQualifier;
6
7 /***
8 * Is a base class for tree nodes which need to initialize child nodes
9 * explicitly and lazily.
10 *
11 * @author Matthias Burbach
12 */
13 public abstract class BaseMutableTreeNode extends DefaultMutableTreeNode {
14 /***
15 * Whether children have already been initialized.
16 */
17 private boolean childrenAreInitialized = false;
18
19 /***
20 * Initializes the children nodes of this node.
21 */
22 protected abstract void initChildren();
23
24
25
26
27 /***
28 * {@inheritDoc}
29 */
30 public int getChildCount() {
31 if (!childrenAreInitialized) {
32 childrenAreInitialized = true;
33 initChildren();
34 }
35 return super.getChildCount();
36 }
37
38
39
40
41 /***
42 * {@inheritDoc}
43 */
44 public boolean isLeaf() {
45 return getChildCount() <= 0;
46 }
47
48 /***
49 * @return Whether children have already been initialized.
50 */
51 protected boolean childrenAreInitialized() {
52 return childrenAreInitialized;
53 }
54
55 /***
56 * Helper method to format the suffix to certain project names.
57 *
58 * @param cause The qualifier of the project that has caused a relationship.
59 * @return The suffix to be appended to the project name in the tree node.
60 */
61 protected String getSuffix(final ProjectQualifier cause) {
62 String result = null;
63 if (cause != null) {
64 result = " (" + cause.getVersion() + ")";
65 }
66 return result;
67 }
68 }