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