001: /* Audio.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Nov 16 11:48:27 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.zul;
020:
021: import org.zkoss.lang.Objects;
022: import org.zkoss.util.media.Media;
023: import org.zkoss.xml.HTMLs;
024:
025: import org.zkoss.zk.ui.Desktop;
026: import org.zkoss.zk.ui.Execution;
027: import org.zkoss.zk.ui.UiException;
028: import org.zkoss.zk.ui.WrongValueException;
029: import org.zkoss.zk.ui.ext.render.DynamicMedia;
030: import org.zkoss.zk.au.out.AuInvoke;
031:
032: import org.zkoss.zul.impl.XulElement;
033: import org.zkoss.zul.impl.Utils;
034:
035: /**
036: * An audio clip.
037: *
038: * <p>An extension to XUL.
039: *
040: * @author tomyeh
041: */
042: public class Audio extends XulElement {
043: private String _align, _border;
044: protected String _src;
045: /** The audio. If not null, _src is generated automatically. */
046: private org.zkoss.sound.Audio _audio;
047: /** Count the version of {@link #_audio}. */
048: private int _audver;
049: private boolean _autostart, _loop;
050:
051: public Audio() {
052: }
053:
054: public Audio(String src) {
055: setSrc(src);
056: }
057:
058: /** Plays the audio at the client.
059: */
060: public void play() {
061: response("ctrl", new AuInvoke(this , "play"));
062: }
063:
064: /** Stops the audio at the cient.
065: */
066: public void stop() {
067: response("ctrl", new AuInvoke(this , "stop"));
068: }
069:
070: /** Pauses the audio at the cient.
071: */
072: public void pause() {
073: response("ctrl", new AuInvoke(this , "pause"));
074: }
075:
076: /** Returns the alignment.
077: * <p>Default: null (use browser default).
078: */
079: public String getAlign() {
080: return _align;
081: }
082:
083: /** Sets the alignment: one of top, texttop, middle, absmiddle,
084: * bottom, absbottom, baseline, left, right and center.
085: */
086: public void setAlign(String align) throws WrongValueException {
087: if (align != null && align.length() == 0)
088: align = null;
089:
090: if (!Objects.equals(_align, align)) {
091: _align = align;
092: smartUpdate("align", _align);
093: }
094: }
095:
096: /** Returns the width of the border.
097: * <p>Default: null (use browser default).
098: */
099: public String getBorder() {
100: return _border;
101: }
102:
103: /** Sets the width of the border.
104: */
105: public void setBorder(String border) throws WrongValueException {
106: if (border != null && border.length() == 0)
107: border = null;
108:
109: if (!Objects.equals(_border, border)) {
110: _border = border;
111: smartUpdate("border", _border);
112: }
113: }
114:
115: /** Returns the src.
116: * <p>Default: null.
117: */
118: public String getSrc() {
119: return _src;
120: }
121:
122: /** Sets the src.
123: *
124: * <p>If {@link #setContent} is ever called with non-null,
125: * it takes heigher priority than this method.
126: */
127: public void setSrc(String src) {
128: if (src != null && src.length() == 0)
129: src = null;
130:
131: if (!Objects.equals(_src, src)) {
132: _src = src;
133: if (_audio == null)
134: updateSrc();
135: //_src is meaningful only if _audio is null
136: }
137: }
138:
139: private String getEncodedSrc() {
140: final Desktop dt = getDesktop();
141: return _audio != null ? getAudioSrc() : //already encoded
142: dt != null ? dt.getExecution().encodeURL(
143: _src != null ? _src : "~./aud/mute.mid") : "";
144: }
145:
146: /** Returns whether to auto start playing the audio.
147: *
148: * <p>Default: false;
149: */
150: public final boolean isAutostart() {
151: return _autostart;
152: }
153:
154: /** Sets whether to auto start playing the audio.
155: */
156: public final void setAutostart(boolean autostart) {
157: if (_autostart != autostart) {
158: _autostart = autostart;
159: smartUpdate("autostart", _autostart);
160: }
161: }
162:
163: /** Sets the content directly.
164: * Default: null.
165: *
166: * @param audio the audio to display. If not null, it has higher
167: * priority than {@link #getSrc}.
168: */
169: public void setContent(org.zkoss.sound.Audio audio) {
170: if (audio != _audio) {
171: _audio = audio;
172: if (_audio != null)
173: ++_audver; //enforce browser to reload
174: updateSrc();
175: }
176: }
177:
178: private void updateSrc() {
179: invalidate();
180: //IE won't work if we only change the src attribute
181: }
182:
183: /** Returns the content set by {@link #setContent}.
184: * <p>Note: it won't fetch what is set thru by {@link #setSrc}.
185: * It simply returns what is passed to {@link #setContent}.
186: */
187: public org.zkoss.sound.Audio getContent() {
188: return _audio;
189: }
190:
191: /** Returns the encoded URL for the current audio content.
192: * Don't call this method unless _audio is not null;
193: */
194: private String getAudioSrc() {
195: return Utils.getDynamicMediaURI(this , _audver,
196: _audio.getName(), _audio.getFormat());
197: }
198:
199: //-- super --//
200: public String getOuterAttrs() {
201: final StringBuffer sb = new StringBuffer(64).append(super
202: .getOuterAttrs());
203: HTMLs.appendAttribute(sb, "src", getEncodedSrc());
204: HTMLs.appendAttribute(sb, "autostart", _autostart);
205: HTMLs.appendAttribute(sb, "loop", _loop);
206: HTMLs.appendAttribute(sb, "align", _align);
207: HTMLs.appendAttribute(sb, "border", _border);
208: return sb.toString();
209: }
210:
211: //-- Component --//
212: /** Default: not childable.
213: */
214: public boolean isChildable() {
215: return false;
216: }
217:
218: //-- ComponentCtrl --//
219: protected Object newExtraCtrl() {
220: return new ExtraCtrl();
221: }
222:
223: /** A utility class to implement {@link #getExtraCtrl}.
224: * It is used only by component developers.
225: */
226: protected class ExtraCtrl extends XulElement.ExtraCtrl implements
227: DynamicMedia {
228: //-- DynamicMedia --//
229: public Media getMedia(String pathInfo) {
230: return _audio;
231: }
232: }
233: }
|