001: /* Image.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: May 22, 2007 6:42:15 PM, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 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:
020: package org.zkoss.mil;
021:
022: import org.zkoss.lang.Objects;
023: import org.zkoss.lang.Strings;
024: import org.zkoss.xml.HTMLs;
025: import org.zkoss.zk.ui.Desktop;
026:
027: /**
028: * The Image item component that can be layout under the {@link Frame}.
029: * @author henrichen
030: */
031: public class Image extends Item {
032: private static final long serialVersionUID = 200705231321L;
033:
034: private String _src;
035: private org.zkoss.image.Image _image;
036: private int _imgver;
037:
038: /** Returns the source URI of the image.
039: * <p>Default: null.
040: */
041: public String getSrc() {
042: return _src;
043: }
044:
045: /** Sets the source URI of the image.
046: *
047: * <p>If {@link #setContent} is ever called with non-null,
048: * it takes heigher priority than this method.
049: *
050: * @param src the URI of the image source
051: */
052: public void setSrc(String src) {
053: if (src != null && src.length() == 0)
054: src = null;
055:
056: if (!Objects.equals(_src, src)) {
057: _src = src;
058: if (_image == null)
059: smartUpdate("im", getEncodedSrc());
060: //_src is meaningful only if _image is null
061: }
062: }
063:
064: /** Returns the encoded src ({@link #getSrc}).
065: */
066: private String getEncodedSrc() {
067: final Desktop dt = getDesktop(); //it might not belong to any desktop
068: return _image != null ? getContentSrc() : //already encoded
069: dt != null ? dt.getExecution().encodeURL(
070: _src != null ? _src : "~./img/spacer.gif") : "";
071: }
072:
073: /** Sets the content directly.
074: * Default: null.
075: *
076: * @param image the image to display. If not null, it has higher
077: * priority than {@link #getSrc}.
078: */
079: public void setContent(org.zkoss.image.Image image) {
080: if (image != _image) {
081: _image = image;
082: if (_image != null)
083: ++_imgver; //enforce browser to reload image
084: smartUpdate("im", getEncodedSrc());
085: }
086: }
087:
088: /** Returns the content set by {@link #setContent}.
089: * <p>Note: it won't fetch what is set thru by {@link #setSrc}.
090: * It simply returns what is passed to {@link #setContent}.
091: */
092: public org.zkoss.image.Image getContent() {
093: return _image;
094: }
095:
096: public String getInnerAttrs() {
097: final StringBuffer sb = new StringBuffer(64).append(super
098: .getInnerAttrs());
099: HTMLs.appendAttribute(sb, "im", getEncodedSrc());
100: return sb.toString();
101: }
102:
103: /** Returns the encoded URL for the current image content.
104: * Don't call this method unless _image is not null;
105: *
106: * <p>Used only for component template, not for application developers.
107: */
108: private String getContentSrc() {
109: final Desktop desktop = getDesktop();
110: if (desktop == null)
111: return ""; //no avail at client
112:
113: final StringBuffer sb = new StringBuffer(64).append('/');
114: Strings.encode(sb, _imgver);
115: final String name = _image.getName();
116: final String format = _image.getFormat();
117: if (name != null || format != null) {
118: sb.append('/');
119: boolean bExtRequired = true;
120: if (name != null && name.length() != 0) {
121: sb.append(name);
122: bExtRequired = name.lastIndexOf('.') < 0;
123: } else {
124: sb.append(getId());
125: }
126: if (bExtRequired && format != null)
127: sb.append('.').append(format);
128: }
129: return desktop.getDynamicMediaURI(this , sb.toString()); //already encoded
130: }
131: }
|