1. /*
  2. * @(#)BasicToolTipUI.java 1.30 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import java.awt.*;
  9. import java.beans.PropertyChangeEvent;
  10. import java.beans.PropertyChangeListener;
  11. import javax.swing.*;
  12. import javax.swing.BorderFactory;
  13. import javax.swing.border.Border;
  14. import javax.swing.plaf.ToolTipUI;
  15. import javax.swing.plaf.ComponentUI;
  16. import javax.swing.plaf.UIResource;
  17. import javax.swing.text.View;
  18. /**
  19. * Standard tool tip L&F.
  20. * <p>
  21. *
  22. * @version 1.30 11/29/01
  23. * @author Dave Moore
  24. */
  25. public class BasicToolTipUI extends ToolTipUI
  26. {
  27. static BasicToolTipUI sharedInstance = new BasicToolTipUI();
  28. private PropertyChangeListener propertyChangeListener;
  29. public static ComponentUI createUI(JComponent c) {
  30. return sharedInstance;
  31. }
  32. public BasicToolTipUI() {
  33. super();
  34. }
  35. public void installUI(JComponent c) {
  36. installDefaults(c);
  37. installComponents(c);
  38. installListeners(c);
  39. }
  40. public void uninstallUI(JComponent c) {
  41. // REMIND: this is NOT getting called
  42. uninstallDefaults(c);
  43. uninstallComponents(c);
  44. uninstallListeners(c);
  45. }
  46. protected void installDefaults(JComponent c){
  47. LookAndFeel.installColorsAndFont(c, "ToolTip.background",
  48. "ToolTip.foreground",
  49. "ToolTip.font");
  50. LookAndFeel.installBorder(c, "ToolTip.border");
  51. }
  52. protected void uninstallDefaults(JComponent c){
  53. LookAndFeel.uninstallBorder(c);
  54. }
  55. /* Unfortunately this has to remain private until we can make API additions.
  56. */
  57. private void installComponents(JComponent c){
  58. BasicHTML.updateRenderer(c, ((JToolTip)c).getTipText());
  59. }
  60. /* Unfortunately this has to remain private until we can make API additions.
  61. */
  62. private void uninstallComponents(JComponent c){
  63. BasicHTML.updateRenderer(c, "");
  64. }
  65. protected void installListeners(JComponent c) {
  66. propertyChangeListener = createPropertyChangeListener(c);
  67. c.addPropertyChangeListener(propertyChangeListener);
  68. }
  69. protected void uninstallListeners(JComponent c) {
  70. c.removePropertyChangeListener(propertyChangeListener);
  71. propertyChangeListener = null;
  72. }
  73. /* Unfortunately this has to remain private until we can make API additions.
  74. */
  75. private PropertyChangeListener createPropertyChangeListener(JComponent c) {
  76. return new PropertyChangeHandler();
  77. }
  78. public void paint(Graphics g, JComponent c) {
  79. Font font = c.getFont();
  80. FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
  81. Dimension size = c.getSize();
  82. g.setColor(c.getBackground());
  83. g.fillRect(0, 0, size.width, size.height);
  84. g.setColor(c.getForeground());
  85. g.setFont(font);
  86. // fix for bug 4153892
  87. String tipText = ((JToolTip)c).getTipText();
  88. if (tipText == null) {
  89. tipText = "";
  90. }
  91. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  92. if (v != null) {
  93. Rectangle paintTextR = c.getBounds();
  94. Insets insets = c.getInsets();
  95. paintTextR.x += insets.left;
  96. paintTextR.y += insets.top;
  97. paintTextR.width -= insets.left+insets.right;
  98. paintTextR.height -= insets.top+insets.bottom;
  99. v.paint(g, paintTextR);
  100. } else {
  101. g.drawString(tipText, 3, 2 + metrics.getAscent());
  102. }
  103. }
  104. public Dimension getPreferredSize(JComponent c) {
  105. Font font = c.getFont();
  106. FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
  107. Insets insets = c.getInsets();
  108. Dimension prefSize = new Dimension(insets.left+insets.right,
  109. insets.top+insets.bottom);
  110. String text = ((JToolTip)c).getTipText();
  111. if ((text == null) || text.equals("")) {
  112. text = "";
  113. }
  114. else {
  115. View v = (c != null) ? (View) c.getClientProperty("html") : null;
  116. if (v != null) {
  117. prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
  118. prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
  119. } else {
  120. prefSize.width += SwingUtilities.computeStringWidth(fm,text) + 6;
  121. prefSize.height += fm.getHeight() + 4;
  122. }
  123. }
  124. return prefSize;
  125. }
  126. public Dimension getMinimumSize(JComponent c) {
  127. Dimension d = getPreferredSize(c);
  128. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  129. if (v != null) {
  130. d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
  131. }
  132. return d;
  133. }
  134. public Dimension getMaximumSize(JComponent c) {
  135. Dimension d = getPreferredSize(c);
  136. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  137. if (v != null) {
  138. d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
  139. }
  140. return d;
  141. }
  142. private class PropertyChangeHandler implements PropertyChangeListener {
  143. public void propertyChange(PropertyChangeEvent e) {
  144. String name = e.getPropertyName();
  145. if (name.equals("tiptext")) {
  146. // remove the old html view client property if one
  147. // existed, and install a new one if the text installed
  148. // into the JLabel is html source.
  149. JToolTip tip = ((JToolTip) e.getSource());
  150. String text = tip.getTipText();
  151. BasicHTML.updateRenderer(tip, text);
  152. }
  153. }
  154. }
  155. }