1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.mindcrimeilab.xsanalyzer.model;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.Vector;
26
27 import javax.swing.event.TreeModelListener;
28 import javax.swing.tree.DefaultMutableTreeNode;
29 import javax.swing.tree.TreeModel;
30 import javax.swing.tree.TreePath;
31
32 import org.apache.xerces.xs.XSObject;
33 import org.apache.xerces.xs.XSObjectList;
34
35 import de.mindcrimeilab.xsanalyzer.util.DefaultMutableTreeNodeComparator;
36 import de.mindcrimeilab.xsanalyzer.util.NameComparator;
37
38
39
40
41
42
43
44 public class UsedTypesTreeModelAdapter implements TreeModel {
45
46 private final static String ROOT_NODE = "Used Types";
47
48 private final Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();
49
50 private final Map<XSObject, ? extends XSObjectList> usedBy;
51
52 private final List<DefaultMutableTreeNode> types;
53
54 public UsedTypesTreeModelAdapter(Map<XSObject, ? extends XSObjectList> usage) {
55 usedBy = usage;
56 final Set<XSObject> keySet = usedBy.keySet();
57 types = new ArrayList<DefaultMutableTreeNode>(keySet.size());
58 for (XSObject xso : keySet) {
59 if (null != xso) {
60 types.add(new DefaultMutableTreeNode(xso));
61 }
62 }
63 Collections.sort(types, new DefaultMutableTreeNodeComparator(new NameComparator()));
64 }
65
66
67
68
69
70
71 @Override
72 public void addTreeModelListener(TreeModelListener l) {
73 treeModelListeners.addElement(l);
74 }
75
76
77
78
79
80
81 @Override
82 public Object getChild(Object parent, int index) {
83 Object child;
84 if (parent == UsedTypesTreeModelAdapter.ROOT_NODE) {
85 child = types.get(index);
86 }
87 else if (parent instanceof DefaultMutableTreeNode) {
88 DefaultMutableTreeNode tn = (DefaultMutableTreeNode) parent;
89 XSObjectList list = usedBy.get(tn.getUserObject());
90 child = list.item(index);
91 }
92 else {
93 child = null;
94 }
95 return child;
96 }
97
98
99
100
101
102
103 @Override
104 public int getChildCount(Object parent) {
105 int count;
106 if (parent == UsedTypesTreeModelAdapter.ROOT_NODE) {
107 count = types.size();
108 }
109 else if (parent instanceof DefaultMutableTreeNode) {
110 DefaultMutableTreeNode tn = (DefaultMutableTreeNode) parent;
111 XSObjectList list = usedBy.get(tn.getUserObject());
112 count = list.getLength();
113 }
114 else {
115 count = 0;
116 }
117 return count;
118 }
119
120
121
122
123
124
125 @Override
126 public int getIndexOfChild(Object parent, Object child) {
127 int index = 0;
128 if (parent == UsedTypesTreeModelAdapter.ROOT_NODE) {
129 index = types.indexOf(child);
130 }
131 else if (parent instanceof DefaultMutableTreeNode) {
132 DefaultMutableTreeNode tn = (DefaultMutableTreeNode) parent;
133 XSObjectList list = usedBy.get(tn.getUserObject());
134 for (int i = 0; i < list.getLength(); ++i) {
135 if (child.equals(list.item(i))) {
136 index = i;
137 break;
138 }
139 }
140 }
141 else {
142 index = 0;
143 }
144 return index;
145 }
146
147
148
149
150
151
152 @Override
153 public Object getRoot() {
154 return UsedTypesTreeModelAdapter.ROOT_NODE;
155 }
156
157
158
159
160
161
162 @Override
163 public boolean isLeaf(Object node) {
164 return 0 == getChildCount(node);
165 }
166
167
168
169
170
171
172 @Override
173 public void removeTreeModelListener(TreeModelListener l) {
174 treeModelListeners.removeElement(l);
175 }
176
177
178
179
180
181
182 @Override
183 public void valueForPathChanged(TreePath path, Object newValue) {
184 System.out.println("*** valueForPathChanged : " + path + " --> " + newValue);
185 }
186
187 }