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