01: package net.sourceforge.squirrel_sql.plugins.hibernate.completion;
02:
03: import net.sourceforge.squirrel_sql.fw.completion.CompletionCandidates;
04: import net.sourceforge.squirrel_sql.fw.completion.ICompletorModel;
05: import net.sourceforge.squirrel_sql.fw.completion.util.CompletionParser;
06: import net.sourceforge.squirrel_sql.plugins.hibernate.IHibernateConnectionProvider;
07: import net.sourceforge.squirrel_sql.plugins.hibernate.ConnectionListener;
08: import net.sourceforge.squirrel_sql.plugins.hibernate.HqlSyntaxHighlightTokenMatcherProxy;
09: import net.sourceforge.squirrel_sql.plugins.hibernate.HibernateConnection;
10: import net.sourceforge.squirrel_sql.plugins.hibernate.configuration.HibernateConfiguration;
11:
12: import java.util.ArrayList;
13:
14: public class HQLCodeCompletorModel implements ICompletorModel {
15:
16: private HQLCompletionInfoCollection _codeCompletionInfos;
17: private IHibernateConnectionProvider _hibernateConnectionProvider;
18: private HQLAliasFinder _hqlAliasFinder;
19: private HqlSyntaxHighlightTokenMatcherProxy _hqlSyntaxHighlightTokenMatcherProxy;
20:
21: public HQLCodeCompletorModel(
22: IHibernateConnectionProvider hibernateConnectionProvider,
23: HQLAliasFinder hqlAliasFinder,
24: HqlSyntaxHighlightTokenMatcherProxy hqlSyntaxHighlightTokenMatcherProxy) {
25: _hibernateConnectionProvider = hibernateConnectionProvider;
26: _hqlAliasFinder = hqlAliasFinder;
27: _hqlSyntaxHighlightTokenMatcherProxy = hqlSyntaxHighlightTokenMatcherProxy;
28:
29: _hibernateConnectionProvider
30: .addConnectionListener(new ConnectionListener() {
31: public void connectionOpened(
32: HibernateConnection con,
33: HibernateConfiguration cfg) {
34: init();
35: }
36:
37: public void connectionClosed() {
38: _hqlAliasFinder.stop();
39: _codeCompletionInfos = null;
40: _hqlSyntaxHighlightTokenMatcherProxy
41: .setDelegate(null);
42:
43: }
44: });
45: }
46:
47: public CompletionCandidates getCompletionCandidates(
48: String textTillCarret) {
49: init();
50:
51: CompletionParser parser = new CompletionParser(textTillCarret,
52: true);
53: return _codeCompletionInfos.getInfosStartingWith(parser);
54:
55: }
56:
57: private void init() {
58: if (null == _codeCompletionInfos) {
59: _codeCompletionInfos = new HQLCompletionInfoCollection(
60: _hibernateConnectionProvider
61: .getHibernateConnection());
62:
63: AliasFinderListener aliasFinderListener = new AliasFinderListener() {
64:
65: public void aliasesFound(ArrayList<AliasInfo> aliasInfos) {
66: onAliasesFound(aliasInfos);
67: }
68: };
69:
70: _hqlAliasFinder.start(_codeCompletionInfos,
71: aliasFinderListener);
72:
73: _hqlSyntaxHighlightTokenMatcherProxy
74: .setDelegate(_codeCompletionInfos
75: .getHqlSyntaxHighlightTokenMatcher());
76: }
77: }
78:
79: private void onAliasesFound(ArrayList<AliasInfo> aliasInfos) {
80: _codeCompletionInfos.setCurrentAliasInfos(aliasInfos);
81: }
82:
83: }
|