00001 00002 //---------------------------------------------------------------------------------------------------------------- 00003 // Class definition include 00004 //---------------------------------------------------------------------------------------------------------------- 00005 #include "CommandsHandler.h" 00006 00007 //---------------------------------------------------------------------------------------------------------------- 00008 // Class implementation 00009 //---------------------------------------------------------------------------------------------------------------- 00012 //------------------------------------------------------------------------------------------------------------ 00013 // Constructors & Destructors 00014 //------------------------------------------------------------------------------------------------------------ 00015 00016 /* 00017 * Constructs the CommandsHandler 00018 */ 00019 CommandsHandler :: CommandsHandler() 00020 { 00021 redo_actions = new CommandsRegisterStructure(); 00022 unDo_actions = new CommandsRegisterStructure(); 00023 00024 posibleREDO = false; 00025 posibleUNDO = false; 00026 00027 isLastREDO_executed = true; 00028 isLastUNDO_executed = false; 00029 } 00030 00031 /* 00032 * Destructs the CommandsHandler 00033 */ 00034 CommandsHandler :: ~CommandsHandler() 00035 { 00036 clearActions(); 00037 delete redo_actions; 00038 delete unDo_actions; 00039 } 00040 00041 //------------------------------------------------------------------------------------------------------------ 00042 // Methods 00043 //------------------------------------------------------------------------------------------------------------ 00044 00045 /* 00046 * Registers in the vectors of doneActions and unDoActions the given commands that all ready corresponds each other to the inverse of the otherone. 00047 * If is the first registered action notifies the posibleUNDO avaliability. 00048 * @param doneAction Is the action to register in the redo_actions vector. 00049 * @param unDoAction Is the action to register in the unDo_actions vector. 00050 */ 00051 void CommandsHandler :: registerCommand(CommandObject* doneAction, CommandObject* unDoAction) 00052 { 00053 int actToUNDO = unDo_actions->getActualIndex(); 00054 redo_actions->setActualIndex( actToUNDO ); 00055 00056 redo_actions->registerCommand(doneAction); 00057 unDo_actions->registerCommand(unDoAction); 00058 00059 posibleREDO = false; 00060 posibleUNDO = true; 00061 00062 isLastREDO_executed = true; 00063 isLastUNDO_executed = false; 00064 00065 } 00066 00067 /* 00068 * Undo a command. Managing the correspondig vectors and the execution of the inverse action to the - ACTUAL DONE- action 00069 * @return Returns true if the inverse command ( the actual to execute in UNDO actions) is executed. If it is false the state of the vector must not change. 00070 */ 00071 bool CommandsHandler :: undo() 00072 { 00073 bool executed = posibleUNDO; 00074 if( posibleUNDO ) 00075 { 00076 validateOperationsAvaliability();//para borrar!!!!!!!!-----------------------------*********************--------------------- 00077 executed = theWorksSpaceBoss->executeCommand(getActual_UNDO(), true); 00078 isLastUNDO_executed = true; 00079 if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext()) 00080 { 00081 if(unDo_actions->hasActualPrevious()) 00082 executed = unDo_actions->moveBack_Actual() ; 00083 else 00084 executed = true; 00085 if( unDo_actions->hasLastNext() ) 00086 executed = executed && unDo_actions->moveBack_Last(); 00087 isLastREDO_executed = false; 00088 } 00089 else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious()) 00090 { 00091 executed = redo_actions->moveBack_Actual(); 00092 executed = executed && redo_actions->moveBack_Last(); 00093 executed = executed && unDo_actions->moveBack_Last(); 00094 } 00095 else 00096 { 00097 executed = unDo_actions->moveBack_Last(); 00098 executed = executed && unDo_actions->moveBack_Actual(); 00099 executed = executed && redo_actions->moveBack_Actual(); 00100 if( redo_actions->hasLastNext() ) 00101 executed = executed && redo_actions->moveBack_Last(); 00102 } 00103 validateOperationsAvaliability(); 00104 } 00105 return executed; 00106 } 00107 00108 /* 00109 * Redo a command. Managing the correspondig vectors and the execution of the inverse action to the - ACTUAL DONE- action 00110 * @return Returns true if the actual command to execute ( the actual to execute in REDO actions )has been executed. If it is false the state of the vector must not change. 00111 */ 00112 bool CommandsHandler :: redo() 00113 { 00114 bool executed = posibleREDO; 00115 if( posibleREDO ) 00116 { 00117 validateOperationsAvaliability();//para borrar!!!!!!!!-----------------------------*********************--------------------- 00118 isLastREDO_executed = true; 00119 //isLastUNDO_executed = false; // Posible needed 00120 executed = theWorksSpaceBoss->executeCommand(getActual_REDO(), true); 00121 if (!unDo_actions->hasActualPrevious() && !redo_actions->hasActualPrevious()) 00122 { 00123 executed = redo_actions->moveForward_Actual(); 00124 executed = executed && redo_actions->moveBack_Last(); 00125 isLastUNDO_executed = false; 00126 } 00127 else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious()) 00128 { 00129 executed = redo_actions->moveForward_Actual(); 00130 if(unDo_actions->hasLastNext()) 00131 executed = executed && unDo_actions->moveForward_Last(); 00132 executed = executed && unDo_actions->moveForward_Actual(); 00133 if(redo_actions->hasLastNext()) 00134 executed = executed && redo_actions->moveForward_Last(); 00135 } 00136 else 00137 { 00138 executed = unDo_actions->moveForward_Actual(); 00139 executed = executed && unDo_actions->moveForward_Last(); 00140 executed = executed && redo_actions->moveForward_Actual(); 00141 if( redo_actions->hasLastNext() ) 00142 executed = executed && redo_actions->moveForward_Last(); 00143 else 00144 executed = executed && redo_actions->moveBack_Last(); 00145 } 00146 if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious()) 00147 { 00148 isLastUNDO_executed = false; 00149 } 00150 if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext()) 00151 { 00152 isLastUNDO_executed = false; 00153 } 00154 validateOperationsAvaliability(); 00155 } 00156 return executed; 00157 } 00158 00159 /* 00160 * Notitify if posibleREDO is posible or not. 00161 * @return Returns the state of posibleUNDO 00162 */ 00163 bool CommandsHandler :: isPosibleUNDO() 00164 { 00165 return posibleUNDO; 00166 } 00167 00168 /* 00169 * Indicates if posibleUNDO is posible or not. 00170 * @return Returns the state of posibleREDO 00171 */ 00172 bool CommandsHandler :: isPosibleREDO() 00173 { 00174 return posibleREDO; 00175 } 00176 00177 /* 00178 * Sets posibleREDO state. 00179 * @param UNDOstate The state of posibleUNDO to set 00180 */ 00181 void CommandsHandler :: setPosibleUNDO( bool UNDOstate ) 00182 { 00183 posibleUNDO = UNDOstate; 00184 } 00185 00186 /* 00187 * Sets posibleUNDO state. 00188 * @param REDOstate The state of posibleREDO to set 00189 */ 00190 void CommandsHandler :: setPosibleREDO( bool REDOstate ) 00191 { 00192 posibleREDO = REDOstate; 00193 } 00194 00195 /* 00196 * Clear the registered actions in the DO and UNDO vectors. 00197 */ 00198 void CommandsHandler :: clearActions() 00199 { 00200 redo_actions->clearActions(); 00201 unDo_actions->clearActions(); 00202 } 00203 00204 /* 00205 * Returns hoy mane paired commands had been registered, if the done and unDo vectors dont match returns -1 as error code. 00206 * @return Returns how many paired-commands had been registered 00207 */ 00208 int CommandsHandler :: getTotalCommands() 00209 { 00210 int value = redo_actions->getTotalCommandsCount(); 00211 return value == (unDo_actions->getTotalCommandsCount()) ? value : -1; 00212 } 00213 00214 00215 /* 00216 * Gets the actual command in the UNDO-list 00217 * @return Returns a pointer to the actual undo action 00218 */ 00219 CommandObject * CommandsHandler :: getActual_UNDO() 00220 { 00221 return unDo_actions->getActual_Pointer(); 00222 } 00223 00224 /* 00225 * Gets the actual command in the REDO-list 00226 * @return Returns a pointer to the actual do action 00227 */ 00228 CommandObject * CommandsHandler :: getActual_REDO() 00229 { 00230 return redo_actions->getActual_Pointer(); 00231 } 00232 00233 /* 00234 * Gets the command at the given position in the DO (REDO) vector 00235 * @return The pointer to the referenced object by the position 00236 */ 00237 CommandObject * CommandsHandler :: get_DO_CommandAt(int position) 00238 { 00239 return redo_actions->getCommandAt(position); 00240 } 00241 00242 /* 00243 * Gets the command at the given position in the UNDO vector 00244 * @return The pointer to the referenced object by the position 00245 */ 00246 CommandObject * CommandsHandler :: get_UNDO_CommandAt(int position) 00247 { 00248 return unDo_actions->getCommandAt(position); 00249 } 00250 00251 /* 00252 * Sets the model parent of the action handler 00253 * @param theModelParent The boss reference 00254 */ 00255 void CommandsHandler :: setModelBoss(ICommandsUser * theModelParent) 00256 { 00257 theWorksSpaceBoss = theModelParent; 00258 } 00259 00260 /* 00261 * Validates if it is posible of not to do UNDO and/or REDO and sets the corresponding values 00262 */ 00263 void CommandsHandler :: validateOperationsAvaliability() 00264 { 00265 posibleREDO = (redo_actions->hasActualNext() && unDo_actions->hasActualNext() )? true : 00266 ( (!redo_actions->hasActualNext()&& unDo_actions->hasActualNext() ) ? true : 00267 ( !unDo_actions->hasActualNext() && !redo_actions->hasActualNext() )? false : true && !isLastREDO_executed ); 00268 00269 posibleUNDO = (redo_actions->hasActualPrevious() && unDo_actions->hasActualPrevious() )? true : 00270 ( (!unDo_actions->hasActualPrevious()&& redo_actions->hasActualPrevious() ) ? true : 00271 ( !unDo_actions->hasActualPrevious() && !redo_actions->hasActualPrevious() )? false : true && !isLastUNDO_executed ); 00272 00273 }