001: package net.sourceforge.squirrel_sql.client.session.parser;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import java.awt.event.KeyAdapter;
006: import java.awt.event.KeyEvent;
007: import java.util.Vector;
008:
009: import javax.swing.SwingUtilities;
010: import javax.swing.Timer;
011:
012: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
013: import net.sourceforge.squirrel_sql.client.session.ISession;
014: import net.sourceforge.squirrel_sql.client.session.parser.kernel.ErrorInfo;
015: import net.sourceforge.squirrel_sql.client.session.parser.kernel.ParserThread;
016: import net.sourceforge.squirrel_sql.client.session.parser.kernel.ParsingFinishedListener;
017: import net.sourceforge.squirrel_sql.client.session.parser.kernel.TableAliasInfo;
018: import net.sourceforge.squirrel_sql.fw.util.BaseRuntimeException;
019:
020: public class ParserEventsProcessor implements IParserEventsProcessor {
021: private Timer _parserTimer;
022: private ParserThread _parserThread;
023: private Vector<ParserEventsListener> _listeners = new Vector<ParserEventsListener>();
024: private ISession _session;
025: private ISQLPanelAPI _sqlPanelApi;
026: private KeyAdapter _triggerParserKeyListener;
027: private boolean _processingEnded;
028:
029: public ParserEventsProcessor(ISQLPanelAPI sqlPanelApi,
030: ISession session) {
031: _session = session;
032: _sqlPanelApi = sqlPanelApi;
033:
034: ActionListener al = new ActionListener() {
035: public void actionPerformed(ActionEvent e) {
036: onTimerStart();
037: }
038: };
039:
040: _triggerParserKeyListener = new KeyAdapter() {
041: public void keyTyped(KeyEvent e) {
042: onKeyTyped(e);
043: }
044: };
045:
046: _parserTimer = new Timer(500, al);
047: _parserTimer.start();
048: }
049:
050: private void onParserExitedOnException(final Throwable e) {
051: SwingUtilities.invokeLater(new Runnable() {
052: public void run() {
053: throw new BaseRuntimeException(e);
054: }
055: });
056:
057: }
058:
059: public void addParserEventsListener(ParserEventsListener l) {
060: if (_listeners != null && l != null) {
061: _listeners.add(l);
062: }
063: }
064:
065: public void removeParserEventsListener(ParserEventsListener l) {
066: if (_listeners != null && l != null) {
067: _listeners.add(l);
068: }
069: }
070:
071: public void endProcessing() {
072: _processingEnded = true;
073:
074: _sqlPanelApi.getSQLEntryPanel().getTextComponent()
075: .removeKeyListener(_triggerParserKeyListener);
076:
077: if (_parserTimer != null) {
078: _parserTimer.stop();
079: }
080:
081: if (_parserThread != null) {
082: _parserThread.exitThread();
083: }
084:
085: _session = null;
086: _sqlPanelApi = null;
087: _listeners = null;
088:
089: }
090:
091: public void triggerParser() {
092: _parserTimer.restart();
093: }
094:
095: private void onParsingFinished() {
096: SwingUtilities.invokeLater(new Runnable() {
097: public void run() {
098: fireParsingFinished();
099: }
100: });
101: }
102:
103: private void fireParsingFinished() {
104: if (_processingEnded) {
105: return;
106: }
107:
108: ParserEventsListener[] clone = _listeners
109: .toArray(new ParserEventsListener[_listeners.size()]);
110:
111: TableAliasInfo[] aliasInfos = _parserThread
112: .getTableAliasInfos();
113: ErrorInfo[] errorInfos = _parserThread.getErrorInfos();
114:
115: for (int i = 0; i < clone.length; i++) {
116: clone[i].aliasesFound(aliasInfos);
117: clone[i].errorsFound(errorInfos);
118: }
119:
120: }
121:
122: private void onTimerStart() {
123: if (null == _sqlPanelApi.getSQLEntryPanel()
124: || null == _session.getSchemaInfo()
125: || false == _session.getSchemaInfo().isLoaded()) {
126: // Entry panel or schema info not yet available, try again next time.
127: //System.out.println("ParserEventsProcessor.onTimerStart entry panel not yet set");
128: return;
129: }
130:
131: initParserThread();
132: _parserThread.notifyParser(_sqlPanelApi.getSQLEntryPanel()
133: .getText());
134: }
135:
136: private void initParserThread() {
137: if (null != _parserThread) {
138: return;
139: }
140:
141: _parserThread = new ParserThread(new SQLSchemaImpl(_session));
142:
143: _sqlPanelApi.getSQLEntryPanel().getTextComponent()
144: .addKeyListener(_triggerParserKeyListener);
145:
146: // No more automatic restarts because
147: // key events will restart the parser from now on.
148: _parserTimer.setRepeats(false);
149:
150: _parserThread
151: .setParsingFinishedListener(new ParsingFinishedListener() {
152: public void parsingFinished() {
153: onParsingFinished();
154: }
155:
156: public void parserExitedOnException(Throwable e) {
157: onParserExitedOnException(e);
158: }
159: });
160: }
161:
162: private void onKeyTyped(KeyEvent e) {
163: if (false == e.isActionKey()) {
164: _parserTimer.restart();
165: }
166: }
167:
168: }
|