1 package org.xnap.commons.gui.shortcut;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import javax.swing.Action;
6 import javax.swing.JComponent;
7 import javax.swing.KeyStroke;
8 import org.xnap.commons.settings.KeyStrokeSetting;
9
10 public class MediatorShortcut {
11
12 private JComponent component;
13 private KeyStrokeSetting setting;
14 private Action action;
15 private boolean enabled = true;
16 private PropertyChangeHandler handler = new PropertyChangeHandler();
17
18 public MediatorShortcut(KeyStrokeSetting setting, Action action, JComponent component)
19 {
20 this.component = component;
21 this.action = action;
22 this.setting = setting;
23 component.getActionMap().put(action, action);
24 updateKeyStroke(null);
25 setting.addPropertyChangeListener(handler);
26 }
27
28 private void updateKeyStroke(KeyStroke oldStroke)
29 {
30 if (oldStroke != null && component.getInputMap().get(oldStroke) == action) {
31
32 component.getInputMap().remove(oldStroke);
33 }
34
35 if (enabled) {
36 component.getInputMap().put(setting.getValue(), action);
37 }
38 else {
39 component.getInputMap().remove(setting.getValue());
40 }
41 }
42
43 public JComponent getComonent()
44 {
45 return component;
46 }
47
48 public Action getAction()
49 {
50 return action;
51 }
52
53 public KeyStrokeSetting getSetting()
54 {
55 return setting;
56 }
57
58 public void setEnabled(boolean enabled)
59 {
60 if (this.enabled != enabled) {
61 this.enabled = enabled;
62 updateKeyStroke(null);
63 if (enabled) {
64 setting.addPropertyChangeListener(handler);
65 }
66 else {
67 setting.removePropertyChangeListener(handler);
68 }
69 }
70 }
71
72 public boolean isEnabled()
73 {
74 return enabled;
75 }
76
77 private class PropertyChangeHandler implements PropertyChangeListener
78 {
79
80 public void propertyChange(PropertyChangeEvent evt)
81 {
82 updateKeyStroke((KeyStroke)evt.getOldValue());
83 }
84
85 }
86
87
88
89 }