01: /*
02: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
03: *
04: * The program is provided "as is" without any warranty express or
05: * implied, including the warranty of non-infringement and the implied
06: * warranties of merchantibility and fitness for a particular purpose.
07: * IBM will not be liable for any damages suffered by you as a result
08: * of using the Program. In no event will IBM be liable for any
09: * special, indirect or consequential damages or lost profits even if
10: * IBM has been advised of the possibility of their occurrence. IBM
11: * will not be liable for any third party claims against you.
12: */
13: package com.ibm.richtext.uiimpl;
14:
15: import java.util.EventObject;
16:
17: import java.util.Hashtable;
18:
19: import com.ibm.richtext.textpanel.MTextPanel;
20: import com.ibm.richtext.uiimpl.resources.MenuData;
21:
22: public abstract class ChoiceMenuItemSet extends MenuItemSet {
23:
24: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
25: private MItem[] fItems;
26: private Hashtable fItemToStyleMap;
27:
28: ChoiceMenuItemSet(Object[] values, MenuData[] menuData) {
29:
30: if (values.length != menuData.length) {
31: throw new IllegalArgumentException(
32: "Values and names must have equal length");
33: }
34:
35: fItems = new MItem[menuData.length];
36: fItemToStyleMap = new Hashtable(menuData.length);
37:
38: EventListener listener = new EventListener() {
39: public void eventOccurred(EventObject event) {
40: handleValueSelected(fItemToStyleMap.get(event
41: .getSource()));
42: }
43: };
44:
45: for (int i = 0; i < menuData.length; i++) {
46: fItems[i] = MItem.createCheckboxItem(menuData[i]);
47: if (values[i] != null) {
48: fItemToStyleMap.put(fItems[i], values[i]);
49: }
50: fItems[i].addListener(listener);
51: fItems[i].setEnabled(false);
52: }
53: }
54:
55: protected abstract void handleValueSelected(Object item);
56:
57: protected abstract Object getCurrentValue();
58:
59: protected final void setChecked() {
60:
61: Object value = getCurrentValue();
62:
63: for (int i = 0; i < fItems.length; i++) {
64: Object itemVal = fItemToStyleMap.get(fItems[i]);
65: if (itemVal == null) {
66: fItems[i].setState(value == null);
67: } else {
68: fItems[i].setState(itemVal.equals(value));
69: }
70: }
71: }
72:
73: protected final void textPanelChanged() {
74:
75: MTextPanel textPanel = getTextPanel();
76: if (textPanel == null) {
77: for (int i = 0; i < fItems.length; i++) {
78: fItems[i].setEnabled(false);
79: fItems[i].setState(false);
80: }
81: } else {
82: for (int i = 0; i < fItems.length; i++) {
83: fItems[i].setEnabled(true);
84: setChecked();
85: }
86: }
87: }
88: }
|