View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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          // replace out of date histories with the new historicStmtVO
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          // delete old element
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  }