1. /*
  2. * @(#)MetalToolTipUI.java 1.17 00/02/02
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.plaf.metal;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.*;
  14. import javax.swing.BorderFactory;
  15. import javax.swing.border.Border;
  16. import javax.swing.plaf.*;
  17. import javax.swing.plaf.basic.BasicToolTipUI;
  18. /**
  19. * A Metal L&F extension of BasicToolTipUI.
  20. * <p>
  21. * <strong>Warning:</strong>
  22. * Serialized objects of this class will not be compatible with
  23. * future Swing releases. The current serialization support is appropriate
  24. * for short term storage or RMI between applications running the same
  25. * version of Swing. A future release of Swing will provide support for
  26. * long term persistence.
  27. *
  28. * @version 1.17 02/02/00
  29. * @author Steve Wilson
  30. */
  31. public class MetalToolTipUI extends BasicToolTipUI {
  32. static MetalToolTipUI sharedInstance = new MetalToolTipUI();
  33. private Font smallFont;
  34. private JToolTip tip;
  35. public static final int padSpaceBetweenStrings = 12;
  36. private String acceleratorDelimiter;
  37. public MetalToolTipUI() {
  38. super();
  39. }
  40. public static ComponentUI createUI(JComponent c) {
  41. return sharedInstance;
  42. }
  43. public void installUI(JComponent c) {
  44. super.installUI(c);
  45. tip = (JToolTip)c;
  46. Font f = c.getFont();
  47. smallFont = new Font( f.getName(), f.getStyle(), f.getSize() - 2 );
  48. acceleratorDelimiter = UIManager.getString( "MenuItem.acceleratorDelimiter" );
  49. if ( acceleratorDelimiter == null ) { acceleratorDelimiter = "-"; }
  50. }
  51. public void paint(Graphics g, JComponent c) {
  52. super.paint(g, c);
  53. Font font = c.getFont();
  54. FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
  55. String keyText = getAcceleratorString();
  56. String tipText = ((JToolTip)c).getTipText();
  57. if (tipText == null) {
  58. tipText = "";
  59. }
  60. if (! (keyText.equals(""))) { // only draw control key if there is one
  61. g.setFont(smallFont);
  62. g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  63. g.drawString(keyText,
  64. metrics.stringWidth(tipText) + padSpaceBetweenStrings,
  65. 2 + metrics.getAscent());
  66. }
  67. }
  68. public Dimension getPreferredSize(JComponent c) {
  69. Dimension d = super.getPreferredSize(c);
  70. String key = getAcceleratorString();
  71. if (! (key.equals(""))) {
  72. FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(smallFont);
  73. d.width += fm.stringWidth(key) + padSpaceBetweenStrings;
  74. }
  75. return d;
  76. }
  77. public String getAcceleratorString() {
  78. JComponent comp = tip.getComponent();
  79. if (comp == null) {
  80. return "";
  81. }
  82. KeyStroke[] keys =comp.getRegisteredKeyStrokes();
  83. String controlKeyStr = "";
  84. for (int i = 0; i < keys.length; i++) {
  85. char c = (char)keys[i].getKeyCode();
  86. int mod = keys[i].getModifiers();
  87. int condition = comp.getConditionForKeyStroke(keys[i]);
  88. if ( condition == JComponent.WHEN_IN_FOCUSED_WINDOW &&
  89. ( (mod & InputEvent.ALT_MASK) != 0 || (mod & InputEvent.CTRL_MASK) != 0 ||
  90. (mod & InputEvent.SHIFT_MASK) != 0 || (mod & InputEvent.META_MASK) != 0 ) )
  91. {
  92. controlKeyStr = KeyEvent.getKeyModifiersText(mod) +
  93. acceleratorDelimiter + (char)keys[i].getKeyCode();
  94. break;
  95. }
  96. }
  97. /* Special case for menu item since they do not register a
  98. keyboard action for their mnemonics and they always use Alt */
  99. if ( controlKeyStr.equals("") && comp instanceof JMenuItem )
  100. {
  101. int mnemonic = ((JMenuItem) comp).getMnemonic();
  102. if ( mnemonic != 0 )
  103. {
  104. controlKeyStr = "Alt" + acceleratorDelimiter + (char) mnemonic;
  105. }
  106. }
  107. return controlKeyStr;
  108. }
  109. }