001: /* LabelImageElement.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Jul 12 12:09:00 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.impl;
020:
021: import org.zkoss.lang.Objects;
022: import org.zkoss.util.media.Media;
023: import org.zkoss.image.Image;
024:
025: import org.zkoss.zk.ui.Execution;
026: import org.zkoss.zk.ui.Desktop;
027: import org.zkoss.zk.ui.UiException;
028: import org.zkoss.zk.ui.ext.render.DynamicMedia;
029:
030: /**
031: * A HTML element with a label ({@link #getLabel})and an image ({@link #getImage}).
032: *
033: * @author tomyeh
034: */
035: public class LabelImageElement extends LabelElement {
036: private String _src = null;
037: /** The image. If not null, _src is generated automatically. */
038: private Image _image;
039: /** Count the version of {@link #_image}. */
040: private int _imgver;
041:
042: /** Returns the image URI.
043: * <p>Default: null.
044: * <p>The same as {@link #getSrc}.
045: */
046: public String getImage() {
047: return _src;
048: }
049:
050: /** Sets the image URI.
051: * <p>If src is changed, the component's inner is invalidate.
052: * Thus, you want to smart-update, you have to override this method.
053: * <p>The same as {@link #setSrc}.
054: */
055: public void setImage(String src) {
056: if (src != null && src.length() == 0)
057: src = null;
058: if (!Objects.equals(_src, src)) {
059: _src = src;
060: if (_image == null)
061: invalidate();
062:
063: //_src is meaningful only if _image is null
064: //NOTE: Tom Yeh: 20051222
065: //It is possible to use smartUpdate if we always generate
066: //an image (with an ID) in getImgTag.
067: //However, it is too costly by making HTML too big, so
068: //we prefer to invalidate (it happens rarely)
069: }
070: }
071:
072: /** Returns the src (an image URI).
073: * <p>Default: null.
074: * <p>The same as {@link #getImage}.
075: */
076: public String getSrc() {
077: return getImage();
078: }
079:
080: /** Sets the src (the image URI).
081: * <p>If src is changed, the component's inner is invalidate.
082: * Thus, you want to smart-update, you have to override this method.
083: * <p>The same as {@link #setImage}.
084: */
085: public void setSrc(String src) {
086: setImage(src);
087: }
088:
089: /** Sets the content directly.
090: * Default: null.
091: *
092: * @param image the image to display. If not null, it has higher
093: * priority than {@link #getSrc}.
094: */
095: public void setImageContent(Image image) {
096: if (image != _image) {
097: _image = image;
098: if (_image != null)
099: ++_imgver; //enforce browser to reload image
100: invalidate();
101: }
102: }
103:
104: /** Returns the content set by {@link #setImageContent}.
105: * <p>Note: it won't fetch what is set thru by {@link #setSrc}.
106: * It simply returns what is passed to {@link #setImageContent}.
107: */
108: public Image getImageContent() {
109: return _image;
110: }
111:
112: /** Returns whether the image is available.
113: * In other words, it return true if {@link #setImage} or
114: * {@link #setImageContent} is called with non-null.
115: */
116: public boolean isImageAssigned() {
117: return _src != null || _image != null;
118: }
119:
120: /** Returns the HTML IMG tag for the image part, or null
121: * if no image is assigned ({@link #isImageAssigned})
122: *
123: * <p>Used only for component template, not for application developers.
124: *
125: * <p>Note: the component template shall use this method to
126: * generate the HTML tag, instead of using {@link #getImage}.
127: */
128: public String getImgTag() {
129: if (_src == null && _image == null)
130: return null;
131:
132: final StringBuffer sb = new StringBuffer(64).append(
133: "<img src=\"").append(_image != null ? getContentSrc() : //already encoded
134: getDesktop().getExecution().encodeURL(_src)).append(
135: "\" align=\"absmiddle\"/>");
136:
137: final String label = getLabel();
138: if (label != null && label.length() > 0)
139: sb.append(' ');
140:
141: return sb.toString(); //keep a space
142: }
143:
144: /** Returns the encoded URL for the current image content.
145: * Don't call this method unless _image is not null;
146: *
147: * <p>Used only for component template, not for application developers.
148: */
149: private String getContentSrc() {
150: return Utils.getDynamicMediaURI(this , _imgver,
151: _image.getName(), _image.getFormat());
152: }
153:
154: //-- ComponentCtrl --//
155: protected Object newExtraCtrl() {
156: return new ExtraCtrl();
157: }
158:
159: /** A utility class to implement {@link #getExtraCtrl}.
160: * It is used only by component developers.
161: */
162: protected class ExtraCtrl extends LabelElement.ExtraCtrl implements
163: DynamicMedia {
164: //-- DynamicMedia --//
165: public Media getMedia(String pathInfo) {
166: return _image;
167: }
168: }
169: }
|