Modal Windows"

From Documentation
m (remove empty version history (via JWB))
 
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{ZKDevelopersGuidePageHeader}}
+
{{ZKDevelopersReferencePageHeader}}
  
This section describes the notes when using the Servlet thread to process the event (the default).
+
= Modal Windows with Servlet Thread =
  
=== Modal Windows ===
+
When the event is processed in the Servlet thread (default), the execution cannot be suspended. Thus, the modal window behaves the same as the highlited window (<javadoc method="doHighlighted()">org.zkoss.zul.Window</javadoc>). At the client side, the visual effect is the same: a semi-transparent mask blocks the end user from access components other than the modal window. However, at the server side, it works just like the overlapped mode – it returns immediately without waiting for user's closing the window.
 
 
Since the event is processed in the Servlet thread, the execution cannot be suspended. Thus, the modal window behaves the same as the highlited window. At the client side, the visual effect is the same: a semi-transparent mask blocks the end user from access components other than the modal window. However, at the server side, it works just like the overlapped mode – it returns immediately without waiting for user's closing the window.
 
  
 
<source lang="java" >
 
<source lang="java" >
  win.doHighlighted(); //returns once the mode is changed; not suspended
+
  win.doModal(); //returns once the mode is changed; not suspended
 
  System.out.println("next");
 
  System.out.println("next");
 
</source>
 
</source>
Line 14: Line 12:
 
The "next" message will be printed to the console before the end user closes the modal window.
 
The "next" message will be printed to the console before the end user closes the modal window.
  
=== Message Boxes ===
+
== Migrate Your Code from Event Thread ==
The message boxes returns immediately so it always returns <tt>Messagebox.OK</tt>. Thus, it is meaningless to show buttons other than the OK button. For example, the <tt>if</tt> clause in the following example is never true.
 
  
 +
With the Event thread, you might write your business logic right after <code>doModal()</code>.
 
<source lang="java" >
 
<source lang="java" >
if (Messagebox.show("Delete?", "Prompt", Messagebox.YES|Messagebox.NO,
+
win.doModal();
    Messagebox.QUESTION) == Messagebox.YES) {
+
doMyTask(); //your business logic, need to move it for a servlet thread
    this_never_executes();
 
}
 
 
</source>
 
</source>
 
Rather, you have to provide an event listener as follows.
 
  
<source lang="java" >
+
Since now servlet thread doesn't stop at <code>doModal()</code>, you need to move your code to another place. You can put it at:
Messagebox.show("Delete?", "Prompt", Messagebox.YES|Messagebox.NO,
+
 
    Messagebox.QUESTION,
+
* Window's <code>onClose</code> event listener
    new EventListener() {
+
* add a button in the modal window and call <code>doMyTask()</code> in an <code>onClick</code> listener and close the modal window.
        public void onEvent(Event evt) {
+
 
            switch (((Integer)evt.getData()).intValue()) {
+
= Modal Windows with Event Thread =
            case Messagebox.YES: doYes(); break; //the Yes button is pressed
 
            case Messagebox.NO: doNo(); break; //the No button is pressed
 
            }
 
        }
 
    }
 
);
 
</source>
 
 
The event listener you provided is invoked when the user clicks one of the buttons. Then, you can identify which button is clicked by examining the data (Event's <tt>getData</tt>). The data is an integer whose value is the button's identifier, such as <tt>Messagebox.YES</tt>.
 
  
Alternatively, you can examine the event name:
+
If the event thread is enabled, <javadoc method="doModal()">org.zkoss.zul.Window</javadoc> will suspend the current thread. Thus, the "next" message won't be shown, until the modal window is closed.
  
<source lang="java" >
+
When the event thread is suspended, the Servlet thread will be resumed and continue to loork another event thread to process other events, if any. Thus, the end user still have the control (such that he can close the modal window if he want).
public void onEvent(Event evt) {
 
    if ("onYes".equals(evt.getName())) {
 
        doYes(); //the Yes button is pressed
 
    } else if ("onNo".equals(evt.getName())) {
 
        doNo(); //the No button is pressed
 
    }
 
}
 
</source>
 
 
'''Note''': The event name for the OK button is <tt>onOK</tt>, not <tt>onOk</tt>.
 
  
=== File Upload ===
 
The file upload dialog is no longer applicable. Rather, you shall use <javadoc>org.zkoss.zul.Button</javadoc> or <javadoc>org.zkoss.zul.Toolbarbutton</javadoc> with upload="true" instead. For example,
 
  
<source lang="xml">
 
<zk>
 
<zscript>
 
void upload(Event event) {
 
org.zkoss.util.media.Media media = event.getMedia();
 
if (media instanceof org.zkoss.image.Image) {
 
org.zkoss.zul.Image image = new org.zkoss.zul.Image();
 
image.setContent(media);
 
image.setParent(pics);
 
} else {
 
Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
 
break; //not to show too many errors
 
}
 
}
 
</zscript>
 
<button label="Upload" upload="true" onUpload="upload(event)"/>
 
<toolbarbutton label="Upload" upload="true" onUpload="upload(event)"/>
 
<vbox id="pics" />
 
</zk>
 
</source>
 
  
If you prefer to use a dialog (<javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>), please take a look at [[ZK_Component_Reference/Essential_Components/Fileupload | ZK Component Reference: Fileupload]] for more inormation.
+
{{ZKDevelopersReferencePageFooter}}
{{ZKDevelopersGuidePageFooter}}
 

Latest revision as of 05:54, 6 February 2024

Modal Windows with Servlet Thread

When the event is processed in the Servlet thread (default), the execution cannot be suspended. Thus, the modal window behaves the same as the highlited window (Window.doHighlighted()). At the client side, the visual effect is the same: a semi-transparent mask blocks the end user from access components other than the modal window. However, at the server side, it works just like the overlapped mode – it returns immediately without waiting for user's closing the window.

 win.doModal(); //returns once the mode is changed; not suspended
 System.out.println("next");

The "next" message will be printed to the console before the end user closes the modal window.

Migrate Your Code from Event Thread

With the Event thread, you might write your business logic right after doModal().

win.doModal(); 
doMyTask(); //your business logic, need to move it for a servlet thread

Since now servlet thread doesn't stop at doModal(), you need to move your code to another place. You can put it at:

  • Window's onClose event listener
  • add a button in the modal window and call doMyTask() in an onClick listener and close the modal window.

Modal Windows with Event Thread

If the event thread is enabled, Window.doModal() will suspend the current thread. Thus, the "next" message won't be shown, until the modal window is closed.

When the event thread is suspended, the Servlet thread will be resumed and continue to loork another event thread to process other events, if any. Thus, the end user still have the control (such that he can close the modal window if he want).




Last Update : 2024/02/06

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.