001: /* Caption.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Oct 11 14:31:07 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.zk.ui.Component;
022: import org.zkoss.zk.ui.UiException;
023:
024: import org.zkoss.zul.impl.LabelImageElement;
025:
026: /**
027: * A header for a {@link Groupbox}.
028: * It may contain either a text label, using {@link #setLabel},
029: * or child elements for a more complex caption.
030: *
031: * @author tomyeh
032: */
033: public class Caption extends LabelImageElement {
034: public Caption() {
035: }
036:
037: public Caption(String label) {
038: setLabel(label);
039: }
040:
041: public Caption(String label, String src) {
042: setLabel(label);
043: setImage(src);
044: }
045:
046: /** Returns a compound label, which is the catenation of
047: * parent's title, if the parent is {@link Window}, and {@link #getLabel}.
048: * <p>Note: this is designed to used for component templating.
049: * Application developers rarely need to access this method.
050: *
051: * <p>It is mainly used for component implementation.
052: */
053: public String getCompoundLabel() {
054: final String label = getLabel();
055: final Component p = getParent();
056: if (p instanceof Window) {
057: final String title = ((Window) p).getTitle();
058: if (title.length() > 0)
059: return label.length() > 0 ? title + " - " + label
060: : title;
061: }
062: return label;
063: }
064:
065: /** Returns whether the legend mold shall be used.
066: * It actually returns {@link Groupbox#isLegend} if the parent
067: * is a {@link Groupbox}.
068: *
069: * <p>Note: this is designed to used for component templating.
070: * Application developers rarely need to access this method.
071: */
072: public boolean isLegend() {
073: final Component p = getParent();
074: return (p instanceof Groupbox) && ((Groupbox) p).isLegend();
075: }
076:
077: /** Returns whether to display the closable button.
078: * <p>Default: it returns true if the parent is window and {@link Window#isClosable}
079: * is true.
080: *
081: * <p>It is mainly used for component implementation.
082: */
083: public boolean isClosableVisible() {
084: final Component p = getParent();
085: return (p instanceof Window) && ((Window) p).isClosable();
086: }
087:
088: //-- super --//
089: /** Returns the style class.
090: * <p>Default: return caption if the parent is groupbox,
091: * or title otherwise (say, the parent is window)
092: */
093: public String getSclass() {
094: final String scls = super .getSclass();
095: if (scls != null)
096: return scls;
097: return getParent() instanceof Groupbox ? "caption" : "title";
098: }
099:
100: public String getOuterAttrs() {
101: final String attrs = super .getOuterAttrs();
102: final String clkattrs = getAllOnClickAttrs(false);
103: return clkattrs == null ? attrs : attrs + clkattrs;
104: }
105:
106: //-- Component --//
107: public void setParent(Component parent) {
108: if (parent != null && !(parent instanceof Window)
109: && !(parent instanceof Groupbox))
110: throw new UiException("Wrong parent: " + parent);
111: super .setParent(parent);
112: }
113:
114: public void invalidate() {
115: final Component p = getParent();
116: if ((p instanceof Groupbox) && ((Groupbox) p).isLegend())
117: p.invalidate(); //Bug 1679629
118: else
119: super.invalidate();
120: }
121: }
|