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.awtui;
014:
015: import java.awt.CheckboxMenuItem;
016: import java.awt.Menu;
017: import java.awt.MenuItem;
018: import java.awt.MenuShortcut;
019:
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.awt.event.ItemEvent;
023: import java.awt.event.ItemListener;
024:
025: import com.ibm.richtext.uiimpl.resources.MenuData;
026: import com.ibm.richtext.uiimpl.MItem;
027: import com.ibm.richtext.uiimpl.MItem.ItemFactory;
028:
029: final class AwtMenuFactory implements ItemFactory {
030:
031: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
032:
033: private final class AwtMItem extends MItem {
034:
035: private MenuItem fItem;
036:
037: AwtMItem(MenuItem item) {
038:
039: fItem = item;
040: try {
041: CheckboxMenuItem chItem = (CheckboxMenuItem) fItem;
042: chItem.addItemListener(new ItemListener() {
043: public void itemStateChanged(ItemEvent e) {
044: handleSelected();
045: }
046: });
047: } catch (ClassCastException e) {
048: item.addActionListener(new ActionListener() {
049: public void actionPerformed(ActionEvent evt) {
050: handleSelected();
051: }
052: });
053: }
054: }
055:
056: // Ack - how do you do this from inner classes??
057: protected void handleSelected() {
058:
059: super .handleSelected();
060: }
061:
062: public final void setEnabled(boolean enabled) {
063:
064: fItem.setEnabled(enabled);
065: }
066:
067: public void setState(boolean checked) {
068:
069: try {
070: CheckboxMenuItem item = (CheckboxMenuItem) fItem;
071: item.setState(checked);
072: } catch (ClassCastException e) {
073: throw new Error(
074: "Cannot perform setChecked on non-checkbox item");
075: }
076: }
077: }
078:
079: private Menu fMenu;
080:
081: AwtMenuFactory(Menu menu) {
082:
083: fMenu = menu;
084: }
085:
086: private MItem handleCreate(MenuItem item, MenuData menuData) {
087:
088: if (menuData.hasShortcut()) {
089: item.setShortcut(new MenuShortcut(menuData
090: .getShortcutChar()));
091: }
092:
093: fMenu.add(item);
094:
095: return new AwtMItem(item);
096: }
097:
098: public MItem createItem(MenuData menuData) {
099:
100: return handleCreate(new MenuItem(menuData.getName()), menuData);
101: }
102:
103: public MItem createCheckboxItem(MenuData menuData) {
104:
105: return handleCreate(new CheckboxMenuItem(menuData.getName()),
106: menuData);
107: }
108:
109: public void createSeparator() {
110:
111: fMenu.add(new MenuItem("-"));
112: }
113: }
|