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