This page describes the use of commands and the command manager.
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.
A command is a component instance that separates the logic from the visual commanders. Commanders or visualizers are all the buttons, menu items, link labels and other controls that the user interact with to execute an action. The command holds the events for execute and inquire. Commanders that are attached to the commands are required to update their visual state depending on the command state. When a commander is invoked, for example when a push button is pressed the, it is the command that will fire the execute event.
Multiple commanders can be connected to one command. For example a menu item and a button can both be driven by the same command.
For a command to function it must be child to a Command Manager. It is the command manager that is responsible for inquiring its' child commands when an application event is raised. The Command Manager also enables and disables commands during server calls etc.
Commands are designed through the Command Manager Designer. All top level window classes expose a Command Manager instance.
In the Command Manager Designer you can create commands and commanders. In the left pane of the editor you create commands and on the right side commanders are created.
Figure 1:The Command Manager Designer
Note: Commanders can be created directly on the form design surface and later edited in this editor. It is normally quite convenient to create the command and the commanders at the same time in the editor.
The command publishes the Inquire
and Execute
events
that normally should be implemented in order to add the correct logic for the command.
Tip: If the required event handler does not exist, double click the event in the property dialog and Visual Studio will generate the handler.
The command's Execute
event is fired when the button is clicked.
When the event is fired, the callback method created when subscribing to the
Execute
event will be called. The callback method should perform the
action appropriate for the button.
private void commandPopulate_Execute(object sender, Ifs.Fnd.Windows.Forms.FndCommandExecuteEventArgs e) { Sys.hWndForm.SendMessage(Const.PM_DataSourcePopulate, Const.METHOD_Execute, 0); }
Figure 2: Example of a command execute event handler
A Command has an Inquire
event
that is fired when the command needs to update its state. This could be e.g. when
a user selects a new record or edits a field.
The Inquire
event callback methods are used by the application to
set the state of commands to reflect the state of the application. The code in Fig.
3. shows how the populate command Enabled state
is set according to if the current form can be populated at the current time.
This is done by changing the Enabled
property on the
Command. That property change on the command will
be reflected on all the connected commanders.
private void commandPopulate_Inquire(object sender, Ifs.Fnd.Windows.Forms.FndCommandInquireEventArgs e) { ((FndCommand)sender).Enabled = Sys.hWndForm.SendMessage(Const.PM_DataSourcePopulate, Const.METHOD_Inquire, 0); }
Figure 3: Example of a command inquire event handler
The default behavior for the command Inquire
is that the
Command Manager will request an inquire whenever
some data source, contextual or edit change happens. This is for example when a
data source is populated, when a new record is created, when the data record is
saved, when the user switches window etc. In some situations there is a need for
a commander to update with much greater frequency. An LOV button for example needs
to change every time focus is moved from one field to another. The overall performance
of the application would however not benefit from all commanders to update their
state that often.
This is what the EventCategory property solves. If you have a command that has special needs, you can set the command's EventCategory property to a specific category. Only commands with the property EventCategory set will be inquired for such a category.
Category | Event |
---|---|
Edit | Commands will be inquired every time a field is edited. This is fired for each key stroke. |
Focus | Commands will be inquired every time the focus is changed in the application |
The action tree displays the existing commanders on the underlying form. An existing button can be connected to a created command directly, but if there is a need for a new commander you create one like this.
Example: If you want to create a button, select the Buttons node.
It is possible to create a commander directly from a command.
When a command is dropped like this, a commander is generated with a name corresponding to the command and with the command assigned.
Drop Target | Control Keys Pressed | Result |
---|---|---|
Buttons Node | A command button is created | |
Existing button | The button will be reassigned to the command that was dropped. | |
Context Menu | A menu item is created in the context menu | |
Existing Menu Item | The menu item will be reassigned to the command that was dropped. | |
Existing Menu Item | Ctrl | A menu item is created as a sub item to target item. |