| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [65b6e0] | 8 | /*
 | 
|---|
 | 9 |  * Action.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Dec 8, 2009
 | 
|---|
 | 12 |  *      Author: crueger
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [112b09] | 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
| [cc04b7] | 22 | #include <string>
 | 
|---|
| [65b6e0] | 23 | 
 | 
|---|
| [cc04b7] | 24 | #include "Actions/Action.hpp"
 | 
|---|
 | 25 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [d56640] | 26 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| [4e145c] | 27 | #include "Exceptions/MissingValueException.hpp"
 | 
|---|
 | 28 | #include "UIElements/Dialog.hpp"
 | 
|---|
| [632bc3] | 29 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| [047878] | 30 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| [cc04b7] | 31 | 
 | 
|---|
| [952f38] | 32 | #include "Helpers/Log.hpp"
 | 
|---|
 | 33 | #include "Helpers/Verbose.hpp"
 | 
|---|
| [4e145c] | 34 | 
 | 
|---|
| [cc04b7] | 35 | using namespace std;
 | 
|---|
 | 36 | 
 | 
|---|
| [6d6b54] | 37 | Action::state_ptr getEmptyState() {
 | 
|---|
 | 38 |   return Action::state_ptr(Memory::ignore(new ActionState()));
 | 
|---|
 | 39 | }
 | 
|---|
 | 40 | 
 | 
|---|
| [5b0b98] | 41 | // An empty state to indicate success
 | 
|---|
| [6d6b54] | 42 | Action::state_ptr Action::success = getEmptyState();
 | 
|---|
 | 43 | Action::state_ptr Action::failure = getEmptyState();
 | 
|---|
| [67e2b3] | 44 | 
 | 
|---|
| [cc04b7] | 45 | Action::Action(std::string _name,bool _doRegister) :
 | 
|---|
 | 46 | name(_name)
 | 
|---|
 | 47 | {
 | 
|---|
 | 48 |   if(_doRegister){
 | 
|---|
| [b2d8d0] | 49 |     ActionRegistry::getInstance().registerInstance(this);
 | 
|---|
| [cc04b7] | 50 |   }
 | 
|---|
 | 51 | }
 | 
|---|
| [65b6e0] | 52 | 
 | 
|---|
 | 53 | Action::~Action()
 | 
|---|
| [ef81b0] | 54 | {}
 | 
|---|
| [cc04b7] | 55 | 
 | 
|---|
 | 56 | const string Action::getName(){
 | 
|---|
 | 57 |   return name;
 | 
|---|
 | 58 | }
 | 
|---|
| [67e2b3] | 59 | 
 | 
|---|
| [047878] | 60 | Dialog * Action::createDialog(){
 | 
|---|
 | 61 |   Dialog *dialog = UIFactory::getInstance().makeDialog();
 | 
|---|
 | 62 |   return fillDialog(dialog);
 | 
|---|
 | 63 | }
 | 
|---|
 | 64 | 
 | 
|---|
| [4e145c] | 65 | void Action::call(enum QueryOptions flag){
 | 
|---|
| [2efa90] | 66 |   if(!isActive()){
 | 
|---|
 | 67 |     return;
 | 
|---|
 | 68 |   }
 | 
|---|
| [67e2b3] | 69 |   // forward to private virtual
 | 
|---|
| [4e145c] | 70 |   if (flag == Interactive) {
 | 
|---|
 | 71 |     Dialog* dialog = createDialog();
 | 
|---|
| [031f62] | 72 |     if (dialog->hasQueries()) {
 | 
|---|
| [4e145c] | 73 |       dialog->display();
 | 
|---|
 | 74 |     }
 | 
|---|
| [031f62] | 75 |     delete(dialog);
 | 
|---|
| [4e145c] | 76 |   }
 | 
|---|
 | 77 |   state_ptr state = Action::failure;
 | 
|---|
| [e2f3114] | 78 | //  try {
 | 
|---|
| [4e145c] | 79 |     state = performCall();
 | 
|---|
| [e2f3114] | 80 | //  } catch(MissingValueException&) {
 | 
|---|
 | 81 | //    DoeLog(0) && (eLog() << Verbose(0) << "There is a value missing for action " << getName() << endl);
 | 
|---|
 | 82 | //  };
 | 
|---|
| [4e145c] | 83 | 
 | 
|---|
| [d56640] | 84 |   if(shouldUndo() && state != failure){
 | 
|---|
 | 85 |     if(canUndo()){
 | 
|---|
 | 86 |       ActionHistory::getInstance().addElement(this,state);
 | 
|---|
 | 87 |     }
 | 
|---|
 | 88 |     else{
 | 
|---|
 | 89 |       ActionHistory::getInstance().clear();
 | 
|---|
 | 90 |     }
 | 
|---|
 | 91 |   }
 | 
|---|
| [67e2b3] | 92 | }
 | 
|---|
| [5b0b98] | 93 | Action::state_ptr Action::undo(state_ptr _state) {
 | 
|---|
| [67e2b3] | 94 |   // forward to private virtual
 | 
|---|
 | 95 |   return performUndo(_state);
 | 
|---|
 | 96 | }
 | 
|---|
| [5b0b98] | 97 | Action::state_ptr Action::redo(state_ptr _state) {
 | 
|---|
| [67e2b3] | 98 |   // forward to private virtual
 | 
|---|
 | 99 |   return performRedo(_state);
 | 
|---|
 | 100 | }
 | 
|---|
| [f9352d] | 101 | 
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 | bool Action::isActive(){
 | 
|---|
 | 104 |   return true;
 | 
|---|
 | 105 | }
 | 
|---|