Clover coverage report - Maven Clover report
Coverage timestamp: Thu Oct 11 2007 08:41:48 CEST
file stats: LOC: 170   Methods: 9
NCLOC: 96   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ActionHistoric.java 57.1% 73.5% 88.9% 71.9%
coverage coverage
 1    /*
 2    * Copyright (C) 2007 Eric MIGNOT - mignots.eric@free.fr
 3    *
 4    * This library is free software; you can redistribute it and/or
 5    * modify it under the terms of the GNU Lesser General Public
 6    * License as published by the Free Software Foundation; either
 7    * version 2.1 of the License, or (at your option) any later version.
 8    *
 9    * This library is distributed in the hope that it will be useful,
 10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 12    * Lesser General Public License for more details.
 13    *
 14    * You should have received a copy of the GNU Lesser General Public
 15    * License along with this library; if not, write to the Free Software
 16    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 17    */
 18   
 19    package org.ericmignot.modeler.util.action;
 20   
 21    import java.util.*;
 22   
 23    /**
 24    * Implements the action historic functionalities.
 25    */
 26    public class ActionHistoric extends ArrayList
 27    {
 28   
 29    private int lastExecutedActionIndex;
 30   
 31    /**
 32    * Constructs the new instance with an initial empty action collection.
 33    * Sets <code>lastExecutedActionIndex</code> -1.
 34    */
 35  125 public ActionHistoric()
 36    {
 37  125 super();
 38  125 this.lastExecutedActionIndex = -1;
 39    }
 40   
 41    /**
 42    * Removes the actions after the <code>lastExecutedActionIndex</code>.<br>
 43    * Adds the given action to the collection.<br>
 44    * Set <code>lastExecutedActionIndex</code> to the index of the added action.<br>
 45    * Executes the given action with the given parameter.<br>
 46    * If the action isnt an UndoableAction empties the collection.<br>
 47    * Returns the returned value of the action.<br>
 48    */
 49  29 public Object execute(Action action, Object actionParameter) throws ActionException
 50    {
 51  29 Object toReturn = action.execute(actionParameter);
 52  29 if (action instanceof UndoableAction)
 53    {
 54  28 if (size() > 0)
 55    {
 56  4 this.removeRange(this.lastExecutedActionIndex + 1, size());
 57    }
 58  28 this.add(action);
 59  28 this.lastExecutedActionIndex ++;
 60    }
 61    else
 62    {
 63  1 clear();
 64  1 this.lastExecutedActionIndex = -1;
 65    }
 66  29 return toReturn;
 67    }
 68   
 69    /**
 70    * Indicates whether or not the <code>lastExecutedActionIndex</code>
 71    * has its value different than -1.
 72    */
 73  24 public boolean canUndo()
 74    {
 75  24 return (this.lastExecutedActionIndex != -1);
 76    }
 77   
 78    /**
 79    *
 80    * @return null or the action that will be cancelled by an undo call
 81    */
 82  7 public UndoableAction getNextUndoAction()
 83    {
 84  7 if (canUndo())
 85    {
 86  7 return (UndoableAction) get(this.lastExecutedActionIndex);
 87    }
 88    else
 89    {
 90  0 return null;
 91    }
 92    }
 93   
 94    /**
 95    * Undo the last executed action if it is an Undoable action with the given parameter.<br>
 96    * Sets <code>lastExecutedActionIndex</code> 1 position behind.<br>
 97    * returns the returned value of the action.<br>
 98    */
 99  11 public Object undo(Object actionParameter) throws ActionException
 100    {
 101  11 if (canUndo())
 102    {
 103  11 UndoableAction lastAction = (UndoableAction) get(this.lastExecutedActionIndex);
 104  11 this.lastExecutedActionIndex--;
 105  11 return lastAction.cancel(actionParameter);
 106    }
 107    else
 108    {
 109  0 throw new ActionException("No Undoable action available");
 110    }
 111    }
 112   
 113    /**
 114    * Indicates whether or not the <code>lastExecutedActionIndex</code>
 115    * has its value different than the size of the collection minus 1.
 116    */
 117  5 public boolean canRedo()
 118    {
 119  5 return (this.lastExecutedActionIndex != (size() -1));
 120    }
 121   
 122    /**
 123    *
 124    * @return null or the action that will be executed by an redo call
 125    */
 126  2 public Action getNextRedoAction()
 127    {
 128  2 if (canRedo())
 129    {
 130  2 return (Action) get(this.lastExecutedActionIndex + 1);
 131    }
 132    else
 133    {
 134  0 return null;
 135    }
 136    }
 137   
 138    /**
 139    * Redo the action after the <code>lastExecutedActionIndex</code> action.<br>
 140    * Sets <code>lastExecutedActionIndex</code> 1 position after.<br>
 141    * returns the returned value of the action.<br>
 142    */
 143  2 public Object redo(Object actionParameter) throws ActionException
 144    {
 145  2 if (canRedo())
 146    {
 147  2 Action action = (Action) get(this.lastExecutedActionIndex + 1);
 148  2 this.lastExecutedActionIndex++;
 149  2 return action.execute(actionParameter);
 150    }
 151    else
 152    {
 153  0 throw new ActionException("No Redoable action available");
 154    }
 155    }
 156   
 157   
 158  0 public String toString()
 159    {
 160  0 String toReturn = "\n*********** ActionHistoric";
 161   
 162  0 for (int i = 0; i < size(); i ++)
 163    {
 164  0 toReturn += "\n" + get(i).toString();
 165    }
 166   
 167  0 toReturn += "\n" + "lastExecutionIndex = " + this.lastExecutedActionIndex;
 168  0 return toReturn;
 169    }
 170    }