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 | |
|
53 | 0 | int index = 0; |
54 | 0 | String sequence = loInfoHelper.getSequence(); |
55 | 0 | if (sequence != null) { |
56 | 0 | index = Integer.parseInt(sequence); |
57 | |
} |
58 | 0 | StringTreeStructure childModel = new StringTreeStructure(index, descriptionHelper.getPlain(), categories); |
59 | 0 | transform(loDisplayInfoHelper.getDisplayInfoList(), childModel); |
60 | 0 | rootModel.addChild(childModel); |
61 | 0 | } |
62 | 0 | } |
63 | |
|
64 | |
private void bind(StringTreeStructure stringTreeStructure, KSListPanel ksListPanel) { |
65 | 0 | stringTreeStructure.sortChildren(); |
66 | 0 | List<StringTreeStructure> firstLevelChildren = stringTreeStructure.getChildren(); |
67 | 0 | for (StringTreeStructure firstLevelChild : firstLevelChildren) { |
68 | 0 | addElement(firstLevelChild, ksListPanel); |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
private void addElement(StringTreeStructure element, KSListPanel listPanel) { |
73 | 0 | if (element.hasChildren()) { |
74 | 0 | KSListPanel subPanel = new KSListPanel(); |
75 | 0 | element.sortChildren(); |
76 | 0 | for (StringTreeStructure child : element.getChildren()) { |
77 | 0 | addElement(child, subPanel); |
78 | |
} |
79 | 0 | if (!element.getCategories().isEmpty()) { |
80 | 0 | listPanel.add(subPanel, element.getValue() + element.getCategoriesString()); |
81 | |
} else { |
82 | 0 | listPanel.add(subPanel, element.getValue()); |
83 | |
} |
84 | 0 | } else { |
85 | 0 | if (!element.getCategories().isEmpty()) { |
86 | 0 | listPanel.add(new Label(element.getValue() + element.getCategoriesString())); |
87 | |
} else { |
88 | 0 | listPanel.add(new Label(element.getValue())); |
89 | |
} |
90 | |
} |
91 | 0 | } |
92 | |
|
93 | 0 | private static class StringTreeStructure { |
94 | |
|
95 | |
private String value; |
96 | |
private int index; |
97 | |
|
98 | 0 | private List<StringTreeStructure> children = new ArrayList<StringTreeStructure>(); |
99 | 0 | private List<String> categories = new LinkedList<String>(); |
100 | |
|
101 | 0 | public StringTreeStructure(int index, String value, List<String> categories) { |
102 | 0 | this.setIndex(index); |
103 | 0 | this.value = value; |
104 | 0 | this.categories = categories; |
105 | 0 | } |
106 | |
|
107 | |
public void sortChildren() { |
108 | 0 | Collections.sort(children, new Comparator<StringTreeStructure>() { |
109 | |
|
110 | |
@Override |
111 | |
public int compare(StringTreeStructure o1, StringTreeStructure o2) { |
112 | 0 | return o1.getIndex() - o2.getIndex(); |
113 | |
} |
114 | |
}); |
115 | 0 | } |
116 | |
|
117 | 0 | public StringTreeStructure() {} |
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 | |
} |