@Override
protected void setup(VaadinRequest request) {
setContent(topLayout);
// Create the main layout for our application (4 columns, 5 rows)
final GridLayout layout = new GridLayout(4, 5);
topLayout.setMargin(true);
topLayout.setSpacing(true);
Label title = new Label("Calculator");
topLayout.addComponent(title);
topLayout.addComponent(log);
HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.setSpacing(true);
horizontalLayout.addComponent(layout);
horizontalLayout.addComponent(log);
topLayout.addComponent(horizontalLayout);
// Create a result label that over all 4 columns in the first row
layout.setSpacing(true);
layout.addComponent(display, 0, 0, 3, 0);
layout.setComponentAlignment(display, Alignment.MIDDLE_RIGHT);
display.setSizeFull();
display.setId("display");
display.setValue("0.0");
// The operations for the calculator in the order they appear on the
// screen (left to right, top to bottom)
String[] operations = new String[] { "7", "8", "9", "/", "4", "5", "6",
"*", "1", "2", "3", "-", "0", "=", "C", "+" };
for (String caption : operations) {
// Create a button and use this application for event handling
Button button = new Button(caption);
button.setWidth("40px");
button.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
// Get the button that was clicked
Button button = event.getButton();
// Get the requested operation from the button caption
char requestedOperation = button.getCaption().charAt(0);
// Calculate the new value
double newValue = calculate(requestedOperation);
// Update the result label with the new value
display.setValue("" + newValue);
}
});
button.setId("button_" + caption);
// Add the button to our main layout
layout.addComponent(button);
}
}