source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/MenuItemSearchDialog.java@ 13434

Last change on this file since 13434 was 13134, checked in by Don-vip, 6 years ago

fix #15568 - properly highlight menu items search results

File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Dimension;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Optional;
11
12import javax.swing.JList;
13import javax.swing.JMenuItem;
14import javax.swing.ListCellRenderer;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.actions.JosmAction;
18import org.openstreetmap.josm.gui.ExtendedDialog;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.gui.MainMenu;
21import org.openstreetmap.josm.gui.widgets.SearchTextResultListPanel;
22import org.openstreetmap.josm.tools.Shortcut;
23
24/**
25 * A dialog that allows you to search for a menu item. The user can input part of the menu item name.
26 */
27public final class MenuItemSearchDialog extends ExtendedDialog {
28
29 private final MenuItemSelector selector;
30 private static final MenuItemSearchDialog INSTANCE = new MenuItemSearchDialog(MainApplication.getMenu());
31
32 private MenuItemSearchDialog(MainMenu menu) {
33 super(Main.parent, tr("Search menu items"), tr("Select"), tr("Cancel"));
34 this.selector = new MenuItemSelector(menu);
35 this.selector.setDblClickListener(e -> buttonAction(0, null));
36 setContent(selector, false);
37 setPreferredSize(new Dimension(600, 300));
38 }
39
40 /**
41 * Returns the unique instance of {@code MenuItemSearchDialog}.
42 *
43 * @return the unique instance of {@code MenuItemSearchDialog}.
44 */
45 public static synchronized MenuItemSearchDialog getInstance() {
46 return INSTANCE;
47 }
48
49 @Override
50 public ExtendedDialog showDialog() {
51 selector.init();
52 super.showDialog();
53 selector.clearSelection();
54 return this;
55 }
56
57 @Override
58 protected void buttonAction(int buttonIndex, ActionEvent evt) {
59 super.buttonAction(buttonIndex, evt);
60 if (buttonIndex == 0 && selector.getSelectedItem() != null && selector.getSelectedItem().isEnabled()) {
61 selector.getSelectedItem().getAction().actionPerformed(evt);
62 }
63 }
64
65 private static class MenuItemSelector extends SearchTextResultListPanel<JMenuItem> {
66
67 private final MainMenu menu;
68
69 MenuItemSelector(MainMenu menu) {
70 super();
71 this.menu = menu;
72 lsResult.setCellRenderer(new CellRenderer());
73 }
74
75 public JMenuItem getSelectedItem() {
76 final JMenuItem selected = lsResult.getSelectedValue();
77 if (selected != null) {
78 return selected;
79 } else if (!lsResultModel.isEmpty()) {
80 return lsResultModel.getElementAt(0);
81 } else {
82 return null;
83 }
84 }
85
86 @Override
87 protected void filterItems() {
88 lsResultModel.setItems(menu.findMenuItems(edSearchText.getText(), true));
89 }
90 }
91
92 private static class CellRenderer implements ListCellRenderer<JMenuItem> {
93
94 @Override
95 public Component getListCellRendererComponent(JList<? extends JMenuItem> list, JMenuItem value, int index,
96 boolean isSelected, boolean cellHasFocus) {
97 final JMenuItem item = new JMenuItem(value.getText());
98 item.setAction(value.getAction());
99 Optional.ofNullable(value.getAction())
100 .filter(JosmAction.class::isInstance)
101 .map(JosmAction.class::cast)
102 .map(JosmAction::getShortcut)
103 .map(Shortcut::getKeyStroke)
104 .ifPresent(item::setAccelerator);
105 item.setArmed(isSelected);
106 if (isSelected) {
107 item.setBackground(list.getSelectionBackground());
108 item.setForeground(list.getSelectionForeground());
109 } else {
110 item.setBackground(list.getBackground());
111 item.setForeground(list.getForeground());
112 }
113 return item;
114 }
115 }
116
117 /**
118 * The action that opens the menu item search dialog.
119 */
120 public static class Action extends JosmAction {
121
122 // CHECKSTYLE.OFF: LineLength
123 /** Action shortcut (ctrl / space by default), made public in order to be used from {@code GettingStarted} page. */
124 public static final Shortcut SHORTCUT = Shortcut.registerShortcut("help:search-items", "Search menu items", KeyEvent.VK_SPACE, Shortcut.CTRL);
125 // CHECKSTYLE.ON: LineLength
126
127 /**
128 * Constructs a new {@code Action}.
129 */
130 public Action() {
131 super(tr("Search menu items"), "dialogs/search", null,
132 SHORTCUT,
133 true, "dialogs/search-items", false);
134 }
135
136 @Override
137 public void actionPerformed(ActionEvent e) {
138 MenuItemSearchDialog.getInstance().showDialog();
139 }
140 }
141}
Note: See TracBrowser for help on using the repository browser.