1 package org.kuali.student.lum.common.client.lo;
2
3 import com.google.gwt.user.client.ui.Label;
4
5 import org.kuali.student.common.assembly.data.Data;
6 import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport;
7 import org.kuali.student.common.ui.client.mvc.DataModel;
8 import org.kuali.student.common.ui.client.widgets.menus.KSListPanel;
9
10 import java.util.*;
11
12
13
14
15 public class TreeStringBinding extends ModelWidgetBindingSupport<KSListPanel> {
16
17 @Override
18 public void setModelValue(KSListPanel widget, DataModel model, String path) {
19 throw new UnsupportedOperationException("Method is not supported");
20 }
21
22 @Override
23 public void setWidgetValue(KSListPanel listPanel, DataModel model, String path) {
24 listPanel.clear();
25 Data loData = model.get(path);
26 if (loData != null) {
27 StringTreeStructure rootModel = new StringTreeStructure();
28 transform(loData, rootModel);
29 bind(rootModel, listPanel);
30 }
31 }
32
33 private void transform(Data loData, StringTreeStructure rootModel) {
34 for (Data.Property property : loData) {
35 Data loDisplayInfoData = property.getValue();
36 LoDisplayInfoHelper loDisplayInfoHelper = new LoDisplayInfoHelper(loDisplayInfoData);
37 LoInfoHelper loInfoHelper = new LoInfoHelper(loDisplayInfoHelper.getLoInfo());
38 RichTextHelper descriptionHelper = new RichTextHelper(loInfoHelper.getDesc());
39 Data categoriesData = loDisplayInfoHelper.getCategoryInfoList();
40 List<String> categories = new ArrayList<String>();
41
42 if (null != categoriesData) {
43 Iterator<Data.Property> itr = categoriesData.realPropertyIterator();
44
45 while (itr.hasNext()) {
46 Data.Property catProp = itr.next();
47 Data catData = catProp.getValue();
48 LoCategoryInfoHelper category = new LoCategoryInfoHelper(catData);
49 categories.add(category.getName());
50 }
51 }
52
53 int index = 0;
54 String sequence = loInfoHelper.getSequence();
55 if (sequence != null) {
56 index = Integer.parseInt(sequence);
57 }
58 StringTreeStructure childModel = new StringTreeStructure(index, descriptionHelper.getPlain(), categories);
59 transform(loDisplayInfoHelper.getDisplayInfoList(), childModel);
60 rootModel.addChild(childModel);
61 }
62 }
63
64 private void bind(StringTreeStructure stringTreeStructure, KSListPanel ksListPanel) {
65 stringTreeStructure.sortChildren();
66 List<StringTreeStructure> firstLevelChildren = stringTreeStructure.getChildren();
67 for (StringTreeStructure firstLevelChild : firstLevelChildren) {
68 addElement(firstLevelChild, ksListPanel);
69 }
70 }
71
72 private void addElement(StringTreeStructure element, KSListPanel listPanel) {
73 if (element.hasChildren()) {
74 KSListPanel subPanel = new KSListPanel();
75 element.sortChildren();
76 for (StringTreeStructure child : element.getChildren()) {
77 addElement(child, subPanel);
78 }
79 if (!element.getCategories().isEmpty()) {
80 listPanel.add(subPanel, element.getValue() + element.getCategoriesString());
81 } else {
82 listPanel.add(subPanel, element.getValue());
83 }
84 } else {
85 if (!element.getCategories().isEmpty()) {
86 listPanel.add(new Label(element.getValue() + element.getCategoriesString()));
87 } else {
88 listPanel.add(new Label(element.getValue()));
89 }
90 }
91 }
92
93 private static class StringTreeStructure {
94
95 private String value;
96 private int index;
97
98 private List<StringTreeStructure> children = new ArrayList<StringTreeStructure>();
99 private List<String> categories = new LinkedList<String>();
100
101 public StringTreeStructure(int index, String value, List<String> categories) {
102 this.setIndex(index);
103 this.value = value;
104 this.categories = categories;
105 }
106
107 public void sortChildren() {
108 Collections.sort(children, new Comparator<StringTreeStructure>() {
109
110 @Override
111 public int compare(StringTreeStructure o1, StringTreeStructure o2) {
112 return o1.getIndex() - o2.getIndex();
113 }
114 });
115 }
116
117 public StringTreeStructure() {}
118
119 public List<String> getCategories() {
120 return categories;
121 }
122
123 public String getValue() {
124 return value;
125 }
126
127 public void setValue(String value) {
128 this.value = value;
129 }
130
131 public List<StringTreeStructure> getChildren() {
132 return children;
133 }
134
135 public void setChildren(List<StringTreeStructure> children) {
136 this.children = children;
137 }
138
139 public void addChild(StringTreeStructure child) {
140 children.add(child);
141 }
142
143 public boolean hasChildren() {
144 return !children.isEmpty();
145 }
146
147 public void setIndex(int index) {
148 this.index = index;
149 }
150
151 public int getIndex() {
152 return index;
153 }
154
155 public String getCategoriesString() {
156 String result = " (";
157 for (int i = 0; i < categories.size(); i++) {
158 if (i != categories.size() - 1) {
159 result = result + categories.get(i) + ", ";
160 } else {
161 result = result + categories.get(i) + ")";
162 }
163 }
164 return result;
165 }
166 }
167 }