|
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 |
| package edu.rice.cs.drjava.ui.config; |
|
38 |
| |
|
39 |
| import edu.rice.cs.drjava.config.*; |
|
40 |
| import edu.rice.cs.drjava.*; |
|
41 |
| import edu.rice.cs.util.swing.SwingFrame; |
|
42 |
| import edu.rice.cs.util.swing.Utilities; |
|
43 |
| |
|
44 |
| import java.awt.*; |
|
45 |
| import java.awt.event.*; |
|
46 |
| import java.util.HashMap; |
|
47 |
| import javax.swing.*; |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| public class KeyStrokeOptionComponent extends OptionComponent<KeyStroke,JPanel> |
|
54 |
| implements Comparable<KeyStrokeOptionComponent> { |
|
55 |
| private static final int DIALOG_HEIGHT = 185; |
|
56 |
| |
|
57 |
| public static final HashMap<KeyStroke, KeyStrokeOptionComponent> _keyToKSOC = |
|
58 |
| new HashMap<KeyStroke, KeyStrokeOptionComponent>(); |
|
59 |
| private JButton _button; |
|
60 |
| private JPanel _panel; |
|
61 |
| private static GetKeyDialog _getKeyDialog = null; |
|
62 |
| |
|
63 |
| private KeyStroke _key; |
|
64 |
| |
|
65 |
3
| public KeyStrokeOptionComponent(KeyStrokeOption opt, String text, final SwingFrame parent) {
|
|
66 |
3
| super(opt, text, parent);
|
|
67 |
| |
|
68 |
3
| _key = DrJava.getConfig().getSetting(opt);
|
|
69 |
| |
|
70 |
3
| _button = new JButton();
|
|
71 |
3
| _button.addActionListener(new ActionListener() {
|
|
72 |
0
| public void actionPerformed(ActionEvent ae) {
|
|
73 |
| |
|
74 |
0
| if (_getKeyDialog == null) { _getKeyDialog = new GetKeyDialog(parent, "Specify Shortcut", true); }
|
|
75 |
| |
|
76 |
0
| String oldText = _button.getText();
|
|
77 |
0
| _getKeyDialog.promptKey(KeyStrokeOptionComponent.this);
|
|
78 |
0
| if (!_button.getText().equals(oldText)) { notifyChangeListeners(); }
|
|
79 |
| } |
|
80 |
| }); |
|
81 |
3
| _button.setText(_option.format(_key));
|
|
82 |
| |
|
83 |
3
| _panel = new JPanel(new BorderLayout());
|
|
84 |
3
| _panel.add(_button, BorderLayout.CENTER);
|
|
85 |
| |
|
86 |
3
| GridLayout gl = new GridLayout(1,0);
|
|
87 |
3
| gl.setHgap(15);
|
|
88 |
3
| _keyToKSOC.put(_key, this);
|
|
89 |
3
| setComponent(_panel);
|
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
0
| public KeyStrokeOptionComponent(KeyStrokeOption opt, String text,
|
|
94 |
| SwingFrame parent, String description) { |
|
95 |
0
| this(opt, text, parent);
|
|
96 |
0
| setDescription(description);
|
|
97 |
| } |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
0
| public void setDescription(String description) {
|
|
103 |
0
| _panel.setToolTipText(description);
|
|
104 |
0
| _button.setToolTipText(description);
|
|
105 |
0
| _label.setToolTipText(description);
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
0
| public String toString() {
|
|
110 |
0
| return "<KSOC>label:" + getLabelText() + "ks: " +
|
|
111 |
| getKeyStroke() + "jb: " + _button.getText() + "</KSOC>\n"; |
|
112 |
| } |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
4
| public boolean updateConfig() {
|
|
118 |
4
| if (!_key.equals(getConfigKeyStroke())) {
|
|
119 |
3
| DrJava.getConfig().setSetting(_option, _key);
|
|
120 |
3
| setValue(_key);
|
|
121 |
| } |
|
122 |
4
| return true;
|
|
123 |
| } |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
11
| public void setValue(KeyStroke value) {
|
|
128 |
11
| _key = value;
|
|
129 |
11
| _button.setText(_option.format(value));
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
0
| public int compareTo(KeyStrokeOptionComponent other) {
|
|
136 |
0
| return this.getLabelText().compareTo(other.getLabelText());
|
|
137 |
| } |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
0
| public KeyStroke getKeyStroke() {
|
|
142 |
0
| return _key;
|
|
143 |
| } |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
4
| public KeyStroke getConfigKeyStroke() {
|
|
148 |
4
| return DrJava.getConfig().getSetting(_option);
|
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| private class GetKeyDialog extends JDialog { |
|
156 |
| private InputField _inputField; |
|
157 |
| private JButton _clearButton; |
|
158 |
| private JButton _cancelButton; |
|
159 |
| private JButton _okButton; |
|
160 |
| private JLabel _instructionLabel; |
|
161 |
| private JLabel _currentLabel; |
|
162 |
| private JLabel _actionLabel; |
|
163 |
| private JPanel _inputAndClearPanel; |
|
164 |
| |
|
165 |
| private JPanel _cancelAndOKPanel; |
|
166 |
| private KeyStroke _currentKeyStroke; |
|
167 |
| private KeyStrokeOptionComponent _ksoc; |
|
168 |
| |
|
169 |
| |
|
170 |
0
| public GetKeyDialog(SwingFrame f, String title, boolean modal) {
|
|
171 |
0
| super(f, title, modal);
|
|
172 |
| |
|
173 |
| |
|
174 |
0
| _inputField = new InputField();
|
|
175 |
0
| _clearButton = new JButton("Clear");
|
|
176 |
0
| _clearButton.addActionListener(new ActionListener() {
|
|
177 |
0
| public void actionPerformed(ActionEvent ae) {
|
|
178 |
0
| _inputField.setText("");
|
|
179 |
0
| _actionLabel.setText("<none>");
|
|
180 |
0
| _currentKeyStroke = KeyStrokeOption.NULL_KEYSTROKE;
|
|
181 |
0
| _inputField.requestFocusInWindow();
|
|
182 |
| } |
|
183 |
| }); |
|
184 |
0
| _cancelButton = new JButton("Cancel");
|
|
185 |
0
| _cancelButton.addActionListener(new ActionListener() {
|
|
186 |
0
| public void actionPerformed(ActionEvent ae) {
|
|
187 |
0
| _inputField.requestFocusInWindow();
|
|
188 |
0
| GetKeyDialog.this.dispose();
|
|
189 |
| } |
|
190 |
| }); |
|
191 |
0
| _okButton = new JButton("OK");
|
|
192 |
0
| _okButton.addActionListener(new ActionListener() {
|
|
193 |
0
| public void actionPerformed(ActionEvent ae) {
|
|
194 |
0
| if (!_ksoc.getKeyStroke().equals(_currentKeyStroke)) {
|
|
195 |
0
| _keyToKSOC.remove(_ksoc.getKeyStroke());
|
|
196 |
| |
|
197 |
0
| KeyStrokeOptionComponent conflict = _keyToKSOC.get(_currentKeyStroke);
|
|
198 |
| |
|
199 |
0
| if (conflict != null) {
|
|
200 |
0
| _keyToKSOC.remove(_currentKeyStroke);
|
|
201 |
0
| conflict.setValue(KeyStrokeOption.NULL_KEYSTROKE);
|
|
202 |
| } |
|
203 |
0
| _keyToKSOC.put(_currentKeyStroke, _ksoc);
|
|
204 |
0
| _ksoc.setValue(_currentKeyStroke);
|
|
205 |
| } |
|
206 |
0
| _inputField.requestFocusInWindow();
|
|
207 |
0
| GetKeyDialog.this.dispose();
|
|
208 |
| } |
|
209 |
| }); |
|
210 |
0
| _instructionLabel = new JLabel("Type in the keystroke you want to use " +
|
|
211 |
| "and click \"OK\""); |
|
212 |
0
| _currentLabel = new JLabel("Current action bound to the keystroke:");
|
|
213 |
0
| _actionLabel = new JLabel("<none>");
|
|
214 |
| |
|
215 |
0
| _inputAndClearPanel = new JPanel(new BorderLayout());
|
|
216 |
0
| _inputAndClearPanel.add(_inputField, BorderLayout.CENTER);
|
|
217 |
0
| _inputAndClearPanel.add(_clearButton, BorderLayout.EAST);
|
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
0
| _cancelAndOKPanel = new JPanel(new GridLayout(1,0));
|
|
224 |
0
| _cancelAndOKPanel.add(_okButton);
|
|
225 |
0
| _cancelAndOKPanel.add(_cancelButton);
|
|
226 |
| |
|
227 |
0
| JPanel panel = (JPanel)this.getContentPane();
|
|
228 |
| |
|
229 |
0
| panel.setLayout(new GridLayout(0, 1));
|
|
230 |
0
| panel.add(_instructionLabel);
|
|
231 |
0
| panel.add(_inputAndClearPanel);
|
|
232 |
| |
|
233 |
0
| panel.add(_currentLabel);
|
|
234 |
0
| panel.add(_actionLabel);
|
|
235 |
0
| panel.add(_cancelAndOKPanel);
|
|
236 |
0
| setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
|
|
237 |
| |
|
238 |
0
| EventQueue.invokeLater(new Runnable() { public void run() { GetKeyDialog.this.pack(); } });
|
|
239 |
| } |
|
240 |
| |
|
241 |
0
| public void promptKey(KeyStrokeOptionComponent k) {
|
|
242 |
0
| _ksoc = k;
|
|
243 |
0
| _instructionLabel.setText("Type in the keystroke you want to use for \"" +
|
|
244 |
| k.getLabelText() + |
|
245 |
| "\" and click \"OK\""); |
|
246 |
0
| _currentKeyStroke = k.getKeyStroke();
|
|
247 |
0
| _actionLabel.setText(k.getLabelText());
|
|
248 |
0
| _inputField.setText(_option.format(_currentKeyStroke));
|
|
249 |
| |
|
250 |
0
| this.setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
|
|
251 |
0
| Utilities.setPopupLoc(this, getOwner());
|
|
252 |
0
| this.setVisible(true);
|
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| private class InputField extends JTextField { |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
0
| public void processKeyEvent(KeyEvent e) {
|
|
264 |
0
| KeyStroke ks = KeyStroke.getKeyStrokeForEvent(e);
|
|
265 |
0
| if (e.getID() == KeyEvent.KEY_PRESSED) {
|
|
266 |
0
| this.setText(_option.format(ks));
|
|
267 |
0
| KeyStrokeOptionComponent configKs = _keyToKSOC.get(ks);
|
|
268 |
0
| if (configKs == null)
|
|
269 |
0
| _actionLabel.setText("<none>");
|
|
270 |
| else { |
|
271 |
0
| String name = configKs.getLabelText();
|
|
272 |
0
| _actionLabel.setText(name);
|
|
273 |
| } |
|
274 |
0
| _currentKeyStroke = ks;
|
|
275 |
| } |
|
276 |
| } |
|
277 |
| } |
|
278 |
| } |
|
279 |
| |
|
280 |
| } |