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

Last change on this file since 9347 was 9347, checked in by simon04, 8 years ago

fix #12224 - Dialog for "Search menu items"

This places the "Search menu items" functionality in the help menu
instead of placing a text field at the very right of the main menu.

File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import org.openstreetmap.josm.Main;
5import org.openstreetmap.josm.actions.JosmAction;
6import org.openstreetmap.josm.gui.ExtendedDialog;
7import org.openstreetmap.josm.gui.MainMenu;
8import org.openstreetmap.josm.gui.widgets.SearchTextResultListPanel;
9import org.openstreetmap.josm.tools.Shortcut;
10
11import javax.swing.*;
12import java.awt.*;
13import java.awt.event.ActionEvent;
14import java.awt.event.ActionListener;
15import java.awt.event.KeyEvent;
16
17import static org.openstreetmap.josm.tools.I18n.tr;
18
19public class MenuItemSearchDialog extends ExtendedDialog {
20
21 private final Selector selector;
22 private static final MenuItemSearchDialog INSTANCE = new MenuItemSearchDialog(Main.main.menu);
23
24 private MenuItemSearchDialog(MainMenu menu) {
25 super(Main.parent, tr("Search menu items"), new String[]{tr("Select"), tr("Cancel")});
26 this.selector = new Selector(menu);
27 this.selector.setDblClickListener(new ActionListener() {
28 @Override
29 public void actionPerformed(ActionEvent e) {
30 buttonAction(0, null);
31 }
32 });
33 setContent(selector);
34 setPreferredSize(new Dimension(600, 300));
35 }
36
37 /**
38 * Returns the unique instance of {@code MenuItemSearchDialog}.
39 *
40 * @return the unique instance of {@code MenuItemSearchDialog}.
41 */
42 public static synchronized MenuItemSearchDialog getInstance() {
43 return INSTANCE;
44 }
45
46 @Override
47 public ExtendedDialog showDialog() {
48 selector.init();
49 super.showDialog();
50 selector.clearSelection();
51 return this;
52 }
53
54 @Override
55 protected void buttonAction(int buttonIndex, ActionEvent evt) {
56 super.buttonAction(buttonIndex, evt);
57 if (buttonIndex == 0 && selector.getSelectedItem() != null && selector.getSelectedItem().isEnabled()) {
58 selector.getSelectedItem().getAction().actionPerformed(evt);
59 }
60 }
61
62 private static class Selector extends SearchTextResultListPanel<JMenuItem> {
63
64 private final MainMenu menu;
65
66 public Selector(MainMenu menu) {
67 super();
68 this.menu = menu;
69 lsResult.setCellRenderer(new CellRenderer());
70 lsResult.setSelectionModel(new DefaultListSelectionModel() {
71
72 });
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 private final DefaultListCellRenderer def = new DefaultListCellRenderer();
95
96 @Override
97 public Component getListCellRendererComponent(JList<? extends JMenuItem> list, JMenuItem value, int index, boolean isSelected, boolean cellHasFocus) {
98 final JLabel label = (JLabel) def.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
99 label.setText(value.getText());
100 label.setIcon(value.getIcon());
101 label.setEnabled(value.isEnabled());
102 final JMenuItem item = new JMenuItem(value.getText());
103 item.setAction(value.getAction());
104 if (isSelected) {
105 item.setBackground(list.getSelectionBackground());
106 item.setForeground(list.getSelectionForeground());
107 } else {
108 item.setBackground(list.getBackground());
109 item.setForeground(list.getForeground());
110 }
111 return item;
112 }
113 }
114
115 public static class Action extends JosmAction {
116
117 public Action() {
118 super(tr("Search menu items"), "dialogs/search", null,
119 Shortcut.registerShortcut("help:search-items", "Search menu items", KeyEvent.VK_SPACE, Shortcut.CTRL), false);
120 }
121
122 @Override
123 public void actionPerformed(ActionEvent e) {
124 MenuItemSearchDialog.getInstance().showDialog();
125 }
126 }
127}
Note: See TracBrowser for help on using the repository browser.