| 1 | /*
 | 
|---|
| 2 |  * NotAtomByIdAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 12, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Actions/SelectionAction/NotAtomByIdAction.hpp"
 | 
|---|
| 11 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 12 | #include "atom.hpp"
 | 
|---|
| 13 | #include "Helpers/Log.hpp"
 | 
|---|
| 14 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 15 | #include "World.hpp"
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <iostream>
 | 
|---|
| 18 | #include <string>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | using namespace std;
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 23 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 24 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | 
 | 
|---|
| 27 | // memento to remember the state when undoing
 | 
|---|
| 28 | 
 | 
|---|
| 29 | class SelectionNotAtomByIdState : public ActionState {
 | 
|---|
| 30 | public:
 | 
|---|
| 31 |   SelectionNotAtomByIdState(atom* _walker) :
 | 
|---|
| 32 |     walker(_walker)
 | 
|---|
| 33 |   {}
 | 
|---|
| 34 |   atom* walker;
 | 
|---|
| 35 | };
 | 
|---|
| 36 | 
 | 
|---|
| 37 | const char SelectionNotAtomByIdAction::NAME[] = "unselect-atom-by-id";
 | 
|---|
| 38 | 
 | 
|---|
| 39 | SelectionNotAtomByIdAction::SelectionNotAtomByIdAction() :
 | 
|---|
| 40 |   Action(NAME)
 | 
|---|
| 41 | {}
 | 
|---|
| 42 | 
 | 
|---|
| 43 | SelectionNotAtomByIdAction::~SelectionNotAtomByIdAction()
 | 
|---|
| 44 | {}
 | 
|---|
| 45 | 
 | 
|---|
| 46 | void SelectionNotAtomById(atom *_atom) {
 | 
|---|
| 47 |   ValueStorage::getInstance().setCurrentValue(SelectionNotAtomByIdAction::NAME, _atom);
 | 
|---|
| 48 |   ActionRegistry::getInstance().getActionByName(SelectionNotAtomByIdAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 49 | };
 | 
|---|
| 50 | 
 | 
|---|
| 51 | Dialog* SelectionNotAtomByIdAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 52 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   dialog->queryAtom(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   return dialog;
 | 
|---|
| 57 | }
 | 
|---|
| 58 | 
 | 
|---|
| 59 | Action::state_ptr SelectionNotAtomByIdAction::performCall() {
 | 
|---|
| 60 |   atom *Walker = NULL;
 | 
|---|
| 61 | 
 | 
|---|
| 62 |   ValueStorage::getInstance().queryCurrentValue(NAME, Walker);
 | 
|---|
| 63 | 
 | 
|---|
| 64 |   if (World::getInstance().isSelected(Walker)) {
 | 
|---|
| 65 |     DoLog(1) && (Log() << Verbose(1) << "Unselecting atom " << *Walker << endl);
 | 
|---|
| 66 |     World::getInstance().unselectAtom(Walker);
 | 
|---|
| 67 |     return Action::state_ptr(new SelectionNotAtomByIdState(Walker));
 | 
|---|
| 68 |   } else {
 | 
|---|
| 69 |     return Action::success;
 | 
|---|
| 70 |   }
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | Action::state_ptr SelectionNotAtomByIdAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 74 |   SelectionNotAtomByIdState *state = assert_cast<SelectionNotAtomByIdState*>(_state.get());
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   if (state->walker != NULL)
 | 
|---|
| 77 |     World::getInstance().selectAtom(state->walker);
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   return Action::state_ptr(new SelectionNotAtomByIdState(state->walker));
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | Action::state_ptr SelectionNotAtomByIdAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 83 |   SelectionNotAtomByIdState *state = assert_cast<SelectionNotAtomByIdState*>(_state.get());
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   if (state->walker != NULL)
 | 
|---|
| 86 |     World::getInstance().unselectAtom(state->walker);
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   return Action::state_ptr(new SelectionNotAtomByIdState(state->walker));
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | bool SelectionNotAtomByIdAction::canUndo() {
 | 
|---|
| 92 |   return true;
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | bool SelectionNotAtomByIdAction::shouldUndo() {
 | 
|---|
| 96 |   return true;
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | const string SelectionNotAtomByIdAction::getName() {
 | 
|---|
| 100 |   return NAME;
 | 
|---|
| 101 | }
 | 
|---|