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.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 | 0 | private List<StatementVO> histories = new ArrayList<StatementVO>(); |
26 | |
private static final int MAX_NUM_HISTORIES = 10; |
27 | 0 | private int currHistoryIndex = -1; |
28 | |
|
29 | 0 | public EditHistory(StatementVO origStatementVO) { |
30 | 0 | save(origStatementVO); |
31 | 0 | } |
32 | |
|
33 | |
public void save(StatementVO historicStmtVO) { |
34 | 0 | List<StatementVO> newHistories = new ArrayList<StatementVO>(); |
35 | |
|
36 | |
|
37 | 0 | if (currHistoryIndex >= 0 && currHistoryIndex < histories.size()) { |
38 | 0 | for (int i = 0; i < currHistoryIndex + 1; i++) { |
39 | 0 | newHistories.add(histories.get(i)); |
40 | |
} |
41 | 0 | histories = newHistories; |
42 | |
} |
43 | |
|
44 | 0 | histories.add(RulesUtil.clone(historicStmtVO)); |
45 | 0 | currHistoryIndex++; |
46 | |
|
47 | |
|
48 | 0 | if (histories.size() > MAX_NUM_HISTORIES) { |
49 | 0 | histories.remove(0); |
50 | 0 | currHistoryIndex = histories.size() - 1; |
51 | |
} |
52 | 0 | } |
53 | |
|
54 | |
public StatementVO getLastHistoricStmtVO() { |
55 | 0 | return RulesUtil.clone(histories.get(currHistoryIndex)); |
56 | |
} |
57 | |
|
58 | |
public StatementVO getSafeHistoricStmtVO(int index) { |
59 | 0 | index = (index < 0 ? 0 : (index < histories.size() ? index : histories.size())); |
60 | 0 | return RulesUtil.clone(histories.get(index)); |
61 | |
} |
62 | |
|
63 | |
public StatementVO undo() { |
64 | 0 | currHistoryIndex--; |
65 | 0 | currHistoryIndex = (currHistoryIndex < 0 ? -1 : currHistoryIndex); |
66 | 0 | StatementVO lastHistoricStmtVO = getSafeHistoricStmtVO(currHistoryIndex); |
67 | 0 | return lastHistoricStmtVO; |
68 | |
} |
69 | |
|
70 | |
public StatementVO redo() { |
71 | 0 | currHistoryIndex++; |
72 | 0 | currHistoryIndex = (currHistoryIndex < histories.size() ? currHistoryIndex : histories.size()); |
73 | 0 | StatementVO futureHistoricStmtVO = getSafeHistoricStmtVO(currHistoryIndex); |
74 | 0 | return futureHistoricStmtVO; |
75 | |
} |
76 | |
|
77 | |
public boolean isUndoable() { |
78 | 0 | return currHistoryIndex > 0; |
79 | |
} |
80 | |
|
81 | |
public boolean isRedoable() { |
82 | 0 | return currHistoryIndex < histories.size() - 1; |
83 | |
} |
84 | |
} |