001: /* Script.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Sun Oct 15 12:15:47 2006, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: }}IS_RIGHT
016: */
017: package org.zkoss.zul;
018:
019: import java.util.Iterator;
020:
021: import org.zkoss.lang.Objects;
022: import org.zkoss.xml.HTMLs;
023:
024: import org.zkoss.zk.ui.AbstractComponent;
025: import org.zkoss.zk.ui.UiException;
026:
027: /**
028: * A component to represent script codes running at the client.
029: * It is the same as HTML SCRIPT tag.
030: *
031: * <p>Note: it is the scripting codes running at the client, not at the
032: * server. Don't confuse it with the <code>zscript</code> element.
033: *
034: * <p>To avoid typo, this compnent requires you to specify the type
035: * ({@link #setType}) -- which is, if JavaScript, "text/javascript".
036: *
037: * <p>There are three formats when used in a ZUML page:
038: *
039: * <p>Method 1: Specify the URL of the JS file
040: * <pre><code><script type="text/javascript" src="my.js"/>
041: * </code></pre>
042: *
043: * <p>Method 2: Specify the JavaScript codes directly
044: * <pre><code><script type="text/javascript">
045: * some_js_at_browser();
046: *</script>
047: * </code></pre>
048: *
049: * <p>Method 3: Specify the JavaScript codes by use of the content
050: * property ({@link #setContent}).
051: * <pre><code><script type="text/javascript">
052: * <attribute name="content">
053: * some_js_at_browser();
054: * </attribute>
055: *</script>
056: * </code></pre>
057: *
058: * @author tomyeh
059: */
060: public class Script extends AbstractComponent {
061: private String _src, _type, _charset;
062: private String _content;
063: private boolean _defer;
064:
065: public Script() {
066: }
067:
068: /** Returns the type of this client script.
069: * <p>Default: null. However, it is invalid.
070: * In other words, you must specify a correct type.
071: * For JavaScript, it is "text/javascript".
072: */
073: public String getType() {
074: return _type;
075: }
076:
077: /** Sets the type of this client script.
078: * For JavaScript, it is <code>text/javascript</code>
079: *
080: * <p>Note: this property is NOT optional. You must specify one.
081: */
082: public void setType(String type) {
083: if (type == null || type.length() == 0)
084: throw new IllegalArgumentException("non-empty is required");
085:
086: if (!Objects.equals(_type, type)) {
087: _type = type;
088: invalidate();
089: }
090: }
091:
092: /** Returns the character enconding of the source.
093: * It is used with {@link #getSrc}.
094: *
095: * <p>Default: null.
096: */
097: public String getCharset() {
098: return _charset;
099: }
100:
101: /** Sets the character encoding of the source.
102: * It is used with {@link #setSrc}.
103: */
104: public void setCharset(String charset) {
105: if (charset != null && charset.length() == 0)
106: charset = null;
107:
108: if (!Objects.equals(_charset, charset)) {
109: _charset = charset;
110: invalidate();
111: }
112: }
113:
114: /** Returns the URI of the source that contains the script codes.
115: * <p>Default: null.
116: */
117: public String getSrc() {
118: return _src;
119: }
120:
121: /** Sets the URI of the source that contains the script codes.
122: *
123: * <p>You either add the script codes directly with the {@link Label}
124: * children, or
125: * set the URI to load the script codes with {@link #setSrc}.
126: * But, not both.
127: *
128: * @param src the URI of the source that contains the script codes
129: */
130: public void setSrc(String src) {
131: if (src != null && src.length() == 0)
132: src = null;
133:
134: if (!Objects.equals(_src, src)) {
135: _src = src;
136: invalidate();
137: }
138: }
139:
140: /** Returns whether to defer the execution of the script codes.
141: *
142: * <p>Default: false.
143: */
144: public boolean isDefer() {
145: return _defer;
146: }
147:
148: /** Sets whether to defer the execution of the script codes.
149: */
150: public void setDefer(boolean defer) {
151: if (_defer != defer) {
152: _defer = defer;
153: invalidate();
154: }
155: }
156:
157: /** Returns the content of the script element.
158: * By content we mean the JavaScript codes that will be enclosed
159: * by the HTML SCRIPT element.
160: *
161: * <p>Default: null.
162: *
163: * @since 3.0.0
164: */
165: public String getContent() {
166: return _content;
167: }
168:
169: /** Sets the content of the script element.
170: * By content we mean the JavaScript codes that will be enclosed
171: * by the HTML SCRIPT element.
172: *
173: * @since 3.0.0
174: */
175: public void setContent(String content) {
176: if (content != null && content.length() == 0)
177: content = null;
178:
179: if (!Objects.equals(_content, content)) {
180: _content = content;
181: invalidate();
182: }
183: }
184:
185: //-- Component --//
186: /** Not childable. */
187: public boolean isChildable() {
188: return false;
189: }
190:
191: public void redraw(java.io.Writer out) throws java.io.IOException {
192: if (_type == null)
193: throw new UiException(
194: "The type is required. For example, text/javascript");
195:
196: final StringBuffer sb = new StringBuffer(256)
197: .append("\n<script");
198: HTMLs.appendAttribute(sb, "id", getUuid());
199: HTMLs.appendAttribute(sb, "type", _type);
200: HTMLs.appendAttribute(sb, "charset", _charset);
201:
202: if (_src != null)
203: HTMLs.appendAttribute(sb, "src", getDesktop()
204: .getExecution().encodeURL(_src));
205:
206: if (_defer)
207: sb.append(" defer=\"defer\"");
208:
209: out.write(sb.append(">\n").toString());
210:
211: final String content = getContent();
212: if (content != null) {
213: out.write(content);
214: out.write('\n');
215: }
216:
217: out.write("</script>");
218: }
219: }
|