1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.widgets.rules;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 public class EditHistory implements Serializable {
23
24 private static final long serialVersionUID = 1L;
25 private List<StatementVO> histories = new ArrayList<StatementVO>();
26 private static final int MAX_NUM_HISTORIES = 10;
27 private int currHistoryIndex = -1;
28
29 public EditHistory(StatementVO origStatementVO) {
30 save(origStatementVO);
31 }
32
33 public void save(StatementVO historicStmtVO) {
34 List<StatementVO> newHistories = new ArrayList<StatementVO>();
35
36
37 if (currHistoryIndex >= 0 && currHistoryIndex < histories.size()) {
38 for (int i = 0; i < currHistoryIndex + 1; i++) {
39 newHistories.add(histories.get(i));
40 }
41 histories = newHistories;
42 }
43
44 histories.add(RulesUtil.clone(historicStmtVO));
45 currHistoryIndex++;
46
47
48 if (histories.size() > MAX_NUM_HISTORIES) {
49 histories.remove(0);
50 currHistoryIndex = histories.size() - 1;
51 }
52 }
53
54 public StatementVO getLastHistoricStmtVO() {
55 return RulesUtil.clone(histories.get(currHistoryIndex));
56 }
57
58 public StatementVO getSafeHistoricStmtVO(int index) {
59 index = (index < 0 ? 0 : (index < histories.size() ? index : histories.size()));
60 return RulesUtil.clone(histories.get(index));
61 }
62
63 public StatementVO undo() {
64 currHistoryIndex--;
65 currHistoryIndex = (currHistoryIndex < 0 ? -1 : currHistoryIndex);
66 StatementVO lastHistoricStmtVO = getSafeHistoricStmtVO(currHistoryIndex);
67 return lastHistoricStmtVO;
68 }
69
70 public StatementVO redo() {
71 currHistoryIndex++;
72 currHistoryIndex = (currHistoryIndex < histories.size() ? currHistoryIndex : histories.size());
73 StatementVO futureHistoricStmtVO = getSafeHistoricStmtVO(currHistoryIndex);
74 return futureHistoricStmtVO;
75 }
76
77 public boolean isUndoable() {
78 return currHistoryIndex > 0;
79 }
80
81 public boolean isRedoable() {
82 return currHistoryIndex < histories.size() - 1;
83 }
84 }