001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.swingui;
014:
015: import java.awt.Event;
016:
017: import javax.swing.JCheckBoxMenuItem;
018: import javax.swing.JMenu;
019: import javax.swing.JMenuItem;
020: import javax.swing.JSeparator;
021: import javax.swing.KeyStroke;
022:
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025:
026: import com.ibm.richtext.uiimpl.resources.MenuData;
027: import com.ibm.richtext.uiimpl.MItem;
028: import com.ibm.richtext.uiimpl.MItem.ItemFactory;
029:
030: final class SwingMenuFactory implements ItemFactory {
031:
032: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
033:
034: private final class SwingMItem extends MItem {
035:
036: private JMenuItem fItem;
037:
038: SwingMItem(JMenuItem item) {
039:
040: fItem = item;
041: item.addActionListener(new ActionListener() {
042: public void actionPerformed(ActionEvent e) {
043: handleSelected();
044: }
045: });
046: }
047:
048: protected void handleSelected() {
049:
050: super .handleSelected();
051: }
052:
053: public final void setEnabled(boolean enabled) {
054:
055: fItem.setEnabled(enabled);
056: }
057:
058: public void setState(boolean checked) {
059:
060: try {
061: JCheckBoxMenuItem item = (JCheckBoxMenuItem) fItem;
062: item.setState(checked);
063: } catch (ClassCastException e) {
064: throw new Error(
065: "Cannot perform setChecked on non-checkbox item");
066: }
067: }
068: }
069:
070: private JMenu fMenu;
071:
072: SwingMenuFactory(JMenu menu) {
073:
074: fMenu = menu;
075: }
076:
077: private MItem handleCreate(JMenuItem item, MenuData menuData) {
078:
079: if (menuData.hasShortcut()) {
080: KeyStroke ks = KeyStroke.getKeyStroke(menuData
081: .getShortcutKeyCode(), Event.CTRL_MASK);
082: item.setAccelerator(ks);
083: }
084:
085: fMenu.add(item);
086:
087: return new SwingMItem(item);
088: }
089:
090: public MItem createItem(MenuData menuData) {
091:
092: return handleCreate(new JMenuItem(menuData.getName()), menuData);
093: }
094:
095: public MItem createCheckboxItem(MenuData menuData) {
096:
097: return handleCreate(new JCheckBoxMenuItem(menuData.getName()),
098: menuData);
099: }
100:
101: public void createSeparator() {
102:
103: fMenu.add(new JSeparator());
104: }
105: }
|