|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.zkoss.zk.ui.event.impl.DesktopEventQueue
public class DesktopEventQueue
The default implementation of the desktop-level event queue (EventQueue
).
Constructor Summary | |
---|---|
DesktopEventQueue()
|
Method Summary | |
---|---|
void |
close()
Closes the event queue. |
boolean |
isIdle()
Returns if there is listener being registered. |
boolean |
isSubscribed(EventListener listener)
Returns if an event listener is subscribed. |
void |
publish(Event event)
Publishes an event the queue. |
void |
subscribe(EventListener listener)
Subscribes a listener to this queue. |
void |
subscribe(EventListener listener,
boolean async)
Subscribes a synchronous or asynchronous listener to this event queue. |
void |
subscribe(EventListener listener,
EventListener callback)
Subscribes a synchronous or asynchronous listener to this event queue. |
boolean |
unsubscribe(EventListener listener)
Unsubscribes a listener from the queue. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DesktopEventQueue()
Method Detail |
---|
public boolean isIdle()
public void publish(Event event)
EventQueue
If this is a desktop-level event queue, this method must be called
within an activated exection,
i.e., Executions.getCurrent()
not null.
On the other hand, if this is an application-level event queue, it is OK to be called without the current execution.
publish
in interface EventQueue
public void subscribe(EventListener listener)
EventQueue
subscribe(listener, false)
(EventQueue.subscribe(EventListener,boolean)
. In other words,
it subscribes a synchronous listener.
Note: this method must be called within an activated exection,
i.e., Executions.getCurrent()
not null.
Note: if this is an application-level event queue, the listener
shall not access the component associated with the event
Event.getTarget()
.
An event listener can be subscribed multiple times, and it will be invoked multiple times if an event is published.
Even if this is an application-level or session-level event queue, the listener is subscribed for the current desktop only. If you want to use the same listener for multiple desktops, you have to subscribe them separately when the corresponding execution is available.
subscribe
in interface EventQueue
EventQueue.subscribe(EventListener,EventListener)
,
EventQueue.subscribe(EventListener,boolean)
public void subscribe(EventListener listener, EventListener callback)
EventQueue
Here is an example,
<window title="long operation" border="true">
<zscript>
void print(String msg) {
new Label(msg).setParent(inf);
}
</zscript>
<button label="async long op">
<attribute name="onClick"><![CDATA[
if (EventQueues.exists("longop")) {
print("It is busy. Please wait");
return; //busy
}
EventQueue eq = EventQueues.lookup("longop"); //create a queue
String result;
//subscribe async listener to handle long operation
eq.subscribe(new EventListener() {
public void onEvent(Event evt) { //asynchronous
org.zkoss.lang.Threads.sleep(3000); //simulate a long operation
result = "done"; //store the result
}
}, new EventListener() { //callback
public void onEvent(Event evt) {
print(result); //show the result to the browser
EventQueues.remove("longop");
}
});
print("Wait for 3 seconds");
eq.publish(new Event("whatever")); //kick off the long operation
]]></attribute>
</button>
<vbox id="inf"/>
</window>
Notice that, though an asynchronous listener cannot access
the desktop and has no current execution, it can invoke
EventQueue.publish(org.zkoss.zk.ui.event.Event)
to publish the events. Refer to
another example in EventQueue.subscribe(EventListener,boolean)
.
subscribe
in interface EventQueue
listener
- the asynchronous listener to invoke when an event
is receivedcallback
- the callback listener, which will be invoked if the asynchronous
listen has been invoked.
Unlike the asynchronous listener, the callback listener works
like a normal listener. You can access the current execution,
and update the desktop. Notice that the event argument is null
when the callback listener is called.EventQueue.subscribe(EventListener)
,
EventQueue.subscribe(EventListener,boolean)
public void subscribe(EventListener listener, boolean async)
EventQueue
The use of synchronous listeners is straightforward -- they are just the same a normal event listener. Here is an example of using an asynchronous listener. In this example, we use an asynchronous listener to execute a long operation, a synchronous listener to update the desktop, and they communicate with each other with events.
There is another way to do the same job, callback, refer
to EventQueue.subscribe(EventListener,EventListener)
for example.
<window title="long operation" border="true">
<zscript>
void print(String msg) {
new Label(msg).setParent(inf);
}
</zscript>
<button label="async long op">
<attribute name="onClick"><![CDATA[
if (EventQueues.exists("longop")) {
print("It is busy. Please wait");
return; //busy
}
EventQueue eq = EventQueues.lookup("longop"); //create a queue
String result;
//subscribe async listener to handle long operation
eq.subscribe(new EventListener() {
public void onEvent(Event evt) {
if ("doLongOp".equals(evt.getName())) {
org.zkoss.lang.Threads.sleep(3000); //simulate a long operation
result = "done"; //store the result
eq.publish(new Event("endLongOp")); //notify it is done
}
}
}, true); //asynchronous
//subscribe a normal listener to show the resul to the browser
eq.subscribe(new EventListener() {
public void onEvent(Event evt) {
if ("endLongOp".equals(evt.getName())) {
print(result); //show the result to the browser
EventQueues.remove("longop");
}
}
}); //synchronous
print("Wait for 3 seconds");
eq.publish(new Event("doLongOp")); //kick off the long operation
]]></attribute>
</button>
<vbox id="inf"/>
</window>
The asynchornous event listener requires Server Push which is available in ZK PE or EE, or you have to configure your own implementation.
If you want to show a busy message to cover a portion of the desktop,
use Clients.showBusy(org.zkoss.zk.ui.Component,String)
Note: this method must be called within an activated exection,
i.e., Executions.getCurrent()
not null.
An event listener can be subscribed multiple times, and it will be invoked multiple times if an event is published.
Even if this is an application-level or session-level event queue, the listener is subscribed for the current desktop only. If you want to use the same listener for multiple desktops, you have to subscribe them separately when the corresponding execution is available.
subscribe
in interface EventQueue
listener
- the listenerasync
- whether the listener is asynchronousEventQueue.subscribe(EventListener)
,
EventQueue.subscribe(EventListener, EventListener)
public boolean unsubscribe(EventListener listener)
EventQueue
Note: this method must be called within an activated exection,
i.e., Executions.getCurrent()
not null.
Notice that this method only unsubscribes the listener subscribed for this desktop. It doesn't check the listeners for other desktops even if this is an application-level or session-level event queue.
unsubscribe
in interface EventQueue
public boolean isSubscribed(EventListener listener)
EventQueue
Notice that this method only checks the listeners subscribed for this desktop. It doesn't check the listeners for other desktops even if this is an application-level or session-level event queue.
isSubscribed
in interface EventQueue
public void close()
EventQueue
Don't call this method directly. It is called only internally.
Rather, use EventQueues.remove(java.lang.String)
instead.
close
in interface EventQueue
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |