Class Clients

java.lang.Object
org.zkoss.zk.ui.util.Clients

public class Clients extends Object
Utilities to send AuResponse to the client.

Utilities here are mainly to control how the client (a.k.a., the browser window) behaves. To get the status, you might refer to ClientInfoEvent. Note: this class makes accessing 'client-side' more directly. Methods such as showBusy(String) or showNotification(String) will not encode the strings passed into them, thus the formatting of messages at 'client-side' is allowed. User input should be escaped carefully especially when using evalJavaScript(String) to execute dynamically concatenated JavaScript code.

Author:
tomyeh
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Notification type: error
    static final String
    Notification type: information
    static final String
    Notification type: warning
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final void
    Shows an error message at the browser.
    static final void
    alert(String msg, String title, String icon)
    Shows an error message at the browser.
    static final void
    Cleans the busy message at the browser.
    static final void
    Clears the busy message at the browser that covers only the specified component.
    static final void
    Closes the error message of the specified components, if any, at the browser.
    static final void
    Closes the error message of the specified component, if any, at the browser.
    static final void
    Closes the error message of the specified components, if any, at the browser.
    static final void
    Asks the browser to confirm users whether to close the browser window.
    static final void
    evalJavaScript(String javaScript)
    Asks the browser to evaluate the specified JavaScript.
    static final void
    focus(String selector)
    Focus the first matching component (HtmlBasedComponent) by using selector Note: The Selector will locate the components in the first page of current Desktop.
    static final void
    focus(Component component)
    Focus the component (HtmlBasedComponent)
    static final void
    Loads a CSS file.
    static final void
    Loads a JavaScript file to client and execute it.
    static final void
    loadScript(String url, String callback, boolean cache)
    Loads a JavaScript file to client and execute it.
    static final void
    log(Object msg)
    Logs the message to the client.
    static final void
    log(String msg)
    Logs the message to the client.
    static final void
    moveBy(int x, int y)
    Moves the current desktop (a.k.a., browser window) by the specified number of pixels.
    static final void
    moveTo(int x, int y)
    Moves the current desktop (a.k.a., browser window) to the specified location (in pixels).
    static void
    Asks the client to print the current desktop (a.k.a., browser window).
    static final void
    Reloads the client-side messages in the specified locale.
    static final void
    Forces the client to re-calculate the size of the given component.
    static final void
    resizeBy(int x, int y)
    Resizes the current desktop (a.k.a., browser window) by the specified number of pixels.
    static final void
    resizeTo(int x, int y)
    Resizes the current desktop (a.k.a., browser window) to the specified size (in pixels).
    static final void
    response(String key, AuResponse response)
    Sends an AU response (AuResponse) to the client with the given key (instead of AuResponse.getOverrideKey()).
    static final void
    response(AuResponse response)
    Sends an AU response (AuResponse) to the client.
    static final void
    scrollBy(int x, int y)
    Scrolls the current desktop (a.k.a., browser window) by the specified number of pixels.
    static final void
    Scrolls the ancestor elements to make the selector matched element visible Note: The Selector will locate the components in the first page of current Desktop.
    static final void
    Scrolls the ancestor elements to make the specified element visible.
    static final void
    scrollTo(int x, int y)
    Scrolls the current desktop (a.k.a., browser window) to the specified location (in pixels).
    static final void
    sendClientCommand(Component component, String commandName, Object data)
    Send a command to client (afterCommand)
    static final void
    Shows the busy message at the browser such that the user knows the system is busy.
    static final void
    Shows the busy message at the browser that covers only the specified component.
    static final void
    Shows a message at the center of the browser window.
    static final void
    showNotification(String msg, boolean closable)
    Shows a message at the center of the browser window.
    static final void
    showNotification(String msg, String type, Component ref, int x, int y, int duration)
    Displays a message.
    static final void
    showNotification(String msg, String type, Component ref, int x, int y, int duration, boolean closable)
    Displays a message.
    static final void
    showNotification(String msg, String type, Component ref, String position, int duration)
    Displays a message.
    static final void
    showNotification(String msg, String type, Component ref, String position, int duration, boolean closable)
    Displays a message.
    static final void
    Shows a message at the right side of the given component.
    static final void
    showNotification(String msg, Component ref, boolean closable)
    Shows a message at the right side of the given component.
    static final void
    Submits the form with the specified ID.
    static final void
    Submits the form with the specified form.
    static final void
    Send a command to sync all the errorboxes and its reference component position on the desktop.
    static final void
    Send a command to sync a target component and its errorbox position.
    static final void
    Shows an error message for the specified component, if any, at the browser.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NOTIFICATION_TYPE_INFO

      public static final String NOTIFICATION_TYPE_INFO
      Notification type: information
      See Also:
    • NOTIFICATION_TYPE_WARNING

      public static final String NOTIFICATION_TYPE_WARNING
      Notification type: warning
      See Also:
    • NOTIFICATION_TYPE_ERROR

      public static final String NOTIFICATION_TYPE_ERROR
      Notification type: error
      See Also:
  • Constructor Details

    • Clients

      public Clients()
  • Method Details

    • response

      public static final void response(AuResponse response)
      Sends an AU response (AuResponse) to the client. It is the same as response(response.getOverrideKey(), response).
      Since:
      3.0.0
    • response

      public static final void response(String key, AuResponse response)
      Sends an AU response (AuResponse) to the client with the given key (instead of AuResponse.getOverrideKey()).
      Parameters:
      key - could be anything. The second invocation of this method in the same execution with the same key will override the previous one. In other words, the previous one will be dropped. If null is specified, the response is simply appended to the end without overriding any previous one.
      Since:
      3.0.4
      See Also:
    • confirmClose

      public static final void confirmClose(String mesg)
      Asks the browser to confirm users whether to close the browser window.

      If an non-null (non-empty) string is set, the browser will show up a confirmation dialog when an user tries to close the browser window, or browse to another URL. To reset (i.e., not showing any confirmation dialog), just call this method again with null.

      Parameters:
      mesg - the message to show when confirming users. If null (default) or empty, users can close the browser window directly.
    • alert

      public static final void alert(String msg)
      Shows an error message at the browser. It works and looks similar to Messagebox. However, it is not customizable (at the server), but it is much faster and light-weighted.
      Since:
      5.0.3
    • alert

      public static final void alert(String msg, String title, String icon)
      Shows an error message at the browser. It is similar to Messagebox.
      Parameters:
      msg - the message to display.
      title - the title of the message box
      icon - the icon to show. It could null, "QUESTION", "EXCLAMATION", "INFORMATION", "ERROR", "NONE". If null, "ERROR" is assumed
      Since:
      5.0.3
    • wrongValue

      public static final void wrongValue(Component comp, String msg)
      Shows an error message for the specified component, if any, at the browser.

      You have to clear the error message manually with clearWrongValue(org.zkoss.zk.ui.Component).

      Since:
      5.0.0
    • clearWrongValue

      public static final void clearWrongValue(Component comp)
      Closes the error message of the specified component, if any, at the browser.
      Since:
      5.0.0
    • clearWrongValue

      public static final void clearWrongValue(List<Component> comps)
      Closes the error message of the specified components, if any, at the browser.
      Since:
      5.0.0
    • clearWrongValue

      public static final void clearWrongValue(Component[] comps)
      Closes the error message of the specified components, if any, at the browser.
      Since:
      5.0.0
    • submitForm

      public static final void submitForm(String formId)
      Submits the form with the specified ID.
    • submitForm

      public static final void submitForm(Component form)
      Submits the form with the specified form. It assumes the form component is a HTML form.
    • print

      public static void print()
      Asks the client to print the current desktop (a.k.a., browser window).
    • scrollIntoView

      public static final void scrollIntoView(Component cmp)
      Scrolls the ancestor elements to make the specified element visible.
      Since:
      3.6.1
    • scrollIntoView

      public static final void scrollIntoView(String selector)
      Scrolls the ancestor elements to make the selector matched element visible Note: The Selector will locate the components in the first page of current Desktop.
      Parameters:
      selector - the selector string
      Since:
      9.5.0
      See Also:
    • scrollBy

      public static final void scrollBy(int x, int y)
      Scrolls the current desktop (a.k.a., browser window) by the specified number of pixels. If the number passed is positive, the desktop is scrolled down. If negative, it is scrolled up.
      See Also:
    • scrollTo

      public static final void scrollTo(int x, int y)
      Scrolls the current desktop (a.k.a., browser window) to the specified location (in pixels).
      See Also:
    • resizeBy

      public static final void resizeBy(int x, int y)
      Resizes the current desktop (a.k.a., browser window) by the specified number of pixels. If the numbers passed are positive, the desktop size is increased. Negative numbers reduce the size of the desktop.
      See Also:
    • resizeTo

      public static final void resizeTo(int x, int y)
      Resizes the current desktop (a.k.a., browser window) to the specified size (in pixels).
      See Also:
    • moveBy

      public static final void moveBy(int x, int y)
      Moves the current desktop (a.k.a., browser window) by the specified number of pixels. If the number passed is positive, the desktop is moved down. If negative, it is moved up.
      See Also:
    • moveTo

      public static final void moveTo(int x, int y)
      Moves the current desktop (a.k.a., browser window) to the specified location (in pixels).
      See Also:
    • evalJavaScript

      public static final void evalJavaScript(String javaScript)
      Asks the browser to evaluate the specified JavaScript.

      It has no effect if the client doesn't support JavaScript.

      Parameters:
      javaScript - the javaScript codes to run at the browser
    • showNotification

      public static final void showNotification(String msg)
      Shows a message at the center of the browser window.
      Parameters:
      msg - the message to show
      Since:
      6.0.1
      See Also:
    • showNotification

      public static final void showNotification(String msg, boolean closable)
      Shows a message at the center of the browser window.
      Parameters:
      msg - the message to show
      closable - whether to close notification manually or not. If true there will be a close button on notification message and won't close until user click the button or duration time up, default false.
      Since:
      6.5.0
      See Also:
    • showNotification

      public static final void showNotification(String msg, Component ref)
      Shows a message at the right side of the given component.
      Parameters:
      msg - the message to show
      ref - the referenced component, null to be based on browser window
      Since:
      6.0.1
      See Also:
    • showNotification

      public static final void showNotification(String msg, Component ref, boolean closable)
      Shows a message at the right side of the given component.
      Parameters:
      msg - the message to show
      ref - the referenced component, null to be based on browser window
      closable - whether to close notification manually or not. If true there will be a close button on notification message and won't close until user click the button or duration time up, default false.
      Since:
      6.5.0
      See Also:
    • showNotification

      public static final void showNotification(String msg, String type, Component ref, String position, int duration)
      Displays a message.
      Parameters:
      msg - the message to show
      type - available types are "info", "warning", "error"
      ref - the referenced component, null to be based on browser window
      position - predefined positions.

      Available options are:

      • before_start
        the message appears above the anchor, aligned to the left.
      • before_center
        the message appears above the anchor, aligned to the center.
      • before_end
        the message appears above the anchor, aligned to the right.
      • after_start
        the message appears below the anchor, aligned to the left.
      • after_center
        the message appears below the anchor, aligned to the center.
      • after_end
        the message appears below the anchor, aligned to the right.
      • start_before
        the message appears to the left of the anchor, aligned to the top.
      • start_center
        the message appears to the left of the anchor, aligned to the middle.
      • start_after
        the message appears to the left of the anchor, aligned to the bottom.
      • end_before
        the message appears to the right of the anchor, aligned to the top.
      • end_center
        the message appears to the right of the anchor, aligned to the middle.
      • end_after
        the message appears to the right of the anchor, aligned to the bottom.
      • overlap/top_left
        the message overlaps the anchor, with anchor and message aligned at top-left.
      • top_center
        the message overlaps the anchor, with anchor and message aligned at top-center.
      • overlap_end/top_right
        the message overlaps the anchor, with anchor and message aligned at top-right.
      • middle_left
        the message overlaps the anchor, with anchor and message aligned at middle-left.
      • middle_center
        the message overlaps the anchor, with anchor and message aligned at middle-center.
      • middle_right
        the message overlaps the anchor, with anchor and message aligned at middle-right.
      • overlap_before/bottom_left
        the message overlaps the anchor, with anchor and message aligned at bottom-left.
      • bottom_center
        the message overlaps the anchor, with anchor and message aligned at bottom-center.
      • overlap_after/bottom_right
        the message overlaps the anchor, with anchor and message aligned at bottom-right.
      • at_pointer
        the message appears with the upper-left aligned with the mouse cursor.
      • after_pointer
        the message appears with the top aligned with the bottom of the mouse cursor, with the left side of the message at the horizontal position of the mouse cursor.

      duration - the duration of notification in millisecond. If zero or negative the notification does not dismiss until user left-clicks outside of the notification box.
      Since:
      6.0.1
      See Also:
    • showNotification

      public static final void showNotification(String msg, String type, Component ref, String position, int duration, boolean closable)
      Displays a message.
      Parameters:
      msg - the message to show
      type - available types are "info", "warning", "error"
      ref - the referenced component, null to be based on browser window
      position - predefined positions.

      Available options are:

      • before_start
        the message appears above the anchor, aligned to the left.
      • before_center
        the message appears above the anchor, aligned to the center.
      • before_end
        the message appears above the anchor, aligned to the right.
      • after_start
        the message appears below the anchor, aligned to the left.
      • after_center
        the message appears below the anchor, aligned to the center.
      • after_end
        the message appears below the anchor, aligned to the right.
      • start_before
        the message appears to the left of the anchor, aligned to the top.
      • start_center
        the message appears to the left of the anchor, aligned to the middle.
      • start_after
        the message appears to the left of the anchor, aligned to the bottom.
      • end_before
        the message appears to the right of the anchor, aligned to the top.
      • end_center
        the message appears to the right of the anchor, aligned to the middle.
      • end_after
        the message appears to the right of the anchor, aligned to the bottom.
      • overlap/top_left
        the message overlaps the anchor, with anchor and message aligned at top-left.
      • top_center
        the message overlaps the anchor, with anchor and message aligned at top-center.
      • overlap_end/top_right
        the message overlaps the anchor, with anchor and message aligned at top-right.
      • middle_left
        the message overlaps the anchor, with anchor and message aligned at middle-left.
      • middle_center
        the message overlaps the anchor, with anchor and message aligned at middle-center.
      • middle_right
        the message overlaps the anchor, with anchor and message aligned at middle-right.
      • overlap_before/bottom_left
        the message overlaps the anchor, with anchor and message aligned at bottom-left.
      • bottom_center
        the message overlaps the anchor, with anchor and message aligned at bottom-center.
      • overlap_after/bottom_right
        the message overlaps the anchor, with anchor and message aligned at bottom-right.
      • at_pointer
        the message appears with the upper-left aligned with the mouse cursor.
      • after_pointer
        the message appears with the top aligned with the bottom of the mouse cursor, with the left side of the message at the horizontal position of the mouse cursor.

      duration - the duration of notification in millisecond. If zero or negative the notification does not dismiss until user left-clicks outside of the notification box.
      closable - whether to close notification manually or not. If true there will be a close button on notification message and won't close until user click the button or duration time up, default false.
      Since:
      6.5.0
      See Also:
    • showNotification

      public static final void showNotification(String msg, String type, Component ref, int x, int y, int duration)
      Displays a message.
      Parameters:
      msg - the message to show
      type - available types are "info", "warning", "error"
      ref - the referenced component, null to be based on browser window
      x - the horizontal position of the notification, aligned at top-left (in pixel)
      y - the vertical position of the notification, aligned at top-left (in pixel)
      duration - the duration of notification in millisecond. If zero or negative the notification does not dismiss until user left-clicks outside of the notification box.
      Since:
      6.0.1
      See Also:
    • showNotification

      public static final void showNotification(String msg, String type, Component ref, int x, int y, int duration, boolean closable)
      Displays a message.
      Parameters:
      msg - the message to show
      type - available types are "info", "warning", "error"
      ref - the referenced component, null to be based on browser window
      x - the horizontal position of the notification, aligned at top-left (in pixel)
      y - the vertical position of the notification, aligned at top-left (in pixel)
      duration - the duration of notification in millisecond. If zero or negative the notification does not dismiss until user left-clicks outside of the notification box.
      closable - whether to close notification manually or not. If true there will be a close button on notification message and won't close until user click the button or duration time up, default false.
      Since:
      6.5.0
      See Also:
    • showBusy

      public static final void showBusy(String msg)
      Shows the busy message at the browser such that the user knows the system is busy.

      It is usually used with Events.echoEvent(java.lang.String, org.zkoss.zk.ui.Component, java.lang.String) to prevent the user to click another buttons or components.

      To cover only a particular component, use showBusy(Component, String). To close, use clearBusy().

      Parameters:
      msg - the message to show. If null, the default message (processing) is shown.
      Since:
      5.0.0
      See Also:
    • showBusy

      public static final void showBusy(Component comp, String msg)
      Shows the busy message at the browser that covers only the specified component. It is used to denote a portion of the desktop is busy, and the user still can access the other part. In other words, it means there is a long operation taking place.

      To execute a long operation asynchronously, the developer can use a working thread, or use EventQueue.subscribe(org.zkoss.zk.ui.event.EventListener,boolean).

      See also Long Operations

      Parameters:
      comp - the component that the busy message to cover. Ignored if null. Notice that if the component is not found, the busy message won't be shown. In additions, the busy message is removed automatically if the component is detached later. To manually remove the busy message, use clearBusy(Component)
      msg - the message to show. If null, the default message (processing) is shown.
      Since:
      5.0.0
      See Also:
    • clearBusy

      public static final void clearBusy()
      Cleans the busy message at the browser.
      Since:
      5.0.0
      See Also:
    • clearBusy

      public static final void clearBusy(Component comp)
      Clears the busy message at the browser that covers only the specified component.
      Parameters:
      comp - the component that the busy message to cover.
      Since:
      5.0.0
      See Also:
    • resize

      public static final void resize(Component comp)
      Forces the client to re-calculate the size of the given component. For better performance, ZK Client Engine will cache the size of components with hflex=min or vflex=min, and recalculate it only necessary. However, sometimes it is hard (too costly) to know if the size of a component with flex=min is changed. For example, If another component is added to it and causes the size changed. In this case, you could use this method to enforce the re-calculation.
      Since:
      5.0.8
    • reloadMessages

      public static final void reloadMessages(Locale locale) throws IOException
      Reloads the client-side messages in the specified locale. It is used if you allow the user to change the locale dynamically.

      Notice that this method only reloads the standard messages. The application has to update the component's content (such as labels) manually if necessary.

      Limitation: it reloads only the messages of ZK Client Engine and ZUL components. It does not reload messages loaded by your own JavaScript codes.

      Parameters:
      locale - the locale. If null, Locales.getCurrent() is assumed.
      Throws:
      UnsupportedOperationException - if the device is not ajax.
      IOException
      Since:
      3.6.3
    • log

      public static final void log(String msg)
      Logs the message to the client.

      data[0]: the title

      Since:
      5.0.8
    • log

      public static final void log(Object msg)
      Logs the message to the client.
      Parameters:
      msg - message, cast to String
      Since:
      8.0.0
    • loadScript

      public static final void loadScript(String url)
      Loads a JavaScript file to client and execute it.

      By default, this method won't cache the JavaScript file, but you can enable it by invoking loadScript(String, String, boolean).

      Parameters:
      url - the JavasScript file path, it will be encoded with Encodes.encodeURL(jakarta.servlet.ServletContext, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, String) if any.
      Since:
      8.0.0
      See Also:
    • loadScript

      public static final void loadScript(String url, String callback, boolean cache)
      Loads a JavaScript file to client and execute it.
      Parameters:
      url - the JavasScript file path, it will be encoded with Encodes.encodeURL(jakarta.servlet.ServletContext, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, String) if any.
      callback - a callback script to execute when the JavaScript file has loaded.
      cache - true means the url will be cached and executed only once.
      Since:
      8.0.0
      See Also:
      • Encodes.encodeURL(jakarta.servlet.ServletContext, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String)
    • loadCSS

      public static final void loadCSS(String url)
      Loads a CSS file.
      Parameters:
      url - the URL of the CSS file
      Since:
      8.0.0
    • sendClientCommand

      public static final void sendClientCommand(Component component, String commandName, Object data)
      Send a command to client (afterCommand)
      Parameters:
      component - target component
      commandName - the command name which would be triggered in client
      data - data which would be sent to client
      Since:
      8.0.0
    • syncErrorbox

      public static final void syncErrorbox()
      Send a command to sync all the errorboxes and its reference component position on the desktop.
      Since:
      8.5.2
    • syncErrorbox

      public static final void syncErrorbox(Component component)
      Send a command to sync a target component and its errorbox position.
      Parameters:
      component - target component
      Since:
      8.5.2
    • focus

      public static final void focus(Component component)
      Focus the component (HtmlBasedComponent)
      Parameters:
      component - target component
      Since:
      9.5.0
    • focus

      public static final void focus(String selector)
      Focus the first matching component (HtmlBasedComponent) by using selector Note: The Selector will locate the components in the first page of current Desktop.
      Parameters:
      selector - the selector string
      Since:
      9.5.0
      See Also: