| 1 | /*
 | 
|---|
| 2 |  * ChangeNameAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jan 15, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Actions/MoleculeAction/ChangeNameAction.hpp"
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include <iostream>
 | 
|---|
| 13 | #include <string>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | using namespace std;
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 18 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 19 | #include "Actions/MapOfActions.hpp"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "atom.hpp"
 | 
|---|
| 22 | #include "molecule.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | /****** MoleculeChangeNameAction *****/
 | 
|---|
| 25 | 
 | 
|---|
| 26 | // memento to remember the state when undoing
 | 
|---|
| 27 | 
 | 
|---|
| 28 | class MoleculeChangeNameState : public ActionState {
 | 
|---|
| 29 | public:
 | 
|---|
| 30 |   MoleculeChangeNameState(molecule* _mol,std::string _lastName) :
 | 
|---|
| 31 |     mol(_mol),
 | 
|---|
| 32 |     lastName(_lastName)
 | 
|---|
| 33 |   {}
 | 
|---|
| 34 |   molecule* mol;
 | 
|---|
| 35 |   std::string lastName;
 | 
|---|
| 36 | };
 | 
|---|
| 37 | 
 | 
|---|
| 38 | const char MoleculeChangeNameAction::NAME[] = "change-molname";
 | 
|---|
| 39 | 
 | 
|---|
| 40 | MoleculeChangeNameAction::MoleculeChangeNameAction() :
 | 
|---|
| 41 |   Action(NAME)
 | 
|---|
| 42 | {}
 | 
|---|
| 43 | 
 | 
|---|
| 44 | MoleculeChangeNameAction::~MoleculeChangeNameAction()
 | 
|---|
| 45 | {}
 | 
|---|
| 46 | 
 | 
|---|
| 47 | Action::state_ptr MoleculeChangeNameAction::performCall() {
 | 
|---|
| 48 |   string filename;
 | 
|---|
| 49 |   molecule *mol = NULL;
 | 
|---|
| 50 |   Dialog *dialog = UIFactory::getInstance().makeDialog();
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   dialog->queryMolecule(NAME, &mol, MapOfActions::getInstance().getDescription(NAME));
 | 
|---|
| 53 |   dialog->queryString("Enter name: ",&filename);
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   if(dialog->display()) {
 | 
|---|
| 56 |     string oldName = mol->getName();
 | 
|---|
| 57 |     mol->setName(filename);
 | 
|---|
| 58 |     delete dialog;
 | 
|---|
| 59 |     return Action::state_ptr(new MoleculeChangeNameState(mol,oldName));
 | 
|---|
| 60 |   }
 | 
|---|
| 61 |   delete dialog;
 | 
|---|
| 62 |   return Action::failure;
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | Action::state_ptr MoleculeChangeNameAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 66 |   MoleculeChangeNameState *state = assert_cast<MoleculeChangeNameState*>(_state.get());
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   string newName = state->mol->getName();
 | 
|---|
| 69 |   state->mol->setName(state->lastName);
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   return Action::state_ptr(new MoleculeChangeNameState(state->mol,newName));
 | 
|---|
| 72 | }
 | 
|---|
| 73 | 
 | 
|---|
| 74 | Action::state_ptr MoleculeChangeNameAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 75 |   // Undo and redo have to do the same for this action
 | 
|---|
| 76 |   return performUndo(_state);
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | bool MoleculeChangeNameAction::canUndo() {
 | 
|---|
| 80 |   return true;
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | bool MoleculeChangeNameAction::shouldUndo() {
 | 
|---|
| 84 |   return true;
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | const string MoleculeChangeNameAction::getName() {
 | 
|---|
| 88 |   return NAME;
 | 
|---|
| 89 | }
 | 
|---|