001: /* Style.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Jul 20 15:17:40 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.util.media.Media;
023: import org.zkoss.util.media.AMedia;
024: import org.zkoss.xml.HTMLs;
025:
026: import org.zkoss.zk.ui.AbstractComponent;
027: import org.zkoss.zk.ui.UiException;
028: import org.zkoss.zk.ui.ext.render.DynamicMedia;
029:
030: import org.zkoss.zul.impl.Utils;
031:
032: /**
033: * The style component used to specify CSS styles for the owner desktop.
034: *
035: * <p>Note: a style component can appear anywhere in a ZUML page, but it
036: * affects all components in the same desktop.
037: *
038: * <p>Note: unlike HTML STYLE, this component can be added and removed
039: * dynamically and the rules will be attached and detached accordingly.
040: *
041: * <p>There are three formats when used in a ZUML page:
042: *
043: * <p>Method 1: Specify the URL of the CSS file
044: * <pre><code><style src="my.css"/>
045: * </code></pre>
046: *
047: * <p>Method 2: Specify the CSS directly
048: * <pre><code><style>
049: * .mycls {
050: * border: 1px outset #777;
051: * }
052: *</style>
053: * </code></pre>
054: *
055: * <p>Method 3: Specify the CSS by use of the content
056: * property ({@link #setContent}).
057: * <pre><code><style>
058: * <attribute name="content">
059: * .mycls {
060: * border: 1px outset #777;
061: * }
062: * </attribute>
063: *</style>
064: * </code></pre>
065: *
066: * <p>Note: if the src and content properties are both set,
067: * the content property is ignored.
068: *
069: * @author tomyeh
070: */
071: public class Style extends AbstractComponent {
072: private String _src;
073: private String _content;
074: /** Count the version of {@link #_content}. */
075: private int _cntver;
076:
077: public Style() {
078: super .setVisible(false);
079: }
080:
081: /**
082: * @param src the URI of an external style sheet.
083: */
084: public Style(String src) {
085: this ();
086: setSrc(src);
087: }
088:
089: /** Returns the URI of an external style sheet.
090: *
091: * <p>Default: null. If null, the children's
092: * (must be instances of {@link Label}) value ({@link Label#getValue})
093: * is used as the content of styles.
094: * If not null, the HTML LINK tag is generated to ask the browser
095: * to load the specified style sheet.
096: *
097: * <p>Note: If not null, the content of children are ignored.
098: */
099: public String getSrc() {
100: return _src;
101: }
102:
103: /** Sets the URI of an external style sheet.
104: *
105: * @param src the URI of an external style sheet, or null to use
106: * the content of children ({@link Label#getValue}) instead.
107: */
108: public void setSrc(String src) {
109: if (src != null && src.length() == 0)
110: src = null;
111: if (!Objects.equals(_src, src)) {
112: _src = src;
113: invalidate();
114: }
115: }
116:
117: /** Returns the content of the style element.
118: * By content we mean the CSS rules that will be sent to the client.
119: * Note: if {@link #setSrc} is called with a non-empty value,
120: * this method is ignored, i.e., {@link #getSrc} has the higher priority.
121: *
122: * <p>Default: null.
123: *
124: * @since 3.0.0
125: */
126: public String getContent() {
127: return _content;
128: }
129:
130: /** Sets the content of the style element.
131: * By content we mean the CSS rules that will be sent to the client.
132: *
133: * @since 3.0.0
134: */
135: public void setContent(String content) {
136: if (content != null && content.length() == 0)
137: content = null;
138:
139: if (!Objects.equals(_content, content)) {
140: _content = content;
141: ++_cntver;
142: invalidate();
143: }
144: }
145:
146: /** Returns the attributes for generating the HTML tags.
147: */
148: public String getOuterAttrs() {
149: final StringBuffer sb = new StringBuffer(64);
150: HTMLs.appendAttribute(sb, "z.src", _src != null ? getDesktop()
151: .getExecution().encodeURL(_src) : Utils
152: .getDynamicMediaURI(this , _cntver, "css", "css"));
153: return sb.toString();
154: }
155:
156: //Component//
157: /** Not allowd. */
158: public boolean setVisible(boolean visible) {
159: throw new UnsupportedOperationException(
160: "style is always invisible");
161: }
162:
163: /** Not childable. */
164: public boolean isChildable() {
165: return false;
166: }
167:
168: //-- ComponentCtrl --//
169: protected Object newExtraCtrl() {
170: return new ExtraCtrl();
171: }
172:
173: /** A utility class to implement {@link #getExtraCtrl}.
174: * It is used only by component developers.
175: */
176: protected class ExtraCtrl implements DynamicMedia {
177: //-- DynamicMedia --//
178: public Media getMedia(String pathInfo) {
179: return new AMedia("css", "css", "text/css;charset=UTF-8",
180: _content);
181: }
182: }
183: }
|