01: package abbot.editor;
02:
03: import java.beans.*;
04:
05: import javax.swing.*;
06:
07: import abbot.Log;
08: import abbot.editor.actions.*;
09: import abbot.editor.widgets.Mnemonic;
10:
11: /** A custom JCheckBoxMenuItem that listens to the selected
12: * state of its toggle action, reflecting its state when the action changes.
13: */
14: public class CustomCheckBoxMenuItem extends JCheckBoxMenuItem {
15:
16: private PropertyChangeListener pcl;
17:
18: public CustomCheckBoxMenuItem(EditorToggleAction a) {
19: super (a);
20: setName((String) a.getValue(EditorAction.NAME));
21: Integer i = (Integer) a.getValue(EditorAction.MNEMONIC_INDEX);
22: if (i != null)
23: Mnemonic.setDisplayedMnemonicIndex(this , i.intValue());
24: // prior to 1.4, the accelerator key is not automatically set
25: setAccelerator((KeyStroke) a.getValue(Action.ACCELERATOR_KEY));
26: }
27:
28: protected void configurePropertiesFromAction(javax.swing.Action a) {
29: super .configurePropertiesFromAction(a);
30: boolean s = a != null && ((EditorToggleAction) a).isSelected();
31: super .setSelected(s);
32: }
33:
34: protected PropertyChangeListener createActionPropertyChangeListener(
35: Action a) {
36: pcl = super .createActionPropertyChangeListener(a);
37: return new CustomCheckBoxPropertyListener();
38: }
39:
40: private class CustomCheckBoxPropertyListener implements
41: PropertyChangeListener {
42: public void propertyChange(PropertyChangeEvent e) {
43: Log.debug("Got action prop change: " + e.getPropertyName()
44: + ":" + e.getNewValue());
45: pcl.propertyChange(e);
46: if (e.getPropertyName().equals(EditorToggleAction.STATE)) {
47: Boolean val = (Boolean) e.getNewValue();
48: CustomCheckBoxMenuItem.this
49: .setSelected(val == Boolean.TRUE);
50: } else if (e.getPropertyName().equals(
51: EditorAction.MNEMONIC_INDEX)) {
52: Integer i = (Integer) e.getNewValue();
53: int index = i != null ? i.intValue() : -1;
54: Mnemonic.setDisplayedMnemonicIndex(
55: CustomCheckBoxMenuItem.this, index);
56: }
57: }
58: }
59: }
|