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.uiimpl;
014:
015: import java.util.EventObject;
016:
017: import com.ibm.richtext.textlayout.attributes.AttributeMap;
018: import com.ibm.richtext.textlayout.attributes.AttributeSet;
019: import com.ibm.richtext.styledtext.StyleModifier;
020:
021: import com.ibm.richtext.textpanel.MTextPanel;
022: import com.ibm.richtext.textpanel.TextPanelEvent;
023: import com.ibm.richtext.uiimpl.resources.MenuData;
024:
025: public final class SubtractStyleMenuItem extends SingleCheckMenuItem {
026:
027: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
028: private final Object[] fKeys;
029:
030: private final boolean fCharacter;
031:
032: public SubtractStyleMenuItem(Object[] keys, MenuData menuData,
033: boolean character) {
034:
035: super (menuData);
036:
037: fKeys = (Object[]) keys.clone();
038: fCharacter = character;
039:
040: AttributeSet keySet = new AttributeSet(keys);
041: final StyleModifier modifier = StyleModifier
042: .createRemoveModifier(keySet);
043:
044: fItem.addListener(new EventListener() {
045: public void eventOccurred(EventObject event) {
046: MTextPanel panel = getTextPanel();
047: if (panel == null) {
048: throw new Error(
049: "Menu item is enabled when panel is null!");
050: }
051:
052: if (fCharacter == CHARACTER) {
053: panel.modifyCharacterStyleOnSelection(modifier);
054: } else {
055: panel.modifyParagraphStyleOnSelection(modifier);
056: }
057: }
058: });
059: }
060:
061: private static boolean objectsAreEqual(Object lhs, Object rhs) {
062:
063: if (lhs == null) {
064: return rhs == null;
065: } else {
066: return lhs.equals(rhs);
067: }
068: }
069:
070: protected void setChecked() {
071:
072: MTextPanel panel = getTextPanel();
073: AttributeMap defaults = panel.getDefaultValues();
074:
075: for (int i = 0; i < fKeys.length; i++) {
076: Object defaultV = defaults.get(fKeys[i]);
077:
078: Object value = (fCharacter == CHARACTER) ? panel
079: .getCharacterStyleOverSelection(fKeys[i]) : panel
080: .getParagraphStyleOverSelection(fKeys[i]);
081:
082: if (!objectsAreEqual(defaultV, value)) {
083: fItem.setState(false);
084: return;
085: }
086: }
087:
088: fItem.setState(true);
089: }
090:
091: public boolean respondsToEventType(int type) {
092:
093: return type == TextPanelEvent.SELECTION_STYLES_CHANGED;
094: }
095:
096: public final void textEventOccurred(TextPanelEvent event) {
097:
098: setChecked();
099: }
100: }
|