01: package net.sourceforge.squirrel_sql.client.session.action;
02:
03: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
04: import net.sourceforge.squirrel_sql.client.session.ISession;
05: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
06: import net.sourceforge.squirrel_sql.client.IApplication;
07: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
08: import net.sourceforge.squirrel_sql.fw.gui.IToggleAction;
09: import net.sourceforge.squirrel_sql.fw.gui.ToggleComponentHolder;
10: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
11: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
12:
13: import java.awt.event.ActionEvent;
14: import java.beans.PropertyChangeListener;
15: import java.beans.PropertyChangeEvent;
16:
17: /**
18: * This <CODE>Action</CODE> allows the user to commit the current SQL
19: * transaction.
20: *
21: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
22: */
23: public class ToggleAutoCommitAction extends SquirrelAction implements
24: ISessionAction, IToggleAction {
25: static final long serialVersionUID = 4894924988552643833L;
26:
27: private ISession _session;
28: private PropertyChangeListener _propertyListener;
29: private boolean _inActionPerformed;
30: private ToggleComponentHolder _toogleComponentHolder;
31:
32: public ToggleAutoCommitAction(IApplication app) {
33: super (app);
34:
35: _toogleComponentHolder = new ToggleComponentHolder();
36:
37: _propertyListener = new PropertyChangeListener() {
38: public void propertyChange(PropertyChangeEvent evt) {
39: if (SessionProperties.IPropertyNames.AUTO_COMMIT
40: .equals(evt.getPropertyName())) {
41: Boolean autoCom = (Boolean) evt.getNewValue();
42: _toogleComponentHolder.setSelected(autoCom
43: .booleanValue());
44: }
45: }
46: };
47:
48: setEnabled(false);
49: }
50:
51: public void setSession(final ISession session) {
52: if (null != _session) {
53: _session.getProperties().removePropertyChangeListener(
54: _propertyListener);
55: }
56: _session = session;
57:
58: GUIUtils.processOnSwingEventThread(new Runnable() {
59: public void run() {
60: if (session == null || session.getProperties() == null) {
61: setEnabled(false);
62: _toogleComponentHolder.setSelected(false);
63: } else {
64: SessionProperties props = session.getProperties();
65: setEnabled(true);
66: props.addPropertyChangeListener(_propertyListener);
67: _toogleComponentHolder.setSelected(props
68: .getAutoCommit());
69: }
70: }
71: });
72:
73: }
74:
75: public ToggleComponentHolder getToggleComponentHolder() {
76: return _toogleComponentHolder;
77: }
78:
79: public void actionPerformed(ActionEvent evt) {
80: try {
81: if (_inActionPerformed) {
82: return;
83: }
84:
85: _inActionPerformed = true;
86: _session.getProperties().setAutoCommit(
87: _toogleComponentHolder.isSelected());
88: } finally {
89: _inActionPerformed = false;
90: }
91: }
92: }
|