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

Last change on this file since 8540 was 8540, checked in by Don-vip, 9 years ago

fix remaining checkstyle issues

  • Property svn:eol-style set to native
File size: 16.6 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.Insets;
13import java.awt.Toolkit;
14import java.awt.event.KeyEvent;
15import java.lang.reflect.Field;
16import java.util.ArrayList;
17import java.util.LinkedHashMap;
18import java.util.List;
19import java.util.Map;
20import java.util.regex.PatternSyntaxException;
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.RowFilter;
34import javax.swing.SwingConstants;
35import javax.swing.event.DocumentEvent;
36import javax.swing.event.DocumentListener;
37import javax.swing.event.ListSelectionEvent;
38import javax.swing.event.ListSelectionListener;
39import javax.swing.table.AbstractTableModel;
40import javax.swing.table.DefaultTableCellRenderer;
41import javax.swing.table.TableColumnModel;
42import javax.swing.table.TableModel;
43import javax.swing.table.TableRowSorter;
44
45import org.openstreetmap.josm.Main;
46import org.openstreetmap.josm.gui.widgets.JosmComboBox;
47import org.openstreetmap.josm.gui.widgets.JosmTextField;
48import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
49import org.openstreetmap.josm.tools.Shortcut;
50
51/**
52 * This is the keyboard preferences content.
53 */
54public class PrefJPanel extends JPanel {
55
56 // table of shortcuts
57 private AbstractTableModel model;
58 // this are the display(!) texts for the checkboxes. Let the JVM do the i18n for us <g>.
59 // Ok, there's a real reason for this: The JVM should know best how the keys are labelled
60 // on the physical keyboard. What language pack is installed in JOSM is completely
61 // independent from the keyboard's labelling. But the operation system's locale
62 // usually matches the keyboard. This even works with my English Windows and my German keyboard.
63 private static final String SHIFT = KeyEvent.getKeyModifiersText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
64 KeyEvent.SHIFT_DOWN_MASK).getModifiers());
65 private static final String CTRL = KeyEvent.getKeyModifiersText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
66 KeyEvent.CTRL_DOWN_MASK).getModifiers());
67 private static final String ALT = KeyEvent.getKeyModifiersText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
68 KeyEvent.ALT_DOWN_MASK).getModifiers());
69 private static final String META = KeyEvent.getKeyModifiersText(KeyStroke.getKeyStroke(KeyEvent.VK_A,
70 KeyEvent.META_DOWN_MASK).getModifiers());
71
72 // A list of keys to present the user. Sadly this really is a list of keys Java knows about,
73 // not a list of real physical keys. If someone knows how to get that list?
74 private static Map<Integer, String> keyList = setKeyList();
75
76 private static Map<Integer, String> setKeyList() {
77 Map<Integer, String> list = new LinkedHashMap<>();
78 String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
79 // Assume all known keys are declared in KeyEvent as "public static int VK_*"
80 for (Field field : KeyEvent.class.getFields()) {
81 if (field.getName().startsWith("VK_")) {
82 try {
83 int i = field.getInt(null);
84 String s = KeyEvent.getKeyText(i);
85 if (s != null && s.length() > 0 && !s.contains(unknown)) {
86 list.put(Integer.valueOf(i), s);
87 }
88 } catch (Exception e) {
89 Main.error(e);
90 }
91 }
92 }
93 list.put(Integer.valueOf(-1), "");
94 return list;
95 }
96
97 private JCheckBox cbAlt = new JCheckBox();
98 private JCheckBox cbCtrl = new JCheckBox();
99 private JCheckBox cbMeta = new JCheckBox();
100 private JCheckBox cbShift = new JCheckBox();
101 private JCheckBox cbDefault = new JCheckBox();
102 private JCheckBox cbDisable = new JCheckBox();
103 private JosmComboBox<String> tfKey = new JosmComboBox<>();
104
105 private JTable shortcutTable = new JTable();
106
107 private JosmTextField filterField = new JosmTextField();
108
109 /** Creates new form prefJPanel */
110 public PrefJPanel() {
111 this.model = new ScListModel();
112 initComponents();
113 }
114
115 /**
116 * Show only shortcuts with descriptions containing given substring
117 * @param substring The substring used to filter
118 */
119 public void filter(String substring) {
120 filterField.setText(substring);
121 }
122
123 private static class ScListModel extends AbstractTableModel {
124 private final String[] columnNames = new String[]{tr("Action"), tr("Shortcut")};
125 private transient List<Shortcut> data;
126
127 /**
128 * Constructs a new {@code ScListModel}.
129 */
130 public ScListModel() {
131 data = Shortcut.listAll();
132 }
133
134 @Override
135 public int getColumnCount() {
136 return columnNames.length;
137 }
138
139 @Override
140 public int getRowCount() {
141 return data.size();
142 }
143
144 @Override
145 public String getColumnName(int col) {
146 return columnNames[col];
147 }
148
149 @Override
150 public Object getValueAt(int row, int col) {
151 return (col == 0) ? data.get(row).getLongText() : data.get(row);
152 }
153 }
154
155 private class ShortcutTableCellRenderer extends DefaultTableCellRenderer {
156
157 private boolean name;
158
159 public ShortcutTableCellRenderer(boolean name) {
160 this.name = name;
161 }
162
163 @Override
164 public Component getTableCellRendererComponent(JTable table, Object value, boolean
165 isSelected, boolean hasFocus, int row, int column) {
166 int row1 = shortcutTable.convertRowIndexToModel(row);
167 Shortcut sc = (Shortcut) model.getValueAt(row1, -1);
168 if (sc == null) return null;
169 JLabel label = (JLabel) super.getTableCellRendererComponent(
170 table, name ? sc.getLongText() : sc.getKeyText(), isSelected, hasFocus, row, column);
171 label.setBackground(Main.pref.getUIColor("Table.background"));
172 if (isSelected) {
173 label.setForeground(Main.pref.getUIColor("Table.foreground"));
174 }
175 if (sc.isAssignedUser()) {
176 label.setBackground(Main.pref.getColor(
177 marktr("Shortcut Background: User"),
178 new Color(200, 255, 200)));
179 } else if (!sc.isAssignedDefault()) {
180 label.setBackground(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();
190 JScrollPane listScrollPane = new JScrollPane();
191 JPanel shortcutEditPane = new JPanel();
192
193 CbAction action = new CbAction(this);
194 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
195 add(buildFilterPanel());
196 listPane.setLayout(new java.awt.GridLayout());
197
198 // This is the list of shortcuts:
199 shortcutTable.setModel(model);
200 shortcutTable.getSelectionModel().addListSelectionListener(new CbAction(this));
201 shortcutTable.setFillsViewportHeight(true);
202 shortcutTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
203 shortcutTable.setAutoCreateRowSorter(true);
204 TableColumnModel mod = shortcutTable.getColumnModel();
205 mod.getColumn(0).setCellRenderer(new ShortcutTableCellRenderer(true));
206 mod.getColumn(1).setCellRenderer(new ShortcutTableCellRenderer(false));
207 listScrollPane.setViewportView(shortcutTable);
208
209 listPane.add(listScrollPane);
210
211 add(listPane);
212
213 // and here follows the edit area. I won't object to someone re-designing it, it looks, um, "minimalistic" ;)
214 shortcutEditPane.setLayout(new java.awt.GridLayout(5, 2));
215
216 cbDefault.setAction(action);
217 cbDefault.setText(tr("Use default"));
218 cbShift.setAction(action);
219 cbShift.setText(SHIFT); // see above for why no tr()
220 cbDisable.setAction(action);
221 cbDisable.setText(tr("Disable"));
222 cbCtrl.setAction(action);
223 cbCtrl.setText(CTRL); // see above for why no tr()
224 cbAlt.setAction(action);
225 cbAlt.setText(ALT); // see above for why no tr()
226 tfKey.setAction(action);
227 tfKey.setModel(new DefaultComboBoxModel<>(keyList.values().toArray(new String[0])));
228 cbMeta.setAction(action);
229 cbMeta.setText(META); // see above for why no tr()
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 filterField.setToolTipText(tr("Enter a search expression"));
264 SelectAllOnFocusGainedDecorator.decorate(filterField);
265 filterField.getDocument().addDocumentListener(new FilterFieldAdapter());
266 pnl.setMaximumSize(new Dimension(300, 10));
267 return pnl;
268 }
269
270 private void disableAllModifierCheckboxes() {
271 cbDefault.setEnabled(false);
272 cbDisable.setEnabled(false);
273 cbShift.setEnabled(false);
274 cbCtrl.setEnabled(false);
275 cbAlt.setEnabled(false);
276 cbMeta.setEnabled(false);
277 }
278
279 // this allows to edit shortcuts. it:
280 // * sets the edit controls to the selected shortcut
281 // * enabled/disables the controls as needed
282 // * writes the user's changes to the shortcut
283 // And after I finally had it working, I realized that those two methods
284 // are playing ping-pong (politically correct: table tennis, I know) and
285 // even have some duplicated code. Feel free to refactor, If you have
286 // more expirience with GUI coding than I have.
287 private class CbAction extends AbstractAction implements ListSelectionListener {
288 private PrefJPanel panel;
289
290 public CbAction(PrefJPanel panel) {
291 this.panel = panel;
292 }
293
294 @Override
295 public void valueChanged(ListSelectionEvent e) {
296 ListSelectionModel lsm = panel.shortcutTable.getSelectionModel(); // can't use e here
297 if (!lsm.isSelectionEmpty()) {
298 int row = panel.shortcutTable.convertRowIndexToModel(lsm.getMinSelectionIndex());
299 Shortcut sc = (Shortcut) panel.model.getValueAt(row, -1);
300 panel.cbDefault.setSelected(!sc.isAssignedUser());
301 panel.cbDisable.setSelected(sc.getKeyStroke() == null);
302 panel.cbShift.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.SHIFT_DOWN_MASK) != 0);
303 panel.cbCtrl.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.CTRL_DOWN_MASK) != 0);
304 panel.cbAlt.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.ALT_DOWN_MASK) != 0);
305 panel.cbMeta.setSelected(sc.getAssignedModifier() != -1 && (sc.getAssignedModifier() & KeyEvent.META_DOWN_MASK) != 0);
306 if (sc.getKeyStroke() != null) {
307 tfKey.setSelectedItem(keyList.get(sc.getKeyStroke().getKeyCode()));
308 } else {
309 tfKey.setSelectedItem(keyList.get(-1));
310 }
311 if (!sc.isChangeable()) {
312 disableAllModifierCheckboxes();
313 panel.tfKey.setEnabled(false);
314 } else {
315 panel.cbDefault.setEnabled(true);
316 actionPerformed(null);
317 }
318 model.fireTableRowsUpdated(row, row);
319 } else {
320 panel.disableAllModifierCheckboxes();
321 panel.tfKey.setEnabled(false);
322 }
323 }
324
325 @Override
326 public void actionPerformed(java.awt.event.ActionEvent e) {
327 ListSelectionModel lsm = panel.shortcutTable.getSelectionModel();
328 if (lsm != null && !lsm.isSelectionEmpty()) {
329 if (e != null) { // only if we've been called by a user action
330 int row = panel.shortcutTable.convertRowIndexToModel(lsm.getMinSelectionIndex());
331 Shortcut sc = (Shortcut) panel.model.getValueAt(row, -1);
332 if (panel.cbDisable.isSelected()) {
333 sc.setAssignedModifier(-1);
334 } else if (panel.tfKey.getSelectedItem() == null || "".equals(panel.tfKey.getSelectedItem())) {
335 sc.setAssignedModifier(KeyEvent.VK_CANCEL);
336 } else {
337 sc.setAssignedModifier(
338 (panel.cbShift.isSelected() ? KeyEvent.SHIFT_DOWN_MASK : 0) |
339 (panel.cbCtrl.isSelected() ? KeyEvent.CTRL_DOWN_MASK : 0) |
340 (panel.cbAlt.isSelected() ? KeyEvent.ALT_DOWN_MASK : 0) |
341 (panel.cbMeta.isSelected() ? KeyEvent.META_DOWN_MASK : 0)
342 );
343 for (Map.Entry<Integer, String> entry : keyList.entrySet()) {
344 if (entry.getValue().equals(panel.tfKey.getSelectedItem())) {
345 sc.setAssignedKey(entry.getKey());
346 }
347 }
348 }
349 sc.setAssignedUser(!panel.cbDefault.isSelected());
350 valueChanged(null);
351 }
352 boolean state = !panel.cbDefault.isSelected();
353 panel.cbDisable.setEnabled(state);
354 state = state && !panel.cbDisable.isSelected();
355 panel.cbShift.setEnabled(state);
356 panel.cbCtrl.setEnabled(state);
357 panel.cbAlt.setEnabled(state);
358 panel.cbMeta.setEnabled(state);
359 panel.tfKey.setEnabled(state);
360 } else {
361 panel.disableAllModifierCheckboxes();
362 panel.tfKey.setEnabled(false);
363 }
364 }
365 }
366
367 class FilterFieldAdapter implements DocumentListener {
368 public void filter() {
369 String expr = filterField.getText().trim();
370 if (expr.isEmpty()) {
371 expr = null;
372 }
373 try {
374 final TableRowSorter<? extends TableModel> sorter =
375 (TableRowSorter<? extends TableModel>) shortcutTable.getRowSorter();
376 if (expr == null) {
377 sorter.setRowFilter(null);
378 } else {
379 expr = expr.replace("+", "\\+");
380 // split search string on whitespace, do case-insensitive AND search
381 List<RowFilter<Object, Object>> andFilters = new ArrayList<>();
382 for (String word : expr.split("\\s+")) {
383 andFilters.add(RowFilter.regexFilter("(?i)" + word));
384 }
385 sorter.setRowFilter(RowFilter.andFilter(andFilters));
386 }
387 model.fireTableDataChanged();
388 } catch (PatternSyntaxException | ClassCastException ex) {
389 Main.warn(ex);
390 }
391 }
392
393 @Override
394 public void changedUpdate(DocumentEvent e) {
395 filter();
396 }
397
398 @Override
399 public void insertUpdate(DocumentEvent e) {
400 filter();
401 }
402
403 @Override
404 public void removeUpdate(DocumentEvent e) {
405 filter();
406 }
407 }
408}
Note: See TracBrowser for help on using the repository browser.