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

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

see #15229 - deprecate Main.parent and Main itself

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