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

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

sonar - squid:S3878 - Arrays should not be created for varargs parameters

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