001: package net.sourceforge.squirrel_sql.plugins.hibernate.completion;
002:
003: import net.sourceforge.squirrel_sql.client.IApplication;
004: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
005: import net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel;
006: import net.sourceforge.squirrel_sql.fw.completion.CompletionCandidates;
007: import net.sourceforge.squirrel_sql.fw.completion.CompletionInfo;
008: import net.sourceforge.squirrel_sql.fw.completion.Completor;
009: import net.sourceforge.squirrel_sql.fw.completion.CompletorListener;
010: import net.sourceforge.squirrel_sql.plugins.hibernate.HQLEntryPanelManager;
011: import net.sourceforge.squirrel_sql.plugins.hibernate.HibernatePluginResources;
012: import net.sourceforge.squirrel_sql.plugins.hibernate.HqlSyntaxHighlightTokenMatcherProxy;
013: import net.sourceforge.squirrel_sql.plugins.hibernate.IHibernateConnectionProvider;
014:
015: import javax.swing.*;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.KeyEvent;
018:
019: public class HQLCompleteCodeAction extends SquirrelAction {
020: private ISQLEntryPanel _hqlEntryPanel;
021: private IHibernateConnectionProvider _hibernateConnectionProvider;
022: private Completor _cc;
023: private HQLCodeCompletorModel _model;
024:
025: public HQLCompleteCodeAction(
026: IApplication app,
027: HibernatePluginResources resources,
028: HQLEntryPanelManager hqlEntryPanelManager,
029: IHibernateConnectionProvider hibernateConnectionProvider,
030: HqlSyntaxHighlightTokenMatcherProxy hqlSyntaxHighlightTokenMatcherProxy) {
031: super (app, resources);
032: _hqlEntryPanel = hqlEntryPanelManager.getEntryPanel();
033: _hibernateConnectionProvider = hibernateConnectionProvider;
034:
035: _model = new HQLCodeCompletorModel(hibernateConnectionProvider,
036: new HQLAliasFinder(_hqlEntryPanel),
037: hqlSyntaxHighlightTokenMatcherProxy);
038: _cc = new Completor(_hqlEntryPanel.getTextComponent(), _model);
039:
040: _cc.addCodeCompletorListener(new CompletorListener() {
041: public void completionSelected(CompletionInfo completion,
042: int replaceBegin, int keyCode, int modifiers) {
043: performCompletionSelected(completion, replaceBegin,
044: keyCode, modifiers);
045: }
046: });
047:
048: }
049:
050: public void actionPerformed(ActionEvent evt) {
051: if (null != _hibernateConnectionProvider
052: .getHibernateConnection()) {
053: _cc.show();
054: }
055: }
056:
057: private void performCompletionSelected(CompletionInfo completion,
058: int replaceBegin, int keyCode, int modifiers) {
059: if (KeyEvent.VK_SPACE == keyCode
060: && modifiers == KeyEvent.CTRL_MASK) {
061: // Code Completion has been done within Code Completion. Now just replace what all candidates have in common.
062:
063: CompletionCandidates completionCandidates = _model
064: .getCompletionCandidates(_cc.getTextTillCarret());
065:
066: _hqlEntryPanel.setSelectionStart(replaceBegin);
067: _hqlEntryPanel.setSelectionEnd(_hqlEntryPanel
068: .getCaretPosition());
069: _hqlEntryPanel.replaceSelection(completionCandidates
070: .getAllCandidatesPrefix(true));
071:
072: SwingUtilities.invokeLater(new Runnable() {
073: public void run() {
074: _cc.show();
075: }
076: });
077:
078: } else if (KeyEvent.VK_TAB == keyCode) {
079: _hqlEntryPanel.setSelectionStart(replaceBegin);
080: _hqlEntryPanel
081: .setSelectionEnd(getNextWhiteSpacePos(_hqlEntryPanel
082: .getCaretPosition()));
083: _hqlEntryPanel.replaceSelection(completion
084: .getCompletionString());
085: } else {
086: _hqlEntryPanel.setSelectionStart(replaceBegin);
087: _hqlEntryPanel.setSelectionEnd(_hqlEntryPanel
088: .getCaretPosition());
089: _hqlEntryPanel.replaceSelection(completion
090: .getCompletionString());
091: }
092: }
093:
094: private int getNextWhiteSpacePos(int startPos) {
095: String text = _hqlEntryPanel.getText();
096:
097: int retPos = startPos;
098:
099: for (; retPos < text.length(); ++retPos) {
100: if (Character.isWhitespace(text.charAt(retPos))) {
101: return retPos;
102: }
103: }
104:
105: return retPos;
106: }
107: }
|