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

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

checkstyle

File size: 4.6 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;
10
11import javax.swing.DefaultListCellRenderer;
12import javax.swing.JLabel;
13import javax.swing.JList;
14import javax.swing.JMenuItem;
15import javax.swing.ListCellRenderer;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.actions.JosmAction;
19import org.openstreetmap.josm.gui.ExtendedDialog;
20import org.openstreetmap.josm.gui.MainMenu;
21import org.openstreetmap.josm.gui.widgets.SearchTextResultListPanel;
22import org.openstreetmap.josm.tools.Shortcut;
23
24public final class MenuItemSearchDialog extends ExtendedDialog {
25
26 private final Selector selector;
27 private static final MenuItemSearchDialog INSTANCE = new MenuItemSearchDialog(Main.main.menu);
28
29 private MenuItemSearchDialog(MainMenu menu) {
30 super(Main.parent, tr("Search menu items"), new String[]{tr("Select"), tr("Cancel")});
31 this.selector = new Selector(menu);
32 this.selector.setDblClickListener(e -> buttonAction(0, null));
33 setContent(selector, false);
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 Selector(MainMenu menu) {
67 super();
68 this.menu = menu;
69 lsResult.setCellRenderer(new CellRenderer());
70 }
71
72 public JMenuItem getSelectedItem() {
73 final JMenuItem selected = lsResult.getSelectedValue();
74 if (selected != null) {
75 return selected;
76 } else if (!lsResultModel.isEmpty()) {
77 return lsResultModel.getElementAt(0);
78 } else {
79 return null;
80 }
81 }
82
83 @Override
84 protected void filterItems() {
85 lsResultModel.setItems(menu.findMenuItems(edSearchText.getText(), true));
86 }
87 }
88
89 private static class CellRenderer implements ListCellRenderer<JMenuItem> {
90
91 private final DefaultListCellRenderer def = new DefaultListCellRenderer();
92
93 @Override
94 public Component getListCellRendererComponent(JList<? extends JMenuItem> list, JMenuItem value, int index,
95 boolean isSelected, boolean cellHasFocus) {
96 final JLabel label = (JLabel) def.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
97 label.setText(value.getText());
98 label.setIcon(value.getIcon());
99 label.setEnabled(value.isEnabled());
100 final JMenuItem item = new JMenuItem(value.getText());
101 item.setAction(value.getAction());
102 if (isSelected) {
103 item.setBackground(list.getSelectionBackground());
104 item.setForeground(list.getSelectionForeground());
105 } else {
106 item.setBackground(list.getBackground());
107 item.setForeground(list.getForeground());
108 }
109 return item;
110 }
111 }
112
113 public static class Action extends JosmAction {
114
115 // CHECKSTYLE.OFF: LineLength
116 /** Action shortcut (ctrl / space by default */
117 public static final Shortcut SHORTCUT = Shortcut.registerShortcut("help:search-items", "Search menu items", KeyEvent.VK_SPACE, Shortcut.CTRL);
118 // CHECKSTYLE.ON: LineLength
119
120 /**
121 * Constructs a new {@code Action}.
122 */
123 public Action() {
124 super(tr("Search menu items"), "dialogs/search", null,
125 SHORTCUT,
126 true, "dialogs/search-items", false);
127 }
128
129 @Override
130 public void actionPerformed(ActionEvent e) {
131 MenuItemSearchDialog.getInstance().showDialog();
132 }
133 }
134}
Note: See TracBrowser for help on using the repository browser.