001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.preferences.shortcut; 003 004import static org.openstreetmap.josm.tools.I18n.marktr; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.Color; 008import java.awt.Component; 009import java.awt.Dimension; 010import java.awt.GridBagConstraints; 011import java.awt.GridBagLayout; 012import java.awt.GridLayout; 013import java.awt.Insets; 014import java.awt.Toolkit; 015import java.awt.event.KeyEvent; 016import java.lang.reflect.Field; 017import java.util.ArrayList; 018import java.util.LinkedHashMap; 019import java.util.List; 020import java.util.Map; 021import java.util.regex.PatternSyntaxException; 022 023import javax.swing.AbstractAction; 024import javax.swing.BorderFactory; 025import javax.swing.BoxLayout; 026import javax.swing.DefaultComboBoxModel; 027import javax.swing.JCheckBox; 028import javax.swing.JLabel; 029import javax.swing.JPanel; 030import javax.swing.JScrollPane; 031import javax.swing.JTable; 032import javax.swing.KeyStroke; 033import javax.swing.ListSelectionModel; 034import javax.swing.RowFilter; 035import javax.swing.SwingConstants; 036import javax.swing.UIManager; 037import javax.swing.event.DocumentEvent; 038import javax.swing.event.DocumentListener; 039import javax.swing.event.ListSelectionEvent; 040import javax.swing.event.ListSelectionListener; 041import javax.swing.table.AbstractTableModel; 042import javax.swing.table.DefaultTableCellRenderer; 043import javax.swing.table.TableColumnModel; 044import javax.swing.table.TableModel; 045import javax.swing.table.TableRowSorter; 046 047import org.openstreetmap.josm.data.preferences.NamedColorProperty; 048import org.openstreetmap.josm.gui.util.GuiHelper; 049import org.openstreetmap.josm.gui.widgets.JosmComboBox; 050import org.openstreetmap.josm.gui.widgets.JosmTextField; 051import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator; 052import org.openstreetmap.josm.tools.Logging; 053import org.openstreetmap.josm.tools.Shortcut; 054 055/** 056 * This is the keyboard preferences content. 057 */ 058public class PrefJPanel extends JPanel { 059 060 // table of shortcuts 061 private final AbstractTableModel model; 062 // this are the display(!) texts for the checkboxes. Let the JVM do the i18n for us <g>. 063 // Ok, there's a real reason for this: The JVM should know best how the keys are labelled 064 // on the physical keyboard. What language pack is installed in JOSM is completely 065 // independent from the keyboard's labelling. But the operation system's locale 066 // usually matches the keyboard. This even works with my English Windows and my German keyboard. 067 private static final String SHIFT = KeyEvent.getModifiersExText(KeyStroke.getKeyStroke(KeyEvent.VK_A, 068 KeyEvent.SHIFT_DOWN_MASK).getModifiers()); 069 private static final String CTRL = KeyEvent.getModifiersExText(KeyStroke.getKeyStroke(KeyEvent.VK_A, 070 KeyEvent.CTRL_DOWN_MASK).getModifiers()); 071 private static final String ALT = KeyEvent.getModifiersExText(KeyStroke.getKeyStroke(KeyEvent.VK_A, 072 KeyEvent.ALT_DOWN_MASK).getModifiers()); 073 private static final String META = KeyEvent.getModifiersExText(KeyStroke.getKeyStroke(KeyEvent.VK_A, 074 KeyEvent.META_DOWN_MASK).getModifiers()); 075 076 // A list of keys to present the user. Sadly this really is a list of keys Java knows about, 077 // not a list of real physical keys. If someone knows how to get that list? 078 private static Map<Integer, String> keyList = setKeyList(); 079 080 private final JCheckBox cbAlt = new JCheckBox(); 081 private final JCheckBox cbCtrl = new JCheckBox(); 082 private final JCheckBox cbMeta = new JCheckBox(); 083 private final JCheckBox cbShift = new JCheckBox(); 084 private final JCheckBox cbDefault = new JCheckBox(); 085 private final JCheckBox cbDisable = new JCheckBox(); 086 private final JosmComboBox<String> tfKey = new JosmComboBox<>(); 087 088 private final JTable shortcutTable = new JTable(); 089 090 private final JosmTextField filterField = new JosmTextField(); 091 092 /** Creates new form prefJPanel */ 093 public PrefJPanel() { 094 this.model = new ScListModel(); 095 initComponents(); 096 } 097 098 private static Map<Integer, String> setKeyList() { 099 Map<Integer, String> list = new LinkedHashMap<>(); 100 String unknown = Toolkit.getProperty("AWT.unknown", "Unknown"); 101 // Assume all known keys are declared in KeyEvent as "public static int VK_*" 102 for (Field field : KeyEvent.class.getFields()) { 103 // Ignore VK_KP_DOWN, UP, etc. because they have the same name as VK_DOWN, UP, etc. See #8340 104 if (field.getName().startsWith("VK_") && !field.getName().startsWith("VK_KP_")) { 105 try { 106 int i = field.getInt(null); 107 String s = KeyEvent.getKeyText(i); 108 if (s != null && s.length() > 0 && !s.contains(unknown)) { 109 list.put(Integer.valueOf(i), s); 110 } 111 } catch (IllegalArgumentException | IllegalAccessException e) { 112 Logging.error(e); 113 } 114 } 115 } 116 list.put(Integer.valueOf(-1), ""); 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 = new String[]{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 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 197 add(buildFilterPanel()); 198 199 // This is the list of shortcuts: 200 shortcutTable.setModel(model); 201 shortcutTable.getSelectionModel().addListSelectionListener(action); 202 shortcutTable.setFillsViewportHeight(true); 203 shortcutTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 204 shortcutTable.setAutoCreateRowSorter(true); 205 TableColumnModel mod = shortcutTable.getColumnModel(); 206 mod.getColumn(0).setCellRenderer(new ShortcutTableCellRenderer(true)); 207 mod.getColumn(1).setCellRenderer(new ShortcutTableCellRenderer(false)); 208 JScrollPane listScrollPane = new JScrollPane(); 209 listScrollPane.setViewportView(shortcutTable); 210 211 JPanel listPane = new JPanel(new GridLayout()); 212 listPane.add(listScrollPane); 213 add(listPane); 214 215 // and here follows the edit area. I won't object to someone re-designing it, it looks, um, "minimalistic" ;) 216 217 cbDefault.setAction(action); 218 cbDefault.setText(tr("Use default")); 219 cbShift.setAction(action); 220 cbShift.setText(SHIFT); // see above for why no tr() 221 cbDisable.setAction(action); 222 cbDisable.setText(tr("Disable")); 223 cbCtrl.setAction(action); 224 cbCtrl.setText(CTRL); // see above for why no tr() 225 cbAlt.setAction(action); 226 cbAlt.setText(ALT); // see above for why no tr() 227 tfKey.setAction(action); 228 tfKey.setModel(new DefaultComboBoxModel<>(keyList.values().toArray(new String[keyList.size()]))); 229 cbMeta.setAction(action); 230 cbMeta.setText(META); // see above for why no tr() 231 232 JPanel shortcutEditPane = new JPanel(new GridLayout(5, 2)); 233 234 shortcutEditPane.add(cbDefault); 235 shortcutEditPane.add(new JLabel()); 236 shortcutEditPane.add(cbShift); 237 shortcutEditPane.add(cbDisable); 238 shortcutEditPane.add(cbCtrl); 239 shortcutEditPane.add(new JLabel(tr("Key:"), SwingConstants.LEFT)); 240 shortcutEditPane.add(cbAlt); 241 shortcutEditPane.add(tfKey); 242 shortcutEditPane.add(cbMeta); 243 244 shortcutEditPane.add(new JLabel(tr("Attention: Use real keyboard keys only!"))); 245 246 action.actionPerformed(null); // init checkboxes 247 248 add(shortcutEditPane); 249 } 250 251 private JPanel buildFilterPanel() { 252 // copied from PluginPreference 253 JPanel pnl = new JPanel(new GridBagLayout()); 254 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 255 GridBagConstraints gc = new GridBagConstraints(); 256 257 gc.anchor = GridBagConstraints.NORTHWEST; 258 gc.fill = GridBagConstraints.HORIZONTAL; 259 gc.weightx = 0.0; 260 gc.insets = new Insets(0, 0, 0, 5); 261 pnl.add(new JLabel(tr("Search:")), gc); 262 263 gc.gridx = 1; 264 gc.weightx = 1.0; 265 pnl.add(filterField, gc); 266 filterField.setToolTipText(tr("Enter a search expression")); 267 SelectAllOnFocusGainedDecorator.decorate(filterField); 268 filterField.getDocument().addDocumentListener(new FilterFieldAdapter()); 269 pnl.setMaximumSize(new Dimension(300, 10)); 270 return pnl; 271 } 272 273 // this allows to edit shortcuts. it: 274 // * sets the edit controls to the selected shortcut 275 // * enabled/disables the controls as needed 276 // * writes the user's changes to the shortcut 277 // And after I finally had it working, I realized that those two methods 278 // are playing ping-pong (politically correct: table tennis, I know) and 279 // even have some duplicated code. Feel free to refactor, If you have 280 // more experience with GUI coding than I have. 281 private static class CbAction extends AbstractAction implements ListSelectionListener { 282 private final PrefJPanel panel; 283 284 CbAction(PrefJPanel panel) { 285 this.panel = panel; 286 } 287 288 private void disableAllModifierCheckboxes() { 289 panel.cbDefault.setEnabled(false); 290 panel.cbDisable.setEnabled(false); 291 panel.cbShift.setEnabled(false); 292 panel.cbCtrl.setEnabled(false); 293 panel.cbAlt.setEnabled(false); 294 panel.cbMeta.setEnabled(false); 295 } 296 297 @Override 298 public void valueChanged(ListSelectionEvent e) { 299 ListSelectionModel lsm = panel.shortcutTable.getSelectionModel(); // can't use e here 300 if (!lsm.isSelectionEmpty()) { 301 int row = panel.shortcutTable.convertRowIndexToModel(lsm.getMinSelectionIndex()); 302 Shortcut sc = (Shortcut) panel.model.getValueAt(row, -1); 303 panel.cbDefault.setSelected(!sc.isAssignedUser()); 304 panel.cbDisable.setSelected(sc.getKeyStroke() == null); 305 panel.cbShift.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.SHIFT_DOWN_MASK) != 0); 306 panel.cbCtrl.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.CTRL_DOWN_MASK) != 0); 307 panel.cbAlt.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.ALT_DOWN_MASK) != 0); 308 panel.cbMeta.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.META_DOWN_MASK) != 0); 309 if (sc.getKeyStroke() != null) { 310 panel.tfKey.setSelectedItem(keyList.get(sc.getKeyStroke().getKeyCode())); 311 } else { 312 panel.tfKey.setSelectedItem(keyList.get(-1)); 313 } 314 if (!sc.isChangeable()) { 315 disableAllModifierCheckboxes(); 316 panel.tfKey.setEnabled(false); 317 } else { 318 panel.cbDefault.setEnabled(true); 319 actionPerformed(null); 320 } 321 panel.model.fireTableRowsUpdated(row, row); 322 } else { 323 disableAllModifierCheckboxes(); 324 panel.tfKey.setEnabled(false); 325 } 326 } 327 328 @Override 329 public void actionPerformed(java.awt.event.ActionEvent e) { 330 ListSelectionModel lsm = panel.shortcutTable.getSelectionModel(); 331 if (lsm != null && !lsm.isSelectionEmpty()) { 332 if (e != null) { // only if we've been called by a user action 333 int row = panel.shortcutTable.convertRowIndexToModel(lsm.getMinSelectionIndex()); 334 Shortcut sc = (Shortcut) panel.model.getValueAt(row, -1); 335 if (panel.cbDisable.isSelected()) { 336 sc.setAssignedModifier(-1); 337 } else if (panel.tfKey.getSelectedItem() == null || "".equals(panel.tfKey.getSelectedItem())) { 338 sc.setAssignedModifier(KeyEvent.VK_CANCEL); 339 } else { 340 sc.setAssignedModifier( 341 (panel.cbShift.isSelected() ? KeyEvent.SHIFT_DOWN_MASK : 0) | 342 (panel.cbCtrl.isSelected() ? KeyEvent.CTRL_DOWN_MASK : 0) | 343 (panel.cbAlt.isSelected() ? KeyEvent.ALT_DOWN_MASK : 0) | 344 (panel.cbMeta.isSelected() ? KeyEvent.META_DOWN_MASK : 0) 345 ); 346 for (Map.Entry<Integer, String> entry : keyList.entrySet()) { 347 if (entry.getValue().equals(panel.tfKey.getSelectedItem())) { 348 sc.setAssignedKey(entry.getKey()); 349 } 350 } 351 } 352 sc.setAssignedUser(!panel.cbDefault.isSelected()); 353 valueChanged(null); 354 } 355 boolean state = !panel.cbDefault.isSelected(); 356 panel.cbDisable.setEnabled(state); 357 state = state && !panel.cbDisable.isSelected(); 358 panel.cbShift.setEnabled(state); 359 panel.cbCtrl.setEnabled(state); 360 panel.cbAlt.setEnabled(state); 361 panel.cbMeta.setEnabled(state); 362 panel.tfKey.setEnabled(state); 363 } else { 364 disableAllModifierCheckboxes(); 365 panel.tfKey.setEnabled(false); 366 } 367 } 368 } 369 370 class FilterFieldAdapter implements DocumentListener { 371 private void filter() { 372 String expr = filterField.getText().trim(); 373 if (expr.isEmpty()) { 374 expr = null; 375 } 376 try { 377 final TableRowSorter<? extends TableModel> sorter = 378 (TableRowSorter<? extends TableModel>) shortcutTable.getRowSorter(); 379 if (expr == null) { 380 sorter.setRowFilter(null); 381 } else { 382 expr = expr.replace("+", "\\+"); 383 // split search string on whitespace, do case-insensitive AND search 384 List<RowFilter<Object, Object>> andFilters = new ArrayList<>(); 385 for (String word : expr.split("\\s+")) { 386 andFilters.add(RowFilter.regexFilter("(?i)" + word)); 387 } 388 sorter.setRowFilter(RowFilter.andFilter(andFilters)); 389 } 390 model.fireTableDataChanged(); 391 } catch (PatternSyntaxException | ClassCastException ex) { 392 Logging.warn(ex); 393 } 394 } 395 396 @Override 397 public void changedUpdate(DocumentEvent e) { 398 filter(); 399 } 400 401 @Override 402 public void insertUpdate(DocumentEvent e) { 403 filter(); 404 } 405 406 @Override 407 public void removeUpdate(DocumentEvent e) { 408 filter(); 409 } 410 } 411}