01: /*
02: * ToggleAutoCommitAction.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: import workbench.db.WbConnection;
18: import workbench.resource.ResourceMgr;
19:
20: /**
21: * An action to toggle the auto commit attribute of the
22: * given {@link workbench.db.WbConnection}
23: * @author support@sql-workbench.net
24: */
25: public class ToggleAutoCommitAction extends CheckBoxAction implements
26: PropertyChangeListener {
27: private WbConnection connection;
28:
29: public ToggleAutoCommitAction() {
30: super ("MnuTxtToggleAutoCommit", null);
31: this .setMenuItemName(ResourceMgr.MNU_TXT_SQL);
32: }
33:
34: public void setConnection(WbConnection conn) {
35: if (this .connection != null) {
36: this .connection.removeChangeListener(this );
37: }
38: this .connection = conn;
39: if (this .connection != null) {
40: this .connection.addChangeListener(this );
41: }
42: this .checkState();
43: }
44:
45: public void executeAction(ActionEvent e) {
46: if (this .connection != null && this .isEnabled()) {
47: this .connection.toggleAutoCommit();
48: checkState();
49: }
50: }
51:
52: private void checkState() {
53: if (this .connection != null) {
54: this .setEnabled(true);
55: this .setSwitchedOn(this .connection.getAutoCommit());
56: } else {
57: this .setEnabled(false);
58: }
59: }
60:
61: public void propertyChange(PropertyChangeEvent evt) {
62: if (evt.getSource() == this.connection
63: && WbConnection.PROP_AUTOCOMMIT.equals(evt
64: .getPropertyName())) {
65: this.checkState();
66: }
67: }
68: }
|