|
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 java.awt.*; |
|
40 |
| import java.awt.event.*; |
|
41 |
| import javax.swing.*; |
|
42 |
| import javax.swing.table.*; |
|
43 |
| import javax.swing.*; |
|
44 |
| |
|
45 |
| import java.util.*; |
|
46 |
| |
|
47 |
| import edu.rice.cs.drjava.*; |
|
48 |
| import edu.rice.cs.drjava.ui.*; |
|
49 |
| import edu.rice.cs.drjava.config.*; |
|
50 |
| import edu.rice.cs.util.swing.SwingFrame; |
|
51 |
| import edu.rice.cs.util.swing.CheckBoxJList; |
|
52 |
| import edu.rice.cs.util.swing.SwingFrame; |
|
53 |
| import edu.rice.cs.util.swing.Utilities; |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| public class VectorKeyStrokeOptionComponent extends VectorOptionComponent<KeyStroke> implements OptionConstants, Comparable<VectorKeyStrokeOptionComponent> { |
|
59 |
| private static final int DIALOG_HEIGHT = 185; |
|
60 |
| public static final HashMap<KeyStroke, VectorKeyStrokeOptionComponent> _keyToKSOC = |
|
61 |
| new HashMap<KeyStroke, VectorKeyStrokeOptionComponent>(); |
|
62 |
| |
|
63 |
| |
|
64 |
0
| public static void resetCurrentKeyStrokeMap() {
|
|
65 |
0
| HashMap<KeyStroke, VectorKeyStrokeOptionComponent> newKeyToKSOC =
|
|
66 |
| new HashMap<KeyStroke, VectorKeyStrokeOptionComponent>(); |
|
67 |
| |
|
68 |
0
| for(VectorKeyStrokeOptionComponent vksoc: _keyToKSOC.values()) {
|
|
69 |
0
| for(KeyStroke k: vksoc.getKeyStrokes()) newKeyToKSOC.put(k, vksoc);
|
|
70 |
| } |
|
71 |
| |
|
72 |
0
| _keyToKSOC.clear();
|
|
73 |
0
| for(Map.Entry<KeyStroke, VectorKeyStrokeOptionComponent> e: newKeyToKSOC.entrySet()) {
|
|
74 |
0
| _keyToKSOC.put(e.getKey(), e.getValue());
|
|
75 |
| } |
|
76 |
| } |
|
77 |
| |
|
78 |
3
| public VectorKeyStrokeOptionComponent (VectorOption<KeyStroke> opt, String text, SwingFrame parent) {
|
|
79 |
3
| this(opt, text, parent, null);
|
|
80 |
3
| for(KeyStroke k: getKeyStrokes()) _keyToKSOC.put(k, this);
|
|
81 |
| } |
|
82 |
| |
|
83 |
| |
|
84 |
23553
| public VectorKeyStrokeOptionComponent (VectorOption<KeyStroke> opt, String text, SwingFrame parent, String description) {
|
|
85 |
23553
| this(opt, text, parent, description, false);
|
|
86 |
16549
| for(KeyStroke k: getKeyStrokes()) _keyToKSOC.put(k, this);
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
23553
| public VectorKeyStrokeOptionComponent (VectorOption<KeyStroke> opt, String text, SwingFrame parent,
|
|
91 |
| String description, boolean moveButtonEnabled) { |
|
92 |
23553
| super(opt, text, parent, new String[] { }, description, moveButtonEnabled);
|
|
93 |
16549
| for(KeyStroke k: getKeyStrokes()) _keyToKSOC.put(k, this);
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
23553
| protected AbstractTableModel _makeTableModel() {
|
|
98 |
23553
| return new AbstractTableModel() {
|
|
99 |
113321
| public int getRowCount() { return _data.size(); }
|
|
100 |
117765
| public int getColumnCount() { return 1; }
|
|
101 |
0
| public Object getValueAt(int row, int col) {
|
|
102 |
0
| switch(col) {
|
|
103 |
0
| case 0: return KeyStrokeOption.formatKeyStroke(_data.get(row));
|
|
104 |
| } |
|
105 |
0
| throw new IllegalArgumentException("Illegal column");
|
|
106 |
| } |
|
107 |
0
| public Class<?> getColumnClass(int col) {
|
|
108 |
0
| switch(col) {
|
|
109 |
0
| case 0: return String.class;
|
|
110 |
| } |
|
111 |
0
| throw new IllegalArgumentException("Illegal column");
|
|
112 |
| } |
|
113 |
| }; |
|
114 |
| } |
|
115 |
| |
|
116 |
| |
|
117 |
0
| public int compareTo(VectorKeyStrokeOptionComponent o) {
|
|
118 |
0
| return this.getLabelText().compareTo(o.getLabelText());
|
|
119 |
| } |
|
120 |
| |
|
121 |
47109
| Vector<KeyStroke> getKeyStrokes() { return new Vector<KeyStroke>(_data); }
|
|
122 |
| |
|
123 |
| |
|
124 |
0
| public void chooseKeyStroke() {
|
|
125 |
0
| _table.getSelectionModel().clearSelection();
|
|
126 |
0
| GetKeyDialog getKeyDialog = new GetKeyDialog(_parent, "Specify Shortcut", true);
|
|
127 |
0
| getKeyDialog.promptKey(KeyStrokeOption.NULL_KEYSTROKE);
|
|
128 |
| } |
|
129 |
| |
|
130 |
| |
|
131 |
0
| protected void _removeIndex(int i) {
|
|
132 |
0
| _keyToKSOC.remove(_data.get(i));
|
|
133 |
0
| super._removeIndex(i);
|
|
134 |
| } |
|
135 |
| |
|
136 |
23553
| protected Action _getAddAction() {
|
|
137 |
23553
| return new AbstractAction("Add") {
|
|
138 |
0
| public void actionPerformed(ActionEvent ae) {
|
|
139 |
0
| chooseKeyStroke();
|
|
140 |
| } |
|
141 |
| }; |
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| public class GetKeyDialog extends JDialog { |
|
149 |
| private InputField _inputField; |
|
150 |
| private JButton _cancelButton; |
|
151 |
| private JButton _okButton; |
|
152 |
| private JLabel _instructionLabel; |
|
153 |
| private JLabel _currentLabel; |
|
154 |
| private JLabel _actionLabel; |
|
155 |
| private JPanel _inputAndClearPanel; |
|
156 |
| private JPanel _cancelAndOKPanel; |
|
157 |
| private KeyStroke _currentKeyStroke; |
|
158 |
| |
|
159 |
0
| public GetKeyDialog(SwingFrame f, String title, boolean modal) {
|
|
160 |
0
| super(f, title, modal);
|
|
161 |
| |
|
162 |
0
| _inputField = new InputField();
|
|
163 |
0
| _cancelButton = new JButton("Cancel");
|
|
164 |
0
| _cancelButton.addActionListener(new ActionListener() {
|
|
165 |
0
| public void actionPerformed(ActionEvent ae) {
|
|
166 |
0
| _inputField.requestFocusInWindow();
|
|
167 |
0
| GetKeyDialog.this.dispose();
|
|
168 |
| } |
|
169 |
| }); |
|
170 |
0
| _okButton = new JButton("OK");
|
|
171 |
0
| _okButton.addActionListener(new ActionListener() {
|
|
172 |
0
| public void actionPerformed(ActionEvent ae) {
|
|
173 |
0
| if ((_currentKeyStroke!=KeyStrokeOption.NULL_KEYSTROKE) &&
|
|
174 |
| (!getKeyStrokes().contains(_currentKeyStroke))) { |
|
175 |
0
| VectorKeyStrokeOptionComponent conflict = _keyToKSOC.get(_currentKeyStroke);
|
|
176 |
0
| if (conflict != null) {
|
|
177 |
| |
|
178 |
| |
|
179 |
0
| Vector<KeyStroke> v = conflict.getKeyStrokes();
|
|
180 |
0
| v.removeElement(_currentKeyStroke);
|
|
181 |
0
| conflict.setValue(v);
|
|
182 |
| } |
|
183 |
| |
|
184 |
0
| _keyToKSOC.put(_currentKeyStroke, VectorKeyStrokeOptionComponent.this);
|
|
185 |
0
| _addValue(_currentKeyStroke);
|
|
186 |
| } |
|
187 |
0
| _inputField.requestFocusInWindow();
|
|
188 |
0
| GetKeyDialog.this.dispose();
|
|
189 |
| } |
|
190 |
| }); |
|
191 |
0
| _instructionLabel = new JLabel("Type in the keystroke you want to use " +
|
|
192 |
| "and click \"OK\""); |
|
193 |
0
| _currentLabel = new JLabel("Current action bound to the keystroke:");
|
|
194 |
0
| _actionLabel = new JLabel("<none>");
|
|
195 |
| |
|
196 |
0
| _inputAndClearPanel = new JPanel(new BorderLayout());
|
|
197 |
0
| _inputAndClearPanel.add(_inputField, BorderLayout.CENTER);
|
|
198 |
| |
|
199 |
0
| _cancelAndOKPanel = new JPanel(new GridLayout(1,0));
|
|
200 |
0
| _cancelAndOKPanel.add(_okButton);
|
|
201 |
0
| _cancelAndOKPanel.add(_cancelButton);
|
|
202 |
| |
|
203 |
0
| JPanel panel = (JPanel)this.getContentPane();
|
|
204 |
| |
|
205 |
0
| panel.setLayout(new GridLayout(0, 1));
|
|
206 |
0
| panel.add(_instructionLabel);
|
|
207 |
0
| panel.add(_inputAndClearPanel);
|
|
208 |
0
| panel.add(_currentLabel);
|
|
209 |
0
| panel.add(_actionLabel);
|
|
210 |
0
| panel.add(_cancelAndOKPanel);
|
|
211 |
0
| setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
|
|
212 |
0
| EventQueue.invokeLater(new Runnable() { public void run() { GetKeyDialog.this.pack(); } });
|
|
213 |
| } |
|
214 |
| |
|
215 |
0
| public void promptKey(KeyStroke initial) {
|
|
216 |
0
| _instructionLabel.setText("Type in the keystroke you want to use for \"" +
|
|
217 |
| getLabelText() + |
|
218 |
| "\" and click \"OK\""); |
|
219 |
0
| _currentKeyStroke = initial;
|
|
220 |
0
| _actionLabel.setText(getLabelText());
|
|
221 |
0
| _inputField.setText(KeyStrokeOption.formatKeyStroke(_currentKeyStroke));
|
|
222 |
0
| this.setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
|
|
223 |
0
| Utilities.setPopupLoc(this, getOwner());
|
|
224 |
0
| this.setVisible(true);
|
|
225 |
| } |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| private class InputField extends JTextField { |
|
231 |
0
| public void processKeyEvent(KeyEvent e) {
|
|
232 |
0
| KeyStroke ks = KeyStroke.getKeyStrokeForEvent(e);
|
|
233 |
0
| if (e.getID() == KeyEvent.KEY_PRESSED) {
|
|
234 |
0
| this.setText(KeyStrokeOption.formatKeyStroke(ks));
|
|
235 |
0
| VectorKeyStrokeOptionComponent configKs = _keyToKSOC.get(ks);
|
|
236 |
0
| if (configKs == null)
|
|
237 |
0
| _actionLabel.setText("<none>");
|
|
238 |
| else { |
|
239 |
0
| String name = configKs.getLabelText();
|
|
240 |
0
| _actionLabel.setText(name);
|
|
241 |
| } |
|
242 |
0
| _currentKeyStroke = ks;
|
|
243 |
| } |
|
244 |
| } |
|
245 |
| } |
|
246 |
| } |
|
247 |
| } |