01: /*
02: * CheckPreparedStatementsAction.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.beans.PropertyChangeEvent;
16: import java.beans.PropertyChangeListener;
17:
18: import javax.swing.Action;
19: import javax.swing.JCheckBoxMenuItem;
20: import javax.swing.JMenu;
21:
22: import workbench.resource.ResourceMgr;
23: import workbench.resource.Settings;
24:
25: /**
26: * Action to toggle the detection of prepared statements during SQL execution
27: *
28: * @see workbench.resource.Settings#setCheckPreparedStatements(boolean)
29: *
30: * @author support@sql-workbench.net
31: */
32: public class CheckPreparedStatementsAction extends WbAction implements
33: PropertyChangeListener {
34: private boolean switchedOn = false;
35: private JCheckBoxMenuItem toggleMenu;
36: private boolean inSetter = false;
37:
38: public CheckPreparedStatementsAction() {
39: super ();
40: this .initMenuDefinition("MnuTxtCheckPrepared");
41: this .setMenuItemName(ResourceMgr.MNU_TXT_SQL);
42: this .switchedOn = Settings.getInstance()
43: .getCheckPreparedStatements();
44: Settings.getInstance().addPropertyChangeListener(this ,
45: "workbench.sql.checkprepared");
46: }
47:
48: public void executeAction(ActionEvent e) {
49: this .setSwitchedOn(!this .switchedOn);
50: }
51:
52: public boolean isSwitchedOn() {
53: return this .switchedOn;
54: }
55:
56: private void setSwitchedOn(boolean aFlag) {
57: try {
58: this .switchedOn = aFlag;
59: if (this .toggleMenu != null)
60: this .toggleMenu.setSelected(aFlag);
61: inSetter = true;
62: Settings.getInstance().setCheckPreparedStatements(
63: this .switchedOn);
64: } finally {
65: inSetter = false;
66: }
67: }
68:
69: public void addToMenu(JMenu aMenu) {
70: if (this .toggleMenu == null) {
71: this .toggleMenu = new JCheckBoxMenuItem();
72: this .toggleMenu.setAction(this );
73: String text = this .getValue(Action.NAME).toString();
74: int pos = text.indexOf('&');
75: if (pos > -1) {
76: char mnemonic = text.charAt(pos + 1);
77: text = text.substring(0, pos) + text.substring(pos + 1);
78: this .toggleMenu.setMnemonic((int) mnemonic);
79: }
80: this .toggleMenu.setText(text);
81: this .toggleMenu.setSelected(this .switchedOn);
82: }
83: aMenu.add(this .toggleMenu);
84: }
85:
86: public void propertyChange(PropertyChangeEvent evt) {
87: if (!inSetter) {
88: this.switchedOn = Settings.getInstance()
89: .getCheckPreparedStatements();
90: if (this.toggleMenu != null)
91: this.toggleMenu.setSelected(this.switchedOn);
92: }
93: }
94:
95: }
|