0

Easy way of hide long operation.

asked 2011-06-15 07:20:28 +0800

jelvira gravatar image jelvira
12

updated 2011-06-15 07:34:07 +0800

Hi everybody.

Just thought somebody may find this useful. I'ts a very simple way to hide parts of the UI while an operation is being performed. After searching here, the docs... this is the result.

Picture this... you have a window <listbox> with a <paging> attached. You only load a bunch of items at a time, so every "next page" data is gathered from the database. Or some user triggered action makes some parts of the UI to load, or being created.

The trick is to have 2 <div> in the page: one contains the list, or better, the UI that has not to be shown to the user while the operation is done, and the second an animated gif an a <label> with a big red "Loading..." text.

Add this class:

public class CustomEventAttacher {

    public static void Attach(final Component toComponent, final String eventName) {
        toComponent.addEventListener(eventName, new EventListener() {
            @Override
            public void onEvent(Event event) throws Exception {
                Method method;             
                if (event.getData() != null) {
                    method = toComponent.getClass().getDeclaredMethod(eventName, event.getData().getClass());
                    method.invoke(toComponent, event.getData());
                }
            }
        });
    }

    // lauches the event "in background"
    public static void BeginInvoke(Component toComponent, String eventName, Object data) {
        Events.echoEvent(eventName, toComponent, data);   
    }

    // immediate execution of the event
    public static void Invoke(Component toComponent, String eventName, Object data) {
        Events.sendEvent(eventName, toComponent, data);
    }

}

Usual disclaimer... works for me as it is, but it may contain a few bugs :D

Now, your class:


public class MyWindow [...] {

public MyWindow() {

}

public someButtonOnClick {
      this.doLongOperation();
      this.refreshUI();
}

You only have to add to the constructor (or onCreate()):


public MyWindow() {
      CustomEventAttacher.attach(this, "onBeginLoading");
}

Next, split the long operation trigger. The method must be named exactly like the event string:


public someButtonOnClick() {
      this.showLoadingDIV():
      this.hideDataDIV();
      CustomEventAttacher.BeginInvoke("onBeginLoading", this, "42"); // 42 is a sample of data passing. and the answer.
}

public onBeginLoading(String data) {
      this.doLongOperation();
      this.refreshUI();
      this.hideLoadingDIV():
      this.showDataDIV();
}

Maybe somebody finds it useful. I use it also for inter-window communication.

Please, let me know if you find a problem, or if there is a better way to do something.

Cheers

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2011-07-11 04:31:23 +0800

PeterKuo gravatar image PeterKuo
481 2

Thanks, jelvira :)

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-06-15 07:20:28 +0800

Seen: 269 times

Last updated: Jul 11 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More