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 com.ibm.richtext.textlayout.attributes.AttributeSet;
18: import com.ibm.richtext.styledtext.StyleModifier;
19:
20: import com.ibm.richtext.textpanel.MTextPanel;
21: import com.ibm.richtext.textpanel.TextPanelEvent;
22: import com.ibm.richtext.uiimpl.resources.MenuData;
23:
24: public final class BooleanStyleMenuItem extends SingleCheckMenuItem {
25:
26: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
27:
28: private final Object fKey;
29: private final boolean fCharacter;
30:
31: private final Object fOnValue;
32:
33: public BooleanStyleMenuItem(Object style, Object onValue,
34: MenuData menuData, boolean character) {
35:
36: super (menuData);
37: if (onValue == null) {
38: throw new IllegalArgumentException(
39: "On value cannot be null");
40: }
41:
42: fKey = style;
43: fCharacter = character;
44:
45: fOnValue = onValue;
46: fItem.addListener(new EventListener() {
47: public void eventOccurred(EventObject event) {
48: StyleModifier modifier;
49: MTextPanel panel = getTextPanel();
50: if (panel == null) {
51: throw new Error(
52: "Menu item is enabled when panel is null!");
53: }
54: if (continuousAndCommand()) {
55: AttributeSet set = new AttributeSet(fKey);
56: modifier = StyleModifier.createRemoveModifier(set);
57: } else {
58: modifier = StyleModifier.createAddModifier(fKey,
59: fOnValue);
60: }
61:
62: if (fCharacter == CHARACTER) {
63: panel.modifyCharacterStyleOnSelection(modifier);
64: } else {
65: panel.modifyParagraphStyleOnSelection(modifier);
66: }
67: }
68: });
69: }
70:
71: private boolean continuousAndCommand() {
72:
73: MTextPanel panel = getTextPanel();
74:
75: Object value = (fCharacter == CHARACTER) ? panel
76: .getCharacterStyleOverSelection(fKey) : panel
77: .getParagraphStyleOverSelection(fKey);
78: return fOnValue.equals(value);
79: }
80:
81: protected void setChecked() {
82:
83: fItem.setState(continuousAndCommand());
84: }
85:
86: public boolean respondsToEventType(int type) {
87:
88: return type == TextPanelEvent.SELECTION_STYLES_CHANGED;
89: }
90:
91: public final void textEventOccurred(TextPanelEvent event) {
92:
93: setChecked();
94: }
95: }
|