| 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.JList;
|
|---|
| 13 | import javax.swing.JMenuItem;
|
|---|
| 14 | import javax.swing.ListCellRenderer;
|
|---|
| 15 |
|
|---|
| 16 | import org.openstreetmap.josm.Main;
|
|---|
| 17 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 18 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 19 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 20 | import org.openstreetmap.josm.gui.MainMenu;
|
|---|
| 21 | import org.openstreetmap.josm.gui.widgets.SearchTextResultListPanel;
|
|---|
| 22 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * A dialog that allows you to search for a menu item. The user can input part of the menu item name.
|
|---|
| 26 | */
|
|---|
| 27 | public final class MenuItemSearchDialog extends ExtendedDialog {
|
|---|
| 28 |
|
|---|
| 29 | private final Selector selector;
|
|---|
| 30 | private static final MenuItemSearchDialog INSTANCE = new MenuItemSearchDialog(MainApplication.getMenu());
|
|---|
| 31 |
|
|---|
| 32 | private MenuItemSearchDialog(MainMenu menu) {
|
|---|
| 33 | super(Main.parent, tr("Search menu items"), tr("Select"), tr("Cancel"));
|
|---|
| 34 | this.selector = new Selector(menu);
|
|---|
| 35 | this.selector.setDblClickListener(e -> buttonAction(0, null));
|
|---|
| 36 | setContent(selector, false);
|
|---|
| 37 | setPreferredSize(new Dimension(600, 300));
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Returns the unique instance of {@code MenuItemSearchDialog}.
|
|---|
| 42 | *
|
|---|
| 43 | * @return the unique instance of {@code MenuItemSearchDialog}.
|
|---|
| 44 | */
|
|---|
| 45 | public static synchronized MenuItemSearchDialog getInstance() {
|
|---|
| 46 | return INSTANCE;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | @Override
|
|---|
| 50 | public ExtendedDialog showDialog() {
|
|---|
| 51 | selector.init();
|
|---|
| 52 | super.showDialog();
|
|---|
| 53 | selector.clearSelection();
|
|---|
| 54 | return this;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | @Override
|
|---|
| 58 | protected void buttonAction(int buttonIndex, ActionEvent evt) {
|
|---|
| 59 | super.buttonAction(buttonIndex, evt);
|
|---|
| 60 | if (buttonIndex == 0 && selector.getSelectedItem() != null && selector.getSelectedItem().isEnabled()) {
|
|---|
| 61 | selector.getSelectedItem().getAction().actionPerformed(evt);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | private static class Selector extends SearchTextResultListPanel<JMenuItem> {
|
|---|
| 66 |
|
|---|
| 67 | private final MainMenu menu;
|
|---|
| 68 |
|
|---|
| 69 | Selector(MainMenu menu) {
|
|---|
| 70 | super();
|
|---|
| 71 | this.menu = menu;
|
|---|
| 72 | lsResult.setCellRenderer(new CellRenderer());
|
|---|
| 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 | @Override
|
|---|
| 95 | public Component getListCellRendererComponent(JList<? extends JMenuItem> list, JMenuItem value, int index,
|
|---|
| 96 | boolean isSelected, boolean cellHasFocus) {
|
|---|
| 97 | final JMenuItem item = new JMenuItem(value.getText());
|
|---|
| 98 | item.setAction(value.getAction());
|
|---|
| 99 | Optional.ofNullable(value.getAction())
|
|---|
| 100 | .filter(JosmAction.class::isInstance)
|
|---|
| 101 | .map(JosmAction.class::cast)
|
|---|
| 102 | .map(JosmAction::getShortcut)
|
|---|
| 103 | .map(Shortcut::getKeyStroke)
|
|---|
| 104 | .ifPresent(item::setAccelerator);
|
|---|
| 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 */
|
|---|
| 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 | }
|
|---|