]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/uiimpl/SubtractStyleMenuItem.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / uiimpl / SubtractStyleMenuItem.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.uiimpl;\r
14 \r
15 import java.util.EventObject;\r
16 \r
17 import com.ibm.richtext.textlayout.attributes.AttributeMap;\r
18 import com.ibm.richtext.textlayout.attributes.AttributeSet;\r
19 import com.ibm.richtext.styledtext.StyleModifier;\r
20 \r
21 import com.ibm.richtext.textpanel.MTextPanel;\r
22 import com.ibm.richtext.textpanel.TextPanelEvent;\r
23 import com.ibm.richtext.uiimpl.resources.MenuData;\r
24 \r
25 public final class SubtractStyleMenuItem extends SingleCheckMenuItem {\r
26 \r
27     static final String COPYRIGHT =\r
28                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
29     private final Object[] fKeys;\r
30     \r
31     private final boolean fCharacter;\r
32     \r
33     public SubtractStyleMenuItem(Object[] keys,\r
34                                  MenuData menuData,\r
35                                  boolean character) {\r
36         \r
37         super(menuData);\r
38         \r
39         fKeys = (Object[]) keys.clone();\r
40         fCharacter = character;\r
41         \r
42         AttributeSet keySet = new AttributeSet(keys);\r
43         final StyleModifier modifier = StyleModifier.createRemoveModifier(keySet);\r
44         \r
45         fItem.addListener(new EventListener() {\r
46             public void eventOccurred(EventObject event) {\r
47                 MTextPanel panel = getTextPanel();\r
48                 if (panel == null) {\r
49                     throw new Error("Menu item is enabled when panel is null!");\r
50                 }\r
51 \r
52                 if (fCharacter == CHARACTER) {\r
53                     panel.modifyCharacterStyleOnSelection(modifier);\r
54                 }\r
55                 else {\r
56                     panel.modifyParagraphStyleOnSelection(modifier);\r
57                 }\r
58             }\r
59         });\r
60     }\r
61     \r
62     private static boolean objectsAreEqual(Object lhs, Object rhs) {\r
63     \r
64         if (lhs == null) {\r
65             return rhs == null;\r
66         }\r
67         else {\r
68             return lhs.equals(rhs);\r
69         }\r
70     }\r
71 \r
72     protected void setChecked() {\r
73 \r
74         MTextPanel panel = getTextPanel();\r
75         AttributeMap defaults = panel.getDefaultValues();\r
76         \r
77         for (int i=0; i < fKeys.length; i++) {\r
78             Object defaultV = defaults.get(fKeys[i]);\r
79             \r
80             Object value = (fCharacter == CHARACTER)? \r
81                     panel.getCharacterStyleOverSelection(fKeys[i]) :\r
82                     panel.getParagraphStyleOverSelection(fKeys[i]);\r
83                     \r
84             if (!objectsAreEqual(defaultV, value)) {\r
85                 fItem.setState(false);\r
86                 return;\r
87             }\r
88         }\r
89 \r
90         fItem.setState(true);\r
91     }\r
92 \r
93     public boolean respondsToEventType(int type) {\r
94         \r
95         return type == TextPanelEvent.SELECTION_STYLES_CHANGED;\r
96     }\r
97     \r
98     public final void textEventOccurred(TextPanelEvent event) {\r
99         \r
100         setChecked();\r
101     }\r
102 }\r