01: package net.sourceforge.squirrel_sql.plugins.hibernate.completion;
02:
03: import net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel;
04: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
05: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
06: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
07:
08: import java.util.ArrayList;
09: import java.util.Timer;
10: import java.util.TimerTask;
11:
12: public class HQLAliasFinder {
13: private static ILogger s_log = LoggerController
14: .createLogger(HQLAliasFinder.class);
15:
16: private MappingInfoProvider _mappingInfoProvider;
17: private ISQLEntryPanel _hqlEntryPanel;
18: private AliasFinderListener _aliasFinderListener;
19: private Timer _timer;
20:
21: private HqlAliasParser _hqlAliasParser = new HqlAliasParser();
22:
23: public HQLAliasFinder(ISQLEntryPanel hqlEntryPanel) {
24: _hqlEntryPanel = hqlEntryPanel;
25: }
26:
27: public void start(MappingInfoProvider mappingInfoProvider,
28: AliasFinderListener aliasFinderListener) {
29: if (null == _timer) {
30: _mappingInfoProvider = mappingInfoProvider;
31: _aliasFinderListener = aliasFinderListener;
32:
33: // One synchron call for first completion
34: findAliases();
35: start();
36: }
37: }
38:
39: private void start() {
40: _timer = new Timer();
41:
42: _timer.schedule(new TimerTask() {
43: public void run() {
44: findAliases();
45: }
46: }, 1000, 1000);
47: }
48:
49: public void stop() {
50: if (null != _timer) {
51: _timer.cancel();
52: _timer.purge();
53: _timer = null;
54: }
55: }
56:
57: private void findAliases() {
58: try {
59: String hql = _hqlEntryPanel.getText();
60:
61: final ArrayList<AliasInfo> infos = parse(hql);
62:
63: GUIUtils.processOnSwingEventThread(new Runnable() {
64: public void run() {
65: _aliasFinderListener.aliasesFound(infos);
66: }
67: });
68: } catch (Throwable t) {
69: s_log.error("Error in HQLAliasFinder:", t);
70: }
71: }
72:
73: private ArrayList<AliasInfo> parse(String hql) {
74: return _hqlAliasParser.parse(hql, _mappingInfoProvider);
75: }
76: }
|