1
2
3
4
5 package de.mindcrimeilab.xsanalyzer.model;
6
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.List;
10 import java.util.Vector;
11
12 import javax.swing.event.TreeModelListener;
13 import javax.swing.tree.TreeModel;
14 import javax.swing.tree.TreePath;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.apache.xerces.xs.XSObject;
19
20 import de.mindcrimeilab.xsanalyzer.util.XSModelHelper;
21
22
23
24
25
26
27
28 public class TypeHierarchyTreeModelAdapter implements TreeModel {
29
30 private static final Log logger = LogFactory.getLog("xsAnalyzerApplicationLogger");
31
32 private final Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();
33
34 private final List<XSObject> hierarchy;
35
36 public TypeHierarchyTreeModelAdapter(XSObject object) {
37 hierarchy = TypeHierarchyTreeModelAdapter.createModel(object);
38 }
39
40 private static List<XSObject> createModel(XSObject object) {
41 List<XSObject> list = new ArrayList<XSObject>();
42 list.add(object);
43 XSObject parent = object;
44 while (null != (parent = XSModelHelper.getBaseType(parent))) {
45 if (list.contains(parent)) {
46 break;
47 }
48 list.add(parent);
49 }
50
51 Collections.reverse(list);
52 return list;
53 }
54
55
56
57
58
59
60 @Override
61 public void addTreeModelListener(TreeModelListener listener) {
62 treeModelListeners.addElement(listener);
63 }
64
65
66
67
68
69
70 @Override
71 public Object getChild(Object parent, int index) {
72 int childIndex = hierarchy.indexOf(parent) + 1;
73 return (childIndex < hierarchy.size() && index < getChildCount(parent)) ? hierarchy.get(childIndex) : null;
74 }
75
76
77
78
79
80
81 @Override
82 public int getChildCount(Object parent) {
83 int childIndex = hierarchy.indexOf(parent) + 1;
84 return (null != parent && childIndex < hierarchy.size()) ? 1 : 0;
85 }
86
87
88
89
90
91
92 @Override
93 public int getIndexOfChild(Object parent, Object child) {
94 return (null == parent || null == child) ? -1 : 0;
95 }
96
97
98
99
100
101
102 @Override
103 public Object getRoot() {
104 return hierarchy.get(0);
105 }
106
107
108
109
110
111
112 @Override
113 public boolean isLeaf(Object value) {
114 return !(hierarchy.indexOf(value) + 1 < hierarchy.size());
115 }
116
117
118
119
120
121
122 @Override
123 public void removeTreeModelListener(TreeModelListener listener) {
124 treeModelListeners.removeElement(listener);
125 }
126
127
128
129
130
131
132 @Override
133 public void valueForPathChanged(TreePath path, Object newValue) {
134 TypeHierarchyTreeModelAdapter.logger.debug("*** valueForPathChanged : " + path + " --> " + newValue);
135 }
136
137 }