1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.statement.ui.client.widgets.rules; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
22 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
23 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
24 | |
import org.kuali.student.common.ui.client.widgets.KSTextArea; |
25 | |
import org.kuali.student.core.statement.dto.ReqComponentInfo; |
26 | |
|
27 | |
import com.google.gwt.event.dom.client.ClickEvent; |
28 | |
import com.google.gwt.event.dom.client.ClickHandler; |
29 | |
import com.google.gwt.event.dom.client.KeyUpEvent; |
30 | |
import com.google.gwt.event.dom.client.KeyUpHandler; |
31 | |
import com.google.gwt.user.client.ui.*; |
32 | |
|
33 | 0 | public class RuleExpressionEditor extends FlowPanel { |
34 | |
|
35 | |
|
36 | 0 | private KSTextArea expressionTextBox = new KSTextArea(); |
37 | 0 | private HTML htmlErrorMessage = new HTML(); |
38 | 0 | private KSButton btnUpdate = new KSButton("Update"); |
39 | 0 | private KSButton btnUndo = new KSButton("Undo"); |
40 | 0 | private FlowPanel ruleTablePanel = new FlowPanel(); |
41 | 0 | private KSLabel noRuleText = new KSLabel(""); |
42 | 0 | private Panel pnlMissingExpressions = new VerticalPanel(); |
43 | |
|
44 | |
|
45 | 0 | private RuleTable ruleTable = new RuleTable(false); |
46 | 0 | private ClickHandler ruleTableEditClauseHandler = null; |
47 | |
|
48 | |
|
49 | |
private RuleInfo rule; |
50 | |
private String previewExpression; |
51 | |
private Callback reqCompEditCallback; |
52 | |
private Callback ruleChangedCallback; |
53 | |
private RuleManageWidget parent; |
54 | 0 | private boolean ruleChanged = false; |
55 | |
|
56 | |
|
57 | 0 | private RuleExpressionParser ruleExpressionParser = new RuleExpressionParser(); |
58 | |
|
59 | 0 | public RuleExpressionEditor(RuleManageWidget parent) { |
60 | 0 | this.parent = parent; |
61 | |
|
62 | 0 | FlowPanel expressionBox = new FlowPanel(); |
63 | 0 | HorizontalPanel expressionAndMsg = new HorizontalPanel(); |
64 | 0 | expressionBox.setStyleName("KS-Program-Rule-LogicView-ExpressionPanel"); |
65 | 0 | expressionTextBox.addStyleName("KS-Program-Rule-LogicView-ExpressionText"); |
66 | 0 | expressionAndMsg.add(expressionTextBox); |
67 | 0 | expressionAndMsg.add(htmlErrorMessage); |
68 | 0 | expressionBox.add(expressionAndMsg); |
69 | 0 | btnUpdate.addStyleName("KS-Program-Rule-LogicView-Update-Button"); |
70 | 0 | FlowPanel buttonsPanel = new FlowPanel(); |
71 | 0 | buttonsPanel.add(btnUpdate); |
72 | 0 | btnUndo.addStyleName("KS-Program-Rule-LogicView-Undo-Button"); |
73 | 0 | buttonsPanel.add(btnUndo); |
74 | 0 | buttonsPanel.addStyleName("KS-Program-Rule-LogicView-ButtonPanel"); |
75 | 0 | expressionBox.add(buttonsPanel); |
76 | 0 | add(expressionBox); |
77 | |
|
78 | 0 | ruleTablePanel.setStyleName("KS-Program-Rule-LogicView-RulePanel"); |
79 | 0 | ruleTablePanel.add(ruleTable); |
80 | 0 | ruleTablePanel.add(noRuleText); |
81 | 0 | ruleTablePanel.add(pnlMissingExpressions); |
82 | 0 | add(ruleTablePanel); |
83 | |
|
84 | 0 | previewExpression = ""; |
85 | |
|
86 | 0 | setupHandlers(); |
87 | 0 | } |
88 | |
|
89 | |
public void setRule(RuleInfo rule) { |
90 | 0 | this.rule = rule; |
91 | 0 | redraw(); |
92 | 0 | } |
93 | |
|
94 | |
private void setupHandlers() { |
95 | 0 | ruleTableEditClauseHandler = new ClickHandler() { |
96 | |
@Override |
97 | |
public void onClick(ClickEvent event) { |
98 | 0 | HTMLTable.Cell cell = ruleTable.getCellForEvent(event); |
99 | 0 | if (cell == null) { |
100 | 0 | return; |
101 | |
} |
102 | |
|
103 | 0 | RuleNodeWidget widget = (RuleNodeWidget) ruleTable.getWidget(cell.getRowIndex(), cell.getCellIndex()); |
104 | 0 | Object userObject = widget.getNode().getUserObject(); |
105 | 0 | if (userObject instanceof ReqComponentVO) { |
106 | 0 | widget.setEditMode(true); |
107 | 0 | final ReqComponentVO tempRule = (ReqComponentVO) userObject; |
108 | 0 | reqCompEditCallback.exec(tempRule.getReqComponentInfo()); |
109 | |
} |
110 | 0 | } |
111 | |
}; |
112 | 0 | expressionTextBox.addKeyUpHandler(new KeyUpHandler() { |
113 | |
public void onKeyUp(KeyUpEvent event) { |
114 | |
|
115 | 0 | if(event.getNativeKeyCode() == 37 |
116 | |
||event.getNativeKeyCode() == 38 |
117 | |
||event.getNativeKeyCode() == 39 |
118 | |
||event.getNativeKeyCode() == 40){ |
119 | 0 | return; |
120 | |
} |
121 | 0 | btnUpdate.setEnabled(true); |
122 | 0 | } |
123 | |
}); |
124 | 0 | btnUpdate.addClickHandler(new ClickHandler() { |
125 | |
public void onClick(ClickEvent event) { |
126 | |
|
127 | |
|
128 | 0 | previewExpression = expressionTextBox.getText(); |
129 | 0 | previewExpression = previewExpression.replaceAll("\n", " "); |
130 | 0 | previewExpression = previewExpression.replaceAll("\r", " "); |
131 | 0 | ruleExpressionParser.setExpression(previewExpression); |
132 | |
|
133 | |
|
134 | 0 | List<ReqComponentVO> rcs = (rule.getStatementVO() == null || rule.getStatementVO().getAllReqComponentVOs() == null) ? |
135 | |
new ArrayList<ReqComponentVO>() : rule.getStatementVO().getAllReqComponentVOs(); |
136 | 0 | List<String> errorMessages = new ArrayList<String>(); |
137 | 0 | boolean validExpression = ruleExpressionParser.validateExpression(errorMessages, rcs); |
138 | |
|
139 | |
|
140 | 0 | if (!validExpression) { |
141 | |
|
142 | |
|
143 | 0 | List<ReqComponentVO> missingRCs = new ArrayList<ReqComponentVO>(); |
144 | 0 | rcs = (rule.getStatementVO() == null || rule.getStatementVO().getAllReqComponentVOs() == null) ? |
145 | |
new ArrayList<ReqComponentVO>() : rule.getStatementVO().getAllReqComponentVOs(); |
146 | 0 | ruleExpressionParser.checkMissingRCs(missingRCs, rcs); |
147 | 0 | showMissingRCs(missingRCs); |
148 | |
|
149 | 0 | showErrors(errorMessages); |
150 | 0 | return; |
151 | |
} |
152 | |
|
153 | |
|
154 | 0 | StatementVO newStatementVO = ruleExpressionParser.parseExpressionIntoStatementVO(previewExpression, rule.getStatementVO(), rule.getStatementTypeKey()); |
155 | 0 | rule.setStatementVO(newStatementVO); |
156 | 0 | rule.getEditHistory().save(newStatementVO); |
157 | |
|
158 | |
|
159 | 0 | redraw(); |
160 | |
|
161 | 0 | parent.updateObjectRule(rule); |
162 | 0 | ruleChanged = true; |
163 | 0 | ruleChangedCallback.exec(true); |
164 | 0 | } |
165 | |
}); |
166 | |
|
167 | 0 | btnUndo.addClickHandler(new ClickHandler() { |
168 | |
public void onClick(ClickEvent event) { |
169 | 0 | StatementVO previousState = rule.getEditHistory().undo(); |
170 | 0 | if (previousState != null) { |
171 | 0 | rule.setStatementVO(previousState); |
172 | |
} |
173 | 0 | redraw(); |
174 | 0 | parent.updateObjectRule(rule); |
175 | 0 | ruleChanged = true; |
176 | 0 | ruleChangedCallback.exec(true); |
177 | 0 | } |
178 | |
}); |
179 | 0 | } |
180 | |
|
181 | |
private void redraw() { |
182 | 0 | htmlErrorMessage.setHTML(""); |
183 | 0 | ruleTable.clear(); |
184 | 0 | noRuleText.setText(""); |
185 | 0 | ruleChanged = false; |
186 | 0 | setEnabledView(true); |
187 | 0 | expressionTextBox.setText(rule.getExpression().trim().isEmpty() ? "" : rule.getExpression().trim()); |
188 | |
|
189 | 0 | if (rule.getExpression().trim().isEmpty()) { |
190 | 0 | noRuleText.setText("No rules have been added"); |
191 | 0 | btnUpdate.setEnabled(false); |
192 | 0 | return; |
193 | |
} |
194 | |
|
195 | 0 | btnUpdate.setEnabled(false); |
196 | 0 | ruleTable.buildTable(rule.getStatementTree()); |
197 | 0 | ruleTable.addEditClauseHandler(ruleTableEditClauseHandler); |
198 | 0 | } |
199 | |
|
200 | |
private void showMissingRCs(List<ReqComponentVO> missingRCs) { |
201 | 0 | VerticalPanel pnlRCList = new VerticalPanel(); |
202 | 0 | boolean missingExprFound = false; |
203 | 0 | pnlMissingExpressions.clear(); |
204 | |
|
205 | 0 | for (ReqComponentVO rc : missingRCs) { |
206 | 0 | HorizontalPanel pnlRcListRow = new HorizontalPanel(); |
207 | 0 | if (rc.getGuiReferenceLabelId() != null) { |
208 | 0 | KSLabel rcLabel = new KSLabel(rc.getGuiReferenceLabelId()); |
209 | 0 | rcLabel.setStyleName("KS-Program-Rule-LogicView-Rule-ID"); |
210 | 0 | pnlRcListRow.add(rcLabel); |
211 | 0 | missingExprFound = true; |
212 | |
} |
213 | 0 | pnlRCList.add(pnlRcListRow); |
214 | 0 | } |
215 | |
|
216 | 0 | if (missingExprFound) { |
217 | 0 | KSLabel missingExprTitle = new KSLabel("The following rules do not exist"); |
218 | 0 | missingExprTitle.setStyleName("KS-Program-Rule-LogicView-Missing-Rules"); |
219 | 0 | pnlMissingExpressions.add(missingExprTitle); |
220 | 0 | pnlMissingExpressions.add(pnlRCList); |
221 | |
} |
222 | 0 | } |
223 | |
|
224 | |
private void showErrors(List<String> errorMessages) { |
225 | 0 | StringBuilder sb = new StringBuilder(""); |
226 | 0 | boolean firstRow = true; |
227 | 0 | for (String errorMessage : errorMessages) { |
228 | 0 | if (!firstRow) { |
229 | 0 | sb.append(",<BR>"); |
230 | |
} |
231 | 0 | firstRow = false; |
232 | 0 | sb.append(errorMessage); |
233 | |
} |
234 | 0 | htmlErrorMessage.getElement().getStyle().setProperty("color", "red"); |
235 | 0 | htmlErrorMessage.setHTML(sb.toString()); |
236 | 0 | } |
237 | |
|
238 | |
private void setEnableButtons(boolean enabled) { |
239 | 0 | if (enabled) { |
240 | 0 | btnUpdate.setEnabled(enabled); |
241 | 0 | btnUndo.setEnabled(rule.getEditHistory().isUndoable()); |
242 | |
} else { |
243 | 0 | btnUpdate.setEnabled(false); |
244 | 0 | btnUndo.setEnabled(false); |
245 | |
} |
246 | 0 | } |
247 | |
|
248 | |
public void setEnabledView(boolean enabled) { |
249 | 0 | setEnableButtons(enabled); |
250 | 0 | ruleTable.setEnabled(enabled); |
251 | 0 | expressionTextBox.setEnabled(enabled); |
252 | 0 | } |
253 | |
|
254 | |
public void addReqCompEditButtonClickCallback(Callback<ReqComponentInfo> callback) { |
255 | 0 | reqCompEditCallback = callback; |
256 | 0 | } |
257 | |
|
258 | |
public void addRuleChangedButtonClickCallback(Callback<Boolean> callback) { |
259 | 0 | ruleChangedCallback = callback; |
260 | 0 | } |
261 | |
} |