root / org.gvsig.proj / branches / refactor2018 / org.gvsig.proj / org.gvsig.proj.swing / org.gvsig.proj.swing.impl / src / main / java / org / gvsig / proj / swing / impl / tree / BranchNode.java @ 866
History | View | Annotate | Download (3.27 KB)
1 |
package org.gvsig.proj.swing.impl.tree; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collections; |
5 |
import java.util.Enumeration; |
6 |
|
7 |
import javax.swing.Icon; |
8 |
import javax.swing.tree.TreeNode; |
9 |
|
10 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
11 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
12 |
|
13 |
public class BranchNode implements TreeNode { |
14 |
final protected TreeNode parent; |
15 |
protected ArrayList<TreeNode> children = new ArrayList<TreeNode>(); |
16 |
final protected String message; |
17 |
protected Icon expandedIcon; |
18 |
protected Icon collapsedIcon; |
19 |
final protected TreeContainer container; |
20 |
final protected IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
21 |
|
22 |
public BranchNode(TreeNode parent, |
23 |
TreeContainer container, |
24 |
String message
|
25 |
) { |
26 |
this(parent, container, message, null, null); |
27 |
} |
28 |
|
29 |
|
30 |
public BranchNode(TreeNode parent, |
31 |
TreeContainer container, |
32 |
String message,
|
33 |
Icon icon
|
34 |
) { |
35 |
this(parent, container, message, icon, icon);
|
36 |
} |
37 |
|
38 |
public BranchNode(TreeNode parent, |
39 |
TreeContainer container, |
40 |
String message,
|
41 |
Icon expandedIcon,
|
42 |
Icon collapsedIcon
|
43 |
) { |
44 |
this.container = container;
|
45 |
this.parent = parent;
|
46 |
this.message = message;
|
47 |
this.expandedIcon = expandedIcon;
|
48 |
this.collapsedIcon = collapsedIcon;
|
49 |
} |
50 |
|
51 |
public void add(TreeNode child) { |
52 |
children.add(child); |
53 |
} |
54 |
|
55 |
public void clear() { |
56 |
this.children.clear();
|
57 |
} |
58 |
|
59 |
/**
|
60 |
* Requests children to be loaded if they have not been already
|
61 |
* loaded. Subclasses may load children in an asynchronous way
|
62 |
*/
|
63 |
public void loadChildren() { |
64 |
System.out.println("load"); |
65 |
} |
66 |
|
67 |
/**
|
68 |
* Requests children to be loaded (forcing the load even if they
|
69 |
* were already loaded). Subclasses may load children in an asynchronous way
|
70 |
*/
|
71 |
public void reloadChildren() { |
72 |
for (TreeNode node: children) { |
73 |
if (node instanceof BranchNode) { |
74 |
((BranchNode)node).reloadChildren(); |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
@Override
|
80 |
public TreeNode getParent() { |
81 |
return parent;
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public boolean isLeaf() { |
86 |
return false; |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public boolean getAllowsChildren() { |
91 |
return true; |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public TreeNode getChildAt(int childIndex) { |
96 |
return children.get(childIndex);
|
97 |
} |
98 |
|
99 |
@Override
|
100 |
public int getChildCount() { |
101 |
return children.size();
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public int getIndex(TreeNode node) { |
106 |
if (!isNodeChild(node)) {
|
107 |
return -1; |
108 |
} |
109 |
return children.indexOf(node);
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* Returns true if <code>aNode</code> is a child of this node. If
|
114 |
* <code>aNode</code> is null, this method returns false.
|
115 |
*
|
116 |
* @return true if <code>aNode</code> is a child of this node; false if
|
117 |
* <code>aNode</code> is null
|
118 |
*/
|
119 |
protected boolean isNodeChild(TreeNode aNode) { |
120 |
if (aNode == null) { |
121 |
return false; |
122 |
} |
123 |
if (!(aNode instanceof CrsTreeNode)) { |
124 |
return false; |
125 |
} |
126 |
if (getChildCount() == 0) { |
127 |
return false; |
128 |
} else {
|
129 |
return (aNode.getParent() == this); |
130 |
} |
131 |
} |
132 |
|
133 |
@SuppressWarnings("rawtypes") |
134 |
@Override
|
135 |
public Enumeration children() { |
136 |
return Collections.enumeration(children); |
137 |
} |
138 |
|
139 |
public Icon getIcon(boolean expanded) { |
140 |
if (expanded) {
|
141 |
return expandedIcon;
|
142 |
} |
143 |
return collapsedIcon;
|
144 |
} |
145 |
|
146 |
@Override
|
147 |
public String toString() { |
148 |
return this.message; |
149 |
} |
150 |
|
151 |
} |