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

Last change on this file was 19101, checked in by taylor.smock, 3 weeks ago

Cleanup some new PMD warnings from PMD 7.x

I haven't updated to PMD 7.x in ivy/maven yet since I still have 83 PMD violations
to go through, and I don't know (yet) if any of them were supposed to be ignored
by the xpath expressions in tools/pmd/josm-ruleset.xml.

I may re-enable some of the PMD checks I've temporarily disabled prior to committing
the update for PMD.

Additionally cleanup some SonarLint issues in the modified files.

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