001: /* AuResponse.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue May 31 11:35:49 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zk.au;
020:
021: import java.util.Map;
022: import java.util.HashMap;
023:
024: import org.zkoss.zk.ui.Component;
025: import org.zkoss.zk.ui.Page;
026:
027: /**
028: * A response sent from the server to the client via
029: * {@link org.zkoss.zk.ui.sys.UiEngine}.
030: *
031: * <p>Application developers rarely need access this class and its derived
032: * directly.
033: * Rather, use {@link org.zkoss.zk.ui.util.Clients} instead.
034: * If you prefer to use the derives directly, you can use them with
035: * {@link org.zkoss.zk.ui.Execution#addAuResponse}.
036: *
037: * @author tomyeh
038: */
039: public class AuResponse {
040: protected String _cmd;
041: private final Object _depends;
042: protected String[] _data;
043:
044: /** Constructs a component-independent response.
045: */
046: protected AuResponse(String cmd) {
047: this (cmd, (Component) null, (String[]) null);
048: }
049:
050: /** Constructs a component-independent response.
051: */
052: protected AuResponse(String cmd, String data) {
053: this (cmd, (Component) null, data);
054: }
055:
056: /** Constructs a component-independent response.
057: */
058: protected AuResponse(String cmd, String[] data) {
059: this (cmd, (Component) null, data);
060: }
061:
062: /** Constructs a response with one or zero data.
063: *
064: * @param depends specifies whether this response depends on whether
065: * the depends component.
066: * If depends is not null, this response shall be purged if the depends
067: * component is removed.
068: * If null, this response is called component-independent, and
069: * always sent to the client.
070: *
071: * <p>Note: info of the depends component doesn't send to the client.
072: * It is used only to optimize what responses to send.
073: *
074: * @param data specifies the data to be sent. If null, no data at all.
075: */
076: protected AuResponse(String cmd, Component depends, String data) {
077: this (cmd, depends, data != null ? new String[] { data } : null);
078: }
079:
080: /** Constructs a response with multiple data.
081: */
082: protected AuResponse(String cmd, Component depends, String[] data) {
083: if (cmd == null || cmd.length() == 0)
084: throw new IllegalArgumentException("cmd");
085: _cmd = cmd;
086: _depends = depends;
087: _data = data;
088: }
089:
090: /** Constructs a response with multiple data.
091: */
092: protected AuResponse(String cmd, Page depends, String data) {
093: this (cmd, depends, data != null ? new String[] { data } : null);
094: }
095:
096: /** Constructs a response with multiple data.
097: */
098: protected AuResponse(String cmd, Page depends, String[] data) {
099: if (cmd == null || cmd.length() == 0)
100: throw new IllegalArgumentException("cmd");
101: _cmd = cmd;
102: _depends = depends;
103: _data = data;
104: }
105:
106: /** Returns the command of this response (never null).
107: */
108: public String getCommand() {
109: return _cmd;
110: }
111:
112: /** Returns the associated data of this response (might be null).
113: */
114: public String[] getData() {
115: return _data;
116: }
117:
118: /** Returns the component or page that this response depends on.
119: * If it is not null and the depends component/page is removed,
120: * this response shall be removed, too.
121: *
122: * <p>Note: the returned object is either a {@link Component} or a
123: * {@link Page}.
124: */
125: public final Object getDepends() {
126: return _depends;
127: }
128:
129: //-- Object --//
130: public final boolean equals(Object o) { //prevent override
131: return this == o;
132: }
133:
134: public String toString() {
135: //Don't call getCommand and getData since it causes
136: //AuSetDeferredAttribute to evaluate the deferred value
137: final StringBuffer sb = new StringBuffer(60).append("[cmd=")
138: .append(_cmd);
139: if (_data != null && _data.length > 0) {
140: sb.append(", data0=").append(_data[0]);
141: if (_data.length > 1) {
142: sb.append(", data1=").append(trimOutput(_data[1]));
143: if (_data.length > 2)
144: sb.append(", data2=").append(trimOutput(_data[2]));
145: }
146: }
147: return sb.append(']').toString();
148: }
149:
150: private static String trimOutput(String s) {
151: if (s == null)
152: return null;
153: s = s.trim();
154: return s.length() <= 36 ? s : s.substring(0, 36) + "...";
155: }
156: }
|