]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textpanel/PanelEventBroadcaster.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textpanel / PanelEventBroadcaster.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.textpanel;\r
14 \r
15 import java.util.Vector;\r
16 \r
17 /**\r
18  * This class listens for text state change notifications\r
19  * and broadcasts them to all of its listeners.\r
20  */\r
21 final class PanelEventBroadcaster {\r
22 \r
23     static final String COPYRIGHT =\r
24                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
25     private static final int FIRST = TextPanelEvent.TEXT_PANEL_FIRST;\r
26 \r
27     private final Vector[] fListeners;\r
28     private final TextPanelEvent[] fEvents;\r
29 \r
30     /**\r
31      * Construct a new PanelEventBroadcaster.\r
32      * @param panel the TextPanel for which events are broadcasted\r
33      */\r
34     public PanelEventBroadcaster(MTextPanel panel) {\r
35 \r
36         int count = TextPanelEvent.TEXT_PANEL_LAST - FIRST + 1;\r
37 \r
38         fEvents = new TextPanelEvent[count];\r
39         fListeners = new Vector[count];\r
40         \r
41         for (int i=0; i < fListeners.length; i++) {\r
42             fEvents[i] = new TextPanelEvent(panel, i+FIRST);\r
43             fListeners[i] = new Vector();\r
44         }\r
45     }\r
46 \r
47     /**\r
48      * Add the given TextPanelListener to the TextPanelListeners to\r
49      * which notifications are forwarded.\r
50      * @param listener the listener to add\r
51      */\r
52     public synchronized void addListener(TextPanelListener listener) {\r
53 \r
54         for (int i=FIRST; i <= TextPanelEvent.TEXT_PANEL_LAST; i++) {\r
55             Vector listeners = fListeners[i-FIRST];\r
56             if (listener.respondsToEventType(i)) {\r
57                 if (!listeners.contains(listener)) {\r
58                     listeners.addElement(listener);\r
59                 }\r
60             }\r
61         }\r
62     }\r
63 \r
64     /**\r
65      * Remove the given TextPanelListener from the TextPanelListeners to\r
66      * which notifications are forwarded.\r
67      * @param listener the listener to remove\r
68      */\r
69     public synchronized void removeListener(TextPanelListener listener) {\r
70 \r
71         for (int i=FIRST; i <= TextPanelEvent.TEXT_PANEL_LAST; i++) {\r
72             Vector listeners = fListeners[i-FIRST];\r
73             if (listener.respondsToEventType(i)) {\r
74                 listeners.removeElement(listener);\r
75             }\r
76         }\r
77     }\r
78 \r
79     /**\r
80      * Receive a notification and forward it to all listeners.\r
81      * @changeCode one of the constants in the TextPanelListener class\r
82      */\r
83     public synchronized void textStateChanged(int id) {\r
84 \r
85         int index = id-FIRST;\r
86         TextPanelEvent event = fEvents[index];\r
87         Vector listeners = fListeners[index];\r
88         \r
89         int size = listeners.size();\r
90 \r
91         for (int i=0; i < size; i++) {\r
92 \r
93             TextPanelListener listener =\r
94                             (TextPanelListener) listeners.elementAt(i);\r
95             listener.textEventOccurred(event);\r
96         }\r
97     }\r
98 }\r