01: /*
02: * AutoCompletionAction.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.actions;
13:
14: import java.awt.event.ActionEvent;
15: import java.awt.event.KeyEvent;
16: import javax.swing.KeyStroke;
17: import workbench.db.WbConnection;
18: import workbench.gui.completion.CompletionHandler;
19: import workbench.gui.editor.JEditTextArea;
20: import workbench.interfaces.StatusBar;
21: import workbench.resource.ResourceMgr;
22:
23: /**
24: * Action to display the code-completion for SQL statements.
25: * @see workbench.gui.completion.DefaultCompletionHandler
26: *
27: * @author support@sql-workbench.net
28: */
29: public class AutoCompletionAction extends WbAction {
30: private CompletionHandler handler;
31: private JEditTextArea editor;
32: private StatusBar status;
33:
34: public AutoCompletionAction(JEditTextArea editor, StatusBar status) {
35: this .editor = editor;
36: this .status = status;
37: this .initMenuDefinition("MnuTxtAutoComplete", KeyStroke
38: .getKeyStroke(KeyEvent.VK_SPACE, KeyEvent.CTRL_MASK));
39: this .setMenuItemName(ResourceMgr.MNU_TXT_SQL);
40: this .setEnabled(false);
41:
42: // we have to register this keybinding with the editor
43: // otherwise Ctrl-Space will not work properly
44: editor.addKeyBinding(this );
45: }
46:
47: public void closePopup() {
48: if (handler != null)
49: handler.cancelPopup();
50: }
51:
52: public void setConnection(WbConnection conn) {
53: if (conn == null) {
54: this .handler = null;
55: } else if (this .handler == null) {
56: try {
57: // Use reflection to create the instance so
58: // that the classes are not loaded during startup
59: this .handler = (CompletionHandler) Class
60: .forName(
61: "workbench.gui.completion.DefaultCompletionHandler")
62: .newInstance();
63: } catch (Exception e) {
64: e.printStackTrace();
65: }
66: }
67:
68: if (conn != null) {
69: this .handler.setStatusBar(status);
70: this .handler.setEditor(editor);
71: this .handler.setConnection(conn);
72: }
73:
74: this .setEnabled(conn != null);
75: }
76:
77: public void setAccelerator(KeyStroke key) {
78: KeyStroke old = this .getAccelerator();
79: editor.removeKeyBinding(old);
80: super .setAccelerator(key);
81: editor.addKeyBinding(this );
82: }
83:
84: public void executeAction(ActionEvent e) {
85: handler.showCompletionPopup();
86: }
87: }
|