| [65b6e0] | 1 | /* | 
|---|
| [56f73b] | 2 | * Action.hpp | 
|---|
| [65b6e0] | 3 | * | 
|---|
|  | 4 | *  Created on: Dec 8, 2009 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [56f73b] | 8 | #ifndef ACTION_HPP_ | 
|---|
|  | 9 | #define ACTION_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
| [65b6e0] | 15 |  | 
|---|
| [cc04b7] | 16 | #include <string> | 
|---|
| [5b0b98] | 17 | #include <boost/shared_ptr.hpp> | 
|---|
| [cc04b7] | 18 |  | 
|---|
| [e4afb4] | 19 | /** Used in .def files in paramdefaults define to set that no default value exists. | 
|---|
|  | 20 | * We define NODEFAULT here, as it is used in .def files and needs to be present | 
|---|
|  | 21 | * before these are included. | 
|---|
|  | 22 | */ | 
|---|
| [d1115d] | 23 | #define NODEFAULT "" | 
|---|
| [e4afb4] | 24 |  | 
|---|
| [67e2b3] | 25 | // forward declaration | 
|---|
|  | 26 |  | 
|---|
| [ce7fdc] | 27 | namespace MoleCuilder { | 
|---|
|  | 28 | class ActionState; | 
|---|
|  | 29 | class ActionSequence; | 
|---|
|  | 30 | } | 
|---|
| [ba7418] | 31 | class Dialog; | 
|---|
| [67e2b3] | 32 |  | 
|---|
| [3139b2] | 33 | #include "Actions/ActionTrait.hpp" | 
|---|
| [df32ee] | 34 |  | 
|---|
| [2efa90] | 35 |  | 
|---|
| [ce7fdc] | 36 | namespace MoleCuilder { | 
|---|
| [df32ee] | 37 |  | 
|---|
| [ef81b0] | 38 | /** | 
|---|
|  | 39 | * Base class for all actions. | 
|---|
|  | 40 | * | 
|---|
|  | 41 | * Actions describe something that has to be done. | 
|---|
|  | 42 | * Actions can be passed around, stored, performed and undone (Command-Pattern). | 
|---|
|  | 43 | */ | 
|---|
| [65b6e0] | 44 | class Action | 
|---|
|  | 45 | { | 
|---|
| [67e2b3] | 46 | friend class ActionSequence; | 
|---|
| [2efa90] | 47 | friend class ActionHistory; | 
|---|
| [1fa107] | 48 | public: | 
|---|
| [5b0b98] | 49 |  | 
|---|
| [4e145c] | 50 | enum QueryOptions {Interactive, NonInteractive}; | 
|---|
|  | 51 |  | 
|---|
| [8a34392] | 52 | /** | 
|---|
|  | 53 | * This type is used to store pointers to ActionStates while allowing multiple ownership | 
|---|
|  | 54 | */ | 
|---|
| [5b0b98] | 55 | typedef boost::shared_ptr<ActionState> state_ptr; | 
|---|
|  | 56 |  | 
|---|
| [8a34392] | 57 | /** | 
|---|
|  | 58 | * Standard constructor of Action Base class | 
|---|
|  | 59 | * | 
|---|
|  | 60 | * All Actions need to have a name. The second flag indicates, whether the action should | 
|---|
|  | 61 | * be registered with the ActionRegistry. If the Action is registered the name of the | 
|---|
|  | 62 | * Action needs to be unique for all Actions that are registered. | 
|---|
| [e4afb4] | 63 | * | 
|---|
|  | 64 | * \note NO reference for \a _Traits as we do have to copy it, otherwise _Traits would have | 
|---|
|  | 65 | * to be present throughout the program's run. | 
|---|
|  | 66 | * | 
|---|
|  | 67 | * \param Traits information class to this action | 
|---|
|  | 68 | * \param _doRegister whether to register with ActionRegistry | 
|---|
| [8a34392] | 69 | */ | 
|---|
| [3139b2] | 70 | Action(const ActionTrait &_Traits, bool _doRegister=true); | 
|---|
| [65b6e0] | 71 | virtual ~Action(); | 
|---|
|  | 72 |  | 
|---|
| [8a34392] | 73 | /** | 
|---|
|  | 74 | * This method is used to call an action. The basic operations for the Action | 
|---|
|  | 75 | * are carried out and if necessary/possible the Action is added to the History | 
|---|
|  | 76 | * to allow for undo of this action. | 
|---|
|  | 77 | * | 
|---|
|  | 78 | * If the call needs to undone you have to use the History, to avoid destroying | 
|---|
|  | 79 | * invariants used by the History. | 
|---|
| [4e145c] | 80 | * | 
|---|
|  | 81 | * Note that this call can be Interactive (i.e. a dialog will ask the user for | 
|---|
|  | 82 | * necessary information) and NonInteractive (i.e. the information will have to | 
|---|
|  | 83 | * be present already within the ValueStorage class or else a MissingArgumentException | 
|---|
|  | 84 | * is thrown) | 
|---|
| [8a34392] | 85 | */ | 
|---|
| [6bf52f] | 86 | void call(enum QueryOptions state = Interactive); | 
|---|
| [67e2b3] | 87 |  | 
|---|
| [8a34392] | 88 | /** | 
|---|
|  | 89 | * This method provides a flag that indicates if an undo mechanism is implemented | 
|---|
|  | 90 | * for this Action. If this is true, and this action was called last, you can | 
|---|
|  | 91 | * use the History to undo this action. | 
|---|
|  | 92 | */ | 
|---|
| [65b6e0] | 93 | virtual bool canUndo()=0; | 
|---|
| [8a34392] | 94 |  | 
|---|
|  | 95 | /** | 
|---|
|  | 96 | * This method provides a flag, that indicates if the Action changes the state of | 
|---|
|  | 97 | * the application in a way that needs to be undone for the History to work. | 
|---|
|  | 98 | * | 
|---|
|  | 99 | * If this is false the Action will not be added to the History upon calling. However | 
|---|
|  | 100 | * Actions called before this one will still be available for undo. | 
|---|
|  | 101 | */ | 
|---|
| [67e2b3] | 102 | virtual bool shouldUndo()=0; | 
|---|
| [65b6e0] | 103 |  | 
|---|
| [8a34392] | 104 | /** | 
|---|
|  | 105 | * Indicates whether the Action can do it's work at the moment. If this | 
|---|
|  | 106 | * is false calling the action will result in a no-op. | 
|---|
|  | 107 | */ | 
|---|
| [f9352d] | 108 | virtual bool isActive(); | 
|---|
|  | 109 |  | 
|---|
| [8a34392] | 110 | /** | 
|---|
|  | 111 | * Returns the name of the Action. | 
|---|
|  | 112 | */ | 
|---|
| [13799e] | 113 | const std::string getName() const; | 
|---|
| [cc04b7] | 114 |  | 
|---|
| [e4b2f6] | 115 | /** | 
|---|
|  | 116 | * returns a detailed help message. | 
|---|
|  | 117 | */ | 
|---|
|  | 118 | const std::string help() const; | 
|---|
|  | 119 |  | 
|---|
| [e4afb4] | 120 | /** | 
|---|
|  | 121 | * Traits resemble all necessary information that "surrounds" an action, such as | 
|---|
|  | 122 | * its name (for ActionRegistry and as ref from string to instance and vice versa), | 
|---|
|  | 123 | * which menu, which position, what parameters, their types, if it is itself a | 
|---|
|  | 124 | * parameter and so on ... | 
|---|
|  | 125 | * | 
|---|
|  | 126 | * Note that is important that we do not use a reference here. We want to copy the | 
|---|
|  | 127 | * information in the Action's constructor and have it contained herein. Hence, we | 
|---|
| [3139b2] | 128 | * also have our own copy constructor for ActionTrait. Information should be | 
|---|
| [e4afb4] | 129 | * encapsulated in the Action, no more references to the outside than absolutely | 
|---|
|  | 130 | * necessary. | 
|---|
|  | 131 | */ | 
|---|
| [3139b2] | 132 | const ActionTrait Traits; | 
|---|
| [df32ee] | 133 |  | 
|---|
| [dfef3f] | 134 | protected: | 
|---|
| [41449c] | 135 | /** Removes the static entities Action::success and Action::failure. | 
|---|
|  | 136 | * This is only to be called on the program's exit, i.e. in cleanUp(), | 
|---|
|  | 137 | * as these static entities are used throughout all Actions. | 
|---|
|  | 138 | */ | 
|---|
|  | 139 | static void removeStaticStateEntities(); | 
|---|
|  | 140 |  | 
|---|
| [dfef3f] | 141 | /** Creates the static entities Action::success and Action::failure. | 
|---|
|  | 142 | * This is only to be called by ActionHistory. | 
|---|
|  | 143 | */ | 
|---|
|  | 144 | static void createStaticStateEntities(); | 
|---|
|  | 145 |  | 
|---|
| [8a34392] | 146 | /** | 
|---|
|  | 147 | * This method is called by the History, when an undo is performed. It is | 
|---|
|  | 148 | * provided with the corresponding state produced by the performCall or | 
|---|
|  | 149 | * performRedo method and needs to provide a state that can be used for redo. | 
|---|
|  | 150 | */ | 
|---|
| [2efa90] | 151 | state_ptr undo(state_ptr); | 
|---|
| [8a34392] | 152 |  | 
|---|
|  | 153 | /** | 
|---|
|  | 154 | * This method is called by the Histor, when a redo is performed. It is | 
|---|
|  | 155 | * provided with the corresponding state produced by the undo method and | 
|---|
|  | 156 | * needs to produce a State that can then be used for another undo. | 
|---|
|  | 157 | */ | 
|---|
| [2efa90] | 158 | state_ptr redo(state_ptr); | 
|---|
|  | 159 |  | 
|---|
| [8a34392] | 160 | /** | 
|---|
|  | 161 | * This special state can be used to indicate that the Action was successfull | 
|---|
|  | 162 | * without providing a special state. Use this if your Action does not need | 
|---|
|  | 163 | * a speciallized state. | 
|---|
|  | 164 | */ | 
|---|
| [5b0b98] | 165 | static state_ptr success; | 
|---|
| [8a34392] | 166 |  | 
|---|
|  | 167 | /** | 
|---|
|  | 168 | * This special state can be returned, to indicate that the action could not do it's | 
|---|
|  | 169 | * work, was abborted by the user etc. If you return this state make sure to transactionize | 
|---|
|  | 170 | * your Actions and unroll the complete transaction before this is returned. | 
|---|
|  | 171 | */ | 
|---|
| [5b0b98] | 172 | static state_ptr failure; | 
|---|
| [67e2b3] | 173 |  | 
|---|
| [8a34392] | 174 | /** | 
|---|
| [ba7418] | 175 | * This creates the dialog requesting the information needed for this action from the user | 
|---|
|  | 176 | * via means of the user interface. | 
|---|
|  | 177 | */ | 
|---|
| [047878] | 178 | Dialog * createDialog(); | 
|---|
|  | 179 |  | 
|---|
| [862b6a] | 180 | /** Virtual function that starts the timer. | 
|---|
|  | 181 | * | 
|---|
|  | 182 | */ | 
|---|
|  | 183 | virtual void startTimer() const {}; | 
|---|
|  | 184 |  | 
|---|
|  | 185 | /** Virtual function that ends the timer. | 
|---|
|  | 186 | * | 
|---|
|  | 187 | */ | 
|---|
|  | 188 | virtual void endTimer() const {}; | 
|---|
|  | 189 |  | 
|---|
| [047878] | 190 | private: | 
|---|
|  | 191 |  | 
|---|
| [0b2ce9] | 192 | /** | 
|---|
|  | 193 | * This is called internally before the Action::performCall(). It initializes the | 
|---|
|  | 194 | * necessary ActionParameters by retrieving the values from ValueStorage. | 
|---|
|  | 195 | */ | 
|---|
|  | 196 | virtual void getParametersfromValueStorage()=0; | 
|---|
|  | 197 |  | 
|---|
|  | 198 | /** | 
|---|
|  | 199 | * This is called internally before the action is processed. This adds necessary queries | 
|---|
|  | 200 | * to a given dialog to obtain parameters for the user for processing the action accordingly. | 
|---|
|  | 201 | * The dialog will be given to the user before Action::performCall() is initiated, values | 
|---|
|  | 202 | * are transfered via ValueStorage. | 
|---|
|  | 203 | */ | 
|---|
| [047878] | 204 | virtual Dialog * fillDialog(Dialog*)=0; | 
|---|
| [ba7418] | 205 |  | 
|---|
|  | 206 | /** | 
|---|
|  | 207 | * This is called internally when the call is being done. Implement this method to do the actual | 
|---|
| [8a34392] | 208 | * work of the Action. Implement this in your Derived classes. Needs to return a state that can be | 
|---|
|  | 209 | * used to undo the action. | 
|---|
|  | 210 | */ | 
|---|
| [5b0b98] | 211 | virtual state_ptr performCall()=0; | 
|---|
| [8a34392] | 212 |  | 
|---|
|  | 213 | /** | 
|---|
|  | 214 | * This is called internally when the undo process is chosen. This Method should use the state | 
|---|
|  | 215 | * produced by the performCall method to return the state of the application to the state | 
|---|
|  | 216 | * it had before the Action. | 
|---|
|  | 217 | */ | 
|---|
| [5b0b98] | 218 | virtual state_ptr performUndo(state_ptr)=0; | 
|---|
| [8a34392] | 219 |  | 
|---|
|  | 220 | /** | 
|---|
|  | 221 | * This is called internally when the redo process is chosen. This method shoudl use the state | 
|---|
|  | 222 | * produced by the performUndo method to return the application to the state it should have after | 
|---|
|  | 223 | * the action. | 
|---|
|  | 224 | * | 
|---|
|  | 225 | * Often this method can be implement to re-use the performCall method. However if user interaction | 
|---|
|  | 226 | * or further parameters are needed, those should be taken from the state and not query the user | 
|---|
|  | 227 | * again. | 
|---|
|  | 228 | */ | 
|---|
| [5b0b98] | 229 | virtual state_ptr performRedo(state_ptr)=0; | 
|---|
| [65b6e0] | 230 | }; | 
|---|
|  | 231 |  | 
|---|
| [67e2b3] | 232 | /** | 
|---|
|  | 233 | * This class can be used by actions to save the state. | 
|---|
|  | 234 | * | 
|---|
|  | 235 | * It is implementing a memento pattern. The base class is completely empty, | 
|---|
|  | 236 | * since no general state internals can be given. The Action performing | 
|---|
|  | 237 | * the Undo should downcast to the apropriate type. | 
|---|
|  | 238 | */ | 
|---|
|  | 239 | class ActionState{ | 
|---|
|  | 240 | public: | 
|---|
|  | 241 | ActionState(){} | 
|---|
|  | 242 | virtual ~ActionState(){} | 
|---|
|  | 243 | }; | 
|---|
|  | 244 |  | 
|---|
| [0b2ce9] | 245 | /** | 
|---|
|  | 246 | * This class can be used by actions to contain parameters. | 
|---|
|  | 247 | * | 
|---|
|  | 248 | * The base class is completely empty, since no general parameters can be given. The | 
|---|
|  | 249 | * Action performing the function should construct its own parameter class derived | 
|---|
|  | 250 | * from it. | 
|---|
|  | 251 | */ | 
|---|
|  | 252 | class ActionParameters{ | 
|---|
|  | 253 | public: | 
|---|
|  | 254 | ActionParameters(){} | 
|---|
|  | 255 | virtual ~ActionParameters(){} | 
|---|
|  | 256 | }; | 
|---|
|  | 257 |  | 
|---|
| [ce7fdc] | 258 | } | 
|---|
|  | 259 |  | 
|---|
| [56f73b] | 260 | #endif /* ACTION_HPP_ */ | 
|---|