Vaadin 7 UI Design By Example:Beginner’s Guide
上QQ阅读APP看书,第一时间看更新

Time for action – running the test set

Implement your runSelectedTest method. This method will be called when the user presses the Time it! button:

public void runSelectedTest() {
  Long times = Long.parseLong(textField.getValue());
  Collection<String> results = TestSetExecutor.execute(
      (TestSet) combo.getValue(), times);
  showResults(results);
}

What just happened?

Here, we're converting the string stored in the text field to a Long number using Long.parseLong (a potential exception, NumberFormatException, pokes its head).

Once we have the Long value, we execute the results using a helper class in the biz package (TestSetExecutor). This helper class has an execute method that expects the TestSet to execute and the number of iterations to perform for each test in the TestSet. The execute method returns all the results as a collection of strings that we can proudly show to the user. As proudly as Susan when she presented her code.

Have a go hero – add a validation to Time It

When converting the value in textField, the method parseLong will throw an evil NumberFormatException if the string to parse is not a number. Try it! Use "Time It" and type a wrong numeric expression in the text field. Now try making "Time It" a production-ready application by adding the missing validation (inside the isValid method) to show a proper error message when incorrect input is given for the textField component.