-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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
Asked: 2011-06-15 07:20:28 +0800
Seen: 269 times
Last updated: Jul 11 '11