01: /*
02: * Beryl - A web platform based on XML, XSLT and Java
03: * This file is part of the Beryl XML GUI
04: *
05: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11:
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
20: */
21:
22: package org.beryl.gui.widgets;
23:
24: import java.awt.Component;
25:
26: import javax.swing.JMenu;
27: import javax.swing.JMenuItem;
28: import javax.swing.JPopupMenu;
29:
30: import org.beryl.gui.GUIException;
31: import org.beryl.gui.Widget;
32: import org.beryl.gui.WidgetInfo;
33:
34: public class Menu extends Widget {
35: protected static WidgetInfo menuInfo = null;
36: private JMenu menu = null;
37:
38: static {
39: menuInfo = new WidgetInfo(Menu.class, widgetInfo);
40: menuInfo.addProperty("mnemonic", "istring", "");
41: menuInfo.addProperty("text", "istring", "");
42: menuInfo.setSupportsAnchor(false);
43: };
44:
45: public Menu(Widget parent, String name) throws GUIException {
46: super (parent, name);
47: menu = new JMenu();
48: }
49:
50: public void addChild(Widget widget, Object constraint)
51: throws GUIException {
52: if (widget instanceof MenuItem || widget instanceof CheckBox) {
53: menu.add((JMenuItem) widget.getWidget());
54: addChild(widget);
55: } else if (widget instanceof Separator) {
56: Separator separator = (Separator) widget;
57: separator.menuSeparator = new JPopupMenu.Separator();
58: menu.add(separator.menuSeparator);
59: addChild(widget);
60: } else {
61: throw new GUIException(
62: "Only MenuItem,CheckBox and Separator children allowed");
63: }
64: }
65:
66: public void setProperty(String name, Object value)
67: throws GUIException {
68: if (name.equals("mnemonic")) {
69: if (((String) value).length() > 0)
70: menu.setMnemonic(((String) value).charAt(0));
71: else
72: menu.setMnemonic('\0');
73: } else {
74: super .setProperty(name, value);
75: }
76: }
77:
78: public void removeChildWidget(Widget widget) throws GUIException {
79: if (widget instanceof MenuItem)
80: menu.remove((JMenuItem) widget.getWidget());
81: else if (widget instanceof Separator)
82: menu.remove(((Separator) widget).menuSeparator);
83: super .removeChildWidget(widget);
84: }
85:
86: public Component getWidget() {
87: return menu;
88: }
89:
90: public WidgetInfo getWidgetInfo() {
91: return menuInfo;
92: }
93: }
|