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

Last change on this file since 15929 was 14744, checked in by simon04, 5 years ago

see #17178 - Search menu items: add help button

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