| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.Component;
|
|---|
| 7 | import java.awt.Dimension;
|
|---|
| 8 | import java.awt.event.ActionEvent;
|
|---|
| 9 | import java.awt.event.KeyEvent;
|
|---|
| 10 | import java.util.Optional;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.DefaultListCellRenderer;
|
|---|
| 13 | import javax.swing.JList;
|
|---|
| 14 | import javax.swing.JMenuItem;
|
|---|
| 15 | import javax.swing.ListCellRenderer;
|
|---|
| 16 |
|
|---|
| 17 | import org.openstreetmap.josm.Main;
|
|---|
| 18 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 19 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 20 | import org.openstreetmap.josm.gui.MainMenu;
|
|---|
| 21 | import org.openstreetmap.josm.gui.widgets.SearchTextResultListPanel;
|
|---|
| 22 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 23 |
|
|---|
| 24 | public 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 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 | 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 | // CHECKSTYLE.OFF: LineLength
|
|---|
| 118 | /** Action shortcut (ctrl / space by default */
|
|---|
| 119 | public static final Shortcut SHORTCUT = Shortcut.registerShortcut("help:search-items", "Search menu items", KeyEvent.VK_SPACE, Shortcut.CTRL);
|
|---|
| 120 | // CHECKSTYLE.ON: LineLength
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Constructs a new {@code Action}.
|
|---|
| 124 | */
|
|---|
| 125 | public Action() {
|
|---|
| 126 | super(tr("Search menu items"), "dialogs/search", null,
|
|---|
| 127 | SHORTCUT,
|
|---|
| 128 | true, "dialogs/search-items", false);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | @Override
|
|---|
| 132 | public void actionPerformed(ActionEvent e) {
|
|---|
| 133 | MenuItemSearchDialog.getInstance().showDialog();
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|