search

Class myphysicslab.lab.controls.ChoiceControlBase

Provided By
All Implemented Interfaces
All Known Direct Subclasses

A pop-up menu which executes a specified setter function when selecting a menu choice.

This is the base class for myphysicslab.lab.controls.ChoiceControl which which connects to a myphysicslab.lab.util.Parameter.

Choices and Values

  • the array of translated localized choices are the strings which are displayed in the menu.

  • the array of values correspond to each choice. The type of the value is that of the generic template type T, which is either string, number, or boolean.

Getter and Setter Functions

The setter and getter functions synchronize between a target object and the menu.

  • the getter returns the value that corresponds to the target state

  • the setter modifies the target state to correspond to the given value

The menu item is initially set to correspond to the position in the array of values of the target's value returned by the getter. When a menu item is selected, we call the setter with the corresponding value in the array of values.

'No Selection' State

A ChoiceControlBase can be in a state of 'no selection', which is indicated by index of –1 in #setChoice or #getChoice. This can also happen when ChoiceControlBase observes that the value returned by the getter function is not among the array of values specified to the constructor or to #setChoices.

When moving into a state of 'no selection', no notification is given via the specified setter function. When moving out of the 'no selection' state, the setter is called as normal.

Updating The Control

To keep the control in sync with the target object, call the #observe method whenever a change in the value of the target object occurs. If the target object is a myphysicslab.lab.util.Subject then you can add this control as an Observer of the Subject.

new ChoiceControlBase( choices, values, getter, setter, opt_label )

Parameters
choicesArray<string>

an array of localized strings giving the names of the menu items.

valuesArray<string>

array of values corresponding to the choices, in string form; these values are supplied to the setter function.

getterfunction(): string

function that returns the value that corresponds to the target state

setterfunction(string): ?

function that modifies the target state to correspond to the given value

opt_label(string|null|undefined)

the text label to show besides this choice; if null or undefined or empty string then no label is made.

Instance Methods

Instance Properties