Custom actions are actions that occur in response to the movement of a slider, the click of a button or the toggling of a checkbox on the Custom Action tab in the settings windows. This document explains how to create such actions and should be used in conjunction with the demonstration source code to the Heat Bugs model, and the Hypercycles model.
The creation of custom actions is coordinated by the
Model
Manipulator class. SimModelImpl contains a ModelManipulator as a
protected instance variable, and it is thus available to all classes
that extend SimModelImpl (that is, all Repast models). ModelManipulator
contains three methods for creating custom actions:
If you want to add a slider, you use addSlider, a button, addButton, and a checkbox,
addCheckBox. The arguments to these three methods are defined in
greater detail in the API documentation for
Model
Manipulator. In all these cases however, the listener argument specifies
what action should be taken when the slider is moved, the button is clicked, or
the checkbox is toggled. These listener classes are abstract
classes (SliderListener, CheckBoxListener) or interfaces (ActionListener)
that are extended or implemented by the modeler to perform the
appropriate action.
public void addSlider(String label, int min, int max, int tickInterval,
SliderListener listener)
public void addButton(String label, ActionListener listener)
public void addCheckBox(String label, CheckBoxListener listener, boolean isSelected)
class Incrementer extends SliderListener {
public void execute() {
if (isSlidingLeft) {
someValue -= value;
} else {
someValue += value;
}
}
};
modelManipulator.addSlider("Increment", 0, 100, 10, new Incrementer());
(This could also be done more concisely as a anonymous inner class.)
Here a new class Incrementer that extends SliderListener is defined. Its
execute method will subtract the current value of the slider from someValue
if the slider is moving left, or add the current value of the slider to
someValue if the slider is moving right. The modelManipulator.addSlider
method associates this behavoir with a slider. The slider will be
labeled "Increment" and range for 0 to 100 with labeled ticks every 10 units.
modelManipulator.addButton("Clear Space", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
space.clear();
}
});
(Here the ActionListener is coded as an anonymous inner class.) The
ModelManipulator creates a button labeled "Clear Space" and the
actionPerformed method will be executed whenever that button is clicked.
CheckBoxListener showVal = new CheckBoxListener() {
public void execute() {
if (isSelected) {
showValues();
} else {
hideValues();
}
}
};
modelManipulator.addCheckBox("Show Values", showVal, false);
Here when the checkbox is clicked the execute
method will be called. This method then checks whether the
checkbox is selected through the isSelected variable and calls
showValues() or hideValues(). ModelManipulator then creates a checkbox labeled
"Show Values", associates it with the showVal
CheckBoxListener and sets its initial state to unselected.