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.Comparator;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import javax.swing.event.TreeModelListener;
29 import javax.swing.tree.TreeModel;
30 import javax.swing.tree.TreePath;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.xerces.xs.XSConstants;
35 import org.apache.xerces.xs.XSModel;
36 import org.apache.xerces.xs.XSNamedMap;
37 import org.apache.xerces.xs.XSObject;
38 import org.apache.xerces.xs.XSTypeDefinition;
39
40 import de.mindcrimeilab.xsanalyzer.util.NameComparator;
41
42
43
44
45
46
47
48 public class TreeModelAdapter implements TreeModel {
49
50 private static final Log logger = LogFactory.getLog("xsAnalyzerApplicationLogger");
51
52 private final Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();
53
54 private final static String ROOT_NODE = "schema.tree.root.label";
55
56 private final static String COMPLEX_TYPES_NODE = "schema.tree.complexTypes.label";
57
58 private final static String SIMPLE_TYPES_NODE = "schema.tree.simpleTypes.label";
59
60 private final static String ELEMENTS_NODE = "schema.tree.elements.label";
61
62 private final static String GROUPS_NODE = "schema.tree.groups.label";
63
64 private final static String[] LEVEL1_NODES = { TreeModelAdapter.COMPLEX_TYPES_NODE, TreeModelAdapter.SIMPLE_TYPES_NODE, TreeModelAdapter.ELEMENTS_NODE, TreeModelAdapter.GROUPS_NODE};
65
66 private final Map<Short, List<XSObject>> model;
67
68 private final XSModel xsmodel;
69
70
71
72 public TreeModelAdapter(XSModel xsmodel) {
73 this.xsmodel = xsmodel;
74 model = TreeModelAdapter.createModel(this.xsmodel);
75 updateModel(true);
76 }
77
78
79
80
81
82
83 @Override
84 public void addTreeModelListener(TreeModelListener l) {
85 treeModelListeners.addElement(l);
86 }
87
88
89
90
91
92
93 @Override
94 public Object getChild(Object parent, int index) {
95 Object obj = null;
96 if (TreeModelAdapter.ROOT_NODE == parent) {
97 obj = TreeModelAdapter.LEVEL1_NODES[index];
98 }
99 else if (TreeModelAdapter.COMPLEX_TYPES_NODE == parent) {
100 obj = getComponent(XSTypeDefinition.COMPLEX_TYPE, index);
101 }
102 else if (TreeModelAdapter.SIMPLE_TYPES_NODE == parent) {
103 obj = getComponent(XSTypeDefinition.SIMPLE_TYPE, index);
104 }
105 else if (TreeModelAdapter.ELEMENTS_NODE == parent) {
106 obj = getComponent(XSConstants.ELEMENT_DECLARATION, index);
107 }
108 else if (TreeModelAdapter.GROUPS_NODE == parent) {
109 obj = getComponent(XSConstants.MODEL_GROUP_DEFINITION, index);
110 }
111 else {
112 obj = null;
113 }
114
115 return obj;
116 }
117
118
119
120
121
122
123 @Override
124 public int getChildCount(Object parent) {
125 int count = 0;
126 if (TreeModelAdapter.ROOT_NODE == parent) {
127 count = TreeModelAdapter.LEVEL1_NODES.length;
128 }
129 else if (TreeModelAdapter.COMPLEX_TYPES_NODE == parent) {
130 count = getLength(XSTypeDefinition.COMPLEX_TYPE);
131 }
132 else if (TreeModelAdapter.SIMPLE_TYPES_NODE == parent) {
133 count = getLength(XSTypeDefinition.SIMPLE_TYPE);
134 }
135 else if (TreeModelAdapter.ELEMENTS_NODE == parent) {
136 count = getLength(XSConstants.ELEMENT_DECLARATION);
137 }
138 else if (TreeModelAdapter.GROUPS_NODE == parent) {
139 count = getLength(XSConstants.MODEL_GROUP_DEFINITION);
140 }
141 else {
142 count = 0;
143 }
144
145 return count;
146 }
147
148
149
150
151
152
153 @Override
154 public int getIndexOfChild(Object parent, Object child) {
155 int index = 0;
156 if (TreeModelAdapter.ROOT_NODE == parent) {
157 for (int i = 0; i < TreeModelAdapter.LEVEL1_NODES.length; ++i) {
158 if (TreeModelAdapter.LEVEL1_NODES[i].equals(child)) {
159 index = i;
160 break;
161 }
162 }
163 }
164 else if (TreeModelAdapter.COMPLEX_TYPES_NODE == parent) {
165 index = indexOf(XSTypeDefinition.COMPLEX_TYPE, child);
166 }
167 else if (TreeModelAdapter.SIMPLE_TYPES_NODE == parent) {
168 index = indexOf(XSTypeDefinition.SIMPLE_TYPE, child);
169 }
170 else if (TreeModelAdapter.ELEMENTS_NODE == parent) {
171 index = indexOf(XSConstants.ELEMENT_DECLARATION, child);
172 }
173 else if (TreeModelAdapter.GROUPS_NODE == parent) {
174 index = indexOf(XSConstants.MODEL_GROUP_DEFINITION, child);
175 }
176 else {
177 index = 0;
178 }
179 return index;
180 }
181
182
183
184
185
186
187 @Override
188 public Object getRoot() {
189 return TreeModelAdapter.ROOT_NODE;
190 }
191
192
193
194
195
196
197 @Override
198 public boolean isLeaf(Object node) {
199 return (0 == getChildCount(node));
200 }
201
202
203
204
205
206
207 @Override
208 public void removeTreeModelListener(TreeModelListener l) {
209 treeModelListeners.removeElement(l);
210 }
211
212
213
214
215
216
217 @Override
218 public void valueForPathChanged(TreePath path, Object newValue) {
219 TreeModelAdapter.logger.debug("*** valueForPathChanged : " + path + " --> " + newValue);
220 }
221
222
223
224
225
226
227
228 private Object getComponent(short componentType, int index) {
229 List<XSObject> list = model.get(componentType);
230 return (null == list) ? null : list.get(index);
231 }
232
233 private int getLength(short componentType) {
234 List<XSObject> list = model.get(componentType);
235 return (null == list) ? 0 : list.size();
236 }
237
238 private int indexOf(short componentType, Object object) {
239 List<XSObject> list = model.get(componentType);
240 return (null != list) ? list.indexOf(object) : null;
241 }
242
243 private void updateModel(boolean sort) {
244 if (sort) {
245 final Comparator<XSObject> comparator = new NameComparator();
246 for (List<XSObject> list : model.values()) {
247 Collections.sort(list, comparator);
248 }
249 }
250 }
251
252 private final static Map<Short, List<XSObject>> createModel(XSModel xsmodel) {
253 Map<Short, List<XSObject>> map = new HashMap<Short, List<XSObject>>();
254
255 short[] types = { XSTypeDefinition.COMPLEX_TYPE, XSTypeDefinition.SIMPLE_TYPE, XSConstants.ELEMENT_DECLARATION, XSConstants.MODEL_GROUP_DEFINITION};
256
257 for (short type : types) {
258 XSNamedMap xsMap = xsmodel.getComponents(type);
259 if (null == xsMap) {
260 map.put(type, null);
261 continue;
262 }
263 List<XSObject> xsoList = new ArrayList<XSObject>(xsMap.getLength());
264 for (int i = 0; i < xsMap.getLength(); ++i) {
265 xsoList.add(xsMap.item(i));
266 }
267
268 map.put(type, xsoList);
269 }
270
271 return map;
272 }
273
274 }