| 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 | WorldSetDefaultNameState *UndoState = new WorldSetDefaultNameState(defaultname);
|
|---|
| 65 | ValueStorage::getInstance().queryCurrentValue(NAME, defaultname);
|
|---|
| 66 |
|
|---|
| 67 | World::getInstance().setDefaultName(defaultname);
|
|---|
| 68 | DoLog(0) && (Log() << Verbose(0) << "Default name of new molecules set to " << World::getInstance().getDefaultName() << "." << endl);
|
|---|
| 69 | return Action::state_ptr(UndoState);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | Action::state_ptr WorldSetDefaultNameAction::performUndo(Action::state_ptr _state) {
|
|---|
| 73 | WorldSetDefaultNameState *state = assert_cast<WorldSetDefaultNameState*>(_state.get());
|
|---|
| 74 |
|
|---|
| 75 | string newName = World::getInstance().getDefaultName();
|
|---|
| 76 | World::getInstance().setDefaultName(state->lastName);
|
|---|
| 77 | DoLog(0) && (Log() << Verbose(0) << "Default name of new molecules set to " << World::getInstance().getDefaultName() << "." << endl);
|
|---|
| 78 |
|
|---|
| 79 | return Action::state_ptr(new WorldSetDefaultNameState(newName));
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | Action::state_ptr WorldSetDefaultNameAction::performRedo(Action::state_ptr _state){
|
|---|
| 83 | return performUndo(_state);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | bool WorldSetDefaultNameAction::canUndo() {
|
|---|
| 87 | return true;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | bool WorldSetDefaultNameAction::shouldUndo() {
|
|---|
| 91 | return true;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | const string WorldSetDefaultNameAction::getName() {
|
|---|
| 95 | return NAME;
|
|---|
| 96 | }
|
|---|