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