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