001: /* Tabs.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Jul 12 10:43:14 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 java.util.Iterator;
022:
023: import org.zkoss.lang.Objects;
024: import org.zkoss.xml.HTMLs;
025: import org.zkoss.zk.ui.Component;
026: import org.zkoss.zk.ui.UiException;
027: import org.zkoss.zk.ui.WrongValueException;
028: import org.zkoss.zk.ui.event.Events;
029:
030: import org.zkoss.zul.Tabbox;
031: import org.zkoss.zul.impl.XulElement;
032:
033: /**
034: * A collection of tabs ({@link Tab}).
035: *
036: * @author tomyeh
037: */
038: public class Tabs extends XulElement {
039:
040: private String _align = "start";
041:
042: /** Returns the tabbox owns this component.
043: * <p>It is the same as {@link #getParent}.
044: */
045: public Tabbox getTabbox() {
046: return (Tabbox) getParent();
047: }
048:
049: /** Returns the alignment of tab.
050: *
051: * <p>Default: "start".
052: *
053: * <p>Note: only the default mold supports it (not supported if accordion).
054: *
055: * @since 3.0.0
056: */
057: public String getAlign() {
058: return _align;
059: }
060:
061: /** Sets the alignment of tab.
062: * @param align must be "start" or "center" or "end".
063: * @since 3.0.0
064: */
065: public void setAlign(String align) throws WrongValueException {
066: if (!"start".equals(align) && !"center".equals(align)
067: && !"end".equals(align))
068: throw new WrongValueException(align);
069:
070: if (!Objects.equals(_align, align)) {
071: _align = align;
072: Tabbox tabbox = getTabbox();
073: if (!tabbox.inAccordionMold()) {
074: //getTabbox().invalidate();
075: invalidate();
076: }
077: }
078: }
079:
080: //-- Component --//
081: public void setParent(Component parent) {
082: if (parent != null && !(parent instanceof Tabbox))
083: throw new UiException("Wrong parent: " + parent);
084:
085: final Tabbox oldp = (Tabbox) getParent();
086: super .setParent(parent);
087:
088: invalidateIfAccordion(oldp);
089: invalidateIfAccordion((Tabbox) parent);
090: }
091:
092: public boolean insertBefore(Component child, Component insertBefore) {
093: if (!(child instanceof Tab))
094: throw new UiException("Unsupported child for tabs: "
095: + child);
096:
097: boolean sel = getChildren().isEmpty(), desel = false;
098: final Tab newtab = (Tab) child;
099: if (!sel && newtab.isSelected()) {
100: newtab.setSelectedDirectly(false); //turn off first
101: sel = desel = true; //trun on later
102: }
103:
104: if (super .insertBefore(child, insertBefore)) {
105: final Tabbox tabbox = getTabbox();
106:
107: if (sel)
108: if (tabbox != null) {
109: tabbox.setSelectedTab(newtab);
110: } else {
111: newtab.setSelectedDirectly(true);
112: if (desel)
113: for (Iterator it = getChildren().iterator(); it
114: .hasNext();) {
115: final Tab tab = (Tab) it.next();
116: if (tab != newtab && tab.isSelected()) {
117: tab.setSelectedDirectly(false);
118: break;
119: }
120: }
121: }
122:
123: invalidateIfAccordion(tabbox);
124: return true;
125: }
126: return false;
127: }
128:
129: public void onChildRemoved(Component child) {
130: super .onChildRemoved(child);
131:
132: final Tabbox tabbox = getTabbox();
133: if (tabbox != null)
134: ((Tab) child).removeEventListener(Events.ON_SELECT,
135: tabbox._listener);
136:
137: if (tabbox == null || !tabbox.inAccordionMold())
138: smartUpdate("z.init", true); //fixWidth
139: }
140:
141: public void onChildAdded(Component child) {
142: super .onChildAdded(child);
143:
144: final Tabbox tabbox = getTabbox();
145: if (tabbox != null)
146: ((Tab) child).addEventListener(Events.ON_SELECT,
147: tabbox._listener);
148:
149: if (tabbox == null || !tabbox.inAccordionMold())
150: smartUpdate("z.init", true); //fixWidth
151: }
152:
153: /** Invalidates the tabbox if it is accordion.
154: */
155: private static void invalidateIfAccordion(Tabbox tabbox) {
156: if (tabbox != null && tabbox.inAccordionMold())
157: tabbox.invalidate();
158: }
159:
160: //-- super --//
161: public String getOuterAttrs() {
162: Tabbox tabbox = getTabbox();
163: if (!tabbox.inAccordionMold()) {
164: final StringBuffer sb = new StringBuffer(64).append(super
165: .getOuterAttrs());
166: if (!"start".equals(_align))
167: HTMLs.appendAttribute(sb, "z.align", _align.substring(
168: 0, 1));
169: return sb.toString();
170: }
171: return super.getOuterAttrs();
172: }
173: }
|