Ignore:
Timestamp:
2019-01-13T11:17:00+01:00 (6 years ago)
Author:
simon04
Message:

fix #17202 - InputMapUtils: display Ctrl+Enter in action tooltip

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Shortcut.java

    r14149 r14689  
    1818import javax.swing.AbstractAction;
    1919import javax.swing.AbstractButton;
     20import javax.swing.Action;
    2021import javax.swing.JMenu;
    2122import javax.swing.KeyStroke;
     23import javax.swing.UIManager;
    2224import javax.swing.text.JTextComponent;
    2325
     
    270272    }
    271273
     274    /**
     275     * Sets the action tooltip to the tooltip text plus the {@linkplain #getKeyText(KeyStroke) key stroke text}
     276     * this shortcut represents.
     277     *
     278     * @param action action
     279     * @param tooltip Tooltip text to display
     280     * @since 14689
     281     */
     282    public void setTooltip(Action action, String tooltip) {
     283        setTooltip(action, tooltip, getKeyStroke());
     284    }
     285
     286    /**
     287     * Sets the action tooltip to the tooltip text plus the {@linkplain #getKeyText(KeyStroke) key stroke text}.
     288     *
     289     * @param action action
     290     * @param tooltip Tooltip text to display
     291     * @param keyStroke Key stroke associated (to display accelerator between parenthesis)
     292     * @since 14689
     293     */
     294    public static void setTooltip(Action action, String tooltip, KeyStroke keyStroke) {
     295        action.putValue(Action.SHORT_DESCRIPTION, makeTooltip(tooltip, keyStroke));
     296    }
     297
    272298    @Override
    273299    public String toString() {
     
    595621                .orElse(null);
    596622    }
     623
     624    /**
     625     * Returns the tooltip text plus the {@linkplain #getKeyText(KeyStroke) key stroke text}.
     626     *
     627     * Tooltips are usually not system dependent, unless the
     628     * JVM is too dumb to provide correct names for all the keys.
     629     *
     630     * Some LAFs don't understand HTML, such as the OSX LAFs.
     631     *
     632     * @param tooltip Tooltip text to display
     633     * @param keyStroke Key stroke associated (to display accelerator between parenthesis)
     634     * @return Full tooltip text (tooltip + accelerator)
     635     * @since 14689
     636     */
     637    public static String makeTooltip(String tooltip, KeyStroke keyStroke) {
     638        final Optional<String> keyStrokeText = Optional.ofNullable(keyStroke)
     639                .map(Shortcut::getKeyText)
     640                .filter(text -> !text.isEmpty());
     641
     642        final String laf = UIManager.getLookAndFeel().getID();
     643        // "Mac" is the native LAF, "Aqua" is Quaqua. Both use native menus with native tooltips.
     644        final boolean canHtml = !(PlatformManager.isPlatformOsx() && (laf.contains("Mac") || laf.contains("Aqua")));
     645
     646        StringBuilder result = new StringBuilder(48);
     647        if (canHtml) {
     648            result.append("<html>");
     649        }
     650        result.append(tooltip);
     651        if (keyStrokeText.isPresent()) {
     652            result.append(' ');
     653            if (canHtml) {
     654                result.append("<font size='-2'>");
     655            }
     656            result.append('(').append(keyStrokeText.get()).append(')');
     657            if (canHtml) {
     658                result.append("</font>");
     659            }
     660        }
     661        if (canHtml) {
     662            result.append("&nbsp;</html>");
     663        }
     664        return result.toString();
     665    }
     666
    597667}
Note: See TracChangeset for help on using the changeset viewer.