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