| 1 | /*
 | 
|---|
| 2 |  * Action.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Dec 8, 2009
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <string>
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include "Actions/Action.hpp"
 | 
|---|
| 13 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 14 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| 15 | #include "Exceptions/MissingValueException.hpp"
 | 
|---|
| 16 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 17 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "log.hpp"
 | 
|---|
| 20 | #include "verbose.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | using namespace std;
 | 
|---|
| 23 | 
 | 
|---|
| 24 | Action::state_ptr getEmptyState() {
 | 
|---|
| 25 |   return Action::state_ptr(Memory::ignore(new ActionState()));
 | 
|---|
| 26 | }
 | 
|---|
| 27 | 
 | 
|---|
| 28 | // An empty state to indicate success
 | 
|---|
| 29 | Action::state_ptr Action::success = getEmptyState();
 | 
|---|
| 30 | Action::state_ptr Action::failure = getEmptyState();
 | 
|---|
| 31 | 
 | 
|---|
| 32 | Action::Action(std::string _name,bool _doRegister) :
 | 
|---|
| 33 | name(_name)
 | 
|---|
| 34 | {
 | 
|---|
| 35 |   if(_doRegister){
 | 
|---|
| 36 |     ActionRegistry::getInstance().registerAction(this);
 | 
|---|
| 37 |   }
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | Action::~Action()
 | 
|---|
| 41 | {}
 | 
|---|
| 42 | 
 | 
|---|
| 43 | const string Action::getName(){
 | 
|---|
| 44 |   return name;
 | 
|---|
| 45 | }
 | 
|---|
| 46 | 
 | 
|---|
| 47 | void Action::call(enum QueryOptions flag){
 | 
|---|
| 48 |   if(!isActive()){
 | 
|---|
| 49 |     return;
 | 
|---|
| 50 |   }
 | 
|---|
| 51 |   // forward to private virtual
 | 
|---|
| 52 |   if (flag == Interactive) {
 | 
|---|
| 53 |     Dialog* dialog = createDialog();
 | 
|---|
| 54 |     if (dialog != NULL) {
 | 
|---|
| 55 |       dialog->display();
 | 
|---|
| 56 |       delete(dialog);
 | 
|---|
| 57 |     }
 | 
|---|
| 58 |   }
 | 
|---|
| 59 |   state_ptr state = Action::failure;
 | 
|---|
| 60 | //  try {
 | 
|---|
| 61 |     state = performCall();
 | 
|---|
| 62 | //  } catch(MissingValueException&) {
 | 
|---|
| 63 | //    DoeLog(0) && (eLog() << Verbose(0) << "There is a value missing for action " << getName() << endl);
 | 
|---|
| 64 | //  };
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   if(shouldUndo() && state != failure){
 | 
|---|
| 67 |     if(canUndo()){
 | 
|---|
| 68 |       ActionHistory::getInstance().addElement(this,state);
 | 
|---|
| 69 |     }
 | 
|---|
| 70 |     else{
 | 
|---|
| 71 |       ActionHistory::getInstance().clear();
 | 
|---|
| 72 |     }
 | 
|---|
| 73 |   }
 | 
|---|
| 74 | }
 | 
|---|
| 75 | Action::state_ptr Action::undo(state_ptr _state) {
 | 
|---|
| 76 |   // forward to private virtual
 | 
|---|
| 77 |   return performUndo(_state);
 | 
|---|
| 78 | }
 | 
|---|
| 79 | Action::state_ptr Action::redo(state_ptr _state) {
 | 
|---|
| 80 |   // forward to private virtual
 | 
|---|
| 81 |   return performRedo(_state);
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | 
 | 
|---|
| 85 | bool Action::isActive(){
 | 
|---|
| 86 |   return true;
 | 
|---|
| 87 | }
 | 
|---|