001: package persistence.antlr.debug;
002:
003: import java.util.Vector;
004: import persistence.antlr.collections.impl.BitSet;
005: import persistence.antlr.RecognitionException;
006:
007: public class InputBufferEventSupport {
008: private Object source;
009: private Vector inputBufferListeners;
010: private InputBufferEvent inputBufferEvent;
011: protected static final int CONSUME = 0;
012: protected static final int LA = 1;
013: protected static final int MARK = 2;
014: protected static final int REWIND = 3;
015:
016: public InputBufferEventSupport(Object source) {
017: inputBufferEvent = new InputBufferEvent(source);
018: this .source = source;
019: }
020:
021: public void addInputBufferListener(InputBufferListener l) {
022: if (inputBufferListeners == null)
023: inputBufferListeners = new Vector();
024: inputBufferListeners.addElement(l);
025: }
026:
027: public void fireConsume(char c) {
028: inputBufferEvent.setValues(InputBufferEvent.CONSUME, c, 0);
029: fireEvents(CONSUME, inputBufferListeners);
030: }
031:
032: public void fireEvent(int type, ListenerBase l) {
033: switch (type) {
034: case CONSUME:
035: ((InputBufferListener) l)
036: .inputBufferConsume(inputBufferEvent);
037: break;
038: case LA:
039: ((InputBufferListener) l).inputBufferLA(inputBufferEvent);
040: break;
041: case MARK:
042: ((InputBufferListener) l).inputBufferMark(inputBufferEvent);
043: break;
044: case REWIND:
045: ((InputBufferListener) l)
046: .inputBufferRewind(inputBufferEvent);
047: break;
048: default:
049: throw new IllegalArgumentException("bad type " + type
050: + " for fireEvent()");
051: }
052: }
053:
054: public void fireEvents(int type, Vector listeners) {
055: Vector targets = null;
056: ListenerBase l = null;
057:
058: synchronized (this ) {
059: if (listeners == null)
060: return;
061: targets = (Vector) listeners.clone();
062: }
063:
064: if (targets != null)
065: for (int i = 0; i < targets.size(); i++) {
066: l = (ListenerBase) targets.elementAt(i);
067: fireEvent(type, l);
068: }
069: }
070:
071: public void fireLA(char c, int la) {
072: inputBufferEvent.setValues(InputBufferEvent.LA, c, la);
073: fireEvents(LA, inputBufferListeners);
074: }
075:
076: public void fireMark(int pos) {
077: inputBufferEvent.setValues(InputBufferEvent.MARK, ' ', pos);
078: fireEvents(MARK, inputBufferListeners);
079: }
080:
081: public void fireRewind(int pos) {
082: inputBufferEvent.setValues(InputBufferEvent.REWIND, ' ', pos);
083: fireEvents(REWIND, inputBufferListeners);
084: }
085:
086: public Vector getInputBufferListeners() {
087: return inputBufferListeners;
088: }
089:
090: protected void refresh(Vector listeners) {
091: Vector v;
092: synchronized (listeners) {
093: v = (Vector) listeners.clone();
094: }
095: if (v != null)
096: for (int i = 0; i < v.size(); i++)
097: ((ListenerBase) v.elementAt(i)).refresh();
098: }
099:
100: public void refreshListeners() {
101: refresh(inputBufferListeners);
102: }
103:
104: public void removeInputBufferListener(InputBufferListener l) {
105: if (inputBufferListeners != null)
106: inputBufferListeners.removeElement(l);
107: }
108: }
|