001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.widgets;
023:
024: import java.awt.Component;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.awt.event.InputEvent;
028: import java.awt.event.KeyEvent;
029:
030: import javax.swing.JMenuItem;
031: import javax.swing.KeyStroke;
032:
033: import org.beryl.gui.GUIEvent;
034: import org.beryl.gui.GUIEventListener;
035: import org.beryl.gui.GUIException;
036: import org.beryl.gui.ImageIconFactory;
037: import org.beryl.gui.Widget;
038: import org.beryl.gui.WidgetInfo;
039:
040: public class MenuItem extends Widget {
041: protected static WidgetInfo menuItemInfo = null;
042: private JMenuItem menuItem = null;
043:
044: static {
045: menuItemInfo = new WidgetInfo(MenuItem.class, widgetInfo);
046: menuItemInfo.addProperty("accelerator", "istring", "");
047: menuItemInfo.addProperty("mnemonic", "istring", "");
048: menuItemInfo.addProperty("text", "istring", "");
049: menuItemInfo.addEvent("selected");
050: menuItemInfo.setSupportsAnchor(false);
051: try {
052: menuItemInfo.addProperty("icon", "icon", ImageIconFactory
053: .getIcon("broken"));
054: } catch (Exception e) {
055: throw new RuntimeException(e);
056: }
057: };
058:
059: public MenuItem(Widget parent, String name) throws GUIException {
060: super (parent, name);
061: menuItem = new JMenuItem();
062: }
063:
064: public void addListener(String event, final String name,
065: final GUIEventListener listener) throws GUIException {
066: if ("selected".equals(event)) {
067: menuItem.addActionListener(new ActionListener() {
068: public void actionPerformed(ActionEvent e) {
069: listener.eventOccured(new GUIEvent(MenuItem.this ,
070: name, e));
071: }
072: });
073: } else {
074: super .addListener(event, name, listener);
075: }
076: }
077:
078: /**
079: * Construct a keystroke from a string
080: */
081: private KeyStroke parseKeyStroke(String keyStroke)
082: throws GUIException {
083: if (keyStroke == null)
084: return null;
085:
086: int modifiers = 0;
087: int index = keyStroke.indexOf('+');
088: if (index != -1) {
089: for (int i = 0; i < index; i++) {
090: switch (Character.toUpperCase(keyStroke.charAt(i))) {
091: case 'A':
092: modifiers |= InputEvent.ALT_MASK;
093: break;
094: case 'C':
095: modifiers |= InputEvent.CTRL_MASK;
096: break;
097: case 'M':
098: modifiers |= InputEvent.META_MASK;
099: break;
100: case 'S':
101: modifiers |= InputEvent.SHIFT_MASK;
102: break;
103: }
104: }
105: }
106: String key = keyStroke.substring(index + 1);
107: if (key.length() == 1) {
108: char ch = Character.toUpperCase(key.charAt(0));
109: if (modifiers == 0)
110: return KeyStroke.getKeyStroke(ch);
111: else
112: return KeyStroke.getKeyStroke(ch, modifiers);
113: } else if (key.length() == 0) {
114: throw new GUIException("Invalid key stroke: " + keyStroke);
115: } else {
116: try {
117: int ch = KeyEvent.class.getField("VK_".concat(key))
118: .getInt(null);
119: return KeyStroke.getKeyStroke(ch, modifiers);
120: } catch (Exception e) {
121: throw new GUIException("Invalid key stroke: "
122: + keyStroke);
123: }
124: }
125: }
126:
127: public void setProperty(String name, Object value)
128: throws GUIException {
129: if ("accelerator".equals(name)) {
130: if (value != null && !value.equals("")) {
131: KeyStroke stroke = parseKeyStroke((String) value);
132: menuItem.setAccelerator(stroke);
133: }
134: } else if (name.equals("mnemonic")) {
135: if (((String) value).length() > 0)
136: menuItem.setMnemonic(((String) value).charAt(0));
137: else
138: menuItem.setMnemonic('\0');
139: } else {
140: super .setProperty(name, value);
141: }
142: }
143:
144: public Component getWidget() {
145: return menuItem;
146: }
147:
148: public WidgetInfo getWidgetInfo() {
149: return menuItemInfo;
150: }
151: }
|