| [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 | 
 | 
|---|
| [b2c302] | 38 | /** Actions are Command patterns to allow for undoing and redoing.
 | 
|---|
 | 39 |  *
 | 
|---|
 | 40 |  * Each specific Action derives from this class to implement a certain functionality.
 | 
|---|
| [ef81b0] | 41 |  *
 | 
|---|
 | 42 |  * Actions describe something that has to be done.
 | 
|---|
| [b2c302] | 43 |  * Actions can be passed around, stored, performed and undone (Command-Pattern:
 | 
|---|
 | 44 |  * http://en.wikipedia.org/wiki/Command_pattern).
 | 
|---|
 | 45 |  *
 | 
|---|
 | 46 |  * Unique to each Action is its ActionTrait, i.e. the options it requires
 | 
|---|
 | 47 |  * to perform a certain function. E.g. in order to execute a "add atom" Action
 | 
|---|
 | 48 |  * we need to know a position and an element. These options have certain
 | 
|---|
 | 49 |  * properties, see \ref OptionTrait and \ref ActionTrait wherein these are stored.
 | 
|---|
 | 50 |  * Essentially, each option is stored as an \ref OptionTrait instance and
 | 
|---|
 | 51 |  * contains the token, default value, a description, the type, ...
 | 
|---|
 | 52 |  *
 | 
|---|
 | 53 |  * ActionTrait itself is also an OptionTrait because the command token may actually
 | 
|---|
 | 54 |  * coincide with an option-token. E.g. this allows "...--add-atom 3" to mean
 | 
|---|
 | 55 |  * both execute the action under token "add-atom" and that the option "add-atom"
 | 
|---|
 | 56 |  * (the new atom's \ref element number) should contain 3.
 | 
|---|
 | 57 |  *
 | 
|---|
 | 58 |  * \ref ActionTrait contains a map of all associated options. With this in the cstor
 | 
|---|
 | 59 |  * we register not only the Action with the \ref ActionRegistry but also each of
 | 
|---|
 | 60 |  * its \link options OptionTrait \endlink with the \ref OptionRegistry.
 | 
|---|
 | 61 |  *
 | 
|---|
 | 62 |  * The token of the option is unique, but two Action's may share the same token if:
 | 
|---|
 | 63 |  * -# they have the same default value
 | 
|---|
 | 64 |  * -# they have the same type
 | 
|---|
 | 65 |  *
 | 
|---|
 | 66 |  * This requirement is easy because if you need to store some option of another
 | 
|---|
 | 67 |  * type, simply think of a new suitable name for it.
 | 
|---|
 | 68 |  *
 | 
|---|
 | 69 |  * The actual value, i.e. "3" in the "add-atom" example, is taken from the
 | 
|---|
 | 70 |  * ValueStorage, see \ref Dialog for how this is possible.
 | 
|---|
 | 71 |  *
 | 
|---|
 | 72 |  * \note There is a unit test that checks on the consistency of all registered
 | 
|---|
 | 73 |  * options, also in "--enable-debug"-mode assertions will check that an option
 | 
|---|
 | 74 |  * has not been registered before under another type.
 | 
|---|
 | 75 |  *
 | 
|---|
| [ef81b0] | 76 |  */
 | 
|---|
| [65b6e0] | 77 | class Action
 | 
|---|
 | 78 | {
 | 
|---|
| [67e2b3] | 79 | friend class ActionSequence;
 | 
|---|
| [2efa90] | 80 | friend class ActionHistory;
 | 
|---|
| [1fa107] | 81 | public:
 | 
|---|
| [5b0b98] | 82 | 
 | 
|---|
| [4e145c] | 83 |   enum QueryOptions {Interactive, NonInteractive};
 | 
|---|
 | 84 | 
 | 
|---|
| [8a34392] | 85 |   /**
 | 
|---|
 | 86 |    * This type is used to store pointers to ActionStates while allowing multiple ownership
 | 
|---|
 | 87 |    */
 | 
|---|
| [5b0b98] | 88 |   typedef boost::shared_ptr<ActionState> state_ptr;
 | 
|---|
 | 89 | 
 | 
|---|
| [8a34392] | 90 |   /**
 | 
|---|
 | 91 |    * Standard constructor of Action Base class
 | 
|---|
 | 92 |    *
 | 
|---|
 | 93 |    * All Actions need to have a name. The second flag indicates, whether the action should
 | 
|---|
 | 94 |    * be registered with the ActionRegistry. If the Action is registered the name of the
 | 
|---|
 | 95 |    * Action needs to be unique for all Actions that are registered.
 | 
|---|
| [e4afb4] | 96 |    *
 | 
|---|
 | 97 |    * \note NO reference for \a _Traits as we do have to copy it, otherwise _Traits would have
 | 
|---|
 | 98 |    * to be present throughout the program's run.
 | 
|---|
 | 99 |    *
 | 
|---|
 | 100 |    * \param Traits information class to this action
 | 
|---|
 | 101 |    * \param _doRegister whether to register with ActionRegistry
 | 
|---|
| [8a34392] | 102 |    */
 | 
|---|
| [3139b2] | 103 |   Action(const ActionTrait &_Traits, bool _doRegister=true);
 | 
|---|
| [65b6e0] | 104 |   virtual ~Action();
 | 
|---|
 | 105 | 
 | 
|---|
| [8a34392] | 106 |   /**
 | 
|---|
 | 107 |    * This method is used to call an action. The basic operations for the Action
 | 
|---|
 | 108 |    * are carried out and if necessary/possible the Action is added to the History
 | 
|---|
 | 109 |    * to allow for undo of this action.
 | 
|---|
 | 110 |    *
 | 
|---|
 | 111 |    * If the call needs to undone you have to use the History, to avoid destroying
 | 
|---|
 | 112 |    * invariants used by the History.
 | 
|---|
| [4e145c] | 113 |    *
 | 
|---|
 | 114 |    * Note that this call can be Interactive (i.e. a dialog will ask the user for
 | 
|---|
 | 115 |    * necessary information) and NonInteractive (i.e. the information will have to
 | 
|---|
 | 116 |    * be present already within the ValueStorage class or else a MissingArgumentException
 | 
|---|
 | 117 |    * is thrown)
 | 
|---|
| [8a34392] | 118 |    */
 | 
|---|
| [6bf52f] | 119 |   void call(enum QueryOptions state = Interactive);
 | 
|---|
| [67e2b3] | 120 | 
 | 
|---|
| [8a34392] | 121 |   /**
 | 
|---|
 | 122 |    * This method provides a flag that indicates if an undo mechanism is implemented
 | 
|---|
 | 123 |    * for this Action. If this is true, and this action was called last, you can
 | 
|---|
 | 124 |    * use the History to undo this action.
 | 
|---|
 | 125 |    */
 | 
|---|
| [65b6e0] | 126 |   virtual bool canUndo()=0;
 | 
|---|
| [8a34392] | 127 | 
 | 
|---|
 | 128 |   /**
 | 
|---|
 | 129 |    * This method provides a flag, that indicates if the Action changes the state of
 | 
|---|
 | 130 |    * the application in a way that needs to be undone for the History to work.
 | 
|---|
 | 131 |    *
 | 
|---|
 | 132 |    * If this is false the Action will not be added to the History upon calling. However
 | 
|---|
 | 133 |    * Actions called before this one will still be available for undo.
 | 
|---|
 | 134 |    */
 | 
|---|
| [67e2b3] | 135 |   virtual bool shouldUndo()=0;
 | 
|---|
| [65b6e0] | 136 | 
 | 
|---|
| [8a34392] | 137 |   /**
 | 
|---|
 | 138 |    * Indicates whether the Action can do it's work at the moment. If this
 | 
|---|
 | 139 |    * is false calling the action will result in a no-op.
 | 
|---|
 | 140 |    */
 | 
|---|
| [f9352d] | 141 |   virtual bool isActive();
 | 
|---|
 | 142 | 
 | 
|---|
| [8a34392] | 143 |   /**
 | 
|---|
 | 144 |    * Returns the name of the Action.
 | 
|---|
 | 145 |    */
 | 
|---|
| [13799e] | 146 |   const std::string getName() const;
 | 
|---|
| [cc04b7] | 147 | 
 | 
|---|
| [e4b2f6] | 148 |   /**
 | 
|---|
 | 149 |    * returns a detailed help message.
 | 
|---|
 | 150 |    */
 | 
|---|
 | 151 |   const std::string help() const;
 | 
|---|
 | 152 | 
 | 
|---|
| [e4afb4] | 153 |   /**
 | 
|---|
 | 154 |    * Traits resemble all necessary information that "surrounds" an action, such as
 | 
|---|
 | 155 |    * its name (for ActionRegistry and as ref from string to instance and vice versa),
 | 
|---|
 | 156 |    * which menu, which position, what parameters, their types, if it is itself a
 | 
|---|
 | 157 |    * parameter and so on ...
 | 
|---|
 | 158 |    *
 | 
|---|
 | 159 |    * Note that is important that we do not use a reference here. We want to copy the
 | 
|---|
 | 160 |    * information in the Action's constructor and have it contained herein. Hence, we
 | 
|---|
| [3139b2] | 161 |    * also have our own copy constructor for ActionTrait. Information should be
 | 
|---|
| [e4afb4] | 162 |    * encapsulated in the Action, no more references to the outside than absolutely
 | 
|---|
 | 163 |    * necessary.
 | 
|---|
 | 164 |    */
 | 
|---|
| [3139b2] | 165 |   const ActionTrait Traits;
 | 
|---|
| [df32ee] | 166 | 
 | 
|---|
| [dfef3f] | 167 | protected:
 | 
|---|
| [41449c] | 168 |   /** Removes the static entities Action::success and Action::failure.
 | 
|---|
 | 169 |    * This is only to be called on the program's exit, i.e. in cleanUp(),
 | 
|---|
 | 170 |    * as these static entities are used throughout all Actions.
 | 
|---|
 | 171 |    */
 | 
|---|
 | 172 |   static void removeStaticStateEntities();
 | 
|---|
 | 173 | 
 | 
|---|
| [dfef3f] | 174 |   /** Creates the static entities Action::success and Action::failure.
 | 
|---|
 | 175 |    * This is only to be called by ActionHistory.
 | 
|---|
 | 176 |    */
 | 
|---|
 | 177 |   static void createStaticStateEntities();
 | 
|---|
 | 178 | 
 | 
|---|
| [8a34392] | 179 |   /**
 | 
|---|
 | 180 |    * This method is called by the History, when an undo is performed. It is
 | 
|---|
 | 181 |    * provided with the corresponding state produced by the performCall or
 | 
|---|
 | 182 |    * performRedo method and needs to provide a state that can be used for redo.
 | 
|---|
 | 183 |    */
 | 
|---|
| [2efa90] | 184 |   state_ptr undo(state_ptr);
 | 
|---|
| [8a34392] | 185 | 
 | 
|---|
 | 186 |   /**
 | 
|---|
| [992bd5] | 187 |    * This method is called by the History, when a redo is performed. It is
 | 
|---|
| [8a34392] | 188 |    * provided with the corresponding state produced by the undo method and
 | 
|---|
 | 189 |    * needs to produce a State that can then be used for another undo.
 | 
|---|
 | 190 |    */
 | 
|---|
| [2efa90] | 191 |   state_ptr redo(state_ptr);
 | 
|---|
 | 192 | 
 | 
|---|
| [8a34392] | 193 |   /**
 | 
|---|
| [992bd5] | 194 |    * This special state can be used to indicate that the Action was successful
 | 
|---|
| [8a34392] | 195 |    * without providing a special state. Use this if your Action does not need
 | 
|---|
| [992bd5] | 196 |    * a specialized state.
 | 
|---|
| [8a34392] | 197 |    */
 | 
|---|
| [5b0b98] | 198 |   static state_ptr success;
 | 
|---|
| [8a34392] | 199 | 
 | 
|---|
 | 200 |   /**
 | 
|---|
 | 201 |    * This special state can be returned, to indicate that the action could not do it's
 | 
|---|
| [992bd5] | 202 |    * work, was aborted by the user etc. If you return this state make sure to transactionize
 | 
|---|
| [8a34392] | 203 |    * your Actions and unroll the complete transaction before this is returned.
 | 
|---|
 | 204 |    */
 | 
|---|
| [5b0b98] | 205 |   static state_ptr failure;
 | 
|---|
| [67e2b3] | 206 | 
 | 
|---|
| [8a34392] | 207 |   /**
 | 
|---|
| [ba7418] | 208 |    * This creates the dialog requesting the information needed for this action from the user
 | 
|---|
 | 209 |    * via means of the user interface.
 | 
|---|
 | 210 |    */
 | 
|---|
| [047878] | 211 |   Dialog * createDialog();
 | 
|---|
 | 212 | 
 | 
|---|
| [862b6a] | 213 |   /** Virtual function that starts the timer.
 | 
|---|
 | 214 |    *
 | 
|---|
 | 215 |    */
 | 
|---|
 | 216 |   virtual void startTimer() const {};
 | 
|---|
 | 217 | 
 | 
|---|
 | 218 |   /** Virtual function that ends the timer.
 | 
|---|
 | 219 |    *
 | 
|---|
 | 220 |    */
 | 
|---|
 | 221 |   virtual void endTimer() const {};
 | 
|---|
 | 222 | 
 | 
|---|
| [047878] | 223 | private:
 | 
|---|
 | 224 | 
 | 
|---|
| [0b2ce9] | 225 |   /**
 | 
|---|
 | 226 |    * This is called internally before the Action::performCall(). It initializes the
 | 
|---|
 | 227 |    * necessary ActionParameters by retrieving the values from ValueStorage.
 | 
|---|
 | 228 |    */
 | 
|---|
 | 229 |   virtual void getParametersfromValueStorage()=0;
 | 
|---|
 | 230 | 
 | 
|---|
 | 231 |   /**
 | 
|---|
 | 232 |    * This is called internally before the action is processed. This adds necessary queries
 | 
|---|
 | 233 |    * to a given dialog to obtain parameters for the user for processing the action accordingly.
 | 
|---|
 | 234 |    * The dialog will be given to the user before Action::performCall() is initiated, values
 | 
|---|
 | 235 |    * are transfered via ValueStorage.
 | 
|---|
 | 236 |    */
 | 
|---|
| [047878] | 237 |   virtual Dialog * fillDialog(Dialog*)=0;
 | 
|---|
| [ba7418] | 238 | 
 | 
|---|
 | 239 |   /**
 | 
|---|
 | 240 |    * This is called internally when the call is being done. Implement this method to do the actual
 | 
|---|
| [8a34392] | 241 |    * work of the Action. Implement this in your Derived classes. Needs to return a state that can be
 | 
|---|
 | 242 |    * used to undo the action.
 | 
|---|
 | 243 |    */
 | 
|---|
| [5b0b98] | 244 |   virtual state_ptr performCall()=0;
 | 
|---|
| [8a34392] | 245 | 
 | 
|---|
 | 246 |   /**
 | 
|---|
 | 247 |    * This is called internally when the undo process is chosen. This Method should use the state
 | 
|---|
 | 248 |    * produced by the performCall method to return the state of the application to the state
 | 
|---|
 | 249 |    * it had before the Action.
 | 
|---|
 | 250 |    */
 | 
|---|
| [5b0b98] | 251 |   virtual state_ptr performUndo(state_ptr)=0;
 | 
|---|
| [8a34392] | 252 | 
 | 
|---|
 | 253 |   /**
 | 
|---|
 | 254 |    * This is called internally when the redo process is chosen. This method shoudl use the state
 | 
|---|
 | 255 |    * produced by the performUndo method to return the application to the state it should have after
 | 
|---|
 | 256 |    * the action.
 | 
|---|
 | 257 |    *
 | 
|---|
 | 258 |    * Often this method can be implement to re-use the performCall method. However if user interaction
 | 
|---|
 | 259 |    * or further parameters are needed, those should be taken from the state and not query the user
 | 
|---|
 | 260 |    * again.
 | 
|---|
 | 261 |    */
 | 
|---|
| [5b0b98] | 262 |   virtual state_ptr performRedo(state_ptr)=0;
 | 
|---|
| [65b6e0] | 263 | };
 | 
|---|
 | 264 | 
 | 
|---|
| [67e2b3] | 265 | /**
 | 
|---|
 | 266 |  * This class can be used by actions to save the state.
 | 
|---|
 | 267 |  *
 | 
|---|
 | 268 |  * It is implementing a memento pattern. The base class is completely empty,
 | 
|---|
 | 269 |  * since no general state internals can be given. The Action performing
 | 
|---|
| [992bd5] | 270 |  * the Undo should downcast to the appropriate type.
 | 
|---|
| [67e2b3] | 271 |  */
 | 
|---|
 | 272 | class ActionState{
 | 
|---|
 | 273 | public:
 | 
|---|
 | 274 |   ActionState(){}
 | 
|---|
 | 275 |   virtual ~ActionState(){}
 | 
|---|
 | 276 | };
 | 
|---|
 | 277 | 
 | 
|---|
| [0b2ce9] | 278 | /**
 | 
|---|
 | 279 |  * This class can be used by actions to contain parameters.
 | 
|---|
 | 280 |  *
 | 
|---|
 | 281 |  * The base class is completely empty, since no general parameters can be given. The
 | 
|---|
 | 282 |  * Action performing the function should construct its own parameter class derived
 | 
|---|
 | 283 |  * from it.
 | 
|---|
 | 284 |  */
 | 
|---|
 | 285 | class ActionParameters{
 | 
|---|
 | 286 | public:
 | 
|---|
 | 287 |   ActionParameters(){}
 | 
|---|
 | 288 |   virtual ~ActionParameters(){}
 | 
|---|
 | 289 | };
 | 
|---|
 | 290 | 
 | 
|---|
| [ce7fdc] | 291 | }
 | 
|---|
 | 292 | 
 | 
|---|
| [56f73b] | 293 | #endif /* ACTION_HPP_ */
 | 
|---|