-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWrappedAction.java
More file actions
172 lines (147 loc) · 3.97 KB
/
WrappedAction.java
File metadata and controls
172 lines (147 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
/*
* The WrappedAction class is a convenience class that allows you to replace
* an installed Action with a custom Action of your own. There are two
* benefits to using this class:
*
* a) Key Bindings of the original Action are retained for the custom Action.
* b) the original Action is retained so your custom Action can invoke the
* original Action.
*
* This class is abstract so your custom Action must extend this class and
* implement the actionPerformed() method.
*/
abstract class WrappedAction implements Action
{
private Action originalAction;
private JComponent component;
private Object actionKey;
/*
* Replace the default Action for the given KeyStroke with a custom Action
*/
public WrappedAction(JComponent component, KeyStroke keyStroke)
{
this.component = component;
Object actionKey = getKeyForActionMap(component, keyStroke);
if (actionKey == null)
{
String message = "no input mapping for KeyStroke: " + keyStroke;
throw new IllegalArgumentException(message);
}
setActionForKey( actionKey );
}
/*
* Replace the default Action with a custom Action
*/
public WrappedAction(JComponent component, Object actionKey)
{
this.component = component;
setActionForKey( actionKey );
}
/*
* Search the 3 InputMaps to find the KeyStroke binding
*/
private Object getKeyForActionMap(JComponent component, KeyStroke keyStroke)
{
for (int i = 0; i < 3; i++)
{
InputMap inputMap = component.getInputMap(i);
if (inputMap != null)
{
Object key = inputMap.get(keyStroke);
if (key != null)
return key;
}
}
return null;
}
/*
* Replace the existing Action for the given action key with a
* wrapped custom Action
*/
private void setActionForKey(Object actionKey)
{
// Save the original Action
this.actionKey = actionKey;
originalAction = component.getActionMap().get(actionKey);
if (originalAction == null)
{
String message = "no Action for action key: " + actionKey;
throw new IllegalArgumentException(message);
}
// Replace the existing Action with this class
install();
}
/*
* Child classes should use this method to invoke the original Action
*/
public void invokeOriginalAction(ActionEvent e)
{
originalAction.actionPerformed(e);
}
/*
* Install this class as the default Action
*/
public void install()
{
component.getActionMap().put(actionKey, this);
}
/*
* Restore the original Action as the default Action
*/
public void unInstall()
{
component.getActionMap().put(actionKey, originalAction);
}
//
// Delegate the Action interface methods to the original Action
//
public void addPropertyChangeListener(PropertyChangeListener listener)
{
originalAction.addPropertyChangeListener(listener);
}
public Object getValue(String key)
{
return originalAction.getValue(key);
}
public boolean isEnabled()
{
return originalAction.isEnabled();
}
public void putValue(String key, Object newValue)
{
originalAction.putValue(key, newValue);
}
public void removePropertyChangeListener(PropertyChangeListener listener)
{
originalAction.removePropertyChangeListener(listener);
}
public void setEnabled(boolean newValue)
{
originalAction.setEnabled(newValue);
}
//
// Implement some AbstractAction methods
//
public Object[] getKeys()
{
if (originalAction instanceof AbstractAction)
{
AbstractAction abstractAction = (AbstractAction)originalAction;
return abstractAction.getKeys();
}
return null;
}
public PropertyChangeListener[] getPropertyChangeListeners()
{
if (originalAction instanceof AbstractAction)
{
AbstractAction abstractAction = (AbstractAction)originalAction;
return abstractAction.getPropertyChangeListeners();
}
return null;
}
}