001: /* Menu.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Sep 22 10:57:58 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.io.IOException;
022:
023: import org.zkoss.lang.Objects;
024: import org.zkoss.xml.HTMLs;
025:
026: import org.zkoss.zk.ui.Component;
027: import org.zkoss.zk.ui.UiException;
028: import org.zkoss.zk.ui.WrongValueException;
029:
030: import org.zkoss.zul.impl.LabelImageElement;
031:
032: /**
033: * An element, much like a button, that is placed on a menu bar.
034: * When the user clicks the menu element, the child {@link Menupopup}
035: * of the menu will be displayed.
036: * This element is also used to create submenus (of {@link Menupopup}.
037: *
038: * @author tomyeh
039: */
040: public class Menu extends LabelImageElement {
041: private Menupopup _popup;
042:
043: public Menu() {
044: }
045:
046: public Menu(String label) {
047: setLabel(label);
048: }
049:
050: public Menu(String label, String src) {
051: setLabel(label);
052: setImage(src);
053: }
054:
055: /** Returns whether this is an top-level menu, i.e., not owning
056: * by another {@link Menupopup}.
057: */
058: public boolean isTopmost() {
059: return !(getParent() instanceof Menupopup);
060: }
061:
062: /** Returns the {@link Menupopup} it owns, or null if not available.
063: */
064: public Menupopup getMenupopup() {
065: return _popup;
066: }
067:
068: //-- Component --//
069: public String getOuterAttrs() {
070: final StringBuffer sb = new StringBuffer(64).append(super
071: .getOuterAttrs());
072: if (_popup != null)
073: HTMLs.appendAttribute(sb, "z.mpop", _popup.getUuid());
074: if (isTopmost()) {
075: sb.append(" z.top=\"true\"");
076: final Component parent = getParent();
077: if (parent instanceof Menubar
078: && "vertical"
079: .equals(((Menubar) parent).getOrient()))
080: sb.append(" z.vert=\"true\"");
081: }
082: return sb.toString();
083: }
084:
085: protected String getRealStyle() {
086: final String style = super .getRealStyle();
087: return isTopmost() ? style
088: + "padding-left:4px;padding-right:4px;" : style;
089: }
090:
091: public void setParent(Component parent) {
092: if (parent != null && !(parent instanceof Menubar)
093: && !(parent instanceof Menupopup))
094: throw new UiException("Unsupported parent for menu: "
095: + parent);
096: super .setParent(parent);
097: }
098:
099: public boolean insertBefore(Component child, Component insertBefore) {
100: if (child instanceof Menupopup) {
101: if (_popup != null && _popup != child)
102: throw new UiException("Only one menupopup is allowed: "
103: + this );
104: _popup = (Menupopup) child;
105: } else {
106: throw new UiException("Unsupported child for menu: "
107: + child);
108: }
109: return super .insertBefore(child, insertBefore);
110: }
111:
112: //Cloneable//
113: public Object clone() {
114: final Menu clone = (Menu) super .clone();
115: if (clone._popup != null)
116: clone.afterUnmarshal();
117: return clone;
118: }
119:
120: private void afterUnmarshal() {
121: _popup = (Menupopup) getFirstChild();
122: }
123:
124: //Serializable//
125: private synchronized void readObject(java.io.ObjectInputStream s)
126: throws java.io.IOException, ClassNotFoundException {
127: s.defaultReadObject();
128:
129: if (!getChildren().isEmpty())
130: afterUnmarshal();
131: }
132: }
|