001: /* Iframe.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Jul 21 11:11:18 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.ext.render.DynamicMedia;
029:
030: import org.zkoss.zul.impl.XulElement;
031: import org.zkoss.zul.impl.Utils;
032:
033: /**
034: * Includes an inline frame.
035: *
036: * @author tomyeh
037: */
038: public class Iframe extends XulElement {
039: private String _align, _name;
040: private String _src;
041: /** The media. If not null, _src is generated automatically. */
042: private Media _media;
043: /** Count the version of {@link #_media}. */
044: private int _medver;
045: /** Whether to hide when a popup or dropdown is placed on top of it. */
046: private boolean _autohide;
047:
048: public Iframe() {
049: }
050:
051: public Iframe(String src) {
052: setSrc(src);
053: }
054:
055: /** Returns the alignment.
056: * <p>Default: null (use browser default).
057: */
058: public String getAlign() {
059: return _align;
060: }
061:
062: /** Sets the alignment: one of top, middle, bottom, left, right and
063: * center.
064: */
065: public void setAlign(String align) {
066: if (align != null && align.length() == 0)
067: align = null;
068:
069: if (!Objects.equals(_align, align)) {
070: _align = align;
071: smartUpdate("align", _align);
072: }
073: }
074:
075: /** Returns the frame name.
076: * <p>Default: null (use browser default).
077: */
078: public String getName() {
079: return _name;
080: }
081:
082: /** Sets the frame name.
083: */
084: public void setName(String name) {
085: if (name != null && name.length() == 0)
086: name = null;
087:
088: if (!Objects.equals(_name, name)) {
089: _name = name;
090: smartUpdate("name", _name);
091: }
092: }
093:
094: /** Returns whether to automatically hide this component if
095: * a popup or dropdown is overlapped with it.
096: *
097: * <p>Default: false.
098: *
099: * <p>If an iframe contains PDF or other embeds, it will be placed
100: * on top of other components. It may then make popups and dropdowns
101: * obscure. In this case, you have to specify autohide="true" to
102: * ask ZK to hide the iframe when popups or dropdowns is overlapped
103: * with the iframe.
104: */
105: public boolean isAutohide() {
106: return _autohide;
107: }
108:
109: /** Sets whether to automatically hide this component if
110: * a popup or dropdown is overlapped with it.
111: */
112: public void setAutohide(boolean autohide) {
113: if (_autohide != autohide) {
114: _autohide = autohide;
115: smartUpdate("z.autohide", "true");
116: }
117: }
118:
119: /** Returns the src.
120: * <p>Default: null.
121: */
122: public String getSrc() {
123: return _src;
124: }
125:
126: /** Sets the src.
127: * <p>If src is changed, the whole component is invalidate.
128: * Thus, you want to smart-update, you have to override this method.
129: *
130: * @param src the source URL. If null or empty, nothing is included.
131: */
132: public void setSrc(String src) {
133: if (src != null && src.length() == 0)
134: src = null;
135:
136: if (!Objects.equals(_src, src)) {
137: _src = src;
138: if (_media == null)
139: smartUpdateDeferred("src", new EncodedSrc()); //Bug 1850895
140: //_src is meaningful only if _media is null
141: }
142: }
143:
144: /** Returns the encoded src ({@link #getSrc}).
145: */
146: private String getEncodedSrc() {
147: final Desktop dt = getDesktop();
148: return _media != null ? getMediaSrc() : //already encoded
149: dt != null ? dt.getExecution().encodeURL(
150: _src != null ? _src : "~./img/spacer.gif") : "";
151: }
152:
153: /** Sets the content directly.
154: * Default: null.
155: *
156: * @param media the media for this inline frame.
157: * If not null, it has higher priority than {@link #getSrc}.
158: */
159: public void setContent(Media media) {
160: if (media != _media) {
161: _media = media;
162: if (_media != null)
163: ++_medver; //enforce browser to reload
164: smartUpdateDeferred("src", new EncodedSrc()); //Bug 1850895
165: }
166: }
167:
168: /** Returns the content set by {@link #setContent}.
169: * <p>Note: it won't fetch what is set thru by {@link #setSrc}.
170: * It simply returns what is passed to {@link #setContent}.
171: */
172: public Media getContent() {
173: return _media;
174: }
175:
176: /** Returns the encoded URL for the current media content.
177: * Don't call this method unless _media is not null;
178: */
179: private String getMediaSrc() {
180: return Utils.getDynamicMediaURI(this , _medver,
181: _media.getName(), _media.getFormat());
182: }
183:
184: //-- super --//
185: public String getOuterAttrs() {
186: final StringBuffer sb = new StringBuffer(64).append(super
187: .getOuterAttrs());
188: HTMLs.appendAttribute(sb, "align", _align);
189: HTMLs.appendAttribute(sb, "name", _name);
190: HTMLs.appendAttribute(sb, "src", getEncodedSrc());
191: if (_autohide)
192: HTMLs.appendAttribute(sb, "z.autohide", _autohide);
193: return sb.toString();
194: }
195:
196: //-- Component --//
197: /** Default: not childable.
198: */
199: public boolean isChildable() {
200: return false;
201: }
202:
203: //-- ComponentCtrl --//
204: protected Object newExtraCtrl() {
205: return new ExtraCtrl();
206: }
207:
208: /** A utility class to implement {@link #getExtraCtrl}.
209: * It is used only by component developers.
210: */
211: protected class ExtraCtrl extends XulElement.ExtraCtrl implements
212: DynamicMedia {
213: //-- DynamicMedia --//
214: public Media getMedia(String pathInfo) {
215: return _media;
216: }
217: }
218:
219: private class EncodedSrc implements
220: org.zkoss.zk.ui.util.DeferredValue {
221: public String getValue() {
222: return getEncodedSrc();
223: }
224: }
225: }
|