1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.pkg;
21
22 import java.util.Hashtable;
23 import java.util.Iterator;
24 import java.util.StringTokenizer;
25 import javax.swing.JTree;
26 import javax.swing.tree.DefaultMutableTreeNode;
27 import javax.swing.tree.DefaultTreeModel;
28 import javax.swing.tree.TreeNode;
29 import javax.swing.tree.TreePath;
30 import org.xnap.commons.i18n.I18n;
31 import org.xnap.commons.pkg.PackageInfo;
32 import org.xnap.commons.util.StringHelper;
33
34 public class PackageInfoTree extends JTree {
35
36 private DefaultMutableTreeNode root;
37 private DefaultTreeModel tmPackages;
38 private Hashtable nodeBySectionName = new Hashtable();
39
40 public PackageInfoTree(String rootLabel)
41 {
42 root = new DefaultMutableTreeNode(rootLabel);
43 tmPackages = new DefaultTreeModel(root);
44 setModel(tmPackages);
45 }
46
47 public PackageInfoTree()
48 {
49 this(I18n.tr("Packages"));
50 }
51
52 public void createPackageNode(DefaultMutableTreeNode parent,
53 PackageInfo info)
54 {
55 String section = "/" + info.getSection() + "/" + info.getName();
56 DefaultMutableTreeNode node
57 = (DefaultMutableTreeNode)nodeBySectionName.get(section);
58 if (node == null) {
59 ContainerPackageSection container
60 = new ContainerPackageSection(info.getName());
61 container.setPackageInfo(info);
62
63 node = new DefaultMutableTreeNode(container);
64 nodeBySectionName.put(section, node);
65 parent.add(node);
66 }
67 else if (node.getUserObject() instanceof ContainerPackageSection) {
68 ContainerPackageSection container
69 = (ContainerPackageSection)node.getUserObject();
70 container.setPackageInfo(info);
71
72 container.setUpdateAvailable(!info.isInstalled());
73 }
74
75
76 node.insert(new DefaultMutableTreeNode(info), 0);
77 }
78
79 /***
80 * Creates a section node and returns it.
81 */
82 public DefaultMutableTreeNode createSectionNodes(String sectionName)
83 {
84 DefaultMutableTreeNode parent = root;
85
86 if (sectionName == null) {
87 return getNodeBySection(parent, I18n.tr("Default"));
88 }
89
90 StringBuffer totalSection = new StringBuffer(sectionName.length());
91 StringTokenizer t = new StringTokenizer(sectionName, "/");
92 while (t.hasMoreTokens()) {
93 totalSection.append("/");
94 totalSection.append(t.nextToken());
95
96 DefaultMutableTreeNode node
97 = getNodeBySection(parent, totalSection.toString());
98 parent = node;
99 }
100 return parent;
101 }
102
103 private DefaultMutableTreeNode getNodeBySection
104 (DefaultMutableTreeNode parent, String section)
105 {
106 DefaultMutableTreeNode node
107 = (DefaultMutableTreeNode)nodeBySectionName.get(section);
108 if (node == null) {
109 node = new DefaultMutableTreeNode
110 (StringHelper.lastToken(section, "/"));
111 nodeBySectionName.put(section, node);
112 parent.add(node);
113 }
114 return node;
115 }
116
117 public PackageInfo getSelectedInfo()
118 {
119 if (getSelectionPath() != null) {
120 DefaultMutableTreeNode node = (DefaultMutableTreeNode)
121 getSelectionPath().getLastPathComponent();
122
123 if (node.getUserObject() instanceof ContainerPackageSection) {
124 return ((ContainerPackageSection)node.getUserObject())
125 .getPackageInfo();
126 }
127 if (node.getUserObject() instanceof PackageInfo) {
128 return (PackageInfo)node.getUserObject();
129 }
130 }
131 return null;
132 }
133
134 public void pathChanged(TreePath path)
135 {
136 tmPackages.nodeChanged((TreeNode)path.getLastPathComponent());
137 }
138
139 public void reload()
140 {
141 tmPackages.reload();
142
143
144 expandRow(0);
145 }
146
147 public void selectedNodeChanged()
148 {
149 tmPackages.nodeChanged
150 ((TreeNode)getSelectionPath().getLastPathComponent());
151 }
152
153 public void updatePackages(Iterator it)
154 {
155 root.removeAllChildren();
156 nodeBySectionName.clear();
157
158 while (it.hasNext()) {
159 PackageInfo info = (PackageInfo)it.next();
160 DefaultMutableTreeNode parent
161 = createSectionNodes(info.getSection());
162 createPackageNode(parent, info);
163 }
164
165
166 reload();
167 }
168
169 private void expandNodes(TreeNode child)
170 {
171 expandPath(new TreePath(new Object[] { root, child }));
172 for (int i = child.getChildCount() - 1; i >= 0; i--) {
173 Object[] path
174 = new Object[] { root, child, child.getChildAt(i) };
175 expandPath(new TreePath(path));
176 }
177 }
178
179 }