| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| EditHistory | 
 | 
 | 2.0;2 | 
| 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 | 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 |          // replace out of date histories with the new historicStmtVO | |
| 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 |          // delete old element | |
| 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 | } |