-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I learned from http://docs.zkoss.org/wiki/ZK/How-Tos/Concepts-and-Tricks#Create_a_multiline_Messagebox
The following code is an simple example. I have an more complex inputMessageBox which the input field can be of any type.
1. Create an ZUL File "inputMessageBox.zul" at Folder named "zul" inside "WebContent"
<?xml version="1.0" encoding="UTF-8"?> <?page title="ZK Test" language="xul/html"?> <!-- test.zul {{IS_NOTE Purpose: View the specified subject. Description: History: Mon May 30 19:13:47 2005, Created by tomyeh }}IS_NOTE Copyright (C) 2005 Potix Corporation. All Rights Reserved. {{IS_RIGHT This program is distributed under GPL Version 3.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY. }}IS_RIGHT --> <window title="${arg.title}" border="normal" width="auto" closable="true" use="org.zkoss.zul.impl.MessageboxDlg"> <hbox> <div class="${arg.icon}"/> <div sclass="z-messagebox" width="auto"> <label value="${arg.message}" multiline="true"/> <textbox id="txtQuestion"/> </div> </hbox> <separator bar="true"/> <hbox style="margin-left:auto; margin-right:auto"> <button id="btn1" identity="${arg.OK}" sclass="z-messagebox-btn" use="org.zkoss.zul.impl.MessageboxDlg$Button" if="$"/> <button identity="${arg.CANCEL}" sclass="z-messagebox-btn" use="org.zkoss.zul.impl.MessageboxDlg$Button" if="$"/> <button identity="${arg.YES}" sclass="z-messagebox-btn" use="org.zkoss.zul.impl.MessageboxDlg$Button" if="$"/> <button identity="${arg.NO}" sclass="z-messagebox-btn" use="org.zkoss.zul.impl.MessageboxDlg$Button" if="$"/> <button identity="${arg.RETRY}" sclass="z-messagebox-btn" use="org.zkoss.zul.impl.MessageboxDlg$Button" if="$"/> <button identity="${arg.ABORT}" sclass="z-messagebox-btn" use="org.zkoss.zul.impl.MessageboxDlg$Button" if="$"/> <button identity="${arg.IGNORE}" sclass="z-messagebox-btn" use="org.zkoss.zul.impl.MessageboxDlg$Button" if="$"/> </hbox> </window>
2. Create an JAVA File "InputMessageBox.java"
package com.xxx.project.web; import java.util.HashMap; import java.util.Map; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.UiException; import org.zkoss.zk.ui.WebApp; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zul.Messagebox; import org.zkoss.zul.Textbox; import org.zkoss.zul.impl.MessageboxDlg; /** * @author marcos.sousa * */ public class InputMessageBox extends Messagebox { private static final long serialVersionUID = 1L; // path of the messagebox zul-template private static String oldTemplate = Messagebox.getTemplate(); private static String _templ = "/zul/inputMessageBox.zul"; private static Textbox txtQuestion; /** * Shows a message box and returns an object introduced. * * @param message the message to show before the input field. * @param title the title. If null, {@link WebApp#getAppName} is used. * @return the object. NULL means that CANCEL, otherwise means an Object is being passed. * * Note: if the event processing thread is disable, it always returns null. */ public static final Object showQuestion(String message, String title) throws InterruptedException { setTemplate(_templ); Object object = null; if (showInput(message, title, Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION, 0, null) == Messagebox.OK) { object = txtQuestion.getValue(); // TODO: Remember NULL means that CANCEL, otherwise means an Object is being passed. I used StringUtils from commons lang to trim to empty } setTemplate(oldTemplate); return object; } /** Shows a message box and returns what button is pressed. * * @param title the title. If null, {@link WebApp#getAppName} is used. * @param buttons a combination of {@link #OK}, {@link #CANCEL}, * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY}, * and {@link #IGNORE}. If zero, {@link #OK} is assumed * @param icon one of predefined images: {@link #QUESTION}, * {@link #EXCLAMATION}, {@link #ERROR}, {@link #NONE}, or any style class * name(s) to show an image. * @param focus one of button to have to focus. If 0, the first button * will gain the focus. One of {@link #OK}, {@link #CANCEL}, * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY}, * and {@link #IGNORE}. * @param listener the event listener which is invoked when a button * is clicked. Ignored if null. * It is useful if the event processing thread is disabled * ({@link org.zkoss.zk.ui.util.Configuration#enableEventThread}). * If the event processing thread is disable, this method always * return {@link #OK}. To know which button is pressed, you have to pass an * event listener. Then, when the user clicks a button, the event * listener is invoked. You can identify which button is clicked * by examining the event name ({@link org.zkoss.zk.ui.event.Event#getName}) as shown * in the following table. Alternatively, you can examine the value * of {@link org.zkoss.zk.ui.event.Event#getData}, which must be an * integer represetinng the button, such as {@link #OK}, {@link #YES} * and so on. * <table border="1"> *<tr><td>Button</td><td>Event Name</td></tr> *<tr><td>OK</td><td>onOK</td></tr> *<tr><td>Cancel</td><td>onCancel</td></tr> *<tr><td>Yes</td><td>onYes</td></tr> *<tr><td>No</td><td>onNo</td></tr> *<tr><td>Retry</td><td>onRetry</td></tr> *<tr><td>Abort</td><td>onAbort</td></tr> *<tr><td>Ignore</td><td>onIgnore</td></tr> *</table> * @return the button being pressed (one of {@link #OK}, {@link #CANCEL}, * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY}, * and {@link #IGNORE}). * Note: if the event processing thread is disable, it always * returns {@link #OK}. * @since 3.0.4 */ public static final int showInput(String message, String title, int buttons, String icon, int focus, EventListener listener) throws InterruptedException { final Map params = new HashMap(); params.put("message", message); params.put("title", title != null ? title: Executions.getCurrent().getDesktop().getWebApp().getAppName()); params.put("icon", icon); params.put("buttons", new Integer( (buttons & (OK|CANCEL|YES|NO|ABORT|RETRY|IGNORE)) != 0 ? buttons: OK)); if ((buttons & OK) != 0) params.put("OK", new Integer(OK)); if ((buttons & CANCEL) != 0) params.put("CANCEL", new Integer(CANCEL)); if ((buttons & YES) != 0) params.put("YES", new Integer(YES)); if ((buttons & NO) != 0) params.put("NO", new Integer(NO)); if ((buttons & RETRY) != 0) params.put("RETRY", new Integer(RETRY)); if ((buttons & ABORT) != 0) params.put("ABORT", new Integer(ABORT)); if ((buttons & IGNORE) != 0) params.put("IGNORE", new Integer(IGNORE)); final MessageboxDlg dlg = (MessageboxDlg) Executions.createComponents(_templ, null, params); dlg.setButtons(buttons); dlg.setEventListener(listener); txtQuestion = (Textbox)dlg.getFellowIfAny("txtQuestion"); if (focus > 0) dlg.setFocus(focus); if (dlg.getDesktop().getWebApp().getConfiguration().isEventThreadEnabled()) { try { dlg.doModal(); } catch (Throwable ex) { if (ex instanceof InterruptedException) throw (InterruptedException)ex; try { dlg.detach(); } catch (Throwable ex2) { System.err.println("Failed to detach when recovering from an error " + ex2.ToString()); } throw UiException.Aide.wrap(ex); } return dlg.getResult(); } else { dlg.doHighlighted(); return OK; } } }
3. Sample Code:
String object = (String)InputMessageBox.showQuestion("MY QUESTION?", "MY APPLICATION TITLE");
System.out.println("RESULT: " + object);
Looks great Marcos :-)
This is interessting and brings new ideas.
To take the zul only as template and in the controllers showQuestion() methode
add new components to the parent.
Stephan
hi, I should set a textbox on a popup message, which must then be handled by my controller. I'm replicating this code on my app, but at runtime I get this error:
final MessageboxDlg dlg = (MessageboxDlg) Executions.createComponents(_templ, null, params);
my error:
E org.zkoss.zk.ui.impl.UiEngineImpl handleError:1281 >>java.lang.ClassCastException: org.zkoss.zul.impl.MessageboxDlg incompatible with org.zkoss.zk.ui.util.Composer
at org.zkoss.zk.ui.metainfo.ComponentInfo.toComposer(ComponentInfo.java:410)
why?
Asked: 2009-07-10 17:23:56 +0800
Seen: 2,134 times
Last updated: Jul 01 '13