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

Last change on this file since 16423 was 16423, checked in by simon04, 4 years ago

fix #19247 - MenuItemSearchDialog: initialize imagery menu

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 menu.imageryMenu.refreshImageryMenu();
91 lsResultModel.setItems(menu.findMenuItems(edSearchText.getText(), true));
92 }
93 }
94
95 private static class CellRenderer implements ListCellRenderer<JMenuItem> {
96
97 @Override
98 public Component getListCellRendererComponent(JList<? extends JMenuItem> list, JMenuItem value, int index,
99 boolean isSelected, boolean cellHasFocus) {
100 final JMenuItem item = new JMenuItem(value.getText());
101 item.setAction(value.getAction());
102 Optional.ofNullable(value.getAction())
103 .filter(JosmAction.class::isInstance)
104 .map(JosmAction.class::cast)
105 .map(JosmAction::getShortcut)
106 .map(Shortcut::getKeyStroke)
107 .ifPresent(item::setAccelerator);
108 item.setArmed(isSelected);
109 if (isSelected) {
110 item.setBackground(list.getSelectionBackground());
111 item.setForeground(list.getSelectionForeground());
112 } else {
113 item.setBackground(list.getBackground());
114 item.setForeground(list.getForeground());
115 }
116 return item;
117 }
118 }
119
120 /**
121 * The action that opens the menu item search dialog.
122 */
123 public static class Action extends JosmAction {
124
125 // CHECKSTYLE.OFF: LineLength
126 /** Action shortcut (ctrl / space by default), made public in order to be used from {@code GettingStarted} page. */
127 public static final Shortcut SHORTCUT = Shortcut.registerShortcut("help:search-items", "Search menu items", KeyEvent.VK_SPACE, Shortcut.CTRL);
128 // CHECKSTYLE.ON: LineLength
129
130 /**
131 * Constructs a new {@code Action}.
132 */
133 public Action() {
134 super(tr("Search menu items"), "dialogs/search", null,
135 SHORTCUT,
136 true, "dialogs/search-items", false);
137 setHelpId(ht("Action/SearchMenuItems"));
138 }
139
140 @Override
141 public void actionPerformed(ActionEvent e) {
142 MenuItemSearchDialog.getInstance().showDialog();
143 }
144 }
145}
Note: See TracBrowser for help on using the repository browser.