1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.shortcut;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.GridBagConstraints;
|
---|
11 | import java.awt.GridBagLayout;
|
---|
12 | import java.awt.GridLayout;
|
---|
13 | import java.awt.Insets;
|
---|
14 | import java.awt.Toolkit;
|
---|
15 | import java.awt.event.KeyEvent;
|
---|
16 | import java.lang.reflect.Field;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.LinkedHashMap;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.Map;
|
---|
21 | import java.util.regex.PatternSyntaxException;
|
---|
22 |
|
---|
23 | import javax.swing.AbstractAction;
|
---|
24 | import javax.swing.BorderFactory;
|
---|
25 | import javax.swing.BoxLayout;
|
---|
26 | import javax.swing.DefaultComboBoxModel;
|
---|
27 | import javax.swing.JCheckBox;
|
---|
28 | import javax.swing.JLabel;
|
---|
29 | import javax.swing.JPanel;
|
---|
30 | import javax.swing.JScrollPane;
|
---|
31 | import javax.swing.JTable;
|
---|
32 | import javax.swing.KeyStroke;
|
---|
33 | import javax.swing.ListSelectionModel;
|
---|
34 | import javax.swing.RowFilter;
|
---|
35 | import javax.swing.SwingConstants;
|
---|
36 | import javax.swing.UIManager;
|
---|
37 | import javax.swing.event.DocumentEvent;
|
---|
38 | import javax.swing.event.DocumentListener;
|
---|
39 | import javax.swing.event.ListSelectionEvent;
|
---|
40 | import javax.swing.event.ListSelectionListener;
|
---|
41 | import javax.swing.table.AbstractTableModel;
|
---|
42 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
43 | import javax.swing.table.TableColumnModel;
|
---|
44 | import javax.swing.table.TableModel;
|
---|
45 | import javax.swing.table.TableRowSorter;
|
---|
46 |
|
---|
47 | import org.openstreetmap.josm.Main;
|
---|
48 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
49 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
50 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
51 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
52 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * This is the keyboard preferences content.
|
---|
56 | */
|
---|
57 | public class PrefJPanel extends JPanel {
|
---|
58 |
|
---|
59 | // table of shortcuts
|
---|
60 | private final AbstractTableModel model;
|
---|
61 | // this are the display(!) texts for the checkboxes. Let the JVM do the i18n for us <g>.
|
---|
62 | // Ok, there's a real reason for this: The JVM should know best how the keys are labelled
|
---|
63 | // on the physical keyboard. What language pack is installed in JOSM is completely
|
---|
64 | // independent from the keyboard's labelling. But the operation system's locale
|
---|
65 | // usually matches the keyboard. This even works with my English Windows and my German keyboard.
|
---|
66 | private static final String SHIFT = KeyEvent.getKeyModifiersText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
|
---|
67 | KeyEvent.SHIFT_DOWN_MASK).getModifiers());
|
---|
68 | private static final String CTRL = KeyEvent.getKeyModifiersText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
|
---|
69 | KeyEvent.CTRL_DOWN_MASK).getModifiers());
|
---|
70 | private static final String ALT = KeyEvent.getKeyModifiersText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
|
---|
71 | KeyEvent.ALT_DOWN_MASK).getModifiers());
|
---|
72 | private static final String META = KeyEvent.getKeyModifiersText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
|
---|
73 | KeyEvent.META_DOWN_MASK).getModifiers());
|
---|
74 |
|
---|
75 | // A list of keys to present the user. Sadly this really is a list of keys Java knows about,
|
---|
76 | // not a list of real physical keys. If someone knows how to get that list?
|
---|
77 | private static Map<Integer, String> keyList = setKeyList();
|
---|
78 |
|
---|
79 | private static Map<Integer, String> setKeyList() {
|
---|
80 | Map<Integer, String> list = new LinkedHashMap<>();
|
---|
81 | String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
|
---|
82 | // Assume all known keys are declared in KeyEvent as "public static int VK_*"
|
---|
83 | for (Field field : KeyEvent.class.getFields()) {
|
---|
84 | if (field.getName().startsWith("VK_")) {
|
---|
85 | try {
|
---|
86 | int i = field.getInt(null);
|
---|
87 | String s = KeyEvent.getKeyText(i);
|
---|
88 | if (s != null && s.length() > 0 && !s.contains(unknown)) {
|
---|
89 | list.put(Integer.valueOf(i), s);
|
---|
90 | }
|
---|
91 | } catch (Exception e) {
|
---|
92 | Main.error(e);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | list.put(Integer.valueOf(-1), "");
|
---|
97 | return list;
|
---|
98 | }
|
---|
99 |
|
---|
100 | private final JCheckBox cbAlt = new JCheckBox();
|
---|
101 | private final JCheckBox cbCtrl = new JCheckBox();
|
---|
102 | private final JCheckBox cbMeta = new JCheckBox();
|
---|
103 | private final JCheckBox cbShift = new JCheckBox();
|
---|
104 | private final JCheckBox cbDefault = new JCheckBox();
|
---|
105 | private final JCheckBox cbDisable = new JCheckBox();
|
---|
106 | private final JosmComboBox<String> tfKey = new JosmComboBox<>();
|
---|
107 |
|
---|
108 | private final JTable shortcutTable = new JTable();
|
---|
109 |
|
---|
110 | private final JosmTextField filterField = new JosmTextField();
|
---|
111 |
|
---|
112 | /** Creates new form prefJPanel */
|
---|
113 | public PrefJPanel() {
|
---|
114 | this.model = new ScListModel();
|
---|
115 | initComponents();
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Show only shortcuts with descriptions containing given substring
|
---|
120 | * @param substring The substring used to filter
|
---|
121 | */
|
---|
122 | public void filter(String substring) {
|
---|
123 | filterField.setText(substring);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private static class ScListModel extends AbstractTableModel {
|
---|
127 | private final String[] columnNames = new String[]{tr("Action"), tr("Shortcut")};
|
---|
128 | private final transient List<Shortcut> data;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Constructs a new {@code ScListModel}.
|
---|
132 | */
|
---|
133 | ScListModel() {
|
---|
134 | data = Shortcut.listAll();
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public int getColumnCount() {
|
---|
139 | return columnNames.length;
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public int getRowCount() {
|
---|
144 | return data.size();
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public String getColumnName(int col) {
|
---|
149 | return columnNames[col];
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public Object getValueAt(int row, int col) {
|
---|
154 | return (col == 0) ? data.get(row).getLongText() : data.get(row);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private class ShortcutTableCellRenderer extends DefaultTableCellRenderer {
|
---|
159 |
|
---|
160 | private final boolean name;
|
---|
161 |
|
---|
162 | ShortcutTableCellRenderer(boolean name) {
|
---|
163 | this.name = name;
|
---|
164 | }
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public Component getTableCellRendererComponent(JTable table, Object value, boolean
|
---|
168 | isSelected, boolean hasFocus, int row, int column) {
|
---|
169 | int row1 = shortcutTable.convertRowIndexToModel(row);
|
---|
170 | Shortcut sc = (Shortcut) model.getValueAt(row1, -1);
|
---|
171 | if (sc == null) return null;
|
---|
172 | JLabel label = (JLabel) super.getTableCellRendererComponent(
|
---|
173 | table, name ? sc.getLongText() : sc.getKeyText(), isSelected, hasFocus, row, column);
|
---|
174 | GuiHelper.setBackgroundReadable(label, UIManager.getColor("Table.background"));
|
---|
175 | if (sc.isAssignedUser()) {
|
---|
176 | GuiHelper.setBackgroundReadable(label, Main.pref.getColor(
|
---|
177 | marktr("Shortcut Background: User"),
|
---|
178 | new Color(200, 255, 200)));
|
---|
179 | } else if (!sc.isAssignedDefault()) {
|
---|
180 | GuiHelper.setBackgroundReadable(label, Main.pref.getColor(
|
---|
181 | marktr("Shortcut Background: Modified"),
|
---|
182 | new Color(255, 255, 200)));
|
---|
183 | }
|
---|
184 | return label;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | private void initComponents() {
|
---|
189 | JPanel listPane = new JPanel(new GridLayout());
|
---|
190 | JScrollPane listScrollPane = new JScrollPane();
|
---|
191 | JPanel shortcutEditPane = new JPanel(new GridLayout(5, 2));
|
---|
192 |
|
---|
193 | CbAction action = new CbAction(this);
|
---|
194 | setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
---|
195 | add(buildFilterPanel());
|
---|
196 |
|
---|
197 | // This is the list of shortcuts:
|
---|
198 | shortcutTable.setModel(model);
|
---|
199 | shortcutTable.getSelectionModel().addListSelectionListener(new CbAction(this));
|
---|
200 | shortcutTable.setFillsViewportHeight(true);
|
---|
201 | shortcutTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
202 | shortcutTable.setAutoCreateRowSorter(true);
|
---|
203 | TableColumnModel mod = shortcutTable.getColumnModel();
|
---|
204 | mod.getColumn(0).setCellRenderer(new ShortcutTableCellRenderer(true));
|
---|
205 | mod.getColumn(1).setCellRenderer(new ShortcutTableCellRenderer(false));
|
---|
206 | listScrollPane.setViewportView(shortcutTable);
|
---|
207 |
|
---|
208 | listPane.add(listScrollPane);
|
---|
209 |
|
---|
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[0])));
|
---|
226 | cbMeta.setAction(action);
|
---|
227 | cbMeta.setText(META); // see above for why no tr()
|
---|
228 |
|
---|
229 | shortcutEditPane.add(cbDefault);
|
---|
230 | shortcutEditPane.add(new JLabel());
|
---|
231 | shortcutEditPane.add(cbShift);
|
---|
232 | shortcutEditPane.add(cbDisable);
|
---|
233 | shortcutEditPane.add(cbCtrl);
|
---|
234 | shortcutEditPane.add(new JLabel(tr("Key:"), SwingConstants.LEFT));
|
---|
235 | shortcutEditPane.add(cbAlt);
|
---|
236 | shortcutEditPane.add(tfKey);
|
---|
237 | shortcutEditPane.add(cbMeta);
|
---|
238 |
|
---|
239 | shortcutEditPane.add(new JLabel(tr("Attention: Use real keyboard keys only!")));
|
---|
240 |
|
---|
241 | action.actionPerformed(null); // init checkboxes
|
---|
242 |
|
---|
243 | add(shortcutEditPane);
|
---|
244 | }
|
---|
245 |
|
---|
246 | private JPanel buildFilterPanel() {
|
---|
247 | // copied from PluginPreference
|
---|
248 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
249 | pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
250 | GridBagConstraints gc = new GridBagConstraints();
|
---|
251 |
|
---|
252 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
253 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
254 | gc.weightx = 0.0;
|
---|
255 | gc.insets = new Insets(0, 0, 0, 5);
|
---|
256 | pnl.add(new JLabel(tr("Search:")), gc);
|
---|
257 |
|
---|
258 | gc.gridx = 1;
|
---|
259 | gc.weightx = 1.0;
|
---|
260 | pnl.add(filterField, gc);
|
---|
261 | filterField.setToolTipText(tr("Enter a search expression"));
|
---|
262 | SelectAllOnFocusGainedDecorator.decorate(filterField);
|
---|
263 | filterField.getDocument().addDocumentListener(new FilterFieldAdapter());
|
---|
264 | pnl.setMaximumSize(new Dimension(300, 10));
|
---|
265 | return pnl;
|
---|
266 | }
|
---|
267 |
|
---|
268 | private void disableAllModifierCheckboxes() {
|
---|
269 | cbDefault.setEnabled(false);
|
---|
270 | cbDisable.setEnabled(false);
|
---|
271 | cbShift.setEnabled(false);
|
---|
272 | cbCtrl.setEnabled(false);
|
---|
273 | cbAlt.setEnabled(false);
|
---|
274 | cbMeta.setEnabled(false);
|
---|
275 | }
|
---|
276 |
|
---|
277 | // this allows to edit shortcuts. it:
|
---|
278 | // * sets the edit controls to the selected shortcut
|
---|
279 | // * enabled/disables the controls as needed
|
---|
280 | // * writes the user's changes to the shortcut
|
---|
281 | // And after I finally had it working, I realized that those two methods
|
---|
282 | // are playing ping-pong (politically correct: table tennis, I know) and
|
---|
283 | // even have some duplicated code. Feel free to refactor, If you have
|
---|
284 | // more expirience with GUI coding than I have.
|
---|
285 | private class CbAction extends AbstractAction implements ListSelectionListener {
|
---|
286 | private final PrefJPanel panel;
|
---|
287 |
|
---|
288 | CbAction(PrefJPanel panel) {
|
---|
289 | this.panel = panel;
|
---|
290 | }
|
---|
291 |
|
---|
292 | @Override
|
---|
293 | public void valueChanged(ListSelectionEvent e) {
|
---|
294 | ListSelectionModel lsm = panel.shortcutTable.getSelectionModel(); // can't use e here
|
---|
295 | if (!lsm.isSelectionEmpty()) {
|
---|
296 | int row = panel.shortcutTable.convertRowIndexToModel(lsm.getMinSelectionIndex());
|
---|
297 | Shortcut sc = (Shortcut) panel.model.getValueAt(row, -1);
|
---|
298 | panel.cbDefault.setSelected(!sc.isAssignedUser());
|
---|
299 | panel.cbDisable.setSelected(sc.getKeyStroke() == null);
|
---|
300 | panel.cbShift.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.SHIFT_DOWN_MASK) != 0);
|
---|
301 | panel.cbCtrl.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.CTRL_DOWN_MASK) != 0);
|
---|
302 | panel.cbAlt.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.ALT_DOWN_MASK) != 0);
|
---|
303 | panel.cbMeta.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.META_DOWN_MASK) != 0);
|
---|
304 | if (sc.getKeyStroke() != null) {
|
---|
305 | tfKey.setSelectedItem(keyList.get(sc.getKeyStroke().getKeyCode()));
|
---|
306 | } else {
|
---|
307 | tfKey.setSelectedItem(keyList.get(-1));
|
---|
308 | }
|
---|
309 | if (!sc.isChangeable()) {
|
---|
310 | disableAllModifierCheckboxes();
|
---|
311 | panel.tfKey.setEnabled(false);
|
---|
312 | } else {
|
---|
313 | panel.cbDefault.setEnabled(true);
|
---|
314 | actionPerformed(null);
|
---|
315 | }
|
---|
316 | model.fireTableRowsUpdated(row, row);
|
---|
317 | } else {
|
---|
318 | panel.disableAllModifierCheckboxes();
|
---|
319 | panel.tfKey.setEnabled(false);
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | @Override
|
---|
324 | public void actionPerformed(java.awt.event.ActionEvent e) {
|
---|
325 | ListSelectionModel lsm = panel.shortcutTable.getSelectionModel();
|
---|
326 | if (lsm != null && !lsm.isSelectionEmpty()) {
|
---|
327 | if (e != null) { // only if we've been called by a user action
|
---|
328 | int row = panel.shortcutTable.convertRowIndexToModel(lsm.getMinSelectionIndex());
|
---|
329 | Shortcut sc = (Shortcut) panel.model.getValueAt(row, -1);
|
---|
330 | if (panel.cbDisable.isSelected()) {
|
---|
331 | sc.setAssignedModifier(-1);
|
---|
332 | } else if (panel.tfKey.getSelectedItem() == null || "".equals(panel.tfKey.getSelectedItem())) {
|
---|
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(panel.tfKey.getSelectedItem())) {
|
---|
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 | panel.disableAllModifierCheckboxes();
|
---|
360 | panel.tfKey.setEnabled(false);
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | class FilterFieldAdapter implements DocumentListener {
|
---|
366 | public void filter() {
|
---|
367 | String expr = filterField.getText().trim();
|
---|
368 | if (expr.isEmpty()) {
|
---|
369 | expr = null;
|
---|
370 | }
|
---|
371 | try {
|
---|
372 | final TableRowSorter<? extends TableModel> sorter =
|
---|
373 | (TableRowSorter<? extends TableModel>) shortcutTable.getRowSorter();
|
---|
374 | if (expr == null) {
|
---|
375 | sorter.setRowFilter(null);
|
---|
376 | } else {
|
---|
377 | expr = expr.replace("+", "\\+");
|
---|
378 | // split search string on whitespace, do case-insensitive AND search
|
---|
379 | List<RowFilter<Object, Object>> andFilters = new ArrayList<>();
|
---|
380 | for (String word : expr.split("\\s+")) {
|
---|
381 | andFilters.add(RowFilter.regexFilter("(?i)" + word));
|
---|
382 | }
|
---|
383 | sorter.setRowFilter(RowFilter.andFilter(andFilters));
|
---|
384 | }
|
---|
385 | model.fireTableDataChanged();
|
---|
386 | } catch (PatternSyntaxException | ClassCastException ex) {
|
---|
387 | Main.warn(ex);
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | @Override
|
---|
392 | public void changedUpdate(DocumentEvent e) {
|
---|
393 | filter();
|
---|
394 | }
|
---|
395 |
|
---|
396 | @Override
|
---|
397 | public void insertUpdate(DocumentEvent e) {
|
---|
398 | filter();
|
---|
399 | }
|
---|
400 |
|
---|
401 | @Override
|
---|
402 | public void removeUpdate(DocumentEvent e) {
|
---|
403 | filter();
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|