Changeset 14689 in josm for trunk/src/org/openstreetmap/josm/tools/Shortcut.java
- Timestamp:
- 2019-01-13T11:17:00+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Shortcut.java
r14149 r14689 18 18 import javax.swing.AbstractAction; 19 19 import javax.swing.AbstractButton; 20 import javax.swing.Action; 20 21 import javax.swing.JMenu; 21 22 import javax.swing.KeyStroke; 23 import javax.swing.UIManager; 22 24 import javax.swing.text.JTextComponent; 23 25 … … 270 272 } 271 273 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 272 298 @Override 273 299 public String toString() { … … 595 621 .orElse(null); 596 622 } 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(" </html>"); 663 } 664 return result.toString(); 665 } 666 597 667 }
Note:
See TracChangeset
for help on using the changeset viewer.