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