| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
| [65b6e0] | 23 | /*
 | 
|---|
 | 24 |  * Action.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Dec 8, 2009
 | 
|---|
 | 27 |  *      Author: crueger
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [9eb71b3] | 35 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 36 | 
 | 
|---|
| [df32ee] | 37 | #include <iostream>
 | 
|---|
| [e4b2f6] | 38 | #include <sstream>
 | 
|---|
| [cc04b7] | 39 | #include <string>
 | 
|---|
| [65b6e0] | 40 | 
 | 
|---|
| [cc04b7] | 41 | #include "Actions/Action.hpp"
 | 
|---|
| [e69c87] | 42 | #include "Actions/ActionExceptions.hpp"
 | 
|---|
| [6367dd] | 43 | #include "Actions/ActionQueue.hpp"
 | 
|---|
| [0c8056] | 44 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [4e145c] | 45 | #include "UIElements/Dialog.hpp"
 | 
|---|
| [ad011c] | 46 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [9eb71b3] | 47 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [047878] | 48 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| [cc04b7] | 49 | 
 | 
|---|
| [ad011c] | 50 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 51 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [4e145c] | 52 | 
 | 
|---|
| [ce7fdc] | 53 | using namespace MoleCuilder;
 | 
|---|
| [cc04b7] | 54 | 
 | 
|---|
| [b5b01e] | 55 | ActionState::ptr getEmptyState() {
 | 
|---|
| [9eb71b3] | 56 |   return ActionState::ptr( 
 | 
|---|
 | 57 |         /* Memory::ignore( */
 | 
|---|
 | 58 |         new ActionState())
 | 
|---|
 | 59 |         /* ) */
 | 
|---|
 | 60 |         ;
 | 
|---|
| [6d6b54] | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
| [dfef3f] | 63 | void Action::removeStaticStateEntities()
 | 
|---|
 | 64 | {
 | 
|---|
 | 65 |   Action::success.reset();
 | 
|---|
 | 66 |   Action::failure.reset();
 | 
|---|
 | 67 | }
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 | void Action::createStaticStateEntities()
 | 
|---|
 | 70 | {
 | 
|---|
 | 71 |   Action::success = getEmptyState();
 | 
|---|
 | 72 |   Action::failure = getEmptyState();
 | 
|---|
 | 73 | }
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 | // An empty state to indicate success, these are (de)initialized by ActionHistory
 | 
|---|
| [b5b01e] | 76 | ActionState::ptr Action::success;
 | 
|---|
 | 77 | ActionState::ptr Action::failure;
 | 
|---|
| [67e2b3] | 78 | 
 | 
|---|
| [126867] | 79 | Action::Action(const ActionTrait &_Traits) :
 | 
|---|
 | 80 |     Traits(_Traits)
 | 
|---|
| [36053c] | 81 | {}
 | 
|---|
| [65b6e0] | 82 | 
 | 
|---|
 | 83 | Action::~Action()
 | 
|---|
| [36053c] | 84 | {}
 | 
|---|
| [cc04b7] | 85 | 
 | 
|---|
| [13799e] | 86 | const string Action::getName() const
 | 
|---|
 | 87 | {
 | 
|---|
| [e4afb4] | 88 |   return Traits.getName();
 | 
|---|
| [cc04b7] | 89 | }
 | 
|---|
| [67e2b3] | 90 | 
 | 
|---|
| [e4b2f6] | 91 | const std::string Action::help() const
 | 
|---|
 | 92 | {
 | 
|---|
 | 93 |   std::stringstream outputstream;
 | 
|---|
 | 94 |   outputstream << "Description for Action '" << getName() << "': " << Traits.getDescription()
 | 
|---|
 | 95 |       << std::endl;
 | 
|---|
 | 96 |   if (!Traits.hasOption(getName())) {
 | 
|---|
 | 97 |     outputstream << "\tNote that this Action does not take an argument." << std::endl;
 | 
|---|
 | 98 |   }
 | 
|---|
 | 99 |   outputstream << "Options available for action '" << getName() << "':" << std::endl;
 | 
|---|
| [3139b2] | 100 |   for (ActionTrait::options_const_iterator iter = Traits.getBeginIter();
 | 
|---|
| [e4b2f6] | 101 |       iter != Traits.getEndIter();
 | 
|---|
 | 102 |       ++iter) {
 | 
|---|
 | 103 |     outputstream << "Option '" << iter->first << "':" << std::endl;
 | 
|---|
 | 104 |     outputstream << "\tDescription: " << iter->second->getDescription() << "." << std::endl;
 | 
|---|
 | 105 |     outputstream << "\tArgument's type: " << iter->second->getTypeName() << "." << std::endl;
 | 
|---|
 | 106 |     outputstream << "\tDefault value: ";
 | 
|---|
 | 107 |     if (iter->second->hasDefaultValue()) {
 | 
|---|
 | 108 |       outputstream << "Yes, is '" << iter->second->getDefaultValue() << "'";
 | 
|---|
 | 109 |     } else {
 | 
|---|
 | 110 |       outputstream << "None";
 | 
|---|
 | 111 |     }
 | 
|---|
 | 112 |     outputstream << "." << std::endl;
 | 
|---|
 | 113 |   }
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |   return outputstream.str();
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
| [af5384] | 118 | void Action::prepare(enum QueryOptions flag)
 | 
|---|
 | 119 | {
 | 
|---|
 | 120 |   // fill with
 | 
|---|
| [4e145c] | 121 |   if (flag == Interactive) {
 | 
|---|
 | 122 |     Dialog* dialog = createDialog();
 | 
|---|
| [031f62] | 123 |     if (dialog->hasQueries()) {
 | 
|---|
| [95f965] | 124 |       if (!dialog->display())
 | 
|---|
 | 125 |         // dialog error or aborted -> throw exception
 | 
|---|
 | 126 |         throw ActionFailureException() << ActionNameString(getName());
 | 
|---|
| [4e145c] | 127 |     }
 | 
|---|
| [031f62] | 128 |     delete(dialog);
 | 
|---|
| [4e145c] | 129 |   }
 | 
|---|
| [af5384] | 130 | }
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | Dialog * Action::createDialog(){
 | 
|---|
| [163110] | 133 |   Dialog *dialog = UIFactory::getInstance().makeDialog(Traits.getName());
 | 
|---|
| [af5384] | 134 |   return fillDialog(dialog);
 | 
|---|
 | 135 | }
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 | void Action::call(){
 | 
|---|
 | 138 |   if(!isActive()){
 | 
|---|
 | 139 |     return;
 | 
|---|
 | 140 |   }
 | 
|---|
| [b5b01e] | 141 |   ActionState::ptr state = Action::failure;
 | 
|---|
| [0d4168] | 142 |   startTimer();
 | 
|---|
 | 143 |   try {
 | 
|---|
| [4e145c] | 144 |     state = performCall();
 | 
|---|
| [0d4168] | 145 |   } catch (ParameterException &e) {
 | 
|---|
 | 146 |     if( const std::string *name=boost::get_error_info<ParameterName>(e) )
 | 
|---|
 | 147 |       ELOG(1, "The following parameter value is not valid: " << *name << ".");
 | 
|---|
 | 148 |     state = Action::failure;
 | 
|---|
 | 149 |   }
 | 
|---|
 | 150 |   endTimer();
 | 
|---|
| [4e145c] | 151 | 
 | 
|---|
| [601ef8] | 152 |   if (shouldUndo() && state != Action::failure) {
 | 
|---|
 | 153 |     if (canUndo()) {
 | 
|---|
| [6367dd] | 154 |       ActionQueue::getInstance().addElement(this,state);
 | 
|---|
| [d56640] | 155 |     }
 | 
|---|
| [33c97e] | 156 | //    else{
 | 
|---|
 | 157 | //      ActionQueue::getInstance().clear();
 | 
|---|
 | 158 | //    }
 | 
|---|
| [d56640] | 159 |   }
 | 
|---|
| [e69c87] | 160 | 
 | 
|---|
 | 161 |   // throw an exception that can be caught in case of failure
 | 
|---|
 | 162 |   if (state == Action::failure)
 | 
|---|
 | 163 |     throw ActionFailureException() << ActionNameString(getName());
 | 
|---|
| [67e2b3] | 164 | }
 | 
|---|
| [b5b01e] | 165 | ActionState::ptr Action::undo(ActionState::ptr _state) {
 | 
|---|
| [67e2b3] | 166 |   // forward to private virtual
 | 
|---|
 | 167 |   return performUndo(_state);
 | 
|---|
 | 168 | }
 | 
|---|
| [b5b01e] | 169 | ActionState::ptr Action::redo(ActionState::ptr _state) {
 | 
|---|
| [67e2b3] | 170 |   // forward to private virtual
 | 
|---|
 | 171 |   return performRedo(_state);
 | 
|---|
 | 172 | }
 | 
|---|
| [f9352d] | 173 | 
 | 
|---|
| [975b83] | 174 | void Action::insertAction(Action *_action, enum Action::QueryOptions state)
 | 
|---|
 | 175 | {
 | 
|---|
 | 176 |   ActionQueue::getInstance().insertAction(_action, state);
 | 
|---|
 | 177 | }
 | 
|---|
| [f9352d] | 178 | 
 | 
|---|
| [94232b] | 179 | bool Action::isActive() const {
 | 
|---|
| [f9352d] | 180 |   return true;
 | 
|---|
 | 181 | }
 | 
|---|
| [df32ee] | 182 | 
 | 
|---|
| [26b4eb4] | 183 | void Action::pushStatus(const std::string& _msg)
 | 
|---|
 | 184 | {
 | 
|---|
 | 185 |   ActionQueue::getInstance().pushStatus(_msg);
 | 
|---|
 | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|