| [97ebf8] | 1 | /*
 | 
|---|
 | 2 |  * SetDefaultNameAction.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: May 8, 2010
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [112b09] | 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 9 | 
 | 
|---|
| [97ebf8] | 10 | #include "Actions/WorldAction/SetDefaultNameAction.hpp"
 | 
|---|
| [0430e3] | 11 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [952f38] | 12 | #include "Helpers/Log.hpp"
 | 
|---|
 | 13 | #include "Helpers/Verbose.hpp"
 | 
|---|
| [97ebf8] | 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"
 | 
|---|
| [861874] | 23 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| [792597] | 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 | };
 | 
|---|
| [97ebf8] | 35 | 
 | 
|---|
 | 36 | const char WorldSetDefaultNameAction::NAME[] = "default-molname";
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | WorldSetDefaultNameAction::WorldSetDefaultNameAction() :
 | 
|---|
 | 39 |   Action(NAME)
 | 
|---|
 | 40 | {}
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | WorldSetDefaultNameAction::~WorldSetDefaultNameAction()
 | 
|---|
 | 43 | {}
 | 
|---|
 | 44 | 
 | 
|---|
| [a8f6ae] | 45 | void WorldSetDefaultName(std::string &defaultname) {
 | 
|---|
 | 46 |   ValueStorage::getInstance().setCurrentValue(WorldSetDefaultNameAction::NAME, defaultname);
 | 
|---|
 | 47 |   ActionRegistry::getInstance().getActionByName(WorldSetDefaultNameAction::NAME)->call(Action::NonInteractive);
 | 
|---|
 | 48 | };
 | 
|---|
 | 49 | 
 | 
|---|
| [047878] | 50 | Dialog* WorldSetDefaultNameAction::fillDialog(Dialog *dialog) {
 | 
|---|
 | 51 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| [792597] | 52 | 
 | 
|---|
 | 53 |   string defaultname = World::getInstance().getDefaultName();
 | 
|---|
 | 54 |   ValueStorage::getInstance().setCurrentValue(NAME, defaultname);
 | 
|---|
| [c89fb4] | 55 |   dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| [792597] | 56 | 
 | 
|---|
 | 57 |   return dialog;
 | 
|---|
 | 58 | }
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 | Action::state_ptr WorldSetDefaultNameAction::performCall() {
 | 
|---|
| [97ebf8] | 61 |   string defaultname;
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 |   defaultname = World::getInstance().getDefaultName();
 | 
|---|
| [792597] | 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;
 | 
|---|
| [97ebf8] | 69 | }
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | Action::state_ptr WorldSetDefaultNameAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| [792597] | 72 |   WorldSetDefaultNameState *state = assert_cast<WorldSetDefaultNameState*>(_state.get());
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |   string newName = World::getInstance().getDefaultName();
 | 
|---|
 | 75 |   World::getInstance().setDefaultName(state->lastName);
 | 
|---|
| [97ebf8] | 76 | 
 | 
|---|
| [792597] | 77 |   return Action::state_ptr(new WorldSetDefaultNameState(newName));
 | 
|---|
| [97ebf8] | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | Action::state_ptr WorldSetDefaultNameAction::performRedo(Action::state_ptr _state){
 | 
|---|
| [680470] | 81 |   return performUndo(_state);
 | 
|---|
| [97ebf8] | 82 | }
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | bool WorldSetDefaultNameAction::canUndo() {
 | 
|---|
| [792597] | 85 |   return true;
 | 
|---|
| [97ebf8] | 86 | }
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 | bool WorldSetDefaultNameAction::shouldUndo() {
 | 
|---|
| [792597] | 89 |   return true;
 | 
|---|
| [97ebf8] | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 | const string WorldSetDefaultNameAction::getName() {
 | 
|---|
 | 93 |   return NAME;
 | 
|---|
 | 94 | }
 | 
|---|