Changeset 14689 in josm


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

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

Location:
trunk
Files:
1 added
4 edited

Legend:

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

    r14106 r14689  
    44import java.awt.event.InputEvent;
    55import java.awt.event.KeyEvent;
     6import java.util.Optional;
    67
    78import javax.swing.Action;
     
    128129        c.getActionMap().put("ctrl_enter", a);
    129130        c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "ctrl_enter");
     131        Optional.ofNullable(a.getValue(Action.SHORT_DESCRIPTION))
     132                .map(String::valueOf)
     133                .ifPresent(text -> Shortcut.setTooltip(a, text, stroke));
    130134    }
    131135}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r14273 r14689  
    135135     * @param sc Shortcut associated (to display accelerator between parenthesis)
    136136     * @return Full tooltip text (name + accelerator)
    137       */
     137     * @since 1084
     138     * @deprecated Use {@link Shortcut#makeTooltip} instead.
     139      */
     140    @Deprecated
    138141    default String makeTooltip(String name, Shortcut sc) {
    139         StringBuilder result = new StringBuilder();
    140         result.append("<html>").append(name);
    141         if (sc != null && !sc.getKeyText().isEmpty()) {
    142             result.append(" <font size='-2'>(")
    143                   .append(sc.getKeyText())
    144                   .append(")</font>");
    145         }
    146         return result.append("&nbsp;</html>").toString();
     142        return Shortcut.makeTooltip(name, sc != null ? sc.getKeyStroke() : null);
    147143    }
    148144
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r14368 r14689  
    2727import java.util.Objects;
    2828import java.util.concurrent.ExecutionException;
    29 
    30 import javax.swing.UIManager;
    3129
    3230import org.openstreetmap.josm.data.Preferences;
     
    355353
    356354    @Override
    357     public String makeTooltip(String name, Shortcut sc) {
    358         String lafid = UIManager.getLookAndFeel().getID();
    359         boolean canHtml = true;
    360         // "Mac" is the native LAF, "Aqua" is Quaqua. Both use native menus with native tooltips.
    361         if (lafid.contains("Mac") || lafid.contains("Aqua")) {
    362             canHtml = false;
    363         }
    364         StringBuilder result = new StringBuilder(48);
    365         if (canHtml) {
    366             result.append("<html>");
    367         }
    368         result.append(name);
    369         if (sc != null && !sc.getKeyText().isEmpty()) {
    370             result.append(' ');
    371             if (canHtml) {
    372                 result.append("<font size='-2'>");
    373             }
    374             result.append('(').append(sc.getKeyText()).append(')');
    375             if (canHtml) {
    376                 result.append("</font>");
    377             }
    378         }
    379         if (canHtml) {
    380             result.append("&nbsp;</html>");
    381         }
    382         return result.toString();
    383     }
    384 
    385     @Override
    386355    public String getDefaultStyle() {
    387356        return "com.apple.laf.AquaLookAndFeel";
  • 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.