1 | |
package org.kuali.student.common.ui.client.widgets.rules; |
2 | |
|
3 | |
import java.util.List; |
4 | |
import java.util.Map; |
5 | |
|
6 | |
import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; |
7 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
8 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle; |
9 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel; |
10 | |
import org.kuali.student.core.statement.dto.ReqCompFieldInfo; |
11 | |
import org.kuali.student.core.statement.dto.ReqComponentInfo; |
12 | |
import org.kuali.student.core.statement.dto.StatementOperatorTypeKey; |
13 | |
import org.kuali.student.core.statement.dto.StatementTreeViewInfo; |
14 | |
|
15 | |
import com.google.gwt.event.dom.client.ClickHandler; |
16 | |
import com.google.gwt.user.client.ui.FlowPanel; |
17 | |
import com.google.gwt.user.client.ui.HTML; |
18 | |
import com.google.gwt.user.client.ui.Widget; |
19 | |
|
20 | |
public class SubrulePreviewWidget extends FlowPanel { |
21 | |
|
22 | 0 | private KSButton editButton = new KSButton("Edit", ButtonStyle.DEFAULT_ANCHOR); |
23 | 0 | private SpanPanel separator = new SpanPanel(" | "); |
24 | 0 | private KSButton deleteButton = new KSButton("Delete", ButtonStyle.DEFAULT_ANCHOR); |
25 | 0 | private boolean isReadOnly = true; |
26 | |
private Map<String, Widget> clusetWidgets; |
27 | 0 | private final String CLU_SET_WIDGET_TAG = "[CLU_SET_WIDGET_TAG]"; |
28 | |
|
29 | |
public SubrulePreviewWidget(StatementTreeViewInfo stmtTreeInfo, boolean isReadOnly, Map<String, Widget> clusetWidgets) { |
30 | 0 | super(); |
31 | 0 | this.isReadOnly = isReadOnly; |
32 | 0 | this.clusetWidgets = clusetWidgets; |
33 | |
|
34 | 0 | addStyleName("KS-Rule-Preview-Subrule-Box"); |
35 | 0 | showSubrule(stmtTreeInfo); |
36 | 0 | } |
37 | |
|
38 | |
public void showSubrule(StatementTreeViewInfo stmtTreeInfo) { |
39 | 0 | this.clear(); |
40 | |
|
41 | 0 | if (stmtTreeInfo == null) { |
42 | 0 | return; |
43 | |
} |
44 | |
|
45 | 0 | buildRequirementHeader(stmtTreeInfo); |
46 | 0 | buildRequirement(stmtTreeInfo); |
47 | 0 | } |
48 | |
|
49 | |
private void buildRequirementHeader(StatementTreeViewInfo stmtTreeInfo) { |
50 | |
|
51 | 0 | SectionTitle header = SectionTitle.generateH6Title(""); |
52 | 0 | if (stmtTreeInfo.getOperator() == StatementOperatorTypeKey.AND) { |
53 | 0 | header.setHTML("Must meet <b>all of the following:</b>"); |
54 | |
} else { |
55 | 0 | header.setHTML("Must meet <b>1 of the following:</b>"); |
56 | |
} |
57 | 0 | header.setStyleName("KS-Rule-Preview-Subrule-header"); |
58 | 0 | header.getElement().setAttribute("style", "font-weight: normal"); |
59 | |
|
60 | |
|
61 | 0 | if (!isReadOnly) { |
62 | 0 | buildEditActionsWidget(header); |
63 | |
} |
64 | 0 | this.add(header); |
65 | 0 | } |
66 | |
|
67 | |
private void buildEditActionsWidget(SectionTitle header) { |
68 | 0 | SpanPanel actions = new SpanPanel(); |
69 | 0 | actions.add(editButton); |
70 | 0 | actions.add(separator); |
71 | 0 | actions.add(deleteButton); |
72 | 0 | actions.addStyleName("KS-Rule-Preview-Subrule-header-action"); |
73 | 0 | header.add(actions); |
74 | 0 | } |
75 | |
|
76 | |
private void buildRequirement(StatementTreeViewInfo stmtTreeInfo) { |
77 | 0 | String html = buildOneRequirement(stmtTreeInfo, null, true, true).toString(); |
78 | |
|
79 | 0 | boolean foundOpennigBracket = false; |
80 | 0 | while (html.length() > 0) { |
81 | |
|
82 | 0 | int ix = html.indexOf(CLU_SET_WIDGET_TAG); |
83 | 0 | if (ix == -1) { |
84 | 0 | this.add(new HTML(html)); |
85 | 0 | break; |
86 | |
} |
87 | |
|
88 | 0 | foundOpennigBracket = !foundOpennigBracket; |
89 | 0 | if (ix == 0) { |
90 | 0 | html = html.substring(CLU_SET_WIDGET_TAG.length()); |
91 | 0 | continue; |
92 | |
} |
93 | |
|
94 | 0 | if (ix > 0) { |
95 | 0 | String beforeTagString = html.substring(0, ix); |
96 | 0 | if (foundOpennigBracket) { |
97 | 0 | this.add(new HTML(beforeTagString)); |
98 | |
} else { |
99 | 0 | this.add(clusetWidgets.get(beforeTagString)); |
100 | |
} |
101 | 0 | html = html.substring(ix + CLU_SET_WIDGET_TAG.length()); |
102 | 0 | continue; |
103 | |
} |
104 | 0 | } |
105 | 0 | } |
106 | |
|
107 | |
private StringBuffer buildOneRequirement(StatementTreeViewInfo stmtTreeInfo, StatementOperatorTypeKey upperLevelOperator, boolean firstLevel, boolean firstRequirement) { |
108 | |
|
109 | 0 | StringBuffer htmlText = new StringBuffer(); |
110 | 0 | List<StatementTreeViewInfo> stmtTreeList = stmtTreeInfo.getStatements(); |
111 | |
|
112 | 0 | if ((stmtTreeList != null) && (stmtTreeList.size() > 0)) { |
113 | 0 | StringBuffer htmlOneLevelText = new StringBuffer(); |
114 | 0 | firstRequirement = true; |
115 | 0 | for (StatementTreeViewInfo subTree : stmtTreeList) { |
116 | 0 | boolean trueFirstLevel = (firstLevel && (stmtTreeList.size() <= 1)); |
117 | 0 | htmlOneLevelText.append(buildOneRequirement(subTree, stmtTreeInfo.getOperator(), trueFirstLevel, firstRequirement)); |
118 | 0 | firstRequirement = htmlOneLevelText.toString().trim().isEmpty(); |
119 | 0 | } |
120 | |
|
121 | |
|
122 | 0 | if (firstLevel || stmtTreeInfo.getStatements().size() > 1) { |
123 | |
|
124 | 0 | if (!firstLevel) { |
125 | 0 | String operatorText = (stmtTreeInfo.getOperator() == StatementOperatorTypeKey.AND ? |
126 | |
"Must meet all of the following:" : "Must meet 1 of the following:"); |
127 | 0 | htmlText.append(addRequirementToList(null, upperLevelOperator, operatorText, firstRequirement)); |
128 | |
} |
129 | |
|
130 | 0 | htmlText.append("<ul class=\"KS-Program-Rule-Preview-Subrule-ul\">"); |
131 | 0 | htmlText.append(htmlOneLevelText); |
132 | 0 | htmlText.append("</ul>"); |
133 | |
} else { |
134 | 0 | htmlText.append(htmlOneLevelText); |
135 | |
} |
136 | |
|
137 | 0 | } else if ((stmtTreeInfo.getReqComponents() != null) && !stmtTreeInfo.getReqComponents().isEmpty()) { |
138 | 0 | List<ReqComponentInfo> reqComponents = stmtTreeInfo.getReqComponents(); |
139 | 0 | StringBuilder htmlListText = new StringBuilder(); |
140 | |
|
141 | |
|
142 | 0 | boolean firstListRequirement = firstRequirement; |
143 | 0 | if (!firstLevel && reqComponents.size() > 1) { |
144 | 0 | firstListRequirement = true; |
145 | |
} |
146 | 0 | for (ReqComponentInfo reqComp : reqComponents) { |
147 | 0 | StatementOperatorTypeKey operator = (reqComponents.size() > 1 ? stmtTreeInfo.getOperator() : upperLevelOperator); |
148 | 0 | String nl = null; |
149 | 0 | if (reqComp instanceof ReqComponentInfoUi) { |
150 | 0 | ReqComponentInfoUi reqUi = (ReqComponentInfoUi)reqComp; |
151 | 0 | nl = reqUi.getPreviewNaturalLanguageTranslation(); |
152 | |
} |
153 | 0 | if ( nl == null) { |
154 | 0 | nl = reqComp.getNaturalLanguageTranslation(); |
155 | |
} |
156 | 0 | htmlListText.append(addRequirementToList(reqComp, operator, nl, firstListRequirement)); |
157 | 0 | firstListRequirement = false; |
158 | 0 | } |
159 | |
|
160 | |
|
161 | 0 | if (firstLevel || reqComponents.size() == 1) { |
162 | 0 | htmlText.append(htmlListText); |
163 | |
} else { |
164 | 0 | String operatorText = (stmtTreeInfo.getOperator() == StatementOperatorTypeKey.AND ? |
165 | |
"Must meet all of the following:" : "Must meet 1 of the following:"); |
166 | 0 | htmlText.append(addRequirementToList(null, upperLevelOperator, operatorText, firstRequirement)); |
167 | |
|
168 | 0 | htmlText.append("<ul class=\"KS-Program-Rule-Preview-Subrule-ul\">"); |
169 | 0 | htmlText.append(htmlListText); |
170 | 0 | htmlText.append("</ul>"); |
171 | |
} |
172 | 0 | } else { |
173 | 0 | return new StringBuffer("No rules have been added"); |
174 | |
} |
175 | |
|
176 | 0 | return htmlText; |
177 | |
} |
178 | |
|
179 | |
private StringBuffer addRequirementToList(ReqComponentInfo reqComp, StatementOperatorTypeKey operator, String requirement, boolean firstRequirement) { |
180 | 0 | StringBuffer html = new StringBuffer(); |
181 | 0 | html.append("<li style=\"padding-top: 5px;\">"); |
182 | 0 | if (!firstRequirement) { |
183 | 0 | html.append("<span class=\"KS-Program-Rule-Preview-Subrule-ORAND\">"); |
184 | 0 | html.append(operator == StatementOperatorTypeKey.AND ? "AND " : "OR "); |
185 | 0 | html.append("</span>"); |
186 | |
} |
187 | 0 | html.append(requirement); |
188 | |
|
189 | |
|
190 | 0 | if ((clusetWidgets != null) && (reqComp != null)) { |
191 | 0 | List<ReqCompFieldInfo> fieldInfos = reqComp.getReqCompFields(); |
192 | 0 | for (ReqCompFieldInfo fieldInfo : fieldInfos) { |
193 | 0 | if (RulesUtil.isCluSetWidget(fieldInfo.getType())) { |
194 | 0 | if (clusetWidgets.get(fieldInfo.getValue()) != null) { |
195 | 0 | html.append(CLU_SET_WIDGET_TAG + fieldInfo.getValue() + CLU_SET_WIDGET_TAG); |
196 | 0 | break; |
197 | |
} |
198 | |
} |
199 | |
} |
200 | |
} |
201 | |
|
202 | 0 | html.append("</li>"); |
203 | 0 | return html; |
204 | |
} |
205 | |
|
206 | |
public void addEditButtonClickHandler(ClickHandler handler) { |
207 | 0 | editButton.addClickHandler(handler); |
208 | 0 | } |
209 | |
|
210 | |
public void addDeleteButtonClickHandler(ClickHandler handler) { |
211 | 0 | deleteButton.addClickHandler(handler); |
212 | 0 | } |
213 | |
} |