Observers
Applications can be notified when the state of a manager changes. This notification mechanism is based on
IlvManagerObserver, a subclass of
IlvObserver. Observers are created by the application and set on the manager. The manager is in charge of sending messages to the observer under certain circumstances called
reasons.
Notification messages are classified by their reason into different categories. An observer can choose to receive messages of one or several categories by setting its
interest mask. The manager will only send a message to the observer if the notification reason belongs to a category of the observer interest mask. These categories are shown in
Table 3.1:
Table 3.1 Notification Categories
Category Description | Mask |
General | IlvMgrMsgGeneralMask |
Manager view | IlvMgrMsgViewMask |
Manager layer | IlvMgrMsgLayerMask |
Manager contents | IlvMgrMsgContentsMask |
Object geometry | IlvMgrMsgObjectGeometryMask |
An application wishing to get notification messages must define a subclass of
IlvManagerObserver and overload the virtual member function
update. In this member function, the observer receives an instance of
IlvManagerMessage, or a subclass, containing the reason and additional relevant information about the notification.
General Notifications
This category concerns general notifications on the managers.
Delete the manager
Manager View Notifications
This category concerns notifications on operations performed on manager views.
Interest mask: IlvMgrMsgViewMask
Add a view to the manager
Remove a view from the manager
Set an interactor on a view
Set a transformer on a view
Manager Layer Notifications
This category concerns notifications on operations performed on manager layers.
Add a layer to the manager
Remove a layer from the manager
Change the index of a layer
Swap indexes between two layers
Set the name of a layer
Set the visibility of a layer
Set the selectabililty of a layer
Manager Contents Notifications
This category concerns notifications on the changes in the contents of managers.
Add a graphic object to the manager
Remove a graphic object from the manager
Set the layer of a graphic object
Graphic Object Geometry Notifications
This category concerns notifications on a change of geometry of the objects (for example, move, resize, and rotate).
Change the geometry of a graphic object
Example
Here is the implementation of an observer that receives notifications on adding or removing layers and views.
class MyManagerObserver : public IlvManagerObserver { public: MyManagerObserver(IlvManager* manager) : IlvManagerObserver(manager, IlvMgrMsgLayerMask | IlvMgrMsgViewMask) {} virtual void update(IlvObservable* o, IlvAny arg); }; |
The update member function:
void MyManagerObserver::update(IlvObservable* obs, IlvAny arg) { IlvManager* manager = ((IlvManagerObservable*)obs)->getManager(); switch(((IlvManagerMessage*) arg)->_reason) { // __ Notification on manager view case IlvMgrMsgAddView: IlvPrint("Add view notification"); break; case IlvMgrMsgRemoveView: IlvPrint("Remove view notification"); break; // __ Notification on manager layer case IlvMgrMsgAddLayer: IlvPrint("Add layer notification: %d", ((IlvManagerLayerMessage*)arg)->getLayer()); break; case IlvMgrMsgRemoveLayer: IlvPrint("Remove layer notification: %d", ((IlvManagerLayerMessage*)arg)->getLayer()); break; default: IlvPrint("Unhandled notification"); break; } } |
To attach the observer to the manager:
MyManagerObserver* observer = new MyManagerObserver(manager);
Version 5.5.1
Copyright © 2012, Rogue Wave Software, Inc. All Rights Reserved.