1 | |
package org.kuali.student.core.statement.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.ULPanel; |
10 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel; |
11 | |
import org.kuali.student.core.statement.dto.ReqCompFieldInfo; |
12 | |
import org.kuali.student.core.statement.dto.ReqComponentInfo; |
13 | |
import org.kuali.student.core.statement.dto.StatementOperatorTypeKey; |
14 | |
import org.kuali.student.core.statement.dto.StatementTreeViewInfo; |
15 | |
|
16 | |
import com.google.gwt.event.dom.client.ClickHandler; |
17 | |
import com.google.gwt.user.client.ui.ComplexPanel; |
18 | |
import com.google.gwt.user.client.ui.FlowPanel; |
19 | |
import com.google.gwt.user.client.ui.HTML; |
20 | |
import com.google.gwt.user.client.ui.Widget; |
21 | |
|
22 | |
public class SubrulePreviewWidget extends FlowPanel { |
23 | |
|
24 | |
private static final String OPERATOR_HEADER_OR = "Must meet 1 of the following:"; |
25 | |
private static final String OPERATOR_HEADER_AND = "Must meet all of the following:"; |
26 | |
private static final String SUBRULE_LI_CSS_CLASS = "KS-Rule-Preview-Subrule-li"; |
27 | |
private static final String SUBRULE_UL_CSS_CLASS = "KS-Program-Rule-Preview-Subrule-ul"; |
28 | 0 | private KSButton editButton = new KSButton("Edit", ButtonStyle.DEFAULT_ANCHOR); |
29 | 0 | private SpanPanel separator = new SpanPanel(" | "); |
30 | 0 | private KSButton deleteButton = new KSButton("Delete", ButtonStyle.DEFAULT_ANCHOR); |
31 | 0 | private boolean isReadOnly = true; |
32 | |
private Map<String, Widget> clusetWidgets; |
33 | |
|
34 | |
public SubrulePreviewWidget(StatementTreeViewInfo stmtTreeInfo, boolean isReadOnly, Map<String, Widget> clusetWidgets) { |
35 | 0 | super(); |
36 | 0 | this.isReadOnly = isReadOnly; |
37 | 0 | this.clusetWidgets = clusetWidgets; |
38 | |
|
39 | 0 | addStyleName("KS-Rule-Preview-Subrule-Box"); |
40 | 0 | showSubrule(stmtTreeInfo); |
41 | 0 | } |
42 | |
|
43 | |
public void showSubrule(StatementTreeViewInfo stmtTreeInfo) { |
44 | 0 | this.clear(); |
45 | |
|
46 | 0 | if (stmtTreeInfo == null) { |
47 | 0 | return; |
48 | |
} |
49 | |
|
50 | 0 | buildRequirementHeader(stmtTreeInfo); |
51 | 0 | buildRequirement(stmtTreeInfo); |
52 | 0 | } |
53 | |
|
54 | |
private void buildRequirementHeader(StatementTreeViewInfo stmtTreeInfo) { |
55 | |
|
56 | 0 | SectionTitle header = SectionTitle.generateH6Title(""); |
57 | 0 | if (stmtTreeInfo.getOperator() == StatementOperatorTypeKey.AND) { |
58 | 0 | header.setHTML("Must meet <b>all of the following:</b>"); |
59 | |
} else { |
60 | 0 | header.setHTML("Must meet <b>1 of the following:</b>"); |
61 | |
} |
62 | 0 | header.setStyleName("KS-Rule-Preview-Subrule-header"); |
63 | 0 | header.getElement().setAttribute("style", "font-weight: normal"); |
64 | |
|
65 | |
|
66 | 0 | if (!isReadOnly) { |
67 | 0 | buildEditActionsWidget(header); |
68 | |
} |
69 | 0 | this.add(header); |
70 | 0 | } |
71 | |
|
72 | |
private void buildEditActionsWidget(SectionTitle header) { |
73 | 0 | SpanPanel actions = new SpanPanel(); |
74 | 0 | actions.add(editButton); |
75 | 0 | actions.add(separator); |
76 | 0 | actions.add(deleteButton); |
77 | 0 | actions.addStyleName("KS-Rule-Preview-Subrule-header-action"); |
78 | 0 | header.add(actions); |
79 | 0 | } |
80 | |
|
81 | |
private void buildRequirement(StatementTreeViewInfo stmtTreeInfo) { |
82 | 0 | Widget requirementsWidget = buildRequirementsWidget(stmtTreeInfo); |
83 | |
|
84 | 0 | if(requirementsWidget != null) { |
85 | 0 | this.add(requirementsWidget); |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private ComplexPanel buildRequirementsWidget(StatementTreeViewInfo rootStatement) { |
98 | 0 | ULPanel statementBulletList = new ULPanel(); |
99 | 0 | statementBulletList.setULClassName(SUBRULE_UL_CSS_CLASS); |
100 | |
|
101 | |
|
102 | 0 | if(rootStatement.getReqComponents() != null && !rootStatement.getReqComponents().isEmpty()) { |
103 | |
|
104 | 0 | populateRequirementComponentWidgets(rootStatement, statementBulletList); |
105 | |
|
106 | |
} |
107 | |
else { |
108 | |
|
109 | 0 | if(rootStatement.getStatements() == null || rootStatement.getStatements().isEmpty()) { |
110 | 0 | return null; |
111 | |
} |
112 | |
|
113 | |
|
114 | 0 | boolean firstInList = true; |
115 | 0 | for(StatementTreeViewInfo subStatement : rootStatement.getStatements()) { |
116 | |
|
117 | 0 | statementBulletList.add(buildSubStatementWidget(subStatement, rootStatement, firstInList)); |
118 | 0 | firstInList = false; |
119 | |
} |
120 | |
|
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | 0 | return statementBulletList; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
private void populateRequirementComponentWidgets(StatementTreeViewInfo statement, ULPanel list) { |
138 | 0 | StatementOperatorTypeKey includedOperator = null; |
139 | 0 | for (ReqComponentInfo reqComp : statement.getReqComponents()) { |
140 | 0 | String nl = getPreviewNaturalLanguageForReqComponent(reqComp); |
141 | |
|
142 | 0 | list.add(buildRequirementPanel(reqComp, includedOperator, nl), SUBRULE_LI_CSS_CLASS); |
143 | |
|
144 | |
|
145 | 0 | if(includedOperator == null) { |
146 | 0 | includedOperator = statement.getOperator(); |
147 | |
} |
148 | 0 | } |
149 | 0 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
private Widget buildSubStatementWidget(StatementTreeViewInfo subStatement, StatementTreeViewInfo parentStatement, boolean firstInList) { |
162 | 0 | StatementOperatorTypeKey prefixOperator = null; |
163 | |
|
164 | 0 | boolean hasReqComponents = (subStatement.getReqComponents() != null && !subStatement.getReqComponents().isEmpty()); |
165 | |
|
166 | |
|
167 | 0 | if(hasReqComponents && subStatement.getReqComponents().size() == 1) { |
168 | |
|
169 | 0 | ReqComponentInfo reqComp = subStatement.getReqComponents().iterator().next(); |
170 | 0 | String nl = getPreviewNaturalLanguageForReqComponent(reqComp); |
171 | 0 | if (!firstInList) { |
172 | 0 | prefixOperator = parentStatement.getOperator(); |
173 | |
} |
174 | 0 | return buildRequirementPanel(reqComp, prefixOperator, nl); |
175 | |
} |
176 | |
|
177 | |
|
178 | 0 | StringBuilder headerText = new StringBuilder(); |
179 | |
|
180 | 0 | if (!firstInList) { |
181 | 0 | prefixOperator = parentStatement.getOperator(); |
182 | |
} |
183 | |
|
184 | 0 | appendOperatorTag(headerText, prefixOperator); |
185 | |
|
186 | 0 | headerText.append(subStatement.getOperator() == StatementOperatorTypeKey.AND ? OPERATOR_HEADER_AND : OPERATOR_HEADER_OR); |
187 | |
|
188 | 0 | FlowPanel panel = new FlowPanel(); |
189 | 0 | panel.add(new HTML(headerText.toString())); |
190 | |
|
191 | 0 | if(hasReqComponents) { |
192 | |
|
193 | 0 | ULPanel subrulePanel = new ULPanel(); |
194 | 0 | subrulePanel.setULClassName(SUBRULE_UL_CSS_CLASS); |
195 | 0 | populateRequirementComponentWidgets(subStatement, subrulePanel); |
196 | 0 | panel.add(subrulePanel); |
197 | 0 | } |
198 | |
else { |
199 | |
|
200 | 0 | boolean firstInSubList = true; |
201 | 0 | for(StatementTreeViewInfo childStatement : subStatement.getStatements()) { |
202 | 0 | ULPanel subStatementPanel = new ULPanel(); |
203 | 0 | subStatementPanel.setULClassName(SUBRULE_UL_CSS_CLASS); |
204 | 0 | subStatementPanel.add(buildSubStatementWidget(childStatement, subStatement, firstInSubList), SUBRULE_LI_CSS_CLASS); |
205 | 0 | panel.add(subStatementPanel); |
206 | 0 | firstInSubList = false; |
207 | 0 | } |
208 | |
|
209 | |
} |
210 | |
|
211 | 0 | return panel; |
212 | |
} |
213 | |
|
214 | |
private void appendOperatorTag(StringBuilder builder, StatementOperatorTypeKey operator) { |
215 | 0 | if(operator != null && builder != null) { |
216 | 0 | builder.append("<span class=\"KS-Program-Rule-Preview-Subrule-ORAND\">").append(operator.toString()).append(" ").append("</span>"); |
217 | |
} |
218 | 0 | } |
219 | |
|
220 | |
private String getPreviewNaturalLanguageForReqComponent(ReqComponentInfo reqComp) { |
221 | 0 | String nl = null; |
222 | 0 | if (reqComp instanceof ReqComponentInfoUi) { |
223 | 0 | nl = ((ReqComponentInfoUi)reqComp).getPreviewNaturalLanguageTranslation(); |
224 | |
} |
225 | 0 | if ( nl == null) { |
226 | 0 | nl = reqComp.getNaturalLanguageTranslation(); |
227 | |
} |
228 | 0 | return nl; |
229 | |
} |
230 | |
|
231 | |
private FlowPanel buildRequirementPanel(ReqComponentInfo reqComp, StatementOperatorTypeKey operator, String requirement) { |
232 | |
|
233 | 0 | FlowPanel requirementPanel = new FlowPanel(); |
234 | |
|
235 | 0 | StringBuilder htmlBuilder = new StringBuilder(); |
236 | 0 | appendOperatorTag(htmlBuilder, operator); |
237 | 0 | htmlBuilder.append(requirement); |
238 | |
|
239 | 0 | requirementPanel.add(new HTML(htmlBuilder.toString())); |
240 | |
|
241 | |
|
242 | 0 | if ((clusetWidgets != null) && (reqComp != null)) { |
243 | 0 | List<ReqCompFieldInfo> fieldInfos = reqComp.getReqCompFields(); |
244 | 0 | for (ReqCompFieldInfo fieldInfo : fieldInfos) { |
245 | 0 | if (RulesUtil.isCluSetWidget(fieldInfo.getType())) { |
246 | 0 | Widget csWidget = clusetWidgets.get(fieldInfo.getValue()); |
247 | 0 | if (csWidget != null) { |
248 | 0 | requirementPanel.add(csWidget); |
249 | 0 | break; |
250 | |
} |
251 | 0 | } |
252 | |
} |
253 | |
} |
254 | |
|
255 | 0 | return requirementPanel; |
256 | |
} |
257 | |
|
258 | |
public void addEditButtonClickHandler(ClickHandler handler) { |
259 | 0 | editButton.addClickHandler(handler); |
260 | 0 | } |
261 | |
|
262 | |
public void addDeleteButtonClickHandler(ClickHandler handler) { |
263 | 0 | deleteButton.addClickHandler(handler); |
264 | 0 | } |
265 | |
} |