001: /* Splitter.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Jun 8 14:54:05 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 org.zkoss.lang.Objects;
020: import org.zkoss.xml.HTMLs;
021: import org.zkoss.zk.ui.Component;
022: import org.zkoss.zk.ui.UiException;
023: import org.zkoss.zk.ui.WrongValueException;
024: import org.zkoss.zk.ui.event.Events;
025: import org.zkoss.zk.ui.ext.client.Openable;
026: import org.zkoss.zul.impl.XulElement;
027:
028: /**
029: * An element which should appear before or after an element inside a box
030: * ({@link Box}, {@link Vbox} and {@link Hbox}).
031: *
032: * <p>When the splitter is dragged, the sibling elements of the splitter are
033: * resized. If {@link #getCollapse} is true, a grippy in placed
034: * inside the splitter, and one sibling element of the splitter is collapsed
035: * when the grippy is clicked.
036: *
037: * <p>Events: onOpen
038: *
039: * <p>Default {@link #getSclass}: splitter.
040: *
041: * @author tomyeh
042: */
043: public class Splitter extends XulElement {
044: private String _collapse = "none";
045: private boolean _open = true;
046:
047: public Splitter() {
048: setSclass("splitter");
049: }
050:
051: /** Returns the orientation of the splitter.
052: * It is the same as the parent's orientation ({@link Box#getOrient}.
053: */
054: public String getOrient() {
055: final Box box = (Box) getParent();
056: return box != null ? box.getOrient() : "vertical";
057: }
058:
059: /** Returns which side of the splitter is collapsed when its grippy
060: * is clicked. If this attribute is not specified, the splitter will
061: * not cause a collapse.
062: * If it is collapsed, {@link #isOpen} returns false.
063: *
064: * <p>Default: none.
065: *
066: * <p>The returned value can be one ofthe following.
067: *
068: * <dl>
069: * <dt>none</dt>
070: * <dd>No collpasing occurs.</dd>
071: * <dt>before</dt>
072: * <dd>When the grippy is clicked, the element immediately
073: * before the splitter in the same parent is collapsed so that
074: * its width or height is 0.</dd>
075: * <dt>after</dt>
076: * <dd>When the grippy is clicked, the element immediately
077: * after the splitter in the same parent is collapsed so that
078: * its width or height is 0.</dd>
079: * </dl>
080: *
081: * <p>Unlike XUL, you don't have to put a so-called grippy component
082: * as a child of the spiltter.
083: */
084: public String getCollapse() {
085: return _collapse;
086: }
087:
088: /** Sets which side of the splitter is collapsed when its grippy
089: * is clicked. If this attribute is not specified, the splitter will
090: * not cause a collapse.
091: *
092: * @param collapse one of none, before and after.
093: * If null or empty is specified, none is assumed.
094: * @see #getCollapse
095: */
096: public void setCollapse(String collapse) throws WrongValueException {
097: if (collapse == null || collapse.length() == 0)
098: collapse = "none";
099: else if (!"none".equals(collapse) && !"before".equals(collapse)
100: && !"after".equals(collapse))
101: throw new WrongValueException("Unknown collpase: "
102: + collapse);
103:
104: if (!Objects.equals(_collapse, collapse)) {
105: _collapse = collapse;
106: smartUpdate("z.colps", collapse);
107: }
108: }
109:
110: /** Returns whether it is opne (i.e., not collapsed.
111: * Meaningful only if {@link #getCollapse} is not "none".
112: */
113: public boolean isOpen() {
114: return _open;
115: }
116:
117: /** Opens or collapses the splitter.
118: * Meaningful only if {@link #getCollapse} is not "none".
119: */
120: public void setOpen(boolean open) {
121: if (_open != open) {
122: _open = open;
123: smartUpdate("z.open", open);
124: }
125: }
126:
127: protected String getRealSclass() {
128: if ("vertical".equals(getOrient()))
129: return super .getRealSclass() + "-v";
130: else
131: return super .getRealSclass() + "-h";
132: }
133:
134: //super//
135: public String getOuterAttrs() {
136: final StringBuffer sb = new StringBuffer(80).append(super
137: .getOuterAttrs());
138: appendAsapAttr(sb, Events.ON_OPEN);
139:
140: if ("vertical".equals(getOrient()))
141: HTMLs.appendAttribute(sb, "z.vert", "true");
142:
143: if (!"none".equals(_collapse))
144: HTMLs.appendAttribute(sb, "z.colps", _collapse);
145: if (!_open)
146: sb.append(" z.open=\"false\"");
147: return sb.toString();
148: }
149:
150: public void setParent(Component parent) {
151: if (parent != null && !(parent instanceof Box))
152: throw new UiException("Wrong parent: " + parent);
153: super .setParent(parent);
154: }
155:
156: /** Not allow any children.
157: */
158: public boolean isChildable() {
159: return false;
160: }
161:
162: //-- ComponentCtrl --//
163: protected Object newExtraCtrl() {
164: return new ExtraCtrl();
165: }
166:
167: /** A utility class to implement {@link #getExtraCtrl}.
168: * It is used only by component developers.
169: */
170: protected class ExtraCtrl extends XulElement.ExtraCtrl implements
171: Openable {
172: //-- Openable --//
173: public void setOpenByClient(boolean open) {
174: _open = open;
175: }
176: }
177: }
|