source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/shortcut/PrefJPanel.java@ 16913

Last change on this file since 16913 was 16913, checked in by simon04, 4 years ago

fix #19698 - Refactoring: make private fields final

  • Property svn:eol-style set to native
File size: 15.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.shortcut;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Color;
8import java.awt.Component;
9import java.awt.Dimension;
10import java.awt.GridBagConstraints;
11import java.awt.GridBagLayout;
12import java.awt.GridLayout;
13import java.awt.Insets;
14import java.awt.Toolkit;
15import java.awt.event.KeyEvent;
16import java.awt.im.InputContext;
17import java.lang.reflect.Field;
18import java.util.LinkedHashMap;
19import java.util.List;
20import java.util.Map;
21
22import javax.swing.AbstractAction;
23import javax.swing.BorderFactory;
24import javax.swing.BoxLayout;
25import javax.swing.DefaultComboBoxModel;
26import javax.swing.JCheckBox;
27import javax.swing.JLabel;
28import javax.swing.JPanel;
29import javax.swing.JScrollPane;
30import javax.swing.JTable;
31import javax.swing.KeyStroke;
32import javax.swing.ListSelectionModel;
33import javax.swing.SwingConstants;
34import javax.swing.UIManager;
35import javax.swing.event.ListSelectionEvent;
36import javax.swing.event.ListSelectionListener;
37import javax.swing.table.AbstractTableModel;
38import javax.swing.table.DefaultTableCellRenderer;
39import javax.swing.table.TableColumnModel;
40
41import org.openstreetmap.josm.data.preferences.NamedColorProperty;
42import org.openstreetmap.josm.gui.util.GuiHelper;
43import org.openstreetmap.josm.gui.widgets.FilterField;
44import org.openstreetmap.josm.gui.widgets.JosmComboBox;
45import org.openstreetmap.josm.tools.KeyboardUtils;
46import org.openstreetmap.josm.tools.Logging;
47import org.openstreetmap.josm.tools.Shortcut;
48
49/**
50 * This is the keyboard preferences content.
51 */
52public class PrefJPanel extends JPanel {
53
54 // table of shortcuts
55 private final AbstractTableModel model;
56 // this are the display(!) texts for the checkboxes. Let the JVM do the i18n for us <g>.
57 // Ok, there's a real reason for this: The JVM should know best how the keys are labelled
58 // on the physical keyboard. What language pack is installed in JOSM is completely
59 // independent from the keyboard's labelling. But the operation system's locale
60 // usually matches the keyboard. This even works with my English Windows and my German keyboard.
61 private static final String SHIFT = KeyEvent.getModifiersExText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
62 KeyEvent.SHIFT_DOWN_MASK).getModifiers());
63 private static final String CTRL = KeyEvent.getModifiersExText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
64 KeyEvent.CTRL_DOWN_MASK).getModifiers());
65 private static final String ALT = KeyEvent.getModifiersExText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
66 KeyEvent.ALT_DOWN_MASK).getModifiers());
67 private static final String META = KeyEvent.getModifiersExText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
68 KeyEvent.META_DOWN_MASK).getModifiers());
69
70 // A list of keys to present the user. Sadly this really is a list of keys Java knows about,
71 // not a list of real physical keys. If someone knows how to get that list?
72 private static final Map<Integer, String> keyList = setKeyList();
73
74 private final JCheckBox cbAlt = new JCheckBox();
75 private final JCheckBox cbCtrl = new JCheckBox();
76 private final JCheckBox cbMeta = new JCheckBox();
77 private final JCheckBox cbShift = new JCheckBox();
78 private final JCheckBox cbDefault = new JCheckBox();
79 private final JCheckBox cbDisable = new JCheckBox();
80 private final JosmComboBox<String> tfKey = new JosmComboBox<>();
81
82 private final JTable shortcutTable = new JTable();
83 private final FilterField filterField;
84
85 /** Creates new form prefJPanel */
86 public PrefJPanel() {
87 this.model = new ScListModel();
88 this.filterField = new FilterField();
89 initComponents();
90 }
91
92 private static Map<Integer, String> setKeyList() {
93 Map<Integer, String> list = new LinkedHashMap<>();
94 String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
95 // Assume all known keys are declared in KeyEvent as "public static int VK_*"
96 for (Field field : KeyEvent.class.getFields()) {
97 // Ignore VK_KP_DOWN, UP, etc. because they have the same name as VK_DOWN, UP, etc. See #8340
98 if (field.getName().startsWith("VK_") && !field.getName().startsWith("VK_KP_")) {
99 try {
100 int i = field.getInt(null);
101 String s = KeyEvent.getKeyText(i);
102 if (s != null && s.length() > 0 && !s.contains(unknown)) {
103 list.put(Integer.valueOf(i), s);
104 }
105 } catch (IllegalArgumentException | IllegalAccessException e) {
106 Logging.error(e);
107 }
108 }
109 }
110 KeyboardUtils.getExtendedKeyCodes(InputContext.getInstance().getLocale()).entrySet()
111 .forEach(e -> list.put(e.getKey(), e.getValue().toString()));
112 list.put(Integer.valueOf(-1), "");
113 return list;
114 }
115
116 /**
117 * Show only shortcuts with descriptions containing given substring
118 * @param substring The substring used to filter
119 */
120 public void filter(String substring) {
121 filterField.setText(substring);
122 }
123
124 private static class ScListModel extends AbstractTableModel {
125 private final String[] columnNames = {tr("Action"), tr("Shortcut")};
126 private final transient List<Shortcut> data;
127
128 /**
129 * Constructs a new {@code ScListModel}.
130 */
131 ScListModel() {
132 data = Shortcut.listAll();
133 }
134
135 @Override
136 public int getColumnCount() {
137 return columnNames.length;
138 }
139
140 @Override
141 public int getRowCount() {
142 return data.size();
143 }
144
145 @Override
146 public String getColumnName(int col) {
147 return columnNames[col];
148 }
149
150 @Override
151 public Object getValueAt(int row, int col) {
152 return (col == 0) ? data.get(row).getLongText() : data.get(row);
153 }
154 }
155
156 private class ShortcutTableCellRenderer extends DefaultTableCellRenderer {
157
158 private final transient NamedColorProperty SHORTCUT_BACKGROUND_USER_COLOR = new NamedColorProperty(
159 marktr("Shortcut Background: User"),
160 new Color(200, 255, 200));
161 private final transient NamedColorProperty SHORTCUT_BACKGROUND_MODIFIED_COLOR = new NamedColorProperty(
162 marktr("Shortcut Background: Modified"),
163 new Color(255, 255, 200));
164
165 private final boolean name;
166
167 ShortcutTableCellRenderer(boolean name) {
168 this.name = name;
169 }
170
171 @Override
172 public Component getTableCellRendererComponent(JTable table, Object value, boolean
173 isSelected, boolean hasFocus, int row, int column) {
174 int row1 = shortcutTable.convertRowIndexToModel(row);
175 Shortcut sc = (Shortcut) model.getValueAt(row1, -1);
176 if (sc == null)
177 return null;
178 JLabel label = (JLabel) super.getTableCellRendererComponent(
179 table, name ? sc.getLongText() : sc.getKeyText(), isSelected, hasFocus, row, column);
180 GuiHelper.setBackgroundReadable(label, UIManager.getColor("Table.background"));
181 if (sc.isAssignedUser()) {
182 GuiHelper.setBackgroundReadable(label, SHORTCUT_BACKGROUND_USER_COLOR.get());
183 } else if (!sc.isAssignedDefault()) {
184 GuiHelper.setBackgroundReadable(label, SHORTCUT_BACKGROUND_MODIFIED_COLOR.get());
185 }
186 return label;
187 }
188 }
189
190 private void initComponents() {
191 CbAction action = new CbAction(this);
192 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
193 add(buildFilterPanel());
194
195 // This is the list of shortcuts:
196 shortcutTable.setModel(model);
197 shortcutTable.getSelectionModel().addListSelectionListener(action);
198 shortcutTable.setFillsViewportHeight(true);
199 shortcutTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
200 shortcutTable.setAutoCreateRowSorter(true);
201 filterField.filter(shortcutTable, model);
202 TableColumnModel mod = shortcutTable.getColumnModel();
203 mod.getColumn(0).setCellRenderer(new ShortcutTableCellRenderer(true));
204 mod.getColumn(1).setCellRenderer(new ShortcutTableCellRenderer(false));
205 JScrollPane listScrollPane = new JScrollPane();
206 listScrollPane.setViewportView(shortcutTable);
207
208 JPanel listPane = new JPanel(new GridLayout());
209 listPane.add(listScrollPane);
210 add(listPane);
211
212 // and here follows the edit area. I won't object to someone re-designing it, it looks, um, "minimalistic" ;)
213
214 cbDefault.setAction(action);
215 cbDefault.setText(tr("Use default"));
216 cbShift.setAction(action);
217 cbShift.setText(SHIFT); // see above for why no tr()
218 cbDisable.setAction(action);
219 cbDisable.setText(tr("Disable"));
220 cbCtrl.setAction(action);
221 cbCtrl.setText(CTRL); // see above for why no tr()
222 cbAlt.setAction(action);
223 cbAlt.setText(ALT); // see above for why no tr()
224 tfKey.setAction(action);
225 tfKey.setModel(new DefaultComboBoxModel<>(keyList.values().toArray(new String[keyList.size()])));
226 cbMeta.setAction(action);
227 cbMeta.setText(META); // see above for why no tr()
228
229 JPanel shortcutEditPane = new JPanel(new GridLayout(5, 2));
230
231 shortcutEditPane.add(cbDefault);
232 shortcutEditPane.add(new JLabel());
233 shortcutEditPane.add(cbShift);
234 shortcutEditPane.add(cbDisable);
235 shortcutEditPane.add(cbCtrl);
236 shortcutEditPane.add(new JLabel(tr("Key:"), SwingConstants.LEFT));
237 shortcutEditPane.add(cbAlt);
238 shortcutEditPane.add(tfKey);
239 shortcutEditPane.add(cbMeta);
240
241 shortcutEditPane.add(new JLabel(tr("Attention: Use real keyboard keys only!")));
242
243 action.actionPerformed(null); // init checkboxes
244
245 add(shortcutEditPane);
246 }
247
248 private JPanel buildFilterPanel() {
249 // copied from PluginPreference
250 JPanel pnl = new JPanel(new GridBagLayout());
251 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
252 GridBagConstraints gc = new GridBagConstraints();
253
254 gc.anchor = GridBagConstraints.NORTHWEST;
255 gc.fill = GridBagConstraints.HORIZONTAL;
256 gc.weightx = 0.0;
257 gc.insets = new Insets(0, 0, 0, 5);
258 pnl.add(new JLabel(tr("Search:")), gc);
259
260 gc.gridx = 1;
261 gc.weightx = 1.0;
262 pnl.add(filterField, gc);
263 pnl.setMaximumSize(new Dimension(300, 10));
264 return pnl;
265 }
266
267 // this allows to edit shortcuts. it:
268 // * sets the edit controls to the selected shortcut
269 // * enabled/disables the controls as needed
270 // * writes the user's changes to the shortcut
271 // And after I finally had it working, I realized that those two methods
272 // are playing ping-pong (politically correct: table tennis, I know) and
273 // even have some duplicated code. Feel free to refactor, If you have
274 // more experience with GUI coding than I have.
275 private static class CbAction extends AbstractAction implements ListSelectionListener {
276 private final PrefJPanel panel;
277
278 CbAction(PrefJPanel panel) {
279 this.panel = panel;
280 }
281
282 private void disableAllModifierCheckboxes() {
283 panel.cbDefault.setEnabled(false);
284 panel.cbDisable.setEnabled(false);
285 panel.cbShift.setEnabled(false);
286 panel.cbCtrl.setEnabled(false);
287 panel.cbAlt.setEnabled(false);
288 panel.cbMeta.setEnabled(false);
289 }
290
291 @Override
292 public void valueChanged(ListSelectionEvent e) {
293 ListSelectionModel lsm = panel.shortcutTable.getSelectionModel(); // can't use e here
294 if (!lsm.isSelectionEmpty()) {
295 int row = panel.shortcutTable.convertRowIndexToModel(lsm.getMinSelectionIndex());
296 Shortcut sc = (Shortcut) panel.model.getValueAt(row, -1);
297 panel.cbDefault.setSelected(!sc.isAssignedUser());
298 panel.cbDisable.setSelected(sc.getKeyStroke() == null);
299 panel.cbShift.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.SHIFT_DOWN_MASK) != 0);
300 panel.cbCtrl.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.CTRL_DOWN_MASK) != 0);
301 panel.cbAlt.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.ALT_DOWN_MASK) != 0);
302 panel.cbMeta.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.META_DOWN_MASK) != 0);
303 if (sc.getKeyStroke() != null) {
304 panel.tfKey.setSelectedItem(keyList.get(sc.getKeyStroke().getKeyCode()));
305 } else {
306 panel.tfKey.setSelectedItem(keyList.get(-1));
307 }
308 if (!sc.isChangeable()) {
309 disableAllModifierCheckboxes();
310 panel.tfKey.setEnabled(false);
311 } else {
312 panel.cbDefault.setEnabled(true);
313 actionPerformed(null);
314 }
315 panel.model.fireTableRowsUpdated(row, row);
316 } else {
317 disableAllModifierCheckboxes();
318 panel.tfKey.setEnabled(false);
319 }
320 }
321
322 @Override
323 public void actionPerformed(java.awt.event.ActionEvent e) {
324 ListSelectionModel lsm = panel.shortcutTable.getSelectionModel();
325 if (lsm != null && !lsm.isSelectionEmpty()) {
326 if (e != null) { // only if we've been called by a user action
327 int row = panel.shortcutTable.convertRowIndexToModel(lsm.getMinSelectionIndex());
328 Shortcut sc = (Shortcut) panel.model.getValueAt(row, -1);
329 Object selectedKey = panel.tfKey.getSelectedItem();
330 if (panel.cbDisable.isSelected()) {
331 sc.setAssignedModifier(-1);
332 } else if (selectedKey == null || "".equals(selectedKey)) {
333 sc.setAssignedModifier(KeyEvent.VK_CANCEL);
334 } else {
335 sc.setAssignedModifier(
336 (panel.cbShift.isSelected() ? KeyEvent.SHIFT_DOWN_MASK : 0) |
337 (panel.cbCtrl.isSelected() ? KeyEvent.CTRL_DOWN_MASK : 0) |
338 (panel.cbAlt.isSelected() ? KeyEvent.ALT_DOWN_MASK : 0) |
339 (panel.cbMeta.isSelected() ? KeyEvent.META_DOWN_MASK : 0)
340 );
341 for (Map.Entry<Integer, String> entry : keyList.entrySet()) {
342 if (entry.getValue().equals(selectedKey)) {
343 sc.setAssignedKey(entry.getKey());
344 }
345 }
346 }
347 sc.setAssignedUser(!panel.cbDefault.isSelected());
348 valueChanged(null);
349 }
350 boolean state = !panel.cbDefault.isSelected();
351 panel.cbDisable.setEnabled(state);
352 state = state && !panel.cbDisable.isSelected();
353 panel.cbShift.setEnabled(state);
354 panel.cbCtrl.setEnabled(state);
355 panel.cbAlt.setEnabled(state);
356 panel.cbMeta.setEnabled(state);
357 panel.tfKey.setEnabled(state);
358 } else {
359 disableAllModifierCheckboxes();
360 panel.tfKey.setEnabled(false);
361 }
362 }
363 }
364}
Note: See TracBrowser for help on using the repository browser.