| 1 | /*
 | 
|---|
| 2 |  * SetDefaultNameAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 8, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Actions/WorldAction/SetDefaultNameAction.hpp"
 | 
|---|
| 11 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 12 | #include "Helpers/Log.hpp"
 | 
|---|
| 13 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 14 | #include "World.hpp"
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <iostream>
 | 
|---|
| 17 | #include <string>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | using namespace std;
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 22 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 23 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | 
 | 
|---|
| 26 | // memento to remember the state when undoing
 | 
|---|
| 27 | 
 | 
|---|
| 28 | class WorldSetDefaultNameState : public ActionState {
 | 
|---|
| 29 | public:
 | 
|---|
| 30 |   WorldSetDefaultNameState(std::string _lastName) :
 | 
|---|
| 31 |     lastName(_lastName)
 | 
|---|
| 32 |   {}
 | 
|---|
| 33 |   std::string lastName;
 | 
|---|
| 34 | };
 | 
|---|
| 35 | 
 | 
|---|
| 36 | const char WorldSetDefaultNameAction::NAME[] = "default-molname";
 | 
|---|
| 37 | 
 | 
|---|
| 38 | WorldSetDefaultNameAction::WorldSetDefaultNameAction() :
 | 
|---|
| 39 |   Action(NAME)
 | 
|---|
| 40 | {}
 | 
|---|
| 41 | 
 | 
|---|
| 42 | WorldSetDefaultNameAction::~WorldSetDefaultNameAction()
 | 
|---|
| 43 | {}
 | 
|---|
| 44 | 
 | 
|---|
| 45 | void WorldSetDefaultName(std::string &defaultname) {
 | 
|---|
| 46 |   ValueStorage::getInstance().setCurrentValue(WorldSetDefaultNameAction::NAME, defaultname);
 | 
|---|
| 47 |   ActionRegistry::getInstance().getActionByName(WorldSetDefaultNameAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 48 | };
 | 
|---|
| 49 | 
 | 
|---|
| 50 | Dialog* WorldSetDefaultNameAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 51 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   string defaultname = World::getInstance().getDefaultName();
 | 
|---|
| 54 |   ValueStorage::getInstance().setCurrentValue(NAME, defaultname);
 | 
|---|
| 55 |   dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   return dialog;
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | Action::state_ptr WorldSetDefaultNameAction::performCall() {
 | 
|---|
| 61 |   string defaultname;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   defaultname = World::getInstance().getDefaultName();
 | 
|---|
| 64 |   ValueStorage::getInstance().queryCurrentValue(NAME, defaultname);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   World::getInstance().setDefaultName(defaultname);
 | 
|---|
| 67 |   DoLog(0) && (Log() << Verbose(0) << "Default name of new molecules set to " << World::getInstance().getDefaultName() << "." << endl);
 | 
|---|
| 68 |   return Action::success;
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | Action::state_ptr WorldSetDefaultNameAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 72 |   WorldSetDefaultNameState *state = assert_cast<WorldSetDefaultNameState*>(_state.get());
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   string newName = World::getInstance().getDefaultName();
 | 
|---|
| 75 |   World::getInstance().setDefaultName(state->lastName);
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   return Action::state_ptr(new WorldSetDefaultNameState(newName));
 | 
|---|
| 78 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | Action::state_ptr WorldSetDefaultNameAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 81 |   return performUndo(_state);
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | bool WorldSetDefaultNameAction::canUndo() {
 | 
|---|
| 85 |   return true;
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | bool WorldSetDefaultNameAction::shouldUndo() {
 | 
|---|
| 89 |   return true;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | const string WorldSetDefaultNameAction::getName() {
 | 
|---|
| 93 |   return NAME;
 | 
|---|
| 94 | }
 | 
|---|