Skip to main content

Talking progress indicators

·443 words·3 mins
Vaadin
Alejandro Duarte
Author
Alejandro Duarte
Alejandro Duarte is a Software Engineer, published author, and Developer Relations Engineer at MariaDB. He has been programming computers since the mid-90s. Starting with BASIC, Alejandro transitioned to C, C++, and Java during his academic years at the National University of Colombia. He relocated first to the UK and then to Finland to foster his involvement in the open-source industry. Alejandro is a recognized figure in Java and MariaDB circles.

Willing to learn Vaadin 7 in a funny way? Here there is an excerpt of my book Vaadin 7 UI Design by Example: Beginner’s Guide.

Author: Let me introduce you to a friend. ProgressIndicator, reader. Reader, ProgressIndicator.

A talking progress indicator

Author: Now that you know each other, let’s work together.

ProgressIndicator: Cool, what’s the task to do?

Author: Well, our awesome algorithm is taking too long and users are just leaving our state-of-the-art web application. So ProgressIndicator, we need your help to give the user some feedback about the progress of the process.

ProgressIndicator: Sure.

Author: Thank you sir. Take a look at our original application implementing this Java Thread performing our high-tech algorithm: 

 public class ProgressindicatorUI extends UI {

  private class HighTechAlgorithm extends Thread {
    public void run() {
      try {

        for (int i = 0; i < 10; i++) {
          sleep(1000);
        }

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  protected void init(VaadinRequest request) {
    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);

    Button button = new Button("Start awesome algorithm");

    button.addClickListener(new Button.ClickListener() {
      public void buttonClick(ClickEvent event) {
        new HighTechAlgorithm().start();
      }
    });

    layout.addComponent(button);
  }

}

ProgressIndicator: Wow! That’s an awesome algorithm.

Author: Thank you. It took us months to implement it. Anyways, we would like to add you, Mr. ProgressIndicator, to our layout, so you can tell the user how the progress of the algorithm is going. Is that OK for you?

ProgressIndicator: Sure. Let me place myself as a private member of your UI class.

Author: Of course, come in. And please add yourself into our main layout:

 public class ProgressindicatorUI extends UI {

  private ProgressIndicator mrProgressIndicator =
      new ProgressIndicator();

  // ...

  protected void init(VaadinRequest request) {

    // ...

    layout.addComponent(mrProgressIndicator);
  }
}

ProgressIndicator: What a nice place. Really high-tech.

A progress indicator in a Vaadin app

Author: Yeah, we painted it Vaadin color.

ProgressIndicator: My favorite color!

Author: Nice. For each iteration of our algorithm I will update you, OK?

ProgressIndicator: Yes please.

Author: Cool. You accept values in which range?

ProgressIndicator: Give me a float between 0 and 1.

Author: OK. There you go:

 public class ProgressindicatorUI extends UI {

  // ...

  private class HighTechAlgorithm extends Thread {

    public void run() {
      try {

        for (int i = 1; i <= 10; i++) {
          sleep(1000);
          mrProgressIndicator.setValue(i \* 0.1f);
        }

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

  }
}

ProgressIndicator: Thank you. I think I’m ready.

Author: Great! Let’s run the app. Are you ready reader? You have been kind of quiet lately.

ProgressIndicator: Yeah. I think she/he might be overwhelmed by the high-tech algorithm.

Author: That’s reasonable. Let’s test our application: 

A working progress indicator

ProgressIndicator: Cool. I’m working properly!

Author: Wonderful job ProgressIndicator. Thank you very much.

ProgressIndicator: My pleasure.

Enjoyed this post? I can help your team implement similar solutions—contact me to learn more.

Related

Book Give-away: Win a free copy of the book "Vaadin 7 UI Design By Example" - Just by commenting!
·246 words·2 mins
Vaadin News
Participate in a contest to win a free copy of the book “Vaadin 7 UI Design By Example” by commenting on this post.
Enterprise App now available with Maven
·46 words·1 min
Vaadin UI News
Learn how to add the Enterprise App Vaadin plug-in to your Maven project for Vaadin.