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.List; |
19 | |
|
20 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
21 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
22 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
23 | |
import org.kuali.student.common.ui.client.widgets.KSPlaceholder; |
24 | |
import org.kuali.student.common.ui.client.widgets.KSProgressIndicator; |
25 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel; |
26 | |
import org.kuali.student.core.statement.dto.ReqComponentInfo; |
27 | |
import org.kuali.student.core.statement.dto.StatementOperatorTypeKey; |
28 | |
import org.kuali.student.core.statement.dto.StatementTreeViewInfo; |
29 | |
import org.kuali.student.core.statement.ui.client.widgets.table.Node; |
30 | |
|
31 | |
import com.google.gwt.event.dom.client.ClickEvent; |
32 | |
import com.google.gwt.event.dom.client.ClickHandler; |
33 | |
import com.google.gwt.event.shared.HandlerRegistration; |
34 | |
import com.google.gwt.user.client.Timer; |
35 | |
import com.google.gwt.user.client.ui.FlowPanel; |
36 | |
import com.google.gwt.user.client.ui.SimplePanel; |
37 | |
import com.google.gwt.user.client.ui.HTMLTable.Cell; |
38 | |
|
39 | 0 | public class RuleTableManipulationWidget extends FlowPanel { |
40 | |
|
41 | |
|
42 | 0 | private KSButton btnMakeOR = new KSButton("OR"); |
43 | 0 | private KSButton btnMakeAND = new KSButton("AND"); |
44 | 0 | private KSButton btnAddToGroup = new KSButton("Group"); |
45 | 0 | private KSButton btnMoveRuleDown = new KSButton("Move Down"); |
46 | 0 | private KSButton btnMoveRuleUp = new KSButton("Move Up"); |
47 | 0 | private KSButton btnUndo = new KSButton("Undo"); |
48 | 0 | private KSButton btnRedo = new KSButton("Redo"); |
49 | 0 | private KSButton btnDelete = new KSButton("Delete"); |
50 | 0 | private SpanPanel separator1 = new SpanPanel(" | "); |
51 | 0 | private SpanPanel separator2 = new SpanPanel(" | "); |
52 | 0 | private FlowPanel topButtonsPanel = new FlowPanel(); |
53 | 0 | private FlowPanel ruleTablePanel = new FlowPanel(); |
54 | |
|
55 | 0 | private KSProgressIndicator twiddler = new KSProgressIndicator(); |
56 | |
|
57 | |
|
58 | 0 | private RuleTable ruleTable = new RuleTable(true); |
59 | 0 | private ClickHandler ruleTableSelectionHandler = null; |
60 | 0 | private ClickHandler ruleTableEditClauseHandler = null; |
61 | 0 | private HandlerRegistration textClickHandler = null; |
62 | |
|
63 | |
|
64 | 0 | private RuleInfo rule = new RuleInfo(); |
65 | |
private Callback reqCompEditCallback; |
66 | |
private Callback ruleChangedCallback; |
67 | 0 | private boolean isEnabled = true; |
68 | 0 | private boolean isOperatorChecked = false; |
69 | |
|
70 | 0 | public RuleTableManipulationWidget() { |
71 | 0 | createButtonsPanel(); |
72 | |
|
73 | 0 | twiddler.setVisible(false); |
74 | 0 | SimplePanel twiddlerPanel = new SimplePanel(); |
75 | 0 | twiddlerPanel.setHeight("30px"); |
76 | 0 | twiddlerPanel.setWidget(twiddler); |
77 | |
|
78 | 0 | add(topButtonsPanel); |
79 | |
|
80 | 0 | ruleTablePanel.setStyleName("KS-Program-Rule-ObjectView-RulePanel"); |
81 | 0 | ruleTablePanel.add(ruleTable); |
82 | 0 | add(ruleTablePanel); |
83 | |
|
84 | 0 | setupHandlers(); |
85 | 0 | } |
86 | |
|
87 | |
private void setupHandlers() { |
88 | |
|
89 | 0 | ruleTableSelectionHandler = new ClickHandler() { |
90 | |
|
91 | |
@Override |
92 | |
public void onClick(ClickEvent event) { |
93 | |
|
94 | 0 | Cell cell = ruleTable.getCellForEvent(event); |
95 | 0 | if (cell == null) { |
96 | 0 | return; |
97 | |
} |
98 | |
|
99 | 0 | RuleNodeWidget widget = (RuleNodeWidget) ruleTable.getWidget(cell.getRowIndex(), cell.getCellIndex()); |
100 | |
|
101 | |
|
102 | 0 | if (widget.isEditMode() || !isEnabled) { |
103 | 0 | widget.setEditMode(false); |
104 | 0 | return; |
105 | |
} |
106 | |
|
107 | |
|
108 | 0 | Object userObject = widget.getNode().getUserObject(); |
109 | 0 | if (userObject instanceof StatementVO) { |
110 | 0 | StatementVO statementVO = (StatementVO) userObject; |
111 | 0 | statementVO.setCheckBoxOn(!statementVO.isCheckBoxOn()); |
112 | 0 | } else if (userObject instanceof ReqComponentVO) { |
113 | 0 | ReqComponentVO reqComponentVO = (ReqComponentVO) userObject; |
114 | 0 | reqComponentVO.setCheckBoxOn(!reqComponentVO.isCheckBoxOn()); |
115 | |
} |
116 | 0 | updateTable(); |
117 | 0 | } |
118 | |
}; |
119 | 0 | textClickHandler = ruleTable.addTextClickHandler(ruleTableSelectionHandler); |
120 | |
|
121 | 0 | ruleTableEditClauseHandler = new ClickHandler() { |
122 | |
@Override |
123 | |
public void onClick(ClickEvent event) { |
124 | 0 | Cell cell = ruleTable.getCellForEvent(event); |
125 | 0 | if (cell == null) { |
126 | 0 | return; |
127 | |
} |
128 | |
|
129 | 0 | RuleNodeWidget widget = (RuleNodeWidget) ruleTable.getWidget(cell.getRowIndex(), cell.getCellIndex()); |
130 | 0 | Object userObject = widget.getNode().getUserObject(); |
131 | 0 | if (userObject instanceof ReqComponentVO) { |
132 | 0 | widget.setEditMode(true); |
133 | 0 | final ReqComponentVO rule = (ReqComponentVO) userObject; |
134 | 0 | reqCompEditCallback.exec(rule.getReqComponentInfo()); |
135 | |
} |
136 | 0 | } |
137 | |
}; |
138 | |
|
139 | 0 | btnMoveRuleDown.addClickHandler(new ClickHandler() { |
140 | |
public void onClick(ClickEvent event) { |
141 | 0 | StatementVO statementVO = rule.getStatementVO(); |
142 | 0 | if (statementVO != null) { |
143 | |
|
144 | 0 | if (isAbletoMoveReqComp()) { |
145 | 0 | ReqComponentVO selectedReqCompVO = statementVO.getSelectedReqComponentVOs().get(0); |
146 | 0 | StatementVO enclosingStatementVO = statementVO.getEnclosingStatementVO(statementVO, selectedReqCompVO); |
147 | 0 | enclosingStatementVO.shiftReqComponent("RIGHT", selectedReqCompVO); |
148 | 0 | rule.getEditHistory().save(statementVO); |
149 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, true); |
150 | |
} |
151 | |
} |
152 | 0 | } |
153 | |
}); |
154 | |
|
155 | 0 | btnMoveRuleUp.addClickHandler(new ClickHandler() { |
156 | |
public void onClick(ClickEvent event) { |
157 | 0 | StatementVO statementVO = rule.getStatementVO(); |
158 | 0 | if (statementVO != null) { |
159 | |
|
160 | 0 | if (isAbletoMoveReqComp()) { |
161 | 0 | ReqComponentVO selectedReqCompVO = statementVO.getSelectedReqComponentVOs().get(0); |
162 | 0 | StatementVO enclosingStatementVO = statementVO.getEnclosingStatementVO(statementVO, selectedReqCompVO); |
163 | 0 | enclosingStatementVO.shiftReqComponent("LEFT", selectedReqCompVO); |
164 | 0 | rule.getEditHistory().save(statementVO); |
165 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, true); |
166 | |
} |
167 | |
} |
168 | 0 | } |
169 | |
}); |
170 | |
|
171 | 0 | btnMakeOR.addClickHandler(new ClickHandler() { |
172 | |
public void onClick(ClickEvent event) { |
173 | |
|
174 | |
|
175 | 0 | if (isMatchingOperandSelected(Token.Or)) { |
176 | 0 | StatementVO statementVO = rule.getSelectedStatementVOs().get(0); |
177 | 0 | if (statementVO != null) { |
178 | 0 | statementVO.getStatementInfo().setOperator(StatementOperatorTypeKey.OR); |
179 | 0 | statementVO.toggleAndOr(); |
180 | |
} |
181 | 0 | } else { |
182 | 0 | rule.insertOR(); |
183 | |
} |
184 | |
|
185 | |
|
186 | 0 | StatementVO unsimplified = RulesUtil.clone(rule.getStatementVO()); |
187 | 0 | boolean structureChanged = rule.getStatementVO().simplify(); |
188 | 0 | rule.getEditHistory().save(rule.getStatementVO()); |
189 | |
|
190 | |
|
191 | 0 | if (structureChanged) { |
192 | 0 | showRuleBeforeSimplify(unsimplified); |
193 | |
} else { |
194 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, true); |
195 | |
} |
196 | 0 | } |
197 | |
}); |
198 | |
|
199 | 0 | btnMakeAND.addClickHandler(new ClickHandler() { |
200 | |
public void onClick(ClickEvent event) { |
201 | |
|
202 | |
|
203 | 0 | if (isMatchingOperandSelected(Token.And)) { |
204 | 0 | StatementVO statementVO = rule.getSelectedStatementVOs().get(0); |
205 | 0 | if (statementVO != null) { |
206 | 0 | statementVO.getStatementInfo().setOperator(StatementOperatorTypeKey.AND); |
207 | 0 | statementVO.toggleAndOr(); |
208 | |
} |
209 | 0 | } else { |
210 | 0 | rule.insertAND(); |
211 | |
} |
212 | |
|
213 | |
|
214 | 0 | StatementVO unsimplified = RulesUtil.clone(rule.getStatementVO()); |
215 | 0 | boolean structureChanged = rule.getStatementVO().simplify(); |
216 | 0 | rule.getEditHistory().save(rule.getStatementVO()); |
217 | |
|
218 | |
|
219 | 0 | if (structureChanged) { |
220 | 0 | showRuleBeforeSimplify(unsimplified); |
221 | |
} else { |
222 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, true); |
223 | |
} |
224 | 0 | } |
225 | |
}); |
226 | |
|
227 | 0 | btnDelete.addClickHandler(new ClickHandler() { |
228 | |
public void onClick(ClickEvent event) { |
229 | 0 | rule.deleteItem(); |
230 | |
|
231 | |
|
232 | 0 | StatementVO unsimplified = RulesUtil.clone(rule.getStatementVO()); |
233 | 0 | boolean structureChanged = false; |
234 | 0 | if (rule.getStatementVO() != null) { |
235 | 0 | structureChanged = rule.getStatementVO().simplify(); |
236 | |
} |
237 | |
|
238 | 0 | rule.getEditHistory().save(rule.getStatementVO()); |
239 | |
|
240 | |
|
241 | 0 | if (structureChanged) { |
242 | 0 | showRuleBeforeSimplify(unsimplified); |
243 | |
} else { |
244 | 0 | redraw((rule.getStatementVO() == null ? null : rule.getStatementVO().getStatementTreeViewInfo()), false, true); |
245 | |
} |
246 | 0 | } |
247 | |
}); |
248 | |
|
249 | 0 | btnAddToGroup.addClickHandler(new ClickHandler() { |
250 | |
public void onClick(ClickEvent event) { |
251 | 0 | rule.addToGroup(); |
252 | |
|
253 | |
|
254 | 0 | StatementVO unsimplified = RulesUtil.clone(rule.getStatementVO()); |
255 | 0 | boolean structureChanged = false; |
256 | 0 | if (rule.getStatementVO() != null) { |
257 | 0 | structureChanged = rule.getStatementVO().simplify(); |
258 | |
} |
259 | 0 | rule.getEditHistory().save(rule.getStatementVO()); |
260 | |
|
261 | |
|
262 | 0 | if (structureChanged) { |
263 | 0 | showRuleBeforeSimplify(unsimplified); |
264 | |
} else { |
265 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, true); |
266 | |
} |
267 | 0 | } |
268 | |
}); |
269 | |
|
270 | 0 | btnUndo.addClickHandler(new ClickHandler() { |
271 | |
public void onClick(ClickEvent event) { |
272 | 0 | StatementVO previousState = rule.getEditHistory().undo(); |
273 | 0 | if (previousState != null) { |
274 | 0 | rule.setStatementVO(previousState); |
275 | |
} |
276 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, true); |
277 | 0 | } |
278 | |
}); |
279 | |
|
280 | 0 | btnRedo.addClickHandler(new ClickHandler() { |
281 | |
public void onClick(ClickEvent event) { |
282 | 0 | StatementVO nextState = rule.getEditHistory().redo(); |
283 | 0 | if (nextState != null) { |
284 | 0 | rule.setStatementVO(nextState); |
285 | |
} |
286 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, true); |
287 | 0 | } |
288 | |
}); |
289 | 0 | } |
290 | |
|
291 | |
|
292 | |
public void redraw() { |
293 | 0 | updateTable(); |
294 | 0 | } |
295 | |
|
296 | |
|
297 | |
public void redraw(StatementTreeViewInfo stmtTreeInfo, boolean newRule, Boolean ruleChanged) { |
298 | 0 | rule.getStatementVO().clearStatementAndReqComponents(); |
299 | 0 | rule.getStatementVO().setStatementTreeViewInfo(stmtTreeInfo); |
300 | 0 | if (newRule) { |
301 | 0 | rule.setEditHistory(new EditHistory(rule.getStatementVO())); |
302 | |
} |
303 | |
|
304 | 0 | rule.getStatementVO().clearSelections(); |
305 | 0 | updateTable(); |
306 | |
|
307 | 0 | if (ruleChanged != null) { |
308 | 0 | ruleChangedCallback.exec(ruleChanged); |
309 | |
} |
310 | 0 | } |
311 | |
|
312 | |
private void updateTable() { |
313 | 0 | setEnableButtons(true); |
314 | 0 | ruleTable.clear(); |
315 | 0 | Node tree = rule.getStatementTree(); |
316 | 0 | if ((tree != null) && (rule.getStatementVO().getChildCount() > 0)) { |
317 | |
|
318 | |
|
319 | 0 | if (ruleTablePanel.getWidgetIndex(ruleTable) == -1) { |
320 | 0 | ruleTablePanel.clear(); |
321 | 0 | ruleTablePanel.add(ruleTable); |
322 | |
} |
323 | |
|
324 | 0 | if (rule.isSimplifying()) { |
325 | 0 | twiddler.setText("Simplifying..."); |
326 | 0 | twiddler.setVisible(true); |
327 | |
} else { |
328 | 0 | twiddler.setText(""); |
329 | 0 | twiddler.setVisible(false); |
330 | |
} |
331 | 0 | ruleTable.buildTable(tree); |
332 | 0 | textClickHandler.removeHandler(); |
333 | 0 | ruleTable.addTextClickHandler(ruleTableSelectionHandler); |
334 | 0 | ruleTable.addEditClauseHandler(ruleTableEditClauseHandler); |
335 | |
} else { |
336 | 0 | ruleTablePanel.clear(); |
337 | 0 | ruleTablePanel.add(new KSLabel("No rules have been added")); |
338 | |
} |
339 | 0 | } |
340 | |
|
341 | |
private void showRuleBeforeSimplify(StatementVO unsimplified) { |
342 | 0 | rule.setStatementVO(unsimplified); |
343 | 0 | rule.setSimplifying(true); |
344 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, false); |
345 | |
|
346 | 0 | Timer simplifyingTimer = new Timer() { |
347 | |
public void run() { |
348 | 0 | rule.setSimplifying(false); |
349 | 0 | rule.setStatementVO(rule.getEditHistory().getLastHistoricStmtVO()); |
350 | 0 | redraw(rule.getStatementVO().getStatementTreeViewInfo(), false, true); |
351 | 0 | } |
352 | |
}; |
353 | 0 | simplifyingTimer.schedule(1000); |
354 | 0 | } |
355 | |
|
356 | |
public void setEnabledView(boolean enabled) { |
357 | 0 | setEnableButtons(enabled); |
358 | 0 | ruleTable.setEnabled(enabled); |
359 | 0 | isEnabled = enabled; |
360 | 0 | } |
361 | |
|
362 | |
public void setEnableButtons(boolean enabled) { |
363 | 0 | if (enabled) { |
364 | 0 | btnMakeAND.setEnabled(isMatchingOperandSelected(Token.And) || rule.statementVOIsGroupAble()); |
365 | 0 | btnMakeOR.setEnabled(isMatchingOperandSelected(Token.Or) || rule.statementVOIsGroupAble()); |
366 | 0 | btnAddToGroup.setEnabled(rule.isAddToGroupOK()); |
367 | 0 | btnUndo.setEnabled(rule.getEditHistory().isUndoable()); |
368 | 0 | btnRedo.setEnabled(rule.getEditHistory().isRedoable()); |
369 | 0 | btnDelete.setEnabled(isAbleToDelete()); |
370 | 0 | btnMoveRuleUp.setEnabled(rule.getStatementVO().isNodeSelected() && !rule.getStatementVO().isFirstSelectedReqComp() && isAbletoMoveReqComp()); |
371 | 0 | btnMoveRuleDown.setEnabled(rule.getStatementVO().isNodeSelected() && !rule.getStatementVO().isLastSelectedReqComp() && isAbletoMoveReqComp()); |
372 | |
} else { |
373 | 0 | btnMakeAND.setEnabled(false); |
374 | 0 | btnMakeOR.setEnabled(false); |
375 | 0 | btnAddToGroup.setEnabled(false); |
376 | 0 | btnUndo.setEnabled(false); |
377 | 0 | btnRedo.setEnabled(false); |
378 | 0 | btnDelete.setEnabled(false); |
379 | 0 | btnMoveRuleUp.setEnabled(false); |
380 | 0 | btnMoveRuleDown.setEnabled(false); |
381 | |
} |
382 | 0 | } |
383 | |
|
384 | |
|
385 | |
private boolean isAbletoMoveReqComp() { |
386 | 0 | List<ReqComponentVO> selectedRCs = rule.getStatementVO().getSelectedReqComponentVOs(); |
387 | 0 | List<StatementVO> selectedSs = rule.getStatementVO().getSelectedStatementVOs(); |
388 | 0 | int numSelectedRCs = (selectedRCs == null)? 0 : selectedRCs.size(); |
389 | 0 | int numSelectedSs = (selectedSs == null)? 0 : selectedSs.size(); |
390 | 0 | return (numSelectedRCs == 1 && numSelectedSs == 0); |
391 | |
} |
392 | |
|
393 | |
|
394 | |
private boolean isMatchingOperandSelected(int type) { |
395 | 0 | List<StatementVO> selectedStmts = rule.getSelectedStatementVOs(); |
396 | 0 | if (((rule.getSelectedReqComponentVOs() == null) || (rule.getSelectedReqComponentVOs().isEmpty())) && (selectedStmts.size() == 1) |
397 | |
&& (((StatementVO)selectedStmts.get(0)).getType() != type)) { |
398 | 0 | return true; |
399 | |
} |
400 | 0 | return false; |
401 | |
} |
402 | |
|
403 | |
private boolean isAbleToDelete() { |
404 | |
|
405 | 0 | if ((rule.getSelectedStatementVOs() == null || rule.getSelectedStatementVOs().size() == 0) && (rule.getSelectedReqComponentVOs().size() > 1)) { |
406 | 0 | return true; |
407 | |
} |
408 | |
|
409 | 0 | return rule.statementVOIsDegroupAble(); |
410 | |
} |
411 | |
|
412 | |
private void createButtonsPanel() { |
413 | 0 | final String ruleEditButton = "KS-Program-Rule-ObjectView-Button"; |
414 | |
|
415 | 0 | topButtonsPanel.setStyleName("KS-Program-Rule-ObjectView-ButtonPanel"); |
416 | 0 | btnMakeAND.addStyleName(ruleEditButton); |
417 | 0 | btnMakeAND.addStyleName("KS-Program-Rule-ObjectView-AND-Button"); |
418 | 0 | topButtonsPanel.add(btnMakeAND); |
419 | 0 | btnMakeOR.addStyleName("KS-Program-Rule-ObjectView-OR-Button"); |
420 | 0 | btnMakeOR.addStyleName(ruleEditButton); |
421 | 0 | topButtonsPanel.add(btnMakeOR); |
422 | |
|
423 | |
|
424 | 0 | btnAddToGroup.addStyleName("KS-Program-Rule-ObjectView-Group-Button"); |
425 | 0 | btnAddToGroup.addStyleName(ruleEditButton); |
426 | 0 | topButtonsPanel.add(btnAddToGroup); |
427 | 0 | btnMoveRuleUp.addStyleName("KS-Program-Rule-ObjectView-Up-Button"); |
428 | 0 | btnMoveRuleUp.addStyleName(ruleEditButton); |
429 | 0 | topButtonsPanel.add(btnMoveRuleUp); |
430 | 0 | btnMoveRuleDown.addStyleName("KS-Program-Rule-ObjectView-Down-Button"); |
431 | 0 | btnMoveRuleDown.addStyleName(ruleEditButton); |
432 | 0 | topButtonsPanel.add(btnMoveRuleDown); |
433 | |
|
434 | |
|
435 | 0 | btnUndo.addStyleName("KS-Program-Rule-ObjectView-Undo-Button"); |
436 | 0 | btnUndo.addStyleName(ruleEditButton); |
437 | 0 | topButtonsPanel.add(btnUndo); |
438 | 0 | btnRedo.addStyleName("KS-Program-Rule-ObjectView-Redo-Button"); |
439 | 0 | btnRedo.addStyleName(ruleEditButton); |
440 | 0 | topButtonsPanel.add(btnRedo); |
441 | 0 | btnDelete.addStyleName("KS-Program-Rule-ObjectView-Delete-Button"); |
442 | 0 | btnDelete.addStyleName(ruleEditButton); |
443 | 0 | topButtonsPanel.add(btnDelete); |
444 | 0 | } |
445 | |
|
446 | |
public RuleInfo getRule() { |
447 | 0 | return rule; |
448 | |
} |
449 | |
|
450 | |
public void setRule(RuleInfo rule) { |
451 | 0 | this.rule = rule; |
452 | 0 | } |
453 | |
|
454 | |
public void addReqCompEditButtonClickCallback(Callback<ReqComponentInfo> callback) { |
455 | 0 | reqCompEditCallback = callback; |
456 | 0 | } |
457 | |
|
458 | |
public void addRuleChangedButtonClickCallback(Callback<Boolean> callback) { |
459 | 0 | ruleChangedCallback = callback; |
460 | 0 | } |
461 | |
} |