01: /*
02: * CheckBoxAction.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 javax.swing.Action;
16: import javax.swing.JCheckBoxMenuItem;
17: import javax.swing.JMenu;
18: import javax.swing.JMenuItem;
19: import workbench.gui.WbSwingUtilities;
20: import workbench.resource.ResourceMgr;
21: import workbench.resource.Settings;
22:
23: /**
24: * Toggle the display of the toolbar in the main window
25: * @author Petr Novotnik
26: */
27: public class CheckBoxAction extends WbAction {
28: private boolean switchedOn = false;
29: private String settingsProperty;
30: private JCheckBoxMenuItem toggleMenu;
31:
32: public CheckBoxAction(String resourceKey, String prop) {
33: super ();
34: this .initMenuDefinition(resourceKey);
35: this .setMenuItemName(ResourceMgr.MNU_TXT_VIEW);
36: this .setIcon(null);
37: this .settingsProperty = prop;
38: if (prop != null) {
39: this .switchedOn = Settings.getInstance().getBoolProperty(
40: settingsProperty);
41: }
42: }
43:
44: public void executeAction(ActionEvent e) {
45: this .setSwitchedOn(!this .switchedOn);
46: }
47:
48: public boolean isSwitchedOn() {
49: return this .switchedOn;
50: }
51:
52: public void setSwitchedOn(boolean aFlag) {
53: this .switchedOn = aFlag;
54: if (this .toggleMenu != null) {
55: WbSwingUtilities.invoke(new Runnable() {
56: public void run() {
57: toggleMenu.setSelected(switchedOn);
58: }
59: });
60: }
61: if (this .settingsProperty != null) {
62: Settings.getInstance().setProperty(settingsProperty,
63: this .switchedOn);
64: }
65: }
66:
67: public JMenuItem getMenuItem() {
68: if (this .toggleMenu == null) {
69: createMenuItem();
70: }
71: return toggleMenu;
72: }
73:
74: private void createMenuItem() {
75: this .toggleMenu = new JCheckBoxMenuItem();
76: this .toggleMenu.setAction(this );
77: String text = this .getValue(Action.NAME).toString();
78: int pos = text.indexOf('&');
79: if (pos > -1) {
80: char mnemonic = text.charAt(pos + 1);
81: text = text.substring(0, pos) + text.substring(pos + 1);
82: this .toggleMenu.setMnemonic((int) mnemonic);
83: }
84: this .toggleMenu.setText(text);
85: this .toggleMenu.setSelected(this .switchedOn);
86:
87: }
88:
89: public void addToMenu(JMenu aMenu) {
90: if (this.toggleMenu == null) {
91: createMenuItem();
92: }
93: aMenu.add(this.toggleMenu);
94: }
95: }
|