KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > debug > InputBufferEventSupport


1 package antlr.debug;
2
3 import java.util.Vector JavaDoc;
4 import antlr.collections.impl.BitSet;
5 import antlr.RecognitionException;
6
7 public class InputBufferEventSupport {
8     private Object JavaDoc source;
9     private Vector JavaDoc inputBufferListeners;
10     private InputBufferEvent inputBufferEvent;
11     protected static final int CONSUME=0;
12     protected static final int LA=1;
13     protected static final int MARK=2;
14     protected static final int REWIND=3;
15
16
17     public InputBufferEventSupport(Object JavaDoc source) {
18         inputBufferEvent = new InputBufferEvent(source);
19         this.source = source;
20     }
21     public void addInputBufferListener(InputBufferListener l) {
22         if (inputBufferListeners == null) inputBufferListeners = new Vector JavaDoc();
23         inputBufferListeners.addElement(l);
24     }
25     public void fireConsume(char c) {
26         inputBufferEvent.setValues(InputBufferEvent.CONSUME, c, 0);
27         fireEvents(CONSUME, inputBufferListeners);
28     }
29     public void fireEvent(int type, ListenerBase l) {
30         switch(type) {
31             case CONSUME: ((InputBufferListener)l).inputBufferConsume(inputBufferEvent); break;
32             case LA: ((InputBufferListener)l).inputBufferLA(inputBufferEvent); break;
33             case MARK: ((InputBufferListener)l).inputBufferMark(inputBufferEvent); break;
34             case REWIND: ((InputBufferListener)l).inputBufferRewind(inputBufferEvent); break;
35             default:
36                 throw new IllegalArgumentException JavaDoc("bad type "+type+" for fireEvent()");
37         }
38     }
39     public void fireEvents(int type, Vector JavaDoc listeners) {
40         Vector JavaDoc targets=null;
41         ListenerBase l=null;
42         
43         synchronized (this) {
44             if (listeners == null) return;
45             targets = (Vector JavaDoc)listeners.clone();
46         }
47         
48         if (targets != null)
49             for (int i = 0; i < targets.size(); i++) {
50                 l = (ListenerBase)targets.elementAt(i);
51                 fireEvent(type, l);
52             }
53     }
54     public void fireLA(char c, int la) {
55         inputBufferEvent.setValues(InputBufferEvent.LA, c, la);
56         fireEvents(LA, inputBufferListeners);
57     }
58     public void fireMark(int pos) {
59         inputBufferEvent.setValues(InputBufferEvent.MARK, ' ', pos);
60         fireEvents(MARK, inputBufferListeners);
61     }
62     public void fireRewind(int pos) {
63         inputBufferEvent.setValues(InputBufferEvent.REWIND, ' ', pos);
64         fireEvents(REWIND, inputBufferListeners);
65     }
66     public Vector JavaDoc getInputBufferListeners() {
67         return inputBufferListeners;
68     }
69     protected void refresh(Vector JavaDoc listeners) {
70         Vector JavaDoc v;
71         synchronized (listeners) {
72             v = (Vector JavaDoc)listeners.clone();
73         }
74         if (v != null)
75             for (int i = 0; i < v.size(); i++)
76                 ((ListenerBase)v.elementAt(i)).refresh();
77     }
78     public void refreshListeners() {
79         refresh(inputBufferListeners);
80     }
81     public void removeInputBufferListener(InputBufferListener l) {
82         if (inputBufferListeners != null)
83             inputBufferListeners.removeElement(l);
84     }
85 }
Popular Tags