| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.Component;
|
|---|
| 8 | import java.awt.Dimension;
|
|---|
| 9 | import java.awt.event.ActionEvent;
|
|---|
| 10 | import java.awt.event.KeyEvent;
|
|---|
| 11 | import java.util.Optional;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.JList;
|
|---|
| 14 | import javax.swing.JMenuItem;
|
|---|
| 15 | import javax.swing.ListCellRenderer;
|
|---|
| 16 |
|
|---|
| 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 MenuItemSelector selector;
|
|---|
| 30 | private static final MenuItemSearchDialog INSTANCE = new MenuItemSearchDialog(MainApplication.getMenu());
|
|---|
| 31 |
|
|---|
| 32 | private MenuItemSearchDialog(MainMenu menu) {
|
|---|
| 33 | super(MainApplication.getMainFrame(), tr("Search menu items"), tr("Select"), tr("Cancel"));
|
|---|
| 34 | this.selector = new MenuItemSelector(menu);
|
|---|
| 35 | this.selector.setDblClickListener(e -> buttonAction(0, null));
|
|---|
| 36 | setContent(selector, false);
|
|---|
| 37 | setPreferredSize(new Dimension(600, 300));
|
|---|
| 38 | setButtonIcons("ok", "cancel");
|
|---|
| 39 | configureContextsensitiveHelp(ht("Action/SearchMenuItems"), true);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Returns the unique instance of {@code MenuItemSearchDialog}.
|
|---|
| 44 | *
|
|---|
| 45 | * @return the unique instance of {@code MenuItemSearchDialog}.
|
|---|
| 46 | */
|
|---|
| 47 | public static synchronized MenuItemSearchDialog getInstance() {
|
|---|
| 48 | return INSTANCE;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | @Override
|
|---|
| 52 | public ExtendedDialog showDialog() {
|
|---|
| 53 | selector.init();
|
|---|
| 54 | super.showDialog();
|
|---|
| 55 | selector.clearSelection();
|
|---|
| 56 | return this;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | @Override
|
|---|
| 60 | protected void buttonAction(int buttonIndex, ActionEvent evt) {
|
|---|
| 61 | super.buttonAction(buttonIndex, evt);
|
|---|
| 62 | if (buttonIndex == 0 && selector.getSelectedItem() != null && selector.getSelectedItem().isEnabled()) {
|
|---|
| 63 | selector.getSelectedItem().getAction().actionPerformed(evt);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | private static class MenuItemSelector extends SearchTextResultListPanel<JMenuItem> {
|
|---|
| 68 |
|
|---|
| 69 | private final MainMenu menu;
|
|---|
| 70 |
|
|---|
| 71 | MenuItemSelector(MainMenu menu) {
|
|---|
| 72 | super();
|
|---|
| 73 | this.menu = menu;
|
|---|
| 74 | lsResult.setCellRenderer(new CellRenderer());
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | public JMenuItem getSelectedItem() {
|
|---|
| 78 | final JMenuItem selected = lsResult.getSelectedValue();
|
|---|
| 79 | if (selected != null) {
|
|---|
| 80 | return selected;
|
|---|
| 81 | } else if (!lsResultModel.isEmpty()) {
|
|---|
| 82 | return lsResultModel.getElementAt(0);
|
|---|
| 83 | } else {
|
|---|
| 84 | return null;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Override
|
|---|
| 89 | protected void filterItems() {
|
|---|
| 90 | menu.imageryMenu.refreshImageryMenu();
|
|---|
| 91 | lsResultModel.setItems(menu.findMenuItems(edSearchText.getText(), true));
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | private static final class CellRenderer implements ListCellRenderer<JMenuItem> {
|
|---|
| 96 |
|
|---|
| 97 | @Override
|
|---|
| 98 | public Component getListCellRendererComponent(JList<? extends JMenuItem> list, JMenuItem value, int index,
|
|---|
| 99 | boolean isSelected, boolean cellHasFocus) {
|
|---|
| 100 | final JMenuItem item = new JMenuItem(value.getText());
|
|---|
| 101 | item.setAction(value.getAction());
|
|---|
| 102 | Optional.ofNullable(value.getAction())
|
|---|
| 103 | .filter(JosmAction.class::isInstance)
|
|---|
| 104 | .map(JosmAction.class::cast)
|
|---|
| 105 | .map(JosmAction::getShortcut)
|
|---|
| 106 | .map(Shortcut::getKeyStroke)
|
|---|
| 107 | .ifPresent(item::setAccelerator);
|
|---|
| 108 | item.setArmed(isSelected);
|
|---|
| 109 | if (isSelected) {
|
|---|
| 110 | item.setBackground(list.getSelectionBackground());
|
|---|
| 111 | item.setForeground(list.getSelectionForeground());
|
|---|
| 112 | } else {
|
|---|
| 113 | item.setBackground(list.getBackground());
|
|---|
| 114 | item.setForeground(list.getForeground());
|
|---|
| 115 | }
|
|---|
| 116 | return item;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * The action that opens the menu item search dialog.
|
|---|
| 122 | */
|
|---|
| 123 | public static class Action extends JosmAction {
|
|---|
| 124 |
|
|---|
| 125 | // CHECKSTYLE.OFF: LineLength
|
|---|
| 126 | /** Action shortcut (ctrl / space by default), made public in order to be used from {@code GettingStarted} page. */
|
|---|
| 127 | public static final Shortcut SHORTCUT = Shortcut.registerShortcut("help:search-items", tr("Help: {0}", tr("Search menu items")), KeyEvent.VK_SPACE, Shortcut.CTRL);
|
|---|
| 128 | // CHECKSTYLE.ON: LineLength
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * Constructs a new {@code Action}.
|
|---|
| 132 | */
|
|---|
| 133 | public Action() {
|
|---|
| 134 | super(tr("Search menu items"), "dialogs/search", null,
|
|---|
| 135 | SHORTCUT,
|
|---|
| 136 | true, "dialogs/search-items", false);
|
|---|
| 137 | setHelpId(ht("Action/SearchMenuItems"));
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | @Override
|
|---|
| 141 | public void actionPerformed(ActionEvent e) {
|
|---|
| 142 | MenuItemSearchDialog.getInstance().showDialog();
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|