MATLAB Object-Oriented Programming

(Joyce) #1

Enable and Disable Listeners


Each fcnview object stores the handle of the listener objects it creates so that the
listeners can be enabled or disabled via a context menu after the graphs are created. All
listeners are instances of the event.listener class, which defines a property called
Enabled. By default, this property has a value of true, which enables the listener. If you
set this property to false, the listener still exists, but is disabled. This example creates a
context menu active on the axes of each graph that provides a way to change the value of
the Enabled property.


Context Menu Callback


There are two callbacks used by the context menu corresponding to the two items on the
menu:



  • Listen — Sets the Enabled property for both the UpdateGraph and PostSet
    listeners to true and adds a check mark next to the Listen menu item.

  • Don't Listen — Sets the Enabled property for both the UpdateGraph and PostSet
    listeners to false and adds a check mark next to the Don't Listen menu item.


Both callbacks include the fcnview object as an argument (in addition to the required
source and event data arguments) to provide access to the handle of the listener objects.


The enableLisn function is called when the user selects Listen from the context menu.


function enableLisn(obj,src,evnt)
obj.HLUpdateGraph.Enabled = true; % Enable listener
obj.HLLm.Enabled = true; % Enable listener
set(obj.HEnableCm,'Checked','on') % Check Listen
set(obj.HDisableCm,'Checked','off') % Uncheck Don't Listen
end


The disableLisn function is called when the user selects Don't Listen from the context
menu.


function disableLisn(obj,src,evnt)
obj.HLUpdateGraph.Enabled = false; % Disable listener
obj.HLLm.Enabled = false; % Disable listener
set(obj.HEnableCm,'Checked','off') % Unheck Listen
set(obj.HDisableCm,'Checked','on') % Check Don't Listen
end


Techniques for Using Events and Listeners
Free download pdf