}
@Override
public void handle(ActionEvent event) {
Object source = event.getSource();
Labeled labeled = null;
if (source instanceof Labeled) {
labeled = (Labeled) source;
}
if (labeled != null) {
// check CSS styles, see RPN.fxml
// the inputTextButton style is for buttons that append text to the input text field, e.g. digits
String text = labeled.getText();
if (labeled.getStyleClass().contains("inputTextButton")) {
input = (input == null ? String.valueOf(text) : input + text);
updateValueText();
}
// the inputTextEditButton style is for buttons that (somehow) edit the input text field, e.g. backspace
if (labeled.getStyleClass().contains("inputTextEditButton")) {
if (text.equals("backspace")) {
if (input == null || input.length() == 0) {
input = String.valueOf(rpn.pop(0.0));
}
input = input.substring(0, input.length() - 1);
}
updateValueText();
}
// the operatorButton style is for operator buttons, e.g. +
if (labeled.getStyleClass().contains("operatorButton")) {
char op = text.charAt(0);
// push existing input
if (input != null) {
rpn.push(Double.valueOf(input));
input = null;
}
rpn.performOperation(op);
updateValueText();
}
// the actionButton style is for buttons that otherwise modify the calculator stack, e.g. push
if (labeled.getStyleClass().contains("actionButton")) {
performAction(text);
}
}
}